| 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 */ |
|
| 29 |
|
| 30 #include "input.h" |
|
| 31 #include "game.h" |
|
| 32 #include "network.h" |
|
| 33 #include "chess/pgn.h" |
|
| 34 #include <ncurses.h> |
|
| 35 #include <stdlib.h> |
|
| 36 |
|
| 37 static int client_connect(Server *server, Settings *settings) { |
|
| 38 if (settings->usedomainsocket |
|
| 39 ? net_find_sock(server, settings->serverhost) |
|
| 40 : net_find_tcp(server, settings->serverhost, settings->port)) { |
|
| 41 addstr("Can't find server"); |
|
| 42 return 1; |
|
| 43 } |
|
| 44 |
|
| 45 if (net_connect(server)) { |
|
| 46 addstr("Can't connect to server"); |
|
| 47 return 1; |
|
| 48 } |
|
| 49 |
|
| 50 return 0; |
|
| 51 } |
|
| 52 |
|
| 53 static int client_handshake(Server *server) { |
|
| 54 if (net_recieve_code(server->fd) != NETCODE_VERSION) { |
|
| 55 addstr("Server uses an incompatible software version."); |
|
| 56 return 1; |
|
| 57 } else { |
|
| 58 net_send_code(server->fd, NETCODE_VERSION); |
|
| 59 } |
|
| 60 |
|
| 61 printw("Connection established!\n\n"); |
|
| 62 refresh(); |
|
| 63 |
|
| 64 return 0; |
|
| 65 } |
|
| 66 |
|
| 67 int client_run(Settings *settings) { |
|
| 68 Server server; |
|
| 69 |
|
| 70 if (client_connect(&server, settings)) { |
|
| 71 net_destroy(&server); |
|
| 72 return 1; |
|
| 73 } |
|
| 74 |
|
| 75 if (client_handshake(&server)) { |
|
| 76 net_destroy(&server); |
|
| 77 return 1; |
|
| 78 } |
|
| 79 |
|
| 80 uint8_t code = net_recieve_code(server.fd); |
|
| 81 GameState gamestate; |
|
| 82 gamestate_init(&gamestate); |
|
| 83 bool played = false; |
|
| 84 if (code == NETCODE_GAMEINFO) { |
|
| 85 /* Start new game */ |
|
| 86 net_recieve_data(server.fd, &(settings->gameinfo), sizeof(GameInfo)); |
|
| 87 dump_gameinfo(&(settings->gameinfo)); |
|
| 88 if (prompt_yesno("Accept challenge")) { |
|
| 89 net_send_code(server.fd, NETCODE_ACCEPT); |
|
| 90 game_play(settings, &gamestate, server.fd); |
|
| 91 played = true; |
|
| 92 } else { |
|
| 93 net_send_code(server.fd, NETCODE_DECLINE); |
|
| 94 } |
|
| 95 } else if (code == NETCODE_PGNDATA) { |
|
| 96 net_recieve_data(server.fd, &(settings->gameinfo), sizeof(GameInfo)); |
|
| 97 dump_gameinfo(&(settings->gameinfo)); |
|
| 98 uint16_t mc; |
|
| 99 net_recieve_data(server.fd, &mc, sizeof(mc)); |
|
| 100 Move *moves = calloc(mc, sizeof(Move)); |
|
| 101 net_recieve_data(server.fd, moves, mc*sizeof(Move)); |
|
| 102 for (size_t i = 0 ; i < mc ; i++) { |
|
| 103 apply_move(&gamestate, &(moves[i])); |
|
| 104 } |
|
| 105 free(moves); |
|
| 106 addch('\n'); |
|
| 107 dump_moveinfo(&gamestate); |
|
| 108 if (prompt_yesno( |
|
| 109 "\n\nServer wants to continue a game. Accept challenge")) { |
|
| 110 net_send_code(server.fd, NETCODE_ACCEPT); |
|
| 111 game_play(settings, &gamestate, server.fd); |
|
| 112 played = true; |
|
| 113 } else { |
|
| 114 net_send_code(server.fd, NETCODE_DECLINE); |
|
| 115 } |
|
| 116 } else { |
|
| 117 addstr("Server sent invalid gameinfo."); |
|
| 118 net_destroy(&server); |
|
| 119 return 1; |
|
| 120 } |
|
| 121 |
|
| 122 if (played) { |
|
| 123 game_review(settings, &gamestate); |
|
| 124 } |
|
| 125 gamestate_cleanup(&gamestate); |
|
| 126 |
|
| 127 net_destroy(&server); |
|
| 128 return 0; |
|
| 129 } |
|