diff --git a/wmud/game-networking.c b/wmud/game-networking.c index 8029a65..cc48039 100644 --- a/wmud/game-networking.c +++ b/wmud/game-networking.c @@ -224,12 +224,12 @@ wmud_client_callback(GSocket *client_socket, GIOCondition condition, wmudClient client->authenticated = TRUE; if (client->player->fail_count > 0) { - wmud_client_send(client, "There %s %d failed login attempt%s with your account since your last visit\r\n", (client->player->fail_count == 1) ? "was" : "were", client->player->fail_count, (client->player->fail_count > 1) ? "s" : ""); + wmud_client_send(client, "There %s %d failed login attempt%s with your account since your last visit\r\n", (client->player->fail_count == 1) ? "was" : "were", client->player->fail_count, (client->player->fail_count == 1) ? "" : "s"); } - client->state = WMUD_CLIENT_STATE_MENU; /* TODO: send MOTD */ /* TODO: send menu items */ g_slist_foreach(game_menu, (GFunc)send_menu_item, client); + client->state = WMUD_CLIENT_STATE_MENU; /* TODO: send menu prologue */ } else @@ -262,7 +262,18 @@ wmud_client_callback(GSocket *client_socket, GIOCondition condition, wmudClient } break; case WMUD_CLIENT_STATE_MENU: - //wmud_client_interpret_menu_command(client); + { + gchar *menu_command; + + if ((menu_command = wmud_menu_get_command_by_menuchar(*(client->buffer->str), game_menu)) != NULL) + { + wmud_menu_execute_command(client, menu_command); + } + else + { + wmud_client_send(client, "Unknown menu command.\r\n"); + } + } break; case WMUD_CLIENT_STATE_INGAME: wmud_interpret_game_command(client); diff --git a/wmud/menu.c b/wmud/menu.c index 64a196d..5669130 100644 --- a/wmud/menu.c +++ b/wmud/menu.c @@ -16,6 +16,9 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif #include @@ -31,6 +34,8 @@ * */ +GHashTable *mcmd_table = NULL; + GQuark wmud_menu_error_quark() { @@ -77,7 +82,7 @@ wmud_menu_items_free(GSList **menu_items) } void -menu_item_prepare(wmudMenu *item, gpointer data) +menu_item_prepare(wmudMenu *item, GHashTable *cmdtable) { gchar m1, m2; gchar *a, @@ -125,7 +130,64 @@ menu_item_prepare(wmudMenu *item, gpointer data) g_string_insert(dsa, found - item->text + 8, "\x1b[0m"); item->display_text = g_string_free(ds, FALSE); item->display_text_ansi = g_string_free(dsa, FALSE); - g_debug("Prepared as %s %s", item->display_text, item->display_text_ansi); + +} + +WMUD_MENU_COMMAND(enter_world) +{ +} + +WMUD_MENU_COMMAND(change_password) +{ +} + +WMUD_MENU_COMMAND(toggle_colour) +{ +} + +WMUD_MENU_COMMAND(documentation) +{ +} + +WMUD_MENU_COMMAND(character_select) +{ +} + +WMUD_MENU_COMMAND(character_create) +{ +} + +WMUD_MENU_COMMAND(character_delete) +{ +} + +WMUD_MENU_COMMAND(chat) +{ +} + +WMUD_MENU_COMMAND(player_mail) +{ +} + +WMUD_MENU_COMMAND(colour_test) +{ +} + +WMUD_MENU_COMMAND(change_email) +{ +} + +WMUD_MENU_COMMAND(change_name) +{ +} + +WMUD_MENU_COMMAND(quit) +{ + wmud_client_send(client, "Are you sure you want to get back to the real world? [y/N] "); +} + +WMUD_MENU_COMMAND(redisplay_menu) +{ } gboolean @@ -133,6 +195,7 @@ wmud_menu_init(GSList **menu) { GSList *menu_items = NULL; GError *in_err = NULL; + GHashTable *cmdtable; if (!wmud_db_load_menu(&menu_items, &in_err)) { @@ -160,8 +223,67 @@ wmud_menu_init(GSList **menu) /* TODO: free previous menu list, if *menu is not NULL */ *menu = menu_items; - g_slist_foreach(*menu, (GFunc)menu_item_prepare, NULL); + cmdtable = g_hash_table_new(g_str_hash, g_str_equal); + + g_slist_foreach(*menu, (GFunc)menu_item_prepare, cmdtable); + + g_hash_table_insert(cmdtable, "enter-world", wmud_mcmd_enter_world); + g_hash_table_insert(cmdtable, "change-password", wmud_mcmd_change_password); + g_hash_table_insert(cmdtable, "toggle-colour", wmud_mcmd_toggle_colour); + g_hash_table_insert(cmdtable, "documentation", wmud_mcmd_documentation); + g_hash_table_insert(cmdtable, "caracter-select", wmud_mcmd_character_select); + g_hash_table_insert(cmdtable, "character-create", wmud_mcmd_character_create); + g_hash_table_insert(cmdtable, "character-delete", wmud_mcmd_character_delete); + g_hash_table_insert(cmdtable, "chat", wmud_mcmd_chat); + g_hash_table_insert(cmdtable, "player-mail", wmud_mcmd_player_mail); + g_hash_table_insert(cmdtable, "colour-test", wmud_mcmd_colour_test); + g_hash_table_insert(cmdtable, "change-email", wmud_mcmd_change_email); + g_hash_table_insert(cmdtable, "change-name", wmud_mcmd_change_name); + g_hash_table_insert(cmdtable, "quit", wmud_mcmd_quit); + g_hash_table_insert(cmdtable, "redisplay-menu", wmud_mcmd_redisplay_menu); + + /* TODO: Free previous hash table, if exists */ + mcmd_table = cmdtable; return TRUE; } +static gint +find_by_menuchar(wmudMenu *item, gchar *menuchar) +{ + if (g_ascii_toupper(*menuchar) == g_ascii_toupper(item->menuchar)) + { + return 0; + } + + return 1; +} + +gchar * +wmud_menu_get_command_by_menuchar(gchar menuchar, GSList *game_menu) +{ + GSList *item; + + if ((item = g_slist_find_custom(game_menu, &menuchar, (GCompareFunc)find_by_menuchar)) != NULL) + { + return ((wmudMenu *)(item->data))->func; + } + + return NULL; +} + +void +wmud_menu_execute_command(wmudClient *client, gchar *command) +{ + wmudMenuCommandFunc func; + + if ((func = g_hash_table_lookup(mcmd_table, command)) == NULL) + { + wmud_client_send(client, "Unknown menu command.\r\n"); + } + else + { + func(client); + } +} + diff --git a/wmud/menu.h b/wmud/menu.h index 85f8860..bfcb052 100644 --- a/wmud/menu.h +++ b/wmud/menu.h @@ -19,6 +19,7 @@ #ifndef __WMUD_MENU_H__ #define __WMUD_MENU_H__ +#include "game-networking.h" #include /** @@ -48,11 +49,22 @@ typedef struct _wmudMenu { gchar *func; } wmudMenu; +typedef void (*wmudMenuCommandFunc)(wmudClient *client); +/** + * WMUD_MENU_COMMAND: + * @name: the name of the command. Should be in lowercase + * + * Shorthand to create the function header for menu command handlers + */ +#define WMUD_MENU_COMMAND(name) void wmud_mcmd_ ## name(wmudClient *client) + #define WMUD_MENU_ERROR wmud_menu_error_quark() GQuark wmud_menu_error_quark(); gboolean wmud_menu_init(GSList **menu); gboolean wmud_menu_items_check(GSList *menu_items, GError **err); void wmud_menu_items_free(GSList **menu_items); +gchar *wmud_menu_get_command_by_menuchar(gchar menuchar, GSList *game_menu); +void wmud_menu_execute_command(wmudClient *client, gchar *command); #endif /* __WMUD_MENU_H__ */