Skip to content

Commit 3670719

Browse files
committed
Changed owner of message to print in Client
1 parent 09b41bd commit 3670719

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

client_src/game_protocol_client.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ static Logger& logger = Logger::get_instance();
1212
GameProtocolClient::GameProtocolClient(const std::string& hostname, const std::string& servname):
1313
hostname(hostname),
1414
servname(servname),
15-
ENEMY_DIED_EVENT_MESSAGE("Un enemigo ha muerto."),
16-
ENEMY_REVIVED_EVENT_MESSAGE("Un enemigo ha revivido."),
1715
skt(hostname.c_str(), servname.c_str()),
1816
is_skt_closed(false) {}
1917

@@ -70,12 +68,13 @@ const uint8_t GameProtocolClient::receive_enemy_event() {
7068

7169

7270
const std::string GameProtocolClient::get_enemy_event_message(
73-
const uint8_t& enemy_event_code) const {
71+
const uint8_t& enemy_event_code, const std::string& ENEMY_DIED_EVENT_MESSAGE,
72+
const std::string& ENEMY_REVIVED_EVENT_MESSAGE) const {
7473
if (enemy_event_code == this->game_commands.get_enemy_died_code()) {
75-
return this->ENEMY_DIED_EVENT_MESSAGE;
74+
return ENEMY_DIED_EVENT_MESSAGE;
7675

7776
} else if (enemy_event_code == this->game_commands.get_enemy_revived_code()) {
78-
return this->ENEMY_REVIVED_EVENT_MESSAGE;
77+
return ENEMY_REVIVED_EVENT_MESSAGE;
7978

8079
} else {
8180
logger.client_error("Closed socket from server.");
@@ -84,24 +83,30 @@ const std::string GameProtocolClient::get_enemy_event_message(
8483
}
8584

8685

87-
const GameMessage GameProtocolClient::receive_message_from_server() {
86+
const GameMessage GameProtocolClient::receive_message_from_server(
87+
const std::string& ENEMY_DIED_EVENT_MESSAGE,
88+
const std::string& ENEMY_REVIVED_EVENT_MESSAGE) {
8889
this->receive_server_start_broadcast();
8990
const uint16_t enemies_alive = this->receive_enemies_alive();
9091
const uint16_t enemies_dead = this->receive_enemies_dead();
9192
const uint8_t enemy_event_code = this->receive_enemy_event();
9293

93-
std::string event_message = this->get_enemy_event_message(enemy_event_code);
94+
std::string event_message = this->get_enemy_event_message(
95+
enemy_event_code, ENEMY_DIED_EVENT_MESSAGE, ENEMY_REVIVED_EVENT_MESSAGE);
9496

9597
return GameMessage(enemies_alive, enemies_dead, event_message);
9698
}
9799

98100

99101
const std::vector<GameMessage> GameProtocolClient::receive_messages_from_server(
100-
const uint32_t& number_of_messages) {
102+
const uint32_t& number_of_messages, const std::string& ENEMY_DIED_EVENT_MESSAGE,
103+
const std::string& ENEMY_REVIVED_EVENT_MESSAGE) {
104+
101105
std::vector<GameMessage> messages;
102106

103107
for (uint32_t i = 0; i < number_of_messages; i++) {
104-
GameMessage message = this->receive_message_from_server();
108+
GameMessage message = this->receive_message_from_server(ENEMY_DIED_EVENT_MESSAGE,
109+
ENEMY_REVIVED_EVENT_MESSAGE);
105110
messages.push_back(message);
106111
}
107112

client_src/game_protocol_client.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ class GameProtocolClient {
1515
const std::string hostname;
1616
// cppcheck-suppress unusedStructMember
1717
const std::string servname;
18-
// cppcheck-suppress unusedStructMember
19-
const std::string ENEMY_DIED_EVENT_MESSAGE;
20-
// cppcheck-suppress unusedStructMember
21-
const std::string ENEMY_REVIVED_EVENT_MESSAGE;
2218
Socket skt;
2319
// cppcheck-suppress unusedStructMember
2420
bool is_skt_closed;
@@ -27,7 +23,8 @@ class GameProtocolClient {
2723
/*
2824
* Receives all the information from the server to build a valid GameMessage
2925
*/
30-
const GameMessage receive_message_from_server();
26+
const GameMessage receive_message_from_server(const std::string& ENEMY_DIED_EVENT_MESSAGE,
27+
const std::string& ENEMY_REVIVED_EVENT_MESSAGE);
3128

3229

3330
/*
@@ -50,7 +47,9 @@ class GameProtocolClient {
5047
*/
5148
const uint8_t receive_enemy_event();
5249

53-
const std::string get_enemy_event_message(const uint8_t& enemy_event_code) const;
50+
const std::string get_enemy_event_message(const uint8_t& enemy_event_code,
51+
const std::string& ENEMY_DIED_EVENT_MESSAGE,
52+
const std::string& ENEMY_REVIVED_EVENT_MESSAGE) const;
5453

5554
public:
5655
/*
@@ -81,7 +80,9 @@ class GameProtocolClient {
8180
* Calls GameProtocolClient::receive_message_from_server a number of times represented by the
8281
* parameter number_of_messages and returns a vector will all the messages built
8382
*/
84-
const std::vector<GameMessage> receive_messages_from_server(const uint32_t& number_of_messages);
83+
const std::vector<GameMessage> receive_messages_from_server(
84+
const uint32_t& number_of_messages, const std::string& ENEMY_DIED_EVENT_MESSAGE,
85+
const std::string& ENEMY_REVIVED_EVENT_MESSAGE);
8586

8687
/*
8788
* Return true if the socket connection with the ser is still open or false if it's closed

client_src/jazz_jackrabbit_2_game_client.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ static Logger& logger = Logger::get_instance();
1919
static const char USER_OPTION_ATTACK[] = "ATACAR";
2020
static const char USER_OPTION_RECEIVE_MSG_FROM_SERVER[] = "LEER";
2121
static const char USER_OPTION_QUIT[] = "SALIR";
22+
static const char ENEMY_DIED_EVENT_MESSAGE[] = "Un enemigo ha muerto.";
23+
static const char ENEMY_REVIVED_EVENT_MESSAGE[] = "Un enemigo ha revivido.";
2224

2325
JazzJackrabbit2GameClient::JazzJackrabbit2GameClient(const std::string& hostname,
2426
const std::string& service):
@@ -125,8 +127,8 @@ void JazzJackrabbit2GameClient::execute_read_from_server(
125127

126128
const uint32_t number_of_messages = std::atoi(user_input_vectorized[1].c_str());
127129
logger.client_info("Reading from server: " + std::to_string(number_of_messages) + " messages.");
128-
std::vector<GameMessage> messages =
129-
this->protocol_client.receive_messages_from_server(number_of_messages);
130+
std::vector<GameMessage> messages = this->protocol_client.receive_messages_from_server(
131+
number_of_messages, ENEMY_DIED_EVENT_MESSAGE, ENEMY_REVIVED_EVENT_MESSAGE);
130132
this->print_messages_from_server(messages);
131133
}
132134

0 commit comments

Comments
 (0)