62 |
64 |
63 int server_run(Settings *settings) { |
65 int server_run(Settings *settings) { |
64 Server server; |
66 Server server; |
65 |
67 |
66 dump_gameinfo(&(settings->gameinfo)); |
68 dump_gameinfo(&(settings->gameinfo)); |
|
69 GameState continuegame; |
|
70 gamestate_init(&continuegame); |
|
71 if (settings->continuepgn) { |
|
72 // preload PGN data before handshake |
|
73 FILE *pgnfile = fopen(settings->continuepgn, "r"); |
|
74 if (pgnfile) { |
|
75 int result = read_pgn(pgnfile, &continuegame, |
|
76 &(settings->gameinfo)); |
|
77 fclose(pgnfile); |
|
78 if (result != EXIT_SUCCESS) { |
|
79 addstr("Invalid PGN file content.\n"); |
|
80 return EXIT_FAILURE; |
|
81 } |
|
82 if (!is_game_running(&continuegame)) { |
|
83 addstr("Game has ended. Use -S to analyze it.\n"); |
|
84 return EXIT_FAILURE; |
|
85 } |
|
86 addch('\n'); |
|
87 dump_moveinfo(&continuegame); |
|
88 addch('\n'); |
|
89 } else { |
|
90 printw("Can't read PGN file (%s)\n", strerror(errno)); |
|
91 return EXIT_FAILURE; |
|
92 } |
|
93 } |
67 |
94 |
68 if (server_open(&server, settings->port)) { |
95 if (server_open(&server, settings->port)) { |
69 net_destroy(&server); |
96 net_destroy(&server); |
70 return EXIT_FAILURE; |
97 return EXIT_FAILURE; |
71 } |
98 } |
74 net_destroy(&server); |
101 net_destroy(&server); |
75 return EXIT_FAILURE; |
102 return EXIT_FAILURE; |
76 } |
103 } |
77 |
104 |
78 int fd = server.client->fd; |
105 int fd = server.client->fd; |
79 net_send_data(fd, NETCODE_GAMEINFO, |
106 if (settings->continuepgn) { |
80 &(settings->gameinfo), sizeof(GameInfo)); |
107 // Continue game, send PGN data |
|
108 uint16_t mc = 0; |
|
109 MoveList *movelist = continuegame.movelist; |
|
110 while (movelist) { |
|
111 mc++; |
|
112 movelist = movelist->next; |
|
113 } |
|
114 |
|
115 Move* moves = calloc(mc, sizeof(Move)); |
|
116 |
|
117 movelist = continuegame.movelist; |
|
118 mc = 0; |
|
119 while (movelist) { |
|
120 moves[mc] = movelist->move; |
|
121 mc++; |
|
122 movelist = movelist->next; |
|
123 } |
|
124 |
|
125 size_t pgndata_size = sizeof(GameInfo)+sizeof(mc)+mc*sizeof(Move); |
|
126 char *pgndata = malloc(pgndata_size); |
|
127 memcpy(pgndata, &(settings->gameinfo), sizeof(GameInfo)); |
|
128 memcpy(pgndata+sizeof(GameInfo), &mc, sizeof(mc)); |
|
129 memcpy(pgndata+sizeof(GameInfo)+sizeof(mc), moves, mc*sizeof(Move)); |
|
130 free(moves); |
|
131 net_send_data(fd, NETCODE_PGNDATA, pgndata, pgndata_size); |
|
132 free(pgndata); |
|
133 } else { |
|
134 // Start new game |
|
135 net_send_data(fd, NETCODE_GAMEINFO, |
|
136 &(settings->gameinfo), sizeof(GameInfo)); |
|
137 } |
81 addstr("\rClient connected - awaiting challenge acceptance..."); |
138 addstr("\rClient connected - awaiting challenge acceptance..."); |
82 refresh(); |
139 refresh(); |
83 int code = net_recieve_code(fd); |
140 int code = net_recieve_code(fd); |
84 if (code == NETCODE_ACCEPT) { |
141 if (code == NETCODE_ACCEPT) { |
85 addstr("\rClient connected - challenge accepted."); |
142 addstr("\rClient connected - challenge accepted."); |
86 clrtoeol(); |
143 clrtoeol(); |
87 |
144 if (settings->continuepgn) { |
88 game_start(settings, fd); |
145 game_continue(settings, fd, &continuegame); |
|
146 } else { |
|
147 game_start(settings, fd); |
|
148 } |
89 } else if (code == NETCODE_DECLINE) { |
149 } else if (code == NETCODE_DECLINE) { |
90 addstr("\rClient connected - challenge declined."); |
150 addstr("\rClient connected - challenge declined."); |
91 clrtoeol(); |
151 clrtoeol(); |
92 } else if (code == NETCODE_CONNLOST) { |
152 } else if (code == NETCODE_CONNLOST) { |
93 addstr("\rClient connected - but gave no response."); |
153 addstr("\rClient connected - but gave no response."); |