3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 * Chat msg m <textmessage>
33 * Join to game j <game>
35 * Create game g <game>
41 /* Protocol definition */
42 #define PROTO_CLIENT_CHAT 'm'
43 #define PROTO_CLIENT_JOIN 'j'
44 #define PROTO_CLIENT_NICK 'n'
45 #define PROTO_CLIENT_GAME 'g'
46 #define PROTO_CLIENT_DELG 'd'
47 #define PROTO_CLIENT_SYNC 's'
48 #define PROTO_CLIENT_PPOS 'p'
49 #define PROTO_CLIENT_LIST 'l'
51 /* Send message to all players except sender client */
52 int proto_client_chat (client_t
*client
, char *buf
, unsigned len
)
54 unsigned nick_len
= 0;;
58 nick_len
= strlen (client
->nick
);
60 char *str
= (char *) malloc (sizeof (char) * ( + len
+ 4));
65 /* build protocol chat message */
66 memcpy (str
, "m ", 2);
69 memcpy (str
+2, client
->nick
, nick_len
);
71 memcpy (str
+2, "Anonym", 6);
75 memcpy (str
+2+nick_len
, " ", 1);
76 memcpy (str
+3+nick_len
, buf
, len
);
80 /* when player play, chat will works only on opponent */
81 game_t
*game
= game_findbyclient (client
);
86 if (game
->white
== client
)
91 ret
= client_pmmsg (c
, str
, 3+nick_len
+len
);
93 ret
= client_msgtoall2 (client
, str
, 3+nick_len
+len
);
97 printf ("-> Message '%s' : '%s'\n", client
->nick
, buf
);
102 int proto_client_join (client_t
*client
, char *buf
, unsigned len
)
104 game_t
*game
= game_findbyclient (client
);
109 game
= game_find (buf
);
114 if (!game_join (client
, game
))
117 printf ("-> Player '%s' join to game '%s'\n", client
->nick
, game
->name
);
122 int proto_client_nick (client_t
*client
, char *buf
, unsigned len
)
130 client
->nick
= (char *) malloc (sizeof (char) * (len
+ 1));
135 memcpy (client
->nick
, buf
, len
);
136 client
->nick
[len
] = '\0';
138 printf ("-> Nick setup: '%s'\n", client
->nick
);
143 int proto_client_game (client_t
*client
, char *buf
, unsigned len
)
145 game_t
*game
= game_findbyclient (client
);
150 game
= game_find (buf
);
155 game
= game_new (client
, buf
, len
);
160 printf ("-> Game '%s' was created by '%s'\n", game
->name
, client
->nick
);
165 int proto_client_delg (client_t
*client
, char *buf
, unsigned len
)
167 game_t
*game
= game_findbyclient (client
);
172 if (!game_quit (game
))
175 printf ("-> Player '%s' close game\n", client
->nick
);
180 int proto_client_sync (client_t
*client
, char *buf
, unsigned len
)
182 game_t
*game
= game_findbyclient (client
);
187 if (!game_sync (client
, game
))
190 printf ("-> Player '%s' make sync\n", client
->nick
);
195 int proto_client_ppos (client_t
*client
, char *buf
, unsigned len
)
200 game_t
*game
= game_findbyclient (client
);
208 if (game
->player
== 'w' && game
->white
!= client
)
210 else if (game
->player
== 'b' && game
->black
!= client
)
222 x
= buf
[0]; // pismena souradnice
225 if ((x
>= 0) && (x
<= 7)) {
230 y
= buf
[1]; // ciselna souradnice
233 if ((y
>= 0) && (y
<= 7)){
238 x
= buf
[3]; // pismena souradnice
241 if ((x
>= 0) && (x
<= 7)){
246 y
= buf
[4]; // ciselna souradnice
249 if ((y
>= 0) && (y
<= 7)){
255 puts ("-> Bad move position");
259 if (rules_verify (game
, zx
, zy
, cx
, cy
))
262 if (!game_pos (client
, game
, zx
, zy
, cx
, cy
))
265 if (game
->player
== 'w')
267 else if (game
->player
== 'b')
270 /* send new valid position to players of this game */
272 client_pmmsg (game
->black
, buf
-2, len
+2);
274 client_pmmsg (game
->white
, buf
-2, len
+2);
276 printf ("-> Player '%s' change %c:%c to %c:%c\n", client
->nick
, buf
[0], buf
[1], buf
[3], buf
[4]);
281 int proto_client_list (client_t
*client
, char *buf
, unsigned len
)
283 if (!game_getlist (client
))
286 printf ("-> Player '%s' get list of games\n", client
->nick
);
291 /** Protocol data handler */
292 int proto_handler (client_t
*client
, char *buf
, unsigned len
)
294 /* stuff for netcat - rewrite \n character with \0 */
296 if (buf
[len
-1] == '\n') {
303 if (c
== PROTO_CLIENT_CHAT
)
304 return proto_client_chat (client
, buf
+2, len
-2);
305 if (c
== PROTO_CLIENT_JOIN
)
306 return proto_client_join (client
, buf
+2, len
-2);
307 if (c
== PROTO_CLIENT_NICK
)
308 return proto_client_nick (client
, buf
+2, len
-2);
309 if (c
== PROTO_CLIENT_GAME
)
310 return proto_client_game (client
, buf
+2, len
-2);
311 if (c
== PROTO_CLIENT_DELG
)
312 return proto_client_delg (client
, buf
+2, len
-2);
313 if (c
== PROTO_CLIENT_SYNC
)
314 return proto_client_sync (client
, buf
+2, len
-2);
315 if (c
== PROTO_CLIENT_PPOS
)
316 return proto_client_ppos (client
, buf
+2, len
-2);
317 if (c
== PROTO_CLIENT_LIST
)
318 return proto_client_list (client
, buf
+2, len
-2);
324 /** Protocol Init function */