src/main.c

Mon, 17 Mar 2014 15:39:36 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 17 Mar 2014 15:39:36 +0100
changeset 5
f7dfef88947d
parent 4
560e07f7a6a1
child 7
41468077b5bb
permissions
-rw-r--r--

separated server and client module

universe@0 1 /*
universe@0 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@0 3 *
universe@0 4 * Copyright 2014 Mike Becker. All rights reserved.
universe@0 5 *
universe@0 6 * Redistribution and use in source and binary forms, with or without
universe@0 7 * modification, are permitted provided that the following conditions are met:
universe@0 8 *
universe@0 9 * 1. Redistributions of source code must retain the above copyright
universe@0 10 * notice, this list of conditions and the following disclaimer.
universe@0 11 *
universe@0 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@0 13 * notice, this list of conditions and the following disclaimer in the
universe@0 14 * documentation and/or other materials provided with the distribution.
universe@0 15 *
universe@0 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@0 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@0 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@0 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@0 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@0 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@0 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@0 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@0 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@0 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@0 26 * POSSIBILITY OF SUCH DAMAGE.
universe@0 27 *
universe@0 28 */
universe@0 29
universe@1 30 #include "terminal-chess.h"
universe@1 31 #include <string.h>
universe@2 32 #include <time.h>
universe@3 33 #include <ncurses.h>
universe@1 34
universe@1 35 int get_settings(int argc, char **argv, Settings *settings) {
universe@1 36 char *valid;
universe@2 37 unsigned long int time, port;
universe@2 38 uint8_t timeunit = 60;
universe@2 39 size_t len;
universe@1 40
universe@2 41 for (char opt ; (opt = getopt(argc, argv, "a:bhp:rt:")) != -1 ;) {
universe@1 42 switch (opt) {
universe@2 43 case 'b':
universe@2 44 settings->gameinfo.servercolor = BLACK;
universe@2 45 break;
universe@2 46 case 'r':
universe@2 47 settings->gameinfo.servercolor = (rand()>>5) & 1 ? WHITE : BLACK;
universe@2 48 break;
universe@2 49 case 't':
universe@2 50 case 'a':
universe@2 51 len = strlen(optarg);
universe@2 52 if (optarg[len-1] == 's') {
universe@2 53 optarg[len-1] = '\0';
universe@2 54 timeunit = 1;
universe@2 55 }
universe@2 56
universe@2 57 if ((time = strtoul(optarg, &valid, 10)) > TIME_MAX
universe@2 58 || *valid != '\0') {
universe@2 59 fprintf(stderr, "Specified time is invalid (%s)\n", optarg);
universe@2 60 return 1;
universe@2 61 } else {
universe@2 62 if (opt=='t') {
universe@2 63 settings->gameinfo.time = timeunit * time;
universe@1 64 } else {
universe@2 65 settings->gameinfo.addtime = time;
universe@1 66 }
universe@2 67 }
universe@2 68 break;
universe@2 69 case 'p':
universe@2 70 port = strtol(optarg, &valid, 10);
universe@2 71 if (port < 1025 || port > 65535 || *valid != '\0') {
universe@2 72 fprintf(stderr,
universe@2 73 "Invalid port number (%s) - choose a number between "
universe@2 74 "1025 and 65535\n",
universe@2 75 optarg);
universe@2 76 return 1;
universe@2 77 } else {
universe@2 78 settings->port = optarg;
universe@2 79 }
universe@2 80 break;
universe@2 81 case 'h':
universe@2 82 case '?':
universe@2 83 settings->printhelp = 1;
universe@2 84 break;
universe@1 85 }
universe@1 86 }
universe@1 87
universe@1 88 if (optind == argc - 1) {
universe@1 89 settings->serverhost = argv[optind];
universe@1 90 } else if (optind < argc - 1) {
universe@1 91 fprintf(stderr, "Too many arguments\n");
universe@1 92 return 1;
universe@1 93 }
universe@1 94
universe@1 95 return 0;
universe@1 96 }
universe@1 97
universe@1 98 Settings default_settings() {
universe@1 99 Settings settings;
universe@1 100 memset(&settings, 0, sizeof(Settings));
universe@2 101 settings.gameinfo.servercolor = WHITE;
universe@1 102 settings.port = "27015";
universe@1 103 return settings;
universe@1 104 }
universe@1 105
universe@2 106 void dump_gameinfo(Gameinfo *gameinfo) {
universe@2 107 int serverwhite = gameinfo->servercolor == WHITE;
universe@3 108 attron(A_UNDERLINE);
universe@3 109 printw("Game details\n");
universe@3 110 attroff(A_UNDERLINE);
universe@3 111 printw(" Server: %s\n Client: %s\n",
universe@2 112 serverwhite?"white":"black", serverwhite?"black":"White"
universe@2 113 );
universe@2 114 if (gameinfo->time > 0) {
universe@2 115 if (gameinfo->time % 60) {
universe@3 116 printw(" Time limit: %ds + %ds\n",
universe@2 117 gameinfo->time, gameinfo->addtime);
universe@2 118 } else {
universe@3 119 printw(" Time limit: %dm + %ds\n",
universe@2 120 gameinfo->time/60, gameinfo->addtime);
universe@2 121 }
universe@2 122 } else {
universe@3 123 printw(" No time limit\n");
universe@2 124 }
universe@3 125 refresh();
universe@2 126 }
universe@2 127
universe@4 128 static WINDOW* window;
universe@4 129
universe@4 130 void leavescr() {
universe@4 131 mvprintw(getmaxy(window)-1, 0, "Leaving terminal-chess. Press any key...");
universe@4 132 getch();
universe@4 133 endwin();
universe@4 134 }
universe@4 135
universe@0 136 int main(int argc, char **argv) {
universe@2 137 srand(time(NULL));
universe@1 138
universe@1 139 Settings settings = default_settings();
universe@2 140 if (get_settings(argc, argv, &settings)) {
universe@2 141 return 1;
universe@2 142 }
universe@1 143
universe@1 144 if (settings.printhelp) {
universe@1 145 printf(
universe@2 146 "Usage: terminal-chess [OPTION]... [HOST]\n"
universe@2 147 "Starts/joins a network chess game\n"
universe@2 148 "\nGeneral options\n"
universe@1 149 " -h This help page\n"
universe@2 150 " -p TCP port to use (default: 27015)\n"
universe@2 151 "\nServer options\n"
universe@2 152 " -a <time> Specifies the time to add after each move\n"
universe@2 153 " -b Server plays black pieces (default: white)\n"
universe@2 154 " -r Distribute color randomly\n"
universe@2 155 " -t <time> Specifies time limit (default: no limit)\n"
universe@2 156 "\nNotes\n"
universe@2 157 "White pieces are displayed as uppercase and black pieces as "
universe@2 158 "lowercase letters.\n"
universe@2 159 "The time unit for -a is seconds and for -t minutes by default. To "
universe@2 160 "specify\nseconds for the -t option, use the s suffix.\n"
universe@2 161 "Example: -t 150s\n"
universe@2 162 );
universe@1 163 return EXIT_SUCCESS;
universe@1 164 }
universe@1 165
universe@4 166 window = initscr();
universe@3 167 cbreak();
universe@4 168 atexit(leavescr);
universe@3 169
universe@5 170 return is_server(&settings) ? server_run(&settings) : client_run(&settings);
universe@0 171 }
universe@0 172

mercurial