src/client.c

Wed, 29 Aug 2018 17:31:36 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 29 Aug 2018 17:31:36 +0200
changeset 68
b34de5ce7d0e
parent 64
4eda5df55f86
permissions
-rw-r--r--

error message when syntactically validating a King's move into a check position is now correct

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

mercurial