From: Christian Heller Date: Wed, 22 Jan 2014 01:35:47 +0000 (+0100) Subject: Define server messages and server message arguments in confclient/commands, instead... X-Git-Tag: tce~883 X-Git-Url: https://plomlompom.com/repos/ledger?a=commitdiff_plain;h=c53b1488c5a53e8b12cb3a631763f98010420690;p=plomrogue Define server messages and server message arguments in confclient/commands, instead of hardcoding them. --- diff --git a/confclient/commands b/confclient/commands index 19559de..502ede7 100644 --- a/confclient/commands +++ b/confclient/commands @@ -1,51 +1,51 @@ -quit quit -wait wait / next turn -player_u player up -player_d player down -player_l player left -player_r player right -to_g_keywin toggle global keys window -to_mapwin toggle map window -to_infowin toggle info window -to_logwin toggle log window -to_wg_keywin toggle window geometry keys window -to_wk_keywin toggle window keybinding keys window -cyc_win_f cycle window selection forwards -winconf toggle window configuration view -cyc_win_b cycle window selection backwards -scrl_l scroll pad left -scrl_r scroll pad right -g_keys_d global keybindings nav down -g_keys_u global keybindings nav up -g_keys_m global keybindings modify -shift_f shift window forwards -shift_b shift window backwards -grow_h grow window horizontally -shri_h shrink window horizontally -grow_v grow window vertically -shri_v shrink window vertically -to_height_t toggle window height type -to_width_t toggle window width type -w_keys_d win keys nav down -w_keys_u win keys nav up -w_keys_m win keys mod -wg_keys_d win geometry config keybindings nav down -wg_keys_u win geometry config keybindings nav up -wg_keys_m win geometry config keybindings modify -wk_keys_d win keybindings config keybindings nav down -wk_keys_u win keybindings config keybindings nav up -wk_keys_m win keybindings config keybindings modify -reload_conf reload windows and keybindings configuration -save_conf save windows and keybindings -map_u map up -map_d map down -map_l map left -map_r map right -map_c map center player -to_a_keywin toggle view of available keybindings -to_inv toggle inventory window -drop drop object from inventory -pick pick up object from ground -inv_d inventory selection down -inv_u inventory selection up -use use selected inventory object +wait wait 0 wait / next turn +use use i use selected inventory object +drop drop i drop object from inventory +pick pick_up 0 pick up object from ground +player_u move N player up +player_d move S player down +player_l move W player left +player_r move E player right +quit 0 0 quit +to_g_keywin 0 0 toggle global keys window +to_mapwin 0 0 toggle map window +to_infowin 0 0 toggle info window +to_logwin 0 0 toggle log window +to_wg_keywin 0 0 toggle window geometry keys window +to_wk_keywin 0 0 toggle window keybinding keys window +cyc_win_f 0 0 cycle window selection forwards +winconf 0 0 toggle window configuration view +cyc_win_b 0 0 cycle window selection backwards +scrl_l 0 0 scroll pad left +scrl_r 0 0 scroll pad right +g_keys_d 0 0 global keybindings nav down +g_keys_u 0 0 global keybindings nav up +g_keys_m 0 0 global keybindings modify +shift_f 0 0 shift window forwards +shift_b 0 0 shift window backwards +grow_h 0 0 grow window horizontally +shri_h 0 0 shrink window horizontally +grow_v 0 0 grow window vertically +shri_v 0 0 shrink window vertically +to_height_t 0 0 toggle window height type +to_width_t 0 0 toggle window width type +w_keys_d 0 0 win keys nav down +w_keys_u 0 0 win keys nav up +w_keys_m 0 0 win keys mod +wg_keys_d 0 0 win geometry config keybindings nav down +wg_keys_u 0 0 win geometry config keybindings nav up +wg_keys_m 0 0 win geometry config keybindings modify +wk_keys_d 0 0 win keybindings config keybindings nav down +wk_keys_u 0 0 win keybindings config keybindings nav up +wk_keys_m 0 0 win keybindings config keybindings modify +reload_conf 0 reload windows and keybindings configuration +save_conf 0 0 save windows and keybindings +map_u 0 0 map up +map_d 0 0 map down +map_l 0 0 map left +map_r 0 0 map right +map_c 0 0 map center player +to_a_keywin 0 0 toggle view of available keybindings +to_inv 0 0 toggle inventory window +inv_d 0 0 inventory selection down +inv_u 0 0 inventory selection up diff --git a/src/client/command_db.c b/src/client/command_db.c index e9478c6..fa125c4 100644 --- a/src/client/command_db.c +++ b/src/client/command_db.c @@ -4,10 +4,11 @@ #include /* uint8_t, uint32_t */ #include /* FILE */ #include /* free() */ -#include /* memcpy(), strlen(), strtok() */ +#include /* memcpy(), strlen(), strtok(), strcmp() */ #include "../common/readwrite.h" /* try_fopen(), try_fclose(), try_fgets() * textfile_sizes() */ +#include "../common/rexit.h" /* for exit_err() */ #include "../common/try_malloc.h" /* try_malloc() */ #include "cleanup.h" /* set_cleanup_flag() */ #include "world.h" /* global world */ @@ -29,17 +30,24 @@ static void copy_tokenized_string(char * line, char ** ch_ptr, char * delim) -extern char * get_command_longdsc(char * dsc_short) +extern struct Command * get_command_data(char * dsc_short) { struct Command * cmd_ptr = world.cmd_db.cmds; - while (1) + uint8_t i = 0; + while (i < world.cmd_db.n) { if (0 == strcmp(dsc_short, cmd_ptr->dsc_short)) { - return cmd_ptr->dsc_long; + break; } cmd_ptr = &cmd_ptr[1]; + i++; } + char * err_start = "get_command_data() failed on request for: "; + char err[strlen(err_start) + strlen(dsc_short) + 1]; + sprintf(err, "%s%s", err_start, dsc_short); + exit_err(i == world.cmd_db.n, err); + return cmd_ptr; } @@ -54,13 +62,22 @@ extern void init_command_db() char line[linemax + 1]; world.cmd_db.cmds = try_malloc(lines * sizeof(struct Command), f_name); uint8_t i = 0; + char * delim = " "; while (try_fgets(line, linemax + 1, file, f_name)) { if ('\n' == line[0] || 0 == line[0]) { break; } - copy_tokenized_string(line, &world.cmd_db.cmds[i].dsc_short, " "); + copy_tokenized_string(line, &world.cmd_db.cmds[i].dsc_short, delim); + copy_tokenized_string(NULL, &world.cmd_db.cmds[i].server_msg, delim); + if (!strcmp("0", world.cmd_db.cmds[i].server_msg)) + { + free(world.cmd_db.cmds[i].server_msg); + world.cmd_db.cmds[i].server_msg = NULL; + } + char * arg_string = strtok(NULL, delim); + world.cmd_db.cmds[i].arg = arg_string[0]; copy_tokenized_string(NULL, &world.cmd_db.cmds[i].dsc_long, "\n"); i++; } @@ -78,6 +95,7 @@ extern void free_command_db() { free(world.cmd_db.cmds[i].dsc_short); free(world.cmd_db.cmds[i].dsc_long); + free(world.cmd_db.cmds[i].server_msg); i++; } free(world.cmd_db.cmds); diff --git a/src/client/command_db.h b/src/client/command_db.h index 9ed55d9..0f2ef77 100644 --- a/src/client/command_db.h +++ b/src/client/command_db.h @@ -1,7 +1,8 @@ /* src/client/command_db.h * - * The Command DB collects identifiers and verbal descriptions of all commands - * the user can give. + * The Command DB collects the commands available to the user by internal name, + * description and, optionally, components of a message to send to the server + * when calling it. */ #ifndef COMMAND_DB_H @@ -13,8 +14,10 @@ struct Command { - char * dsc_short; /* short string name of command to be used internally */ - char * dsc_long; /* long string description of command for the user */ + char * dsc_short; /* short name of command to be used internally */ + char * dsc_long; /* long description of command to be shown to the user */ + char * server_msg; /* optionally start string of message to send to server */ + char arg; /* defines server message suffix by player_control() convention */ }; struct CommandDB @@ -25,8 +28,8 @@ struct CommandDB -/* Give short description of command ("dsc_short"), get long description. */ -extern char * get_command_longdsc(char * dsc_short); +/* Return Command struct for command described by its "dsc_short" member. */ +extern struct Command * get_command_data(char * dsc_short); /* Reads CommandDB from CommandDB file, line by line, until first empty line. */ extern void init_command_db(); diff --git a/src/client/control.c b/src/client/control.c index 5585ded..56c41fd 100644 --- a/src/client/control.c +++ b/src/client/control.c @@ -3,7 +3,8 @@ #include "control.h" #include /* uint8_t, uint16_t */ #include /* sprintf() */ -#include /* strlen(), strcmp() */ +#include /* strlen() */ +#include "command_db.h" /* get_command_data() */ #include "io.h" /* try_send() */ #include "keybindings.h" /* struct KeyBindingDB, get_command_to_keycode(), * get_keycode_to_command(), mod_selected_keyb(), @@ -31,12 +32,6 @@ static uint8_t try_cmd_1args(int keycode, char * match, static uint8_t try_cmd_2args(int keycode, char * match, void (* f) (char, char), char c1, char c2); -/* If "command" matches "match", send (via try_send()) a string of - * "command_message" + " " + the string representation of "arg" to the server. - */ -static uint8_t try_player_cmd(char * command, char * match, - char * command_message, uint8_t arg); - /* Return keycode to "command" if it is available in current window config. */ static uint16_t get_available_keycode_to_command(char * command); @@ -89,23 +84,6 @@ static uint8_t try_cmd_2args(int keycode, char * match, -static uint8_t try_player_cmd(char * command, char * match, - char * command_message, uint8_t arg) -{ - if (!strcmp(command, match)) - { - uint8_t command_size = strlen(command_message); - uint8_t arg_size = 3; - char msg[command_size + 1 + arg_size + 1]; - sprintf(msg, "%s %d", command_message, arg); - try_send(msg); - return 1; - } - return 0; -} - - - static uint16_t get_available_keycode_to_command(char * command) { uint16_t keycode = get_keycode_to_command(world.kb_global.kbs, command); @@ -175,16 +153,19 @@ extern uint8_t player_control(int key) struct WinConf * wc = get_winconf_by_win(world.wmeta.active); cmd = get_command_to_keycode(wc->kb.kbs, key); } - if (NULL != cmd - && ( try_player_cmd(cmd, "wait", "wait", 0) - || try_player_cmd(cmd, "drop", "drop",world.player_inventory_select) - || try_player_cmd(cmd, "pick", "pick_up", 0) - || try_player_cmd(cmd, "use", "use", world.player_inventory_select) - || try_player_cmd(cmd, "player_u", "move", 'N') - || try_player_cmd(cmd, "player_d", "move", 'S') - || try_player_cmd(cmd, "player_r", "move", 'E') - || try_player_cmd(cmd, "player_l", "move", 'W'))) + if (NULL != cmd) { + struct Command * command_data = get_command_data(cmd); + uint8_t arg = (uint8_t) command_data->arg; + if ('i' == arg) + { + arg = world.player_inventory_select; + } + uint8_t command_size = strlen(command_data->server_msg); + uint8_t arg_size = 3; + char msg[command_size + 1 + arg_size + 1]; + sprintf(msg, "%s %d", command_data->server_msg, arg); + try_send(msg); return 1; } return 0; diff --git a/src/client/control.h b/src/client/control.h index 71aa30a..56d33c3 100644 --- a/src/client/control.h +++ b/src/client/control.h @@ -6,12 +6,15 @@ #ifndef CONTROL_H #define CONTROL_H -#include /* uint8_t, uint16_t */ +#include /* uint8_t */ -/* Try to call by "key" player actions triggering messages to the server. Return - * 1 on success, 0 on failure. +/* Tries to call by "key" for player actions triggering messages to the server. + * The appropriate Command struct's .server_msg will be sent, followed by a " ", + * followed by either a string representation of the numerical value of the + * Command struct's .arg, or, if .arg == 'i', world.player_inventory_select. + * Returns 1 on success, 0 on failure. */ extern uint8_t player_control(int key); diff --git a/src/client/draw_wins.c b/src/client/draw_wins.c index c61576c..1ad1ab2 100644 --- a/src/client/draw_wins.c +++ b/src/client/draw_wins.c @@ -7,7 +7,7 @@ #include /* free() */ #include /* strlen(), strtok() */ #include "../common/try_malloc.h" /* for try_malloc() */ -#include "command_db.h" /* for get_command_longdsc */ +#include "command_db.h" /* get_command_data() */ #include "keybindings.h" /* struct KeyBinding, get_keyname_to_keycode() */ #include "wincontrol.h" /* struct WinConf, get_winconf_by_win() */ #include "windows.h" /* struct Win */ @@ -237,7 +237,8 @@ static char * get_kb_line_and_iterate(struct KeyBinding ** kb_pp) char * f_name = "get_kb_line_and_iterate()"; struct KeyBinding * kb_p = * kb_pp; char * keyname = get_keyname_to_keycode(kb_p->key); - char * cmd_dsc = get_command_longdsc(kb_p->command); + struct Command * command_data = get_command_data(kb_p->command); + char * cmd_dsc = command_data->dsc_long; uint16_t size = 9 + 1 + strlen(cmd_dsc) + 1; char * line = try_malloc(size, f_name); sprintf(line, "%-9s %s", keyname, cmd_dsc);