1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
#ifndef HYBBX_CHESS_H
#define HYBBX_CHESS_H
#include "hybbx/types.h"
#include "hybbx/limits.h"
#include "hybbx/storage.h"
#ifdef __cplusplus
extern "C" {
#endif
#define CHESS_BOARD_SIZE 8
#define CHESS_SQ_MAX 4
#define CHESS_MOVE_MAX 8
#define CHESS_SAN_MAX 8
#define CHESS_GAME_MAX 16 /* array upper bound */
#define CHESS_SPECTATOR_MAX 32
#define CHESS_REFRESH_SEC 15
#define CHESS_DEFAULT_MAX 8 /* default active games if INI omits [chess] */
#define CHESS_EMPTY '.'
typedef enum {
CHESS_COLOR_NONE = 0,
CHESS_WHITE,
CHESS_BLACK
} chess_color_t;
typedef enum {
CHESS_RESULT_NONE = 0,
CHESS_WHITE_WINS,
CHESS_BLACK_WINS,
CHESS_DRAW_STALEMATE,
CHESS_DRAW_AGREEMENT
} chess_result_t;
typedef struct chess_board {
char sq[CHESS_BOARD_SIZE][CHESS_BOARD_SIZE];
} chess_board_t;
typedef struct chess_game {
int active;
char white_name[HYBBX_USER_NAME_MAX];
char black_name[HYBBX_USER_NAME_MAX];
struct hybbx_session *white_session;
struct hybbx_session *black_session;
struct hybbx_session *spectators[CHESS_SPECTATOR_MAX];
unsigned spectator_count;
chess_board_t board;
chess_color_t turn;
unsigned move_number;
chess_result_t result;
char last_move_san[CHESS_SAN_MAX];
unsigned tick_counter;
char game_id[8];
} chess_game_t;
void hybbx_chess_init(void);
void hybbx_chess_shutdown(void);
void hybbx_chess_config_apply(const struct hybbx_config *config);
unsigned hybbx_chess_max_games(void);
void hybbx_chess_shutdown(void);
chess_game_t *hybbx_chess_create_game(struct hybbx_session *white,
const char *white_name);
chess_game_t *hybbx_chess_find_open_game(void);
chess_game_t *hybbx_chess_find_game_by_id(const char *id);
chess_game_t *hybbx_chess_get_game(unsigned index);
int hybbx_chess_join_game(chess_game_t *game,
struct hybbx_session *black,
const char *black_name);
int hybbx_chess_add_spectator(chess_game_t *game,
struct hybbx_session *spec);
void hybbx_chess_remove_spectator(chess_game_t *game,
struct hybbx_session *spec);
int hybbx_chess_is_player(const chess_game_t *game,
const struct hybbx_session *session);
int hybbx_chess_is_spectator(const chess_game_t *game,
const struct hybbx_session *session);
chess_color_t hybbx_chess_player_color(const chess_game_t *game,
const struct hybbx_session *session);
int hybbx_chess_make_move(chess_game_t *game,
const char *from, const char *to,
char promote, char *err, size_t errlen);
void hybbx_chess_render_board(const chess_board_t *board,
struct hybbx_session *session,
const char *label);
void hybbx_chess_send_board(chess_game_t *game,
struct hybbx_session *session);
void hybbx_chess_tick(struct hybbx_service *service);
void hybbx_chess_resign(chess_game_t *game,
struct hybbx_session *who);
void hybbx_chess_offer_draw(chess_game_t *game,
struct hybbx_session *who);
/* Command handler - called from command.c dispatch */
hybbx_result_t cmd_chess(struct hybbx_service *service,
struct hybbx_session *session,
const struct hybbx_parsed_command *cmd);
#ifdef __cplusplus
}
#endif
#endif /* HYBBX_CHESS_H */
|