/* Callback for GGZDMOD_EVENT_STATE */ static void _handle_ggz_state(GGZdMod *ggz, GGZdModEvent event, const void *data) { switch(ggzdmod_get_state(ggz)) { case GGZDMOD_STATE_PLAYING: break; case GGZDMOD_STATE_WAITING: { GGZSeat seat; int i; int robot_socket; int max = ggzdmod_get_num_seats( g_ggzdmod ); // launch robots if necesary for (i = 0; i < max; i++) { seat = ggzdmod_get_seat( g_ggzdmod, i ); if( seat.type == GGZ_SEAT_BOT ) { if (launch_robot( &robot_socket, "--ggz" ) == TEG_STATUS_SUCCESS ) { seat.fd = robot_socket; ggzdmod_set_seat( g_ggzdmod, &seat ); net_printf(seat.fd,"%s=1\n",TOKEN_GGZ); } else ggzdmod_log(g_ggzdmod, "\tegserver: Failed launching bot :-(!"); } } break; } case GGZDMOD_STATE_DONE: break; default: break; } }
/* Handle message from player */ static void _handle_ggz_player(GGZdMod *ggz, GGZdModEvent event, const void *data) { int num = *(int*)data; int fd; GGZSeat seat; seat = ggzdmod_get_seat(ggz, num); fd = seat.fd; if(play_teg( fd )== TEG_STATUS_CONNCLOSED) { seat.type = GGZ_SEAT_OPEN; ggzdmod_set_seat( g_ggzdmod, &seat ); } }
int main (void) { GGZdMod *mod; GGZdModType type; GGZSeat control[8]; int test = 5, num, i; void * ptr; /* Only enable memory debugging */ ggz_debug_init(NULL, NULL); ggz_debug_enable("MEMCHECK"); printf("-- Testing object creation -- \n"); /* Test with an invalid parameter */ mod = ggzdmod_new(-1); if (mod != NULL) printf("-- Error : ggzdmod_new(-1) returns %p\n", mod); else printf("-- Success: ggzdmod_new(-1) returns NULL.\n"); /* Test ggz side object */ mod = ggzdmod_new(GGZDMOD_GGZ); type = ggzdmod_get_type(mod); if (type == GGZDMOD_GGZ) printf("-- Success: creation of type GGZDMOD_GGZ\n"); else printf("-- Error: type %d != GGZDMOD_GGZ\n", type); ggzdmod_free(mod); /* Test game side object */ mod = ggzdmod_new(GGZDMOD_GAME); type = ggzdmod_get_type(mod); if (type == GGZDMOD_GAME) printf("-- Success: creation of type GGZDMOD_GAME\n"); else printf("-- Error: type %d != GGZDMOD_GAME\n", type); ggzdmod_free(mod); /* Test various accessor functions */ mod = ggzdmod_new(GGZDMOD_GGZ); if (ggzdmod_get_state(mod) != GGZDMOD_STATE_CREATED) printf("-- Error: initial state: %d\n", ggzdmod_get_state(mod)); if (ggzdmod_get_fd(mod) != -1) printf("-- Error: initial fd: %d\n", ggzdmod_get_fd(mod)); if (ggzdmod_get_num_seats(mod) > 0) printf("-- Error: initial seats: %d\n", ggzdmod_get_num_seats(mod)); if (ggzdmod_get_gamedata(mod) != NULL) printf("-- Error: initial gamedata: %p\n", ggzdmod_get_gamedata(mod)); /* Set setting/getting game data */ ggzdmod_set_gamedata(mod, &test); ptr = ggzdmod_get_gamedata(mod); if (ptr == &test) printf("-- Success: setting of gamedata to %p\n", &test); else printf("-- Error: set data to %p, got %p\n", &test, ptr); ggzdmod_set_gamedata(mod, NULL); ptr = ggzdmod_get_gamedata(mod); if (ptr == NULL) printf("-- Success: setting of gamedata to %p\n", NULL); else printf("-- Error: set data to %p, got %p\n", NULL, ptr); /* Set setting/getting num_seats */ ggzdmod_set_num_seats(mod, 3); num = ggzdmod_get_num_seats(mod); if (num == 3) printf("-- Success: setting of seats to %d\n", 3); else printf("-- Error: set seats to %d, got %d\n", 3, num); /* Check initial value of seats */ for (i = 0; i < 8; i++) { control[i].num = i; control[i].type = GGZ_SEAT_OPEN; control[i].name = NULL; control[i].fd = -1; } compare_seats(mod, 3, control); /* Test seat setting/getting */ control[0].num = 0; control[0].type = GGZ_SEAT_PLAYER; control[0].name = "Test 0"; control[0].fd = 17; ggzdmod_set_seat(mod, &control[0]); control[1].num = 1; control[1].type = GGZ_SEAT_BOT; control[1].name = "Test 1"; control[1].fd = 32; ggzdmod_set_seat(mod, &control[1]); control[2].num = 2; control[2].type = GGZ_SEAT_RESERVED; control[2].name = "Test 2"; control[2].fd = 80; ggzdmod_set_seat(mod, &control[2]); compare_seats(mod, 3, control); /* Pass NULL in to each function. */ printf(" -- Testing passing ggzdmod=NULL to functions --\n"); ggzdmod_free(NULL); if (ggzdmod_get_fd(NULL) != -1) printf(" -- Error: ggzdmod_get_fd(NULL) == %d\n", ggzdmod_get_fd(NULL)); (void)ggzdmod_get_type(NULL); (void)ggzdmod_get_state(NULL); if (ggzdmod_get_num_seats(NULL) > 0) printf(" -- Error: ggzdmod_get_num_seats(NULL) == %d\n", ggzdmod_get_num_seats(NULL)); (void)ggzdmod_get_seat(NULL, 0); if (ggzdmod_get_gamedata(NULL) != NULL) printf(" -- Error: ggzdmod_get_gamedata(NULL) == %p\n", ggzdmod_get_gamedata(NULL)); ggzdmod_set_num_seats(NULL, 8); ggzdmod_set_handler(NULL, GGZDMOD_EVENT_PLAYER_DATA, test_handler); ggzdmod_set_module(NULL, "<GAME>", "/tmp", test_args); if (ggzdmod_set_seat(NULL, &control[0]) >= 0) printf(" -- Error: ggzdmod_set_seat(NULL, ...) claimed to succeed.\n"); if (ggzdmod_dispatch(NULL) >= 0) printf(" -- Error: ggzdmod_dispatch(NULL) claimed to succeed.\n"); if (ggzdmod_loop(NULL) >= 0) printf(" -- Error: ggzdmod_loop(NULL) claimed to succeed.\n"); if (ggzdmod_set_state(NULL, GGZDMOD_STATE_DONE) >= 0) printf(" -- Error: ggzdmod_set_state(NULL, GGZDMOD_STATE_DONE) claimed to succeed.\n"); if (ggzdmod_connect(NULL) >= 0) printf(" -- Error: ggzdmod_connect(NULL) claimed to succeed.\n"); if (ggzdmod_disconnect(NULL) >= 0) printf(" -- Error: ggzdmod_disconnect(NULL) claimed to succeed.\n"); if (ggzdmod_log(NULL, "Test message") >= 0) printf(" -- Error: ggzdmod_log(NULL, ...) claimed to succeed.\n"); ggzdmod_free(mod); /* FIXME: test all functions with a NULL object */ /* FIXME: generally test functions with bad parameter values */ ggz_debug_cleanup(GGZ_CHECK_MEM); printf(" -- All tests complete -- \n"); return 0; }