]> git.cworth.org Git - grrobot/blobdiff - src/grrobot.c
Add --watch option
[grrobot] / src / grrobot.c
index 1a8d66579e103777d0462c5bf5a1d4984fffb40a..209c599d9fc1db6a3f5a31fd10ca996d985cbff1 100644 (file)
@@ -92,9 +92,16 @@ main (int argc, char **argv)
        return 1;
     }
 
-    status = rr_client_join (game.client, args.game);
-    if (status == RR_STATUS_NO_GAME) {
-       status = rr_client_new (game.client, args.game);
+    if (args.watch) {
+       status = rr_client_watch (game.client, args.game);
+       if (status == RR_STATUS_NO_GAME) {
+           fprintf (stderr, "No game %s to watch\n", args.game);
+       }
+    } else {
+       status = rr_client_join (game.client, args.game);
+       if (status == RR_STATUS_NO_GAME) {
+           status = rr_client_new (game.client, args.game);
+       }
     }
 
     game.board = rr_board_create (16, 16);
@@ -188,10 +195,16 @@ grr_game_read_notices (grr_game_t *game)
     while (rr_client_notice_pending (game->client)) {
        status = rr_client_next_notice (game->client, &notice);
        if (status) {
-           fprintf (stderr, "Error during rr_client_next_notice: %s\n",
-                    rr_status_str (status));
+           if (status == RR_STATUS_EOF)
+               fprintf (stderr, "The server has disconnected, exiting.\n");
+           else
+               fprintf (stderr, "Error during rr_client_next_notice: %s\n",
+                        rr_status_str (status));
            gtk_exit (1);
-           return;
+       }
+       if (!notice) {
+           fprintf (stderr, "Missing notice\n");
+           continue;
        }
 
        switch (notice->type) {
@@ -220,6 +233,22 @@ grr_game_read_notices (grr_game_t *game)
            grr_game_printf (game, "\nGame state changed to: %s.",
                             rr_gamestate_str (notice->u.gamestate));
            break;
+       case RR_NOTICE_TURN:
+           grr_game_print (game, "\nNew round!");
+           rr_board_set_goal_target (board, notice->u.target);
+           gtk_widget_queue_draw (GTK_WIDGET (game->window));
+           break;
+       case RR_NOTICE_GAMEOVER:
+       {
+           char *diagram;
+           grr_game_printf (game, "\nGame over. New game will begin now.");
+           /* XXX: Can drop this when the BOARD NOTICE is added in the server */
+           rr_client_show (game->client, &diagram);
+           rr_board_parse (board, diagram);
+           free (diagram);
+           gtk_widget_queue_draw (GTK_WIDGET (game->window));
+       }
+       break;
        case RR_NOTICE_JOIN:
            grr_game_printf (game, "\nUser %s has joined the game.",
                             notice->u.string);
@@ -287,13 +316,8 @@ grr_game_read_notices (grr_game_t *game)
                             notice->u.number);
            break;
        case RR_NOTICE_POSITION:
-           rr_board_position (board, notice->u.position.robot,
-                              notice->u.position.x, notice->u.position.y);
-           gtk_widget_queue_draw (GTK_WIDGET (game->window));
-           break;
-       case RR_NOTICE_TURN:
-           grr_game_print (game, "\nNew round!");
-           rr_board_set_goal_target (board, notice->u.target);
+           rr_board_position_robot (board, notice->u.position.robot,
+                                    notice->u.position.x, notice->u.position.y);
            gtk_widget_queue_draw (GTK_WIDGET (game->window));
            break;
        }