git.haldean.org grandmaster / e9bb35f
add new_game and move endpoints Haldean 3 years ago
6 changed file(s) with 140 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
4646
4747 {
4848 "game_id": game ID (long int),
49 "state": game state (see below),
4950 "error": null,
5051 }
5152
5253 The error field will be set to a string description of what
5354 went wrong on error.
55
56 --------------------------------------------------------------
57 kind = "move"
58
59 To make a move in a game, send a request with kind set to
60 "move". The full structure of the request is:
61
62 {
63 "kind": "move",
64 "player": player ID (long int),
65 "game_id": game ID (long int),
66 "move": algebraic notation (string),
67 }
68
69 The response structure for a successful move is:
70
71 {
72 "state": game state (see below),
73 "error": null,
74 }
75
76 The response structure for an invalid move is:
77
78 {
79 "state": null,
80 "error": description of error (string),
81 }
6969 send_str(sockfd, msg);
7070 buf = read_str(sockfd, MAX_MSG_LEN);
7171
72 if (!buf) {
73 fprintf(stderr, "E: got no response\n");
74 return -1;
75 }
7276 fprintf(stderr, "OK\n");
7377 printf("%s\n", buf);
7478 return 0;
00 /*
1 * gm.c: grandmaster server
1 * gmserver.c: grandmaster server
22 * Copyright (C) 2015, Haldean Brown
33 *
44 * This program is free software; you can redistribute it and/or modify
4141 char *req_msg;
4242 char *resp_msg;
4343 const char *req_kind;
44 size_t req_kind_len;
4445 json_t *req;
4546 json_t *resp;
4647 json_t *t;
6970 goto close;
7071 }
7172 req_kind = json_string_value(t);
73 req_kind_len = strnlen(req_kind, MAX_MSG_LEN);
7274
73 resp = json_pack("{si}", "num_states", gt->n_states);
75 if (strncmp(req_kind, "new_game", req_kind_len) == 0) {
76 resp = handle_new_game(gt, req);
77 } else if (strncmp(req_kind, "move", req_kind_len) == 0) {
78 resp = handle_move(gt, req);
79 } else {
80 resp = json_pack("{ss}", "error", "unknown kind");
81 }
7482
7583 close:
7684 free(req_msg);
0 /*
1 * serverhooks.c: handling server requests
2 * Copyright (C) 2015, Haldean Brown
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19
20 #include <grandmaster/core.h>
21 #include <grandmaster/tree.h>
22 #include <grandmaster/gmutil.h>
23
24 #include <jansson.h>
25
26 #define get(t, req, field) \
27 t = json_object_get(req, field); \
28 if (!t) return json_pack("{ss}", "error", "missing field " field);
29
30 json_t *
31 game_state(struct game_tree *gt, game_id_t game)
32 {
33 (void)(gt);
34 (void)(game);
35 return json_null();
36 }
37
38 json_t *
39 handle_new_game(struct game_tree *gt, json_t *req)
40 {
41 game_id_t game;
42 player_id_t white;
43 player_id_t black;
44 json_t *t;
45
46 get(t, req, "player_white");
47 white = json_integer_value(t);
48
49 get(t, req, "player_black");
50 black = json_integer_value(t);
51
52 game = new_game(gt, white, black);
53 return json_pack("{sIsosn}",
54 "game_id", game,
55 "state", game_state(gt, game),
56 "error" /* undefined */);
57 }
58
59 json_t *
60 handle_move(struct game_tree *gt, json_t *req)
61 {
62 game_id_t game_id;
63 player_id_t player;
64 const char *notation;
65 json_t *t;
66 bool success;
67
68 get(t, req, "player");
69 player = json_integer_value(t);
70
71 get(t, req, "game_id");
72 game_id = json_integer_value(t);
73
74 get(t, req, "move");
75 notation = json_string_value(t);
76
77 success = make_move(gt, game_id, notation);
78 if (success) {
79 return json_pack("{sosn}", "state", game_state(gt, game_id), "error");
80 }
81 return json_pack("{snss}", "state", "error", "could not perform move");
82 }
1616 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717 */
1818
19 #include <grandmaster/core.h>
20 #include <grandmaster/tree.h>
21
22 #include <jansson.h>
1923 #include <stdlib.h>
2024
2125 /* Receive a length-encoded string on the given socket. */
2630 * on error.*/
2731 int
2832 send_str(int sock, char *str);
33
34 json_t *
35 handle_new_game(struct game_tree *gt, json_t *req);
36
37 json_t *
38 handle_move(struct game_tree *gt, json_t *req);
1515 * with this program; if not, write to the Free Software Foundation, Inc.,
1616 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717 */
18
19 #ifndef __GRANDMASTER_TREE_H__
20 #define __GRANDMASTER_TREE_H__
1821
1922 #include "grandmaster/core.h"
2023 #include <stdbool.h>
7073
7174 void
7275 game_tree_from_json(json_t *doc, struct game_tree *gt);
76
77 #endif