static int _io_read_rankings(GGZMod *ggzmod) { int i, num; char *name; int position, score; GGZList *rankings; if (ggz_read_int(ggzmod->fd, &num) < 0) return -1; rankings = ggz_list_create(NULL, NULL, (ggzEntryDestroy)rankings_free, GGZ_LIST_ALLOW_DUPS); for (i = 0; i < num; i++) { if (ggz_read_string_alloc(ggzmod->fd, &name) < 0 || ggz_read_int(ggzmod->fd, &position) < 0 || ggz_read_int(ggzmod->fd, &score) < 0) { return -1; } GGZRanking *rank = (GGZRanking*)ggz_malloc(sizeof(GGZRanking)); rank->position = position; rank->score = score; rank->name = name; ggz_list_insert(rankings, rank); } _ggzmod_handle_rankings(ggzmod, rankings); ggz_list_free(rankings); return 0; }
static int _io_read_info(GGZMod *ggzmod) { int i, num; int seat_num; char *realname, *photo, *host; GGZList *infos; if (ggz_read_int(ggzmod->fd, &num) < 0) return -1; infos = ggz_list_create(NULL, NULL, (ggzEntryDestroy)infos_free, GGZ_LIST_ALLOW_DUPS); for (i = 0; i < num; i++) { if (ggz_read_int(ggzmod->fd, &seat_num) < 0 || ggz_read_string_alloc_null(ggzmod->fd, &realname) < 0 || ggz_read_string_alloc_null(ggzmod->fd, &photo) < 0 || ggz_read_string_alloc_null(ggzmod->fd, &host) < 0) { return -1; } GGZPlayerInfo *info = (GGZPlayerInfo*)ggz_malloc(sizeof(GGZPlayerInfo)); info->realname = realname; info->photo = photo; info->host = host; info->num = seat_num; ggz_list_insert(infos, info); } _ggzmod_handle_info(ggzmod, infos); ggz_list_free(infos); return 0; }
static int get_seat (void) { if (ggz_read_int (fd, &num_players) < 0 || ggz_read_int (fd, &seat) < 0) return -1; gnibbles_properties_set_worms_number (num_players); gnibbles_properties_set_ai_number (0); return 0; }
static int get_noboni (void) { int x, y; if (ggz_read_int (fd, &x) < 0 || ggz_read_int (fd, &y) < 0) return -1; gnibbles_remove_spec_bonus (x, y); return 0; }
static int get_move (void) { int player, move; if (ggz_read_int (fd, &player) < 0 || ggz_read_int (fd, &move) < 0) return -1; /* show some kind of pregame thing.... */ worm_set_direction (player, move); return 0; }
static int get_boni (void) { int x, y, type, fake, countdown; if (ggz_read_int (fd, &x) < 0 || ggz_read_int (fd, &y) < 0 || ggz_read_int (fd, &type) < 0 || ggz_read_int (fd, &fake) < 0 || ggz_read_int (fd, &countdown) < 0) return -1; gnibbles_add_spec_bonus (x, y, type, fake, countdown); return 0; }
static int get_settings (void) { int speed, fakes, startlevel; if (ggz_read_int (fd, &speed) < 0 || ggz_read_int (fd, &fakes) < 0 || ggz_read_int (fd, &startlevel) < 0) return -1; gnibbles_properties_set_speed (speed); gnibbles_properties_set_fakes (fakes); gnibbles_properties_set_start_level (startlevel); return 0; }
/* Functions for reading messages */ int _io_read_data(GGZMod *ggzmod) { int op; if (ggz_read_int(ggzmod->fd, &op) < 0) return -1; if (ggzmod->type == GGZMOD_GAME) { switch ((ControlToTable)op) { case MSG_GAME_LAUNCH: return _io_read_msg_launch(ggzmod); case MSG_GAME_SERVER: return _io_read_msg_server(ggzmod); case MSG_GAME_SERVER_FD: return _io_read_msg_server_fd(ggzmod); case MSG_GAME_PLAYER: return _io_read_msg_player(ggzmod); case MSG_GAME_SEAT: return _io_read_msg_seat(ggzmod); case MSG_GAME_SPECTATOR_SEAT: return _io_read_msg_spectator_seat(ggzmod); case MSG_GAME_CHAT: return _io_read_msg_chat(ggzmod); case MSG_GAME_STATS: return _io_read_stats(ggzmod); case MSG_GAME_INFO: return _io_read_info(ggzmod); case MSG_GAME_RANKINGS: return _io_read_rankings(ggzmod); } } return -2; }
int get_gameover() { int winner; GtkWidget *button; if (ggz_read_int(game.fd, &winner) < 0) return -1; // Check if it's ok if ((winner == BLACK && game.black <= game.white) || (winner == WHITE && game.white <= game.black) || (winner == EMPTY && game.black != game.white)) game_status ("Hey! Internal incompatibility! This game was cheated!\n"); if (winner == game.num) game_status("Congratulations! You win!"); else if (winner == -game.num) game_status("That's too bad... you lost!"); else game_status("That's a draw! Not bad!"); button = g_object_get_data(G_OBJECT(main_win), "again_button"); gtk_widget_show(button); return 1; }
static int get_players (void) { int i, old; static int firsttime = 1; char *tmp; if (ggz_read_int (fd, &num_players) < 0) return -1; for (i = 0; i < num_players; i++) { old = seats[i]; if (ggz_read_int (fd, &seats[i]) < 0) return -1; if (seats[i] != GGZ_SEAT_OPEN) { if (ggz_read_string (fd, (char *) &names[i], 17) < 0) return -1; /*display_set_name(i, game.names[i]); */ if (old == GGZ_SEAT_OPEN && !firsttime) { tmp = g_strdup_printf (_("%s joined the game.\n"), names[i]); add_chat_text (tmp); g_free (tmp); } } if (seats[i] == GGZ_SEAT_ABANDONED) { if (i == 0) { tmp = g_strdup_printf (_ ("The game ended because the host %s left the game.\n"), names[i]); add_chat_text (tmp); g_free (tmp); } else { tmp = g_strdup_printf (_("%s left the game.\n"), names[i]); add_chat_text (tmp); g_free (tmp); } } } firsttime = 0; /* game.got_players++; */ return 0; }
static int _io_read_msg_player(GGZMod *ggzmod) { int is_spectator, seat_num; char *name; if (ggz_read_string_alloc_null(ggzmod->fd, &name) < 0 || ggz_read_int(ggzmod->fd, &is_spectator) < 0 || ggz_read_int(ggzmod->fd, &seat_num) < 0) return -1; _ggzmod_handle_player(ggzmod, name, is_spectator, seat_num); if(name) ggz_free(name); return 0; }
static gboolean game_handle_io (GGZMod * mod) { int op = -1; fd = ggzmod_get_server_fd (mod); // Read the fd if (ggz_read_int (fd, &op) < 0) { ggz_error_msg ("Couldn't read the game fd"); return FALSE; } switch (op) { case GN_MSG_SEAT: get_seat (); break; case GN_MSG_PLAYERS: get_players (); break; case GN_MSG_START: gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), MAIN_PAGE); new_game (); break; case GN_MSG_SYNC: get_sync (); break; case GN_MSG_MOVE: get_move (); break; case GN_MSG_SETTINGS: get_settings (); break; case GN_MSG_BONI: get_boni (); break; case GN_MSG_NOBONI: get_noboni (); break; default: ggz_error_msg ("Incorrect opcode %d \n", op); break; } return TRUE; }
static int _io_read_msg_seat(GGZMod *ggzmod) { GGZSeat seat; char *name; int type; if (ggz_read_int(ggzmod->fd, (int*)&seat.num) < 0 || ggz_read_int(ggzmod->fd, &type) < 0 || ggz_read_string_alloc_null(ggzmod->fd, &name) < 0) return -1; /* Set seat values */ seat.type = type; seat.name = name; _ggzmod_handle_seat(ggzmod, &seat); if (seat.name) ggz_free(seat.name); return 0; }
static int _io_read_msg_server(GGZMod *ggzmod) { char *host = NULL, *handle = NULL; int port; if (ggz_read_string_alloc(ggzmod->fd, &host) < 0 || ggz_read_int(ggzmod->fd, &port) < 0 || ggz_read_string_alloc(ggzmod->fd, &handle) < 0) { if (host) ggz_free(host); if (handle) ggz_free(handle); return -1; } _ggzmod_handle_server(ggzmod, host, port, handle); ggz_free(host); ggz_free(handle); return 0; }
static int _io_read_msg_spectator_seat(GGZMod *ggzmod) { GGZSpectatorSeat seat; char *name; if (ggz_read_int(ggzmod->fd, (int*)&seat.num) < 0 || ggz_read_string_alloc_null(ggzmod->fd, &name) < 0) return -1; seat.name = name; _ggzmod_handle_spectator_seat(ggzmod, &seat); if (seat.name) ggz_free(seat.name); return 0; }
static int _io_read_stats(GGZMod *ggzmod) { int players = ggzmod->num_seats; int spectators = ggzmod->num_spectator_seats; GGZStat player_stats[players], *stat, spectator_stats[spectators]; int i; for (i = 0; i < players + spectators; i++) { if (i >= players) { stat = &spectator_stats[i - players]; } else { stat = &player_stats[i]; } if (ggz_read_int(ggzmod->fd, &stat->have_record) < 0 || ggz_read_int(ggzmod->fd, &stat->have_rating) < 0 || ggz_read_int(ggzmod->fd, &stat->have_ranking) < 0 || ggz_read_int(ggzmod->fd, &stat->have_highscore) < 0 || ggz_read_int(ggzmod->fd, &stat->wins) < 0 || ggz_read_int(ggzmod->fd, &stat->losses) < 0 || ggz_read_int(ggzmod->fd, &stat->ties) < 0 || ggz_read_int(ggzmod->fd, &stat->forfeits) < 0 || ggz_read_int(ggzmod->fd, &stat->rating) < 0 || ggz_read_int(ggzmod->fd, &stat->ranking) < 0 || ggz_read_int(ggzmod->fd, &stat->highscore) < 0) { return -1; } } _ggzmod_handle_stats(ggzmod, player_stats, spectator_stats); return 0; }