src/server.c

changeset 51
84f2e380a434
parent 46
4dcfb4c58b6d
child 55
54ea19938d57
     1.1 --- a/src/server.c	Mon Jun 16 13:45:31 2014 +0200
     1.2 +++ b/src/server.c	Mon Jun 16 15:41:06 2014 +0200
     1.3 @@ -30,6 +30,8 @@
     1.4  #include "terminal-chess.h"
     1.5  #include "game.h"
     1.6  #include <ncurses.h>
     1.7 +#include <errno.h>
     1.8 +#include <string.h>
     1.9  
    1.10  static int server_open(Server *server, char *port) {
    1.11      printw("\nListening for client...\n");
    1.12 @@ -64,6 +66,31 @@
    1.13      Server server;
    1.14      
    1.15      dump_gameinfo(&(settings->gameinfo));
    1.16 +    GameState continuegame;
    1.17 +    gamestate_init(&continuegame);
    1.18 +    if (settings->continuepgn) {
    1.19 +        // preload PGN data before handshake
    1.20 +        FILE *pgnfile = fopen(settings->continuepgn, "r");
    1.21 +        if (pgnfile) {
    1.22 +            int result = read_pgn(pgnfile, &continuegame,
    1.23 +                &(settings->gameinfo));
    1.24 +            fclose(pgnfile);
    1.25 +            if (result != EXIT_SUCCESS) {
    1.26 +                addstr("Invalid PGN file content.\n");
    1.27 +                return EXIT_FAILURE;
    1.28 +            }
    1.29 +            if (!is_game_running(&continuegame)) {
    1.30 +                addstr("Game has ended. Use -S to analyze it.\n");
    1.31 +                return EXIT_FAILURE;
    1.32 +            }
    1.33 +            addch('\n');
    1.34 +            dump_moveinfo(&continuegame);
    1.35 +            addch('\n');
    1.36 +        } else {
    1.37 +            printw("Can't read PGN file (%s)\n", strerror(errno));
    1.38 +            return EXIT_FAILURE;
    1.39 +        }
    1.40 +    }
    1.41      
    1.42      if (server_open(&server, settings->port)) {
    1.43          net_destroy(&server);
    1.44 @@ -76,16 +103,49 @@
    1.45      }
    1.46  
    1.47      int fd = server.client->fd;
    1.48 -    net_send_data(fd, NETCODE_GAMEINFO,
    1.49 -        &(settings->gameinfo), sizeof(GameInfo));
    1.50 +    if (settings->continuepgn) {
    1.51 +        // Continue game, send PGN data
    1.52 +        uint16_t mc = 0;
    1.53 +        MoveList *movelist = continuegame.movelist;
    1.54 +        while (movelist) {
    1.55 +            mc++;
    1.56 +            movelist = movelist->next;
    1.57 +        }
    1.58 +        
    1.59 +        Move* moves = calloc(mc, sizeof(Move));
    1.60 +        
    1.61 +        movelist = continuegame.movelist;
    1.62 +        mc = 0;
    1.63 +        while (movelist) {
    1.64 +            moves[mc] = movelist->move;
    1.65 +            mc++;
    1.66 +            movelist = movelist->next;
    1.67 +        }
    1.68 +        
    1.69 +        size_t pgndata_size = sizeof(GameInfo)+sizeof(mc)+mc*sizeof(Move);
    1.70 +        char *pgndata = malloc(pgndata_size);
    1.71 +        memcpy(pgndata, &(settings->gameinfo), sizeof(GameInfo));
    1.72 +        memcpy(pgndata+sizeof(GameInfo), &mc, sizeof(mc));
    1.73 +        memcpy(pgndata+sizeof(GameInfo)+sizeof(mc), moves, mc*sizeof(Move));
    1.74 +        free(moves);
    1.75 +        net_send_data(fd, NETCODE_PGNDATA, pgndata, pgndata_size);
    1.76 +        free(pgndata);
    1.77 +    } else {
    1.78 +        // Start new game
    1.79 +        net_send_data(fd, NETCODE_GAMEINFO,
    1.80 +            &(settings->gameinfo), sizeof(GameInfo));
    1.81 +    }
    1.82      addstr("\rClient connected - awaiting challenge acceptance...");
    1.83      refresh();
    1.84      int code = net_recieve_code(fd);
    1.85      if (code == NETCODE_ACCEPT) {
    1.86          addstr("\rClient connected - challenge accepted.");
    1.87          clrtoeol();
    1.88 -        
    1.89 -        game_start(settings, fd);
    1.90 +        if (settings->continuepgn) {
    1.91 +            game_continue(settings, fd, &continuegame);
    1.92 +        } else {
    1.93 +            game_start(settings, fd);
    1.94 +        }
    1.95      } else if (code == NETCODE_DECLINE) {
    1.96          addstr("\rClient connected - challenge declined.");
    1.97          clrtoeol();

mercurial