67 |
67 |
68 int server_run(Settings *settings) { |
68 int server_run(Settings *settings) { |
69 Server server; |
69 Server server; |
70 |
70 |
71 dump_gameinfo(&(settings->gameinfo)); |
71 dump_gameinfo(&(settings->gameinfo)); |
72 GameState continuegame; |
72 GameState gamestate; |
73 gamestate_init(&continuegame); |
73 gamestate_init(&gamestate); |
74 if (settings->continuepgn) { |
74 if (settings->continuepgn) { |
75 /* preload PGN data before handshake */ |
75 /* preload PGN data before handshake */ |
76 FILE *pgnfile = fopen(settings->continuepgn, "r"); |
76 FILE *pgnfile = fopen(settings->continuepgn, "r"); |
77 if (pgnfile) { |
77 if (pgnfile) { |
78 int result = read_pgn(pgnfile, &continuegame, |
78 int result = read_pgn(pgnfile, &gamestate, |
79 &(settings->gameinfo)); |
79 &(settings->gameinfo)); |
80 long position = ftell(pgnfile); |
80 long position = ftell(pgnfile); |
81 fclose(pgnfile); |
81 fclose(pgnfile); |
82 if (result) { |
82 if (result) { |
83 printw("Invalid PGN file content at position %ld:\n%s\n", |
83 printw("Invalid PGN file content at position %ld:\n%s\n", |
84 position, pgn_error_str(result)); |
84 position, pgn_error_str(result)); |
85 return 1; |
85 return 1; |
86 } |
86 } |
87 if (!is_game_running(&continuegame)) { |
87 if (!is_game_running(&gamestate)) { |
88 addstr("Game has ended. Use -S to analyze it.\n"); |
88 addstr("Game has ended. Use -S to analyze it.\n"); |
89 return 1; |
89 return 1; |
90 } |
90 } |
91 addch('\n'); |
91 addch('\n'); |
92 dump_moveinfo(&continuegame); |
92 dump_moveinfo(&gamestate); |
93 addch('\n'); |
93 addch('\n'); |
94 } else { |
94 } else { |
95 printw("Can't read PGN file (%s)\n", strerror(errno)); |
95 printw("Can't read PGN file (%s)\n", strerror(errno)); |
96 return 1; |
96 return 1; |
97 } |
97 } |
109 |
109 |
110 int fd = server.client->fd; |
110 int fd = server.client->fd; |
111 if (settings->continuepgn) { |
111 if (settings->continuepgn) { |
112 /* Continue game, send PGN data */ |
112 /* Continue game, send PGN data */ |
113 uint16_t mc = 0; |
113 uint16_t mc = 0; |
114 MoveList *movelist = continuegame.movelist; |
114 MoveList *movelist = gamestate.movelist; |
115 while (movelist) { |
115 while (movelist) { |
116 mc++; |
116 mc++; |
117 movelist = movelist->next; |
117 movelist = movelist->next; |
118 } |
118 } |
119 |
119 |
120 Move* moves = calloc(mc, sizeof(Move)); |
120 Move* moves = calloc(mc, sizeof(Move)); |
121 |
121 |
122 movelist = continuegame.movelist; |
122 movelist = gamestate.movelist; |
123 mc = 0; |
123 mc = 0; |
124 while (movelist) { |
124 while (movelist) { |
125 moves[mc] = movelist->move; |
125 moves[mc] = movelist->move; |
126 mc++; |
126 mc++; |
127 movelist = movelist->next; |
127 movelist = movelist->next; |
144 refresh(); |
144 refresh(); |
145 int code = net_recieve_code(fd); |
145 int code = net_recieve_code(fd); |
146 if (code == NETCODE_ACCEPT) { |
146 if (code == NETCODE_ACCEPT) { |
147 addstr("\rClient connected - challenge accepted."); |
147 addstr("\rClient connected - challenge accepted."); |
148 clrtoeol(); |
148 clrtoeol(); |
149 if (settings->continuepgn) { |
149 game_play(settings, &gamestate, fd); |
150 game_continue(settings, fd, &continuegame); |
150 net_destroy(&server); |
151 } else { |
151 game_review(settings, &gamestate); |
152 game_start(settings, fd); |
152 return 0; |
153 } |
|
154 } else if (code == NETCODE_DECLINE) { |
153 } else if (code == NETCODE_DECLINE) { |
155 addstr("\rClient connected - challenge declined."); |
154 addstr("\rClient connected - challenge declined."); |
156 clrtoeol(); |
155 clrtoeol(); |
|
156 net_destroy(&server); |
|
157 return 0; |
157 } else if (code == NETCODE_CONNLOST) { |
158 } else if (code == NETCODE_CONNLOST) { |
158 addstr("\rClient connected - but gave no response."); |
159 addstr("\rClient connected - but gave no response."); |
159 clrtoeol(); |
160 clrtoeol(); |
|
161 net_destroy(&server); |
|
162 return 0; |
160 } else { |
163 } else { |
161 addstr("\rInvalid client response"); |
164 addstr("\rInvalid client response"); |
162 clrtoeol(); |
165 clrtoeol(); |
163 |
166 |
164 net_destroy(&server); |
167 net_destroy(&server); |
165 return 1; |
168 return 1; |
166 } |
169 } |
167 |
|
168 net_destroy(&server); |
|
169 return 0; |
|
170 } |
170 } |