Skip to content

Commit 7aa2fb0

Browse files
committed
Added code documentation for client classes
1 parent a68b252 commit 7aa2fb0

File tree

6 files changed

+74
-20
lines changed

6 files changed

+74
-20
lines changed

client_src/closed_socket_from_server_exception.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ class ClosedSocketFromServerException: public std::exception {
1414
std::string final_message;
1515

1616
public:
17+
/*
18+
* Exception to be thrown where the socket was unexpectedly closed by the server side.
19+
* It improves std::exception because the information regarding line number and module name
20+
* can be added at call time
21+
*/
1722
explicit ClosedSocketFromServerException(const int32_t& line_number, const std::string& module);
1823

24+
/*
25+
* Returns a message error string containing the exception information
26+
*/
1927
const char* what() const noexcept;
2028
};

client_src/game_protocol_client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,4 @@ void GameProtocolClient::send_attack() {
120120
}
121121

122122

123-
const bool GameProtocolClient::is_connection_alive() const { return this->is_skt_closed; }
123+
const bool GameProtocolClient::is_connection_alive() const { return !this->is_skt_closed; }

client_src/game_protocol_client.h

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,30 @@ class GameProtocolClient {
2424
bool is_skt_closed;
2525
GameCommands game_commands;
2626

27-
27+
/*
28+
* Receives all the information from the server to build a valid GameMessage
29+
*/
2830
const GameMessage receive_message_from_server();
2931

32+
33+
/*
34+
* Receives from the server code that represents the start of a broadcast message
35+
*/
3036
void receive_server_start_broadcast();
3137

38+
/*
39+
* Receives from the server the amount of enemies alive as a halfword (2 bytes)
40+
*/
3241
const uint16_t receive_enemies_alive();
3342

43+
/*
44+
* Receives from the server the amount of enemies dead as a halfword (2 bytes)
45+
*/
3446
const uint16_t receive_enemies_dead();
3547

48+
/*
49+
* Receives from the server the event code as a byte
50+
*/
3651
const uint8_t receive_enemy_event();
3752

3853
const std::string get_enemy_event_message(const uint8_t& enemy_event_code) const;
@@ -59,17 +74,18 @@ class GameProtocolClient {
5974
GameProtocolClient(GameProtocolClient&&) = default;
6075
GameProtocolClient& operator=(GameProtocolClient&&) = default;
6176

62-
/*
63-
* "Plays" the action sequence. Meaning that it converts it to action codes and communicates to
64-
* the server with GameProtocolClient::send_action_sequence and delegates the processing to the
65-
* server, using then GameProtocolClient::receive_processed_actions to get the processed output
66-
*/
67-
std::string play_action_sequence(const std::vector<std::string>& actions);
68-
6977
void send_attack();
7078

79+
80+
/*
81+
* Calls GameProtocolClient::receive_message_from_server a number of times represented by the
82+
* parameter number_of_messages and returns a vector will all the messages built
83+
*/
7184
const std::vector<GameMessage> receive_messages_from_server(const uint32_t& number_of_messages);
7285

86+
/*
87+
* Return true if the socket connection with the ser is still open or false if it's closed
88+
*/
7389
const bool is_connection_alive() const;
7490
};
7591

client_src/jazz_jackrabbit_2_game_client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const bool JazzJackrabbit2GameClient::is_option_valid(const std::string& option_
8282

8383
const bool JazzJackrabbit2GameClient::should_game_end(const std::string& option_to_validate) const {
8484
if (this->are_options_equal(option_to_validate, this->USER_OPTION_QUIT) ||
85-
this->protocol_client.is_connection_alive()) {
85+
!this->protocol_client.is_connection_alive()) {
8686
return true;
8787
}
8888
return false;

client_src/jazz_jackrabbit_2_game_client.h

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,41 +35,66 @@ class JazzJackrabbit2GameClient {
3535
// cppcheck-suppress unusedStructMember
3636
const std::unordered_set<std::string> available_user_options;
3737

38+
3839
/*
39-
* Reads from stdin line by line the sequence actions that the user wants to be performed.
40-
* Then it lets the protocol handle the sending to the server of the built sequence with
41-
* GameProtocolClient::play_action_sequence, which also waits for the server to respond with
42-
* the output processed combos
40+
* Compares two options in equal conditions (both are sanitized with
41+
* JazzJackrabbit2GameClient::sanitize_string) and returns true if they are equal
4342
*/
44-
void read_and_play_action_lines();
45-
4643
const bool are_options_equal(const std::string& option1, const std::string& option2) const;
4744

45+
/*
46+
* Returns true if option_to_validate is included in the available_user_options
47+
*/
4848
const bool is_option_valid(const std::string& option_to_validate) const;
4949

50+
/*
51+
* Returns true if option_to_validate is the option JazzJackrabbit2GameClient::USER_OPTION_QUIT
52+
* Or if the socket connection from the protocol_client has closed
53+
*/
5054
const bool should_game_end(const std::string& option_to_validate) const;
5155

56+
/*
57+
* Saves in user_input_vectorized the user_input broken into each word one vector element
58+
*/
5259
void vectorize_user_input(const std::string& user_input,
53-
5460
std::vector<std::string>& user_input_vectorized);
5561

62+
/*
63+
* Calls the appropiate method depending on the desired action
64+
*/
5665
void execute_command(const std::vector<std::string>& user_input_vectorized);
5766

67+
/*
68+
* Returns an uppercase copy of the string
69+
*/
5870
const std::string string_to_uppercase(const std::string& string) const;
5971

72+
/*
73+
* Returns the result of JazzJackrabbit2GameClient::string_to_uppercase
74+
*/
6075
const std::string sanitize_string(const std::string& string) const;
6176

77+
/*
78+
* Uses the client_protocol to send an attack to the server
79+
*/
6280
void execute_attack(const std::vector<std::string>& user_input_vectorized);
6381

82+
/*
83+
* Uses the client_protocol to block the I/O until read all the messages expressed
84+
* by the user
85+
*/
6486
void execute_read_from_server(const std::vector<std::string>& user_input_vectorized);
6587

88+
/*
89+
* Prints to std::out the message read from the server
90+
*/
6691
void print_messages_from_server(const std::vector<GameMessage>& messages) const;
6792

6893
public:
6994
/*
7095
* JazzJackrabbit2GameClient is the client side application interface for the game
7196
* JazzJackrabbit2. It is offered standalone, so everything that the game needs to function is
72-
* included when instancing this class. It throws an std::runtime_error if the number of
97+
* included when instancing this class. It throws an exception if the number of
7398
* arguments argv is not EXPECTED_ARGUMENTS. The error exit code can be obtained with
7499
* JazzJackrabbit2GameClient::get_exit_code_error.
75100
*/
@@ -95,8 +120,7 @@ class JazzJackrabbit2GameClient {
95120
static const int8_t get_exit_code_error() noexcept;
96121

97122
/*
98-
* Executes JazzJackrabbit2GameClient::read_and_play_action_lines and returns
99-
* JazzJackrabbit2GameClient::EXIT_CODE_OK
123+
* Starts and runs the client game loop
100124
*/
101125
const int8_t run_game();
102126
};

common_src/logger.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ class Logger {
2121
const std::string LEVEL_ERROR;
2222
// cppcheck-suppress unusedStructMember
2323
std::ofstream logfile;
24+
25+
26+
/*
27+
* If set to false in constructor the log calls to this class will not write anything to the
28+
* file, nor it will open the file
29+
*/
2430
// cppcheck-suppress unusedStructMember
2531
bool is_activated;
2632

0 commit comments

Comments
 (0)