-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#messages.h#
165 lines (142 loc) · 4.65 KB
/
#messages.h#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**************************************************************************
* The reference server implementation of the EE122 Project #1
*
* Author: Junda Liu (liujd@cs)
* Author: DK Moon (dkmoon@cs)
* Author: David Zats (dzats@cs)
**************************************************************************/
#ifndef _messages_h_
#define _messages_h_
#include <assert.h>
#include "constants.h"
/**************************************************************************
* Message functions for the client
************************************************************************/
static inline void show_prompt()
{
fprintf(stdout, "command> ");
fflush(stdout);
}
/* Error messages for the client. */
static inline void on_client_connect_failure()
{
fprintf(stdout,
"The gate to the tiny world of warcraft is not ready.\n");
}
static inline void on_malformed_message_from_server()
{
fprintf(stdout, "Meteor is striking the world.\n");
abort();
}
static inline void on_disconnection_from_server()
{
fprintf(stdout,
"The gate to the tiny world of warcraft has disappeared.\n");
}
static inline void on_not_visible()
{
fprintf(stdout, "The target is not visible.\n");
show_prompt();
}
static inline void on_login_reply(const int error_code)
{
if (error_code == 0) {
fprintf(stdout, "Welcome to the tiny world of warcraft.\n");
} else if (error_code == 1) {
fprintf(stdout, "A player with the same name is already in the game.\n");
} else {
/* This must not happen. The caller of this function must check the
error code is either 0 or 1 and must perform malformed message
handling before this function. We simply die here. */
assert(0);
}
show_prompt();
}
static inline void on_move_notify(const char *player_name,
const uint8_t x,
const uint8_t y,
const uint32_t hp,
const uint32_t exp)
{
fprintf(stdout, "%s: location=(%u,%u), HP=%u, EXP=%u\n",
player_name, x, y, hp, exp);
show_prompt();
}
static inline void on_attack_notify(const char *attacker_name,
const char *victim_name,
const int damage,
const int updated_hp)
{
if (updated_hp <= 0) {
fprintf(stdout, "%s killed %s\n", attacker_name, victim_name);
} else {
fprintf(stdout, "%s damaged %s by %d. %s's HP is now %d\n",
attacker_name, victim_name, damage, victim_name, updated_hp);
}
show_prompt();
}
static inline void on_speak_notify(const char *speaker, const char *message)
{
fprintf(stdout, "%s: %s\n", speaker, message);
show_prompt();
}
static inline void on_exit_notify(const char *player_name)
{
fprintf(stdout, "Player %s has left the tiny world of warcraft.\n",
player_name);
show_prompt();
}
static inline void on_invalid_state(const int error_code)
{
if (error_code == 0) {
fprintf(stdout, "You must log in first.\n");
} else if (error_code == 1) {
fprintf(stdout, "You already logged in.\n");
} else {
/* This must not happen. The caller of this function must check the
error code is either 0 or 1, and must perform malformed message
handling before this function. We simply die here. */
assert(0);
}
show_prompt();
}
/**************************************************************************
* Message functions for the server
************************************************************************/
/* Error messages for the server. */
static inline const char *message_on_server_port_bind_failure()
{
return "The gate cannot be opened there.";
}
/**************************************************************************
* Utility functions
************************************************************************/
/* Returns 1 if the name is valid. */
static inline int check_player_name(const char *name)
{
size_t i;
/* Must not be null. */
if (!name[0]) return 0;
for (i = 0; i <= EE122_MAX_NAME_LENGTH; ++ i) {
if (!name[i]) return 1;
/* Must not contain a non-alphanumeric character. */
if (!isalnum(name[i])) return 0;
}
/* Must be null terminated. */
return 0;
}
/* Returns if 1 if the text message is valid. */
static inline int check_player_message(const char *message)
{
size_t i;
/* Must not be null. */
if (!message[0]) return 0;
for (i = 0; i <= EE122_MAX_MSG_LENGTH; ++ i) {
if (!message[i]) return 1;
/* Must not contain a non-printable character. */
if (!isprint(message[i])) return 0;
}
/* Must be null terminated. */
return 0;
}
#endif /* _messages_h_ */