Tue, 28 Aug 2018 14:16:30 +0200
fixes inappropriate use of EXIT_ macros + adds a sample PGN file
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2016 Mike Becker. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
30 #include "terminal-chess.h"
31 #include "input.h"
32 #include "game.h"
33 #include <ncurses.h>
35 static int client_connect(Server *server, char *host, char *port) {
36 if (net_find(server, host, port)) {
37 addstr("Can't find server");
38 return 1;
39 }
41 if (net_connect(server)) {
42 addstr("Can't connect to server");
43 return 1;
44 }
46 return 0;
47 }
49 static int client_handshake(Server *server) {
50 if (net_recieve_code(server->fd) != NETCODE_VERSION) {
51 addstr("Server uses an incompatible software version.");
52 return 1;
53 } else {
54 net_send_code(server->fd, NETCODE_VERSION);
55 }
57 printw("Connection established!\n\n");
58 refresh();
60 return 0;
61 }
63 int client_run(Settings *settings) {
64 Server server;
66 if (client_connect(&server, settings->serverhost, settings->port)) {
67 net_destroy(&server);
68 return 1;
69 }
71 if (client_handshake(&server)) {
72 net_destroy(&server);
73 return 1;
74 }
76 uint8_t code = net_recieve_code(server.fd);
77 if (code == NETCODE_GAMEINFO) {
78 // Start new game
79 net_recieve_data(server.fd, &(settings->gameinfo), sizeof(GameInfo));
80 dump_gameinfo(&(settings->gameinfo));
81 if (prompt_yesno("Accept challenge")) {
82 net_send_code(server.fd, NETCODE_ACCEPT);
83 game_start(settings, server.fd);
84 } else {
85 net_send_code(server.fd, NETCODE_DECLINE);
86 }
87 } else if (code == NETCODE_PGNDATA) {
88 net_recieve_data(server.fd, &(settings->gameinfo), sizeof(GameInfo));
89 dump_gameinfo(&(settings->gameinfo));
90 uint16_t mc;
91 net_recieve_data(server.fd, &mc, sizeof(mc));
92 Move *moves = calloc(mc, sizeof(Move));
93 net_recieve_data(server.fd, moves, mc*sizeof(Move));
94 GameState continuegame;
95 gamestate_init(&continuegame);
96 for (size_t i = 0 ; i < mc ; i++) {
97 apply_move(&continuegame, &(moves[i]));
98 }
99 free(moves);
100 addch('\n');
101 dump_moveinfo(&continuegame);
102 if (prompt_yesno(
103 "\n\nServer wants to continue a game. Accept challenge")) {
104 net_send_code(server.fd, NETCODE_ACCEPT);
105 game_continue(settings, server.fd, &continuegame);
106 } else {
107 net_send_code(server.fd, NETCODE_DECLINE);
108 }
109 } else {
110 addstr("Server sent invalid gameinfo.");
111 net_destroy(&server);
112 return 1;
113 }
115 net_destroy(&server);
116 return 0;
117 }