src/client.c

Wed, 29 Aug 2018 13:55:18 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 29 Aug 2018 13:55:18 +0200
changeset 64
4eda5df55f86
parent 59
3fa1de896666
permissions
-rw-r--r--

fixes castling not printed correctly to PGN

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

mercurial