Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / apps / openchess / src / proto.c
blob55f7a0261ea9fe50b14af49496270d4fae6b7e16
1 /*
2 * ZeX/OS
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/>.
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include "config.h"
25 #include "client.h"
26 #include "rules.h"
27 #include "game.h"
30 /** PROTOCOL **
31 * Action Command
32 * Chat msg m <textmessage>
33 * Join to game j <game>
34 * Setup nick n <nick>
35 * Create game g <game>
36 * Delete game d
37 * Synchronize s
38 * List all games l
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;;
56 /* check for nick */
57 if (client->nick)
58 nick_len = strlen (client->nick);
60 char *str = (char *) malloc (sizeof (char) * ( + len + 4));
62 if (!str)
63 return 0;
65 /* build protocol chat message */
66 memcpy (str, "m ", 2);
68 if (nick_len)
69 memcpy (str+2, client->nick, nick_len);
70 else {
71 memcpy (str+2, "Anonym", 6);
72 nick_len = 6;
75 memcpy (str+2+nick_len, " ", 1);
76 memcpy (str+3+nick_len, buf, len);
78 int ret = 0;
80 /* when player play, chat will works only on opponent */
81 game_t *game = game_findbyclient (client);
83 if (game) {
84 client_t *c = 0;
86 if (game->white == client)
87 c = game->black;
88 else
89 c = game->white;
91 ret = client_pmmsg (c, str, 3+nick_len+len);
92 } else
93 ret = client_msgtoall2 (client, str, 3+nick_len+len);
95 free (str);
97 printf ("-> Message '%s' : '%s'\n", client->nick, buf);
99 return ret;
102 int proto_client_join (client_t *client, char *buf, unsigned len)
104 game_t *game = game_findbyclient (client);
106 if (game)
107 return 0;
109 game = game_find (buf);
111 if (!game)
112 return 0;
114 if (!game_join (client, game))
115 return 0;
117 printf ("-> Player '%s' join to game '%s'\n", client->nick, game->name);
119 return 1;
122 int proto_client_nick (client_t *client, char *buf, unsigned len)
124 if (!len)
125 return 0;
127 if (client->nick)
128 free (client->nick);
130 client->nick = (char *) malloc (sizeof (char) * (len + 1));
132 if (!client->nick)
133 return 0;
135 memcpy (client->nick, buf, len);
136 client->nick[len] = '\0';
138 printf ("-> Nick setup: '%s'\n", client->nick);
140 return 1;
143 int proto_client_game (client_t *client, char *buf, unsigned len)
145 game_t *game = game_findbyclient (client);
147 if (game)
148 return 0;
150 game = game_find (buf);
152 if (game)
153 return 0;
155 game = game_new (client, buf, len);
157 if (!game)
158 return 0;
160 printf ("-> Game '%s' was created by '%s'\n", game->name, client->nick);
162 return 1;
165 int proto_client_delg (client_t *client, char *buf, unsigned len)
167 game_t *game = game_findbyclient (client);
169 if (!game)
170 return 0;
172 if (!game_quit (game))
173 return 0;
175 printf ("-> Player '%s' close game\n", client->nick);
177 return 1;
180 int proto_client_sync (client_t *client, char *buf, unsigned len)
182 game_t *game = game_findbyclient (client);
184 if (!game)
185 return 0;
187 if (!game_sync (client, game))
188 return 0;
190 printf ("-> Player '%s' make sync\n", client->nick);
192 return 1;
195 int proto_client_ppos (client_t *client, char *buf, unsigned len)
197 if (len != 5)
198 return 0;
200 game_t *game = game_findbyclient (client);
202 if (!game)
203 return 0;
205 if (!game->play)
206 return 0;
208 if (game->player == 'w' && game->white != client)
209 return 0;
210 else if (game->player == 'b' && game->black != client)
211 return 0;
213 int x;
214 int y;
215 int ok = 4;
217 char zx = 0;
218 char zy = 0;
219 char cx = 0;
220 char cy = 0;
222 x = buf[0]; // pismena souradnice
223 x -= 'a';
225 if ((x >= 0) && (x <= 7)) {
226 zx = x;
227 ok --;
230 y = buf[1]; // ciselna souradnice
231 y -= ('0' + 1);
233 if ((y >= 0) && (y <= 7)){
234 zy = y;
235 ok --;
238 x = buf[3]; // pismena souradnice
239 x -= 'a';
241 if ((x >= 0) && (x <= 7)){
242 cx = x;
243 ok --;
246 y = buf[4]; // ciselna souradnice
247 y -= ('0' + 1);
249 if ((y >= 0) && (y <= 7)){
250 cy = y;
251 ok --;
254 if (ok) {
255 puts ("-> Bad move position");
256 return 0;
259 if (rules_verify (game, zx, zy, cx, cy))
260 return 0;
262 if (!game_pos (client, game, zx, zy, cx, cy))
263 return 0;
265 if (game->player == 'w')
266 game->player = 'b';
267 else if (game->player == 'b')
268 game->player = 'w';
270 /* send new valid position to players of this game */
271 if (game->black)
272 client_pmmsg (game->black, buf-2, len+2);
273 if (game->white)
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]);
278 return 1;
281 int proto_client_list (client_t *client, char *buf, unsigned len)
283 if (!game_getlist (client))
284 return 0;
286 printf ("-> Player '%s' get list of games\n", client->nick);
288 return 1;
291 /** Protocol data handler */
292 int proto_handler (client_t *client, char *buf, unsigned len)
294 /* stuff for netcat - rewrite \n character with \0 */
295 if (len > 1)
296 if (buf[len-1] == '\n') {
297 len --;
298 buf[len] = '\0';
301 char c = buf[0];
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);
321 return 1;
324 /** Protocol Init function */
325 int init_proto ()
329 return 1;