src/server.c

changeset 83
ead00609e5e6
parent 78
ceb9197b3c6d
--- a/src/server.c	Thu Jun 05 19:05:02 2025 +0200
+++ b/src/server.c	Fri Jun 06 19:19:14 2025 +0200
@@ -69,13 +69,13 @@
     Server server;
     
     dump_gameinfo(&(settings->gameinfo));
-    GameState continuegame;
-    gamestate_init(&continuegame);
+    GameState gamestate;
+    gamestate_init(&gamestate);
     if (settings->continuepgn) {
         /* preload PGN data before handshake */
         FILE *pgnfile = fopen(settings->continuepgn, "r");
         if (pgnfile) {
-            int result = read_pgn(pgnfile, &continuegame,
+            int result = read_pgn(pgnfile, &gamestate,
                 &(settings->gameinfo));
             long position = ftell(pgnfile);
             fclose(pgnfile);
@@ -84,12 +84,12 @@
                         position, pgn_error_str(result));
                 return 1;
             }
-            if (!is_game_running(&continuegame)) {
+            if (!is_game_running(&gamestate)) {
                 addstr("Game has ended. Use -S to analyze it.\n");
                 return 1;
             }
             addch('\n');
-            dump_moveinfo(&continuegame);
+            dump_moveinfo(&gamestate);
             addch('\n');
         } else {
             printw("Can't read PGN file (%s)\n", strerror(errno));
@@ -111,7 +111,7 @@
     if (settings->continuepgn) {
         /* Continue game, send PGN data */
         uint16_t mc = 0;
-        MoveList *movelist = continuegame.movelist;
+        MoveList *movelist = gamestate.movelist;
         while (movelist) {
             mc++;
             movelist = movelist->next;
@@ -119,7 +119,7 @@
         
         Move* moves = calloc(mc, sizeof(Move));
         
-        movelist = continuegame.movelist;
+        movelist = gamestate.movelist;
         mc = 0;
         while (movelist) {
             moves[mc] = movelist->move;
@@ -146,17 +146,20 @@
     if (code == NETCODE_ACCEPT) {
         addstr("\rClient connected - challenge accepted.");
         clrtoeol();
-        if (settings->continuepgn) {
-            game_continue(settings, fd, &continuegame);
-        } else {
-            game_start(settings, fd);
-        }
+        game_play(settings, &gamestate, fd);
+        net_destroy(&server);
+        game_review(settings, &gamestate);
+        return 0;
     } else if (code == NETCODE_DECLINE) {
         addstr("\rClient connected - challenge declined.");
         clrtoeol();
+        net_destroy(&server);
+        return 0;
     } else if (code == NETCODE_CONNLOST) {
         addstr("\rClient connected - but gave no response.");
         clrtoeol();
+        net_destroy(&server);
+        return 0;
     } else {
         addstr("\rInvalid client response");
         clrtoeol();
@@ -164,7 +167,4 @@
         net_destroy(&server);
         return 1;
     }
-    
-    net_destroy(&server);
-    return 0;
 }

mercurial