universe@5: /* universe@5: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@5: * universe@55: * Copyright 2016 Mike Becker. All rights reserved. universe@5: * universe@5: * Redistribution and use in source and binary forms, with or without universe@5: * modification, are permitted provided that the following conditions are met: universe@5: * universe@5: * 1. Redistributions of source code must retain the above copyright universe@5: * notice, this list of conditions and the following disclaimer. universe@5: * universe@5: * 2. Redistributions in binary form must reproduce the above copyright universe@5: * notice, this list of conditions and the following disclaimer in the universe@5: * documentation and/or other materials provided with the distribution. universe@5: * universe@5: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@5: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@5: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@5: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@5: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@5: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@5: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@5: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@5: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@5: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@5: * POSSIBILITY OF SUCH DAMAGE. universe@5: * universe@5: */ universe@5: universe@5: #include "terminal-chess.h" universe@5: #include "input.h" universe@6: #include "game.h" universe@5: #include universe@5: universe@6: static int client_connect(Server *server, char *host, char *port) { universe@6: if (net_find(server, host, port)) { universe@34: addstr("Can't find server"); universe@6: return 1; universe@5: } universe@5: universe@6: if (net_connect(server)) { universe@34: addstr("Can't connect to server"); universe@6: return 1; universe@5: } universe@6: universe@6: return 0; universe@6: } universe@5: universe@6: static int client_handshake(Server *server) { universe@6: if (net_recieve_code(server->fd) != NETCODE_VERSION) { universe@34: addstr("Server uses an incompatible software version."); universe@6: return 1; universe@5: } else { universe@6: net_send_code(server->fd, NETCODE_VERSION); universe@5: } universe@5: universe@5: printw("Connection established!\n\n"); universe@5: refresh(); universe@6: universe@6: return 0; universe@6: } universe@5: universe@6: int client_run(Settings *settings) { universe@6: Server server; universe@6: universe@6: if (client_connect(&server, settings->serverhost, settings->port)) { universe@6: net_destroy(&server); universe@59: return 1; universe@6: } universe@6: universe@6: if (client_handshake(&server)) { universe@6: net_destroy(&server); universe@59: return 1; universe@6: } universe@6: universe@51: uint8_t code = net_recieve_code(server.fd); universe@51: if (code == NETCODE_GAMEINFO) { universe@64: /* Start new game */ universe@51: net_recieve_data(server.fd, &(settings->gameinfo), sizeof(GameInfo)); universe@5: dump_gameinfo(&(settings->gameinfo)); universe@7: if (prompt_yesno("Accept challenge")) { universe@6: net_send_code(server.fd, NETCODE_ACCEPT); universe@6: game_start(settings, server.fd); universe@5: } else { universe@6: net_send_code(server.fd, NETCODE_DECLINE); universe@5: } universe@51: } else if (code == NETCODE_PGNDATA) { universe@51: net_recieve_data(server.fd, &(settings->gameinfo), sizeof(GameInfo)); universe@51: dump_gameinfo(&(settings->gameinfo)); universe@51: uint16_t mc; universe@51: net_recieve_data(server.fd, &mc, sizeof(mc)); universe@51: Move *moves = calloc(mc, sizeof(Move)); universe@51: net_recieve_data(server.fd, moves, mc*sizeof(Move)); universe@51: GameState continuegame; universe@51: gamestate_init(&continuegame); universe@51: for (size_t i = 0 ; i < mc ; i++) { universe@51: apply_move(&continuegame, &(moves[i])); universe@51: } universe@51: free(moves); universe@51: addch('\n'); universe@51: dump_moveinfo(&continuegame); universe@51: if (prompt_yesno( universe@51: "\n\nServer wants to continue a game. Accept challenge")) { universe@51: net_send_code(server.fd, NETCODE_ACCEPT); universe@51: game_continue(settings, server.fd, &continuegame); universe@51: } else { universe@51: net_send_code(server.fd, NETCODE_DECLINE); universe@51: } universe@5: } else { universe@34: addstr("Server sent invalid gameinfo."); universe@6: net_destroy(&server); universe@59: return 1; universe@5: } universe@5: universe@5: net_destroy(&server); universe@59: return 0; universe@5: }