Changeset 5579:3887ff7af8e7 in roaraudio for plugins/universal/tic-tac-toe.c
- Timestamp:
- 07/21/12 19:40:17 (12 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
plugins/universal/tic-tac-toe.c
r5569 r5579 24 24 */ 25 25 26 #include <roar d/include/roard.h>26 #include <roaraudio.h> 27 27 28 28 #define _FREE ' ' … … 89 89 }; 90 90 91 static inline int __movein(struct roar_buffer ** buf, struct roar_buffer ** next) { 92 if ( *buf == NULL ) { 93 *buf = *next; 94 *next = NULL; 95 return 0; 96 } else { 97 return roar_buffer_moveinto(*buf, next); 98 } 99 } 100 91 101 static void new_game(game_state * state) { 92 102 memset(state, _FREE, sizeof(game_state)); … … 211 221 } 212 222 213 static void draw_help( int client) {223 static void draw_help(struct roar_buffer ** obuffer) { 214 224 struct roar_buffer * buf; 215 225 ssize_t len = 0; … … 225 235 if ( roar_buffer_set_len(buf, len) == -1 ) { 226 236 roar_buffer_free(buf); 227 clients_delete(client); 228 } 229 230 clients_add_output(client, &buf); 231 } 232 233 static void draw_game(int client, game_state * state, const char * info) { 237 } 238 239 __movein(obuffer, &buf); 240 } 241 242 static void draw_game(game_state * state, const char * info, struct roar_buffer ** obuffer) { 234 243 struct roar_buffer * buf; 235 244 ssize_t len = 0; … … 249 258 if ( roar_buffer_set_len(buf, len) == -1 ) { 250 259 roar_buffer_free(buf); 251 clients_delete(client); 252 } 253 254 clients_add_output(client, &buf); 255 } 256 257 int new_client(int client, struct roar_vio_calls * vio, struct roard_listen * lsock) { 258 struct roar_client_server * cs; 260 } 261 262 __movein(obuffer, &buf); 263 } 264 265 static int _set_proto(int client, struct roar_vio_calls * vio, struct roar_buffer ** obuffer, void ** userdata, const struct roar_keyval * para, ssize_t paralen) { 259 266 game_state * state; 260 267 261 (void)vio, (void)lsock; 262 263 if ( clients_get_server(client, &cs) == -1 ) 264 return -1; 268 (void)client, (void)vio, (void)obuffer, (void)para, (void)paralen; 265 269 266 270 state = roar_mm_malloc(sizeof(game_state)); … … 270 274 new_game(state); 271 275 272 cs->protoinst= state;273 274 draw_game( client, state, MSG__YOUR_TURN);276 *userdata = state; 277 278 draw_game(state, MSG__YOUR_TURN, obuffer); 275 279 return 0; 276 280 } … … 307 311 } 308 312 309 static int check_client(int client, struct roar_vio_calls * vio) { 310 struct roar_client_server * cs; 313 static int _handle(int client, struct roar_vio_calls * vio, struct roar_buffer ** obuffer, void ** userdata, const struct roar_keyval * para, ssize_t paralen) { 311 314 char buf[32]; 312 315 ssize_t len; … … 317 320 char c; 318 321 322 (void)client, (void)obuffer, (void)para, (void)paralen; 323 319 324 ROAR_DBG("check_client(client=%i, vio=%p) = ?", client, vio); 320 325 321 if ( clients_get_server(client, &cs) == -1 ) 326 len = roar_vio_read(vio, buf, sizeof(buf)); 327 328 if ( len < 1 ) { 322 329 return -1; 323 324 len = roar_vio_read(vio, buf, sizeof(buf)); 325 326 if ( len < 1 ) { 327 clients_delete(client); 328 return -1; 329 } 330 331 if ( check_won(cs->protoinst) ) { 330 } 331 332 if ( check_won(*userdata) ) { 332 333 for (i = 0; i < len; i++) { 333 334 if ( map_input(buf[i]) == 'q' ) { 334 clients_delete(client);335 335 return -1; 336 336 } else { 337 new_game( cs->protoinst);338 draw_game( client, cs->protoinst, MSG__YOUR_TURN);337 new_game(*userdata); 338 draw_game(*userdata, MSG__YOUR_TURN, obuffer); 339 339 return 0; 340 340 } … … 346 346 switch (c) { 347 347 case 'q': 348 clients_delete(client);349 348 return -1; 350 349 break; 351 350 case 'n': 352 new_game( cs->protoinst);353 draw_game( client, cs->protoinst, MSG__YOUR_TURN);351 new_game(*userdata); 352 draw_game(*userdata, MSG__YOUR_TURN, obuffer); 354 353 return 0; 355 354 break; 356 355 case 'H': 357 draw_help( client);356 draw_help(obuffer); 358 357 return 0; 359 358 break; 360 359 case 'e': 361 draw_game( client, cs->protoinst, MSG__YOUR_TURN);360 draw_game(*userdata, MSG__YOUR_TURN, obuffer); 362 361 return 0; 363 362 break; 364 363 case '0': case '1': case '2': case '3': case '4': 365 364 case '5': case '6': case '7': case '8': 366 if ( try_put( cs->protoinst, c - '0', _PLAYER) == -1 ) {365 if ( try_put(*userdata, c - '0', _PLAYER) == -1 ) { 367 366 msg = MSG__CANNOT_PUT_HERE; 368 draw_game( client, cs->protoinst, msg);367 draw_game(*userdata, msg, obuffer); 369 368 return 0; 370 369 } else { … … 377 376 } 378 377 379 won = check_won( cs->protoinst);378 won = check_won(*userdata); 380 379 381 380 if ( !won && player_move != -1 ) { 382 auto_move( cs->protoinst, player_move);383 won = check_won( cs->protoinst);381 auto_move(*userdata, player_move); 382 won = check_won(*userdata); 384 383 } 385 384 … … 390 389 } 391 390 392 draw_game( client, cs->protoinst, msg);391 draw_game(*userdata, msg, obuffer); 393 392 394 393 ROAR_DBG("check_client(client=%i, vio=%p) = 0", client, vio); … … 397 396 398 397 399 static struct roard_proto proto[1] = { 400 {ROAR_PROTO_GAME, ROAR_SUBSYS_NONE, "Simple Tic-Tac-Toe game", NULL, new_client, NULL, check_client, NULL, NULL} 398 static const struct roar_dl_proto proto = { 399 .proto = ROAR_PROTO_GAME, 400 .description = "Simple Tic-Tac-Toe game", 401 .flags = ROAR_DL_PROTO_FLAGS_NONE, 402 .set_proto = _set_proto, 403 .unset_proto = NULL, 404 .handle = _handle, 405 .flush = NULL, 406 .flushed = NULL, 407 .status = NULL 401 408 }; 402 409 403 ROARD_DL_REG_PROTO(proto) 410 static int __reg_proto(struct roar_dl_librarypara * para, struct roar_dl_libraryinst * lib) { 411 (void)para, (void)lib; 412 ROAR_DL_PLUGIN_REG_FN(ROAR_DL_PROTO_SUBTYPE, proto, ROAR_DL_PROTO_VERSION); 413 return 0; 414 } 404 415 405 416 ROAR_DL_PLUGIN_START(protocol_tic_tac_toe) { 406 ROARD_DL_CHECK_VERSIONS();407 408 417 ROAR_DL_PLUGIN_META_PRODUCT_NIV("protocol-tic-tac-toe", ROAR_VID_ROARAUDIO, ROAR_VNAME_ROARAUDIO); 409 418 ROAR_DL_PLUGIN_META_VERSION(ROAR_VERSION_STRING); … … 412 421 ROAR_DL_PLUGIN_META_DESC("Implementation of a the well known game Tic-Tac-Toe"); 413 422 414 ROAR D_DL_REGFN_PROTO();423 ROAR_DL_PLUGIN_REG(ROAR_DL_FN_PROTO, __reg_proto); 415 424 } ROAR_DL_PLUGIN_END 416 425
Note: See TracChangeset
for help on using the changeset viewer.