simple implementations of a few more commands for clients
This commit is contained in:
parent
eed9669db9
commit
ef5c5fbb35
|
@ -88,6 +88,13 @@ asciiart_small_draw_laserhit(char *buf, game_t *game)
|
|||
}
|
||||
}
|
||||
|
||||
void
|
||||
asciiart_small_draw_turn_marker(char *buf, game_t *game)
|
||||
{
|
||||
int index = (game_turn(game)==SILVER) ? 747 : 4;
|
||||
buf[index] = '*';
|
||||
}
|
||||
|
||||
void
|
||||
asciiart_small_draw_board(char *buf, game_t *game)
|
||||
{
|
||||
|
@ -99,6 +106,7 @@ asciiart_small_draw_board(char *buf, game_t *game)
|
|||
}
|
||||
}
|
||||
asciiart_small_draw_laserhit(buf, game);
|
||||
asciiart_small_draw_turn_marker(buf, game);
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
147
src/server.c
147
src/server.c
|
@ -19,9 +19,10 @@ typedef struct {
|
|||
server_game_t *server_games[MAX_GAMES];
|
||||
int server_games_sz;
|
||||
|
||||
#define MAX_CLIENT_NAME_LEN 10
|
||||
typedef struct {
|
||||
int connected;
|
||||
char name[10];
|
||||
char name[MAX_CLIENT_NAME_LEN];
|
||||
int game;
|
||||
int requested_game, requested_game_with;
|
||||
} server_client_t;
|
||||
|
@ -76,14 +77,17 @@ server_get_game_request(int client)
|
|||
{
|
||||
int i;
|
||||
server_client_t *c = server_clients;
|
||||
int match_any = -1;
|
||||
for (i = 0; i < MAX_CLIENTS; i++) {
|
||||
if (c[i].connected &&
|
||||
c[i].requested_game &&
|
||||
(c[i].requested_game_with == client ||
|
||||
c[i].requested_game_with == -1))
|
||||
return i;
|
||||
c[i].requested_game) {
|
||||
if (c[i].requested_game_with == client)
|
||||
return i;
|
||||
else if (c[i].requested_game_with == -1)
|
||||
match_any = i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
return match_any;
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -91,7 +95,7 @@ server_request_game(int client, int opponent)
|
|||
{
|
||||
int match = 0;
|
||||
if (server_clients[client].requested_game)
|
||||
return -1;
|
||||
return -2;
|
||||
if (opponent == -1) {
|
||||
opponent = server_get_game_request(client);
|
||||
if (opponent != -1) match = 1;
|
||||
|
@ -111,6 +115,19 @@ server_request_game(int client, int opponent)
|
|||
}
|
||||
}
|
||||
|
||||
int
|
||||
server_client_by_name(char *name)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < MAX_CLIENTS; i++) {
|
||||
if (server_clients[i].connected &&
|
||||
strcasecmp(server_clients[i].name, name)==0)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
terminal_game(void)
|
||||
{
|
||||
|
@ -184,28 +201,34 @@ server_report_game_state(int gameid)
|
|||
for (i = 0; i < 2; i++) {
|
||||
int c = g->players[i];
|
||||
|
||||
if (g->game.last_move.type == M_ROTATE) {
|
||||
net_client_printf(c, "move: %c%c rotated %s\n",
|
||||
g->game.last_move.y+'A',
|
||||
g->game.last_move.x+'0',
|
||||
g->game.last_move.dir==R_CW ? "R" : "L");
|
||||
if (g->game.move == 0) {
|
||||
net_client_printf(c, "started game between %s (silver) and %s (red)\n",
|
||||
server_clients[g->players[SILVER]].name,
|
||||
server_clients[g->players[RED]].name);
|
||||
} else {
|
||||
int nx, ny;
|
||||
move_dest(g->game.last_move, &nx, &ny);
|
||||
net_client_printf(c, "move: %c%c moved to %c%c\n",
|
||||
g->game.last_move.y+'A',
|
||||
g->game.last_move.x+'0',
|
||||
ny+'A', nx+'0');
|
||||
}
|
||||
if (g->game.last_move.type == M_ROTATE) {
|
||||
net_client_printf(c, "move: %c%c rotated %s\n",
|
||||
g->game.last_move.y+'A',
|
||||
g->game.last_move.x+'0',
|
||||
g->game.last_move.dir==R_CW ? "R" : "L");
|
||||
} else {
|
||||
int nx, ny;
|
||||
move_dest(g->game.last_move, &nx, &ny);
|
||||
net_client_printf(c, "move: %c%c moved to %c%c\n",
|
||||
g->game.last_move.y+'A',
|
||||
g->game.last_move.x+'0',
|
||||
ny+'A', nx+'0');
|
||||
}
|
||||
|
||||
if (g->game.last_hit.piece != NONE) {
|
||||
net_client_printf(c, "laser hits %s %s at %c%c\n",
|
||||
sideinfo[g->game.last_hit.side].name,
|
||||
pieceinfo[g->game.last_hit.piece].name,
|
||||
g->game.laser_pos[1]+'A', g->game.laser_pos[0]+'0');
|
||||
} else {
|
||||
net_client_printf(c, "laser hits wall at %c%c\n",
|
||||
g->game.laser_pos[1]+'A', g->game.laser_pos[0]+'0');
|
||||
if (g->game.last_hit.piece != NONE) {
|
||||
net_client_printf(c, "laser hits %s %s at %c%c\n",
|
||||
sideinfo[g->game.last_hit.side].name,
|
||||
pieceinfo[g->game.last_hit.piece].name,
|
||||
g->game.laser_pos[1]+'A', g->game.laser_pos[0]+'0');
|
||||
} else {
|
||||
net_client_printf(c, "laser hits wall at %c%c\n",
|
||||
g->game.laser_pos[1]+'A', g->game.laser_pos[0]+'0');
|
||||
}
|
||||
}
|
||||
|
||||
char aabuf[ASCIIART_SMALL_BUFSZ];
|
||||
|
@ -309,6 +332,8 @@ server_cmd_play(int client, char **args)
|
|||
int g = server_request_game(client, -1);
|
||||
if (g == -1) {
|
||||
net_client_printf(client, "requested game with anyone\n");
|
||||
} else if (g == -2) {
|
||||
net_client_printf(client, "can not request another game\n");
|
||||
} else {
|
||||
server_report_game_state(g);
|
||||
}
|
||||
|
@ -318,42 +343,88 @@ server_cmd_play(int client, char **args)
|
|||
int
|
||||
server_cmd_playwith(int client, char **args)
|
||||
{
|
||||
net_client_printf(client, "sorry, this is not implemented yet\n");
|
||||
int opponent = server_client_by_name(args[0]);
|
||||
if (opponent == -1) {
|
||||
net_client_printf(client, "there is no player named %s\n", args[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int g = server_request_game(client, opponent);
|
||||
if (g == -1) {
|
||||
net_client_printf(client, "requested game with %s\n", args[0]);
|
||||
net_client_printf(opponent, "%s wants to play a game with you\n",
|
||||
server_clients[client].name);
|
||||
} else if (g == -2) {
|
||||
net_client_printf(client, "can not request another game\n");
|
||||
} else {
|
||||
server_report_game_state(g);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
server_cmd_nick(int client, char **args)
|
||||
{
|
||||
net_client_printf(client, "sorry, this is not implemented yet\n");
|
||||
strncpy(server_clients[client].name, args[0], MAX_CLIENT_NAME_LEN);
|
||||
server_clients[client].name[MAX_CLIENT_NAME_LEN-1] = '\0';
|
||||
net_client_printf(client, "nick changed to %s\n",
|
||||
server_clients[client].name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
server_cmd_ls(int client, char **args)
|
||||
{
|
||||
net_client_printf(client, "sorry, this is not implemented yet\n");
|
||||
char *thing = args[0];
|
||||
int i;
|
||||
if (strcasecmp(thing, "players")==0) {
|
||||
for (i = 0; i < MAX_CLIENTS; i++) {
|
||||
if (server_clients[i].connected) {
|
||||
net_client_printf(client, "%s\n", server_clients[i].name);
|
||||
}
|
||||
}
|
||||
} else if (strcasecmp(thing, "games")==0) {
|
||||
for (i = 0; i < server_games_sz; i++) {
|
||||
server_game_t *g = server_games[i];
|
||||
net_client_printf(client, "game %d: %s -- %s (%d moves)\n", i,
|
||||
server_clients[g->players[0]].name,
|
||||
server_clients[g->players[1]].name,
|
||||
g->game.move);
|
||||
}
|
||||
net_client_printf(client, "%d games\n", server_games_sz);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
server_cmd_whoami(int client, char **args)
|
||||
{
|
||||
net_client_printf(client, "%s\n", server_clients[client].name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
server_cmd_t server_commands[] = {
|
||||
{ .name = "PLAY",
|
||||
{ .name = "play",
|
||||
.desc = "play a game with anyone",
|
||||
.argc = 0,
|
||||
.func = server_cmd_play },
|
||||
{ .name = "PLAYWITH",
|
||||
{ .name = "playwith",
|
||||
.desc = "play a game with a named opponent",
|
||||
.argc = 1,
|
||||
.func = server_cmd_playwith },
|
||||
{ .name = "NICK",
|
||||
{ .name = "nick",
|
||||
.desc = "set your nickname",
|
||||
.argc = 1,
|
||||
.func = server_cmd_nick },
|
||||
{ .name = "LS",
|
||||
{ .name = "ls",
|
||||
.desc = "list stuff",
|
||||
.argc = 1,
|
||||
.func = server_cmd_ls } };
|
||||
int server_commands_sz = 4;
|
||||
.func = server_cmd_ls },
|
||||
{ .name = "whoami",
|
||||
.desc = "find out what your name is",
|
||||
.argc = 0,
|
||||
.func = server_cmd_whoami } };
|
||||
int server_commands_sz = 5;
|
||||
|
||||
int
|
||||
server_do_command(int clientid, char *cmd)
|
||||
|
@ -366,9 +437,11 @@ server_do_command(int clientid, char *cmd)
|
|||
move_t move;
|
||||
int i;
|
||||
|
||||
/*
|
||||
for (i = 0; i < strlen(cmd_copy); i++) {
|
||||
cmd_copy[i] = toupper(cmd_copy[i]);
|
||||
}
|
||||
*/
|
||||
|
||||
if (server_clients[clientid].game != -1 &&
|
||||
parse_move(cmd_copy, &move) == HUGE_SUCCESS) {
|
||||
|
@ -388,7 +461,7 @@ server_do_command(int clientid, char *cmd)
|
|||
}
|
||||
|
||||
for (i = 0; i < server_commands_sz; i++) {
|
||||
if (strcmp(cmd_copy, server_commands[i].name)==0)
|
||||
if (strcasecmp(cmd_copy, server_commands[i].name)==0)
|
||||
cmd_type = &server_commands[i];
|
||||
}
|
||||
if (cmd_type == NULL) {
|
||||
|
|
Reference in New Issue