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

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2014 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  */
    30 #include "terminal-chess.h"
    31 #include <string.h>
    32 #include <time.h>
    33 #include <ncurses.h>
    35 int get_settings(int argc, char **argv, Settings *settings) {
    36     char *valid;
    37     unsigned long int time, port;
    38     uint8_t timeunit = 60;
    39     size_t len;
    41     for (char opt ; (opt = getopt(argc, argv, "a:bhp:rt:")) != -1 ;) {
    42         switch (opt) {
    43         case 'b':
    44             settings->gameinfo.servercolor = BLACK;
    45             break;
    46         case 'r':
    47             settings->gameinfo.servercolor = (rand()>>5) & 1 ? WHITE : BLACK;
    48             break;
    49         case 't':
    50         case 'a':
    51             len = strlen(optarg);
    52             if (optarg[len-1] == 's') {
    53                 optarg[len-1] = '\0';
    54                 timeunit = 1;
    55             }
    57             if ((time = strtoul(optarg, &valid, 10)) > TIME_MAX
    58                 || *valid != '\0') {
    59                 fprintf(stderr, "Specified time is invalid (%s)\n", optarg);
    60                 return 1;
    61             } else {
    62                 if (opt=='t') {
    63                     settings->gameinfo.time = timeunit * time;
    64                 } else {
    65                     settings->gameinfo.addtime = time;
    66                 }
    67             }
    68             break;
    69         case 'p':
    70             port = strtol(optarg, &valid, 10);
    71             if (port < 1025 || port > 65535 || *valid != '\0') {
    72                 fprintf(stderr,
    73                     "Invalid port number (%s) - choose a number between "
    74                     "1025 and 65535\n",
    75                     optarg);
    76                 return 1;
    77             } else {
    78                 settings->port = optarg;
    79             }
    80             break;
    81         case 'h':
    82         case '?':
    83             settings->printhelp = 1;
    84             break;
    85         }
    86     }
    88     if (optind == argc - 1) {
    89         settings->serverhost = argv[optind];
    90     } else if (optind < argc - 1) {
    91         fprintf(stderr, "Too many arguments\n");
    92         return 1;
    93     }
    95     return 0;
    96 }
    98 Settings default_settings() {
    99     Settings settings;
   100     memset(&settings, 0, sizeof(Settings));
   101     settings.gameinfo.servercolor = WHITE;
   102     settings.port = "27015";
   103     return settings;
   104 }
   106 void dump_gameinfo(Gameinfo *gameinfo) {
   107     int serverwhite = gameinfo->servercolor == WHITE;
   108     attron(A_UNDERLINE);
   109     printw("Game details\n");
   110     attroff(A_UNDERLINE);
   111     printw("  Server:     %s\n  Client:     %s\n",
   112         serverwhite?"white":"black", serverwhite?"black":"White"
   113     );
   114     if (gameinfo->time > 0) {
   115         if (gameinfo->time % 60) {
   116             printw("  Time limit: %ds + %ds\n",
   117                 gameinfo->time, gameinfo->addtime);
   118         } else {
   119             printw("  Time limit: %dm + %ds\n",
   120                 gameinfo->time/60, gameinfo->addtime);
   121         }
   122     } else {
   123         printw("  No time limit\n");
   124     }
   125     refresh();
   126 }
   128 static WINDOW* window;
   130 void leavescr() {
   131     mvprintw(getmaxy(window)-1, 0, "Leaving terminal-chess. Press any key...");
   132     getch();
   133     endwin();
   134 }
   136 int main(int argc, char **argv) {
   137     srand(time(NULL));
   139     Settings settings = default_settings();
   140     if (get_settings(argc, argv, &settings)) {
   141         return 1;
   142     }
   144     if (settings.printhelp) {
   145         printf(
   146             "Usage: terminal-chess [OPTION]... [HOST]\n"
   147             "Starts/joins a network chess game\n"
   148             "\nGeneral options\n"
   149             "  -h            This help page\n"
   150             "  -p            TCP port to use (default: 27015)\n"
   151             "\nServer options\n"
   152             "  -a <time>     Specifies the time to add after each move\n"
   153             "  -b            Server plays black pieces (default: white)\n"
   154             "  -r            Distribute color randomly\n"
   155             "  -t <time>     Specifies time limit (default: no limit)\n"
   156             "\nNotes\n"
   157             "White pieces are displayed as uppercase and black pieces as "
   158             "lowercase letters.\n"
   159             "The time unit for -a is seconds and for -t minutes by default. To "
   160             "specify\nseconds for the -t option, use the s suffix.\n"
   161             "Example: -t 150s\n"
   162         );
   163         return EXIT_SUCCESS;
   164     }
   166     window = initscr();
   167     cbreak();
   168     atexit(leavescr);
   170     return is_server(&settings) ? server_run(&settings) : client_run(&settings);
   171 }

mercurial