Added basic menu functions.

Signed-off-by: Gergely Polonkai (W00d5t0ck) <polesz@w00d5t0ck.info>
This commit is contained in:
Gergely Polonkai (W00d5t0ck) 2012-05-17 16:19:21 +02:00
parent 93bf11c446
commit 846629de81
3 changed files with 151 additions and 6 deletions

View File

@ -224,12 +224,12 @@ wmud_client_callback(GSocket *client_socket, GIOCondition condition, wmudClient
client->authenticated = TRUE; client->authenticated = TRUE;
if (client->player->fail_count > 0) 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 MOTD */
/* TODO: send menu items */ /* TODO: send menu items */
g_slist_foreach(game_menu, (GFunc)send_menu_item, client); g_slist_foreach(game_menu, (GFunc)send_menu_item, client);
client->state = WMUD_CLIENT_STATE_MENU;
/* TODO: send menu prologue */ /* TODO: send menu prologue */
} }
else else
@ -262,7 +262,18 @@ wmud_client_callback(GSocket *client_socket, GIOCondition condition, wmudClient
} }
break; break;
case WMUD_CLIENT_STATE_MENU: 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; break;
case WMUD_CLIENT_STATE_INGAME: case WMUD_CLIENT_STATE_INGAME:
wmud_interpret_game_command(client); wmud_interpret_game_command(client);

View File

@ -16,6 +16,9 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <glib.h> #include <glib.h>
@ -31,6 +34,8 @@
* *
*/ */
GHashTable *mcmd_table = NULL;
GQuark GQuark
wmud_menu_error_quark() wmud_menu_error_quark()
{ {
@ -77,7 +82,7 @@ wmud_menu_items_free(GSList **menu_items)
} }
void void
menu_item_prepare(wmudMenu *item, gpointer data) menu_item_prepare(wmudMenu *item, GHashTable *cmdtable)
{ {
gchar m1, m2; gchar m1, m2;
gchar *a, gchar *a,
@ -125,7 +130,64 @@ menu_item_prepare(wmudMenu *item, gpointer data)
g_string_insert(dsa, found - item->text + 8, "\x1b[0m"); g_string_insert(dsa, found - item->text + 8, "\x1b[0m");
item->display_text = g_string_free(ds, FALSE); item->display_text = g_string_free(ds, FALSE);
item->display_text_ansi = g_string_free(dsa, 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 gboolean
@ -133,6 +195,7 @@ wmud_menu_init(GSList **menu)
{ {
GSList *menu_items = NULL; GSList *menu_items = NULL;
GError *in_err = NULL; GError *in_err = NULL;
GHashTable *cmdtable;
if (!wmud_db_load_menu(&menu_items, &in_err)) 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 */ /* TODO: free previous menu list, if *menu is not NULL */
*menu = menu_items; *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; 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);
}
}

View File

@ -19,6 +19,7 @@
#ifndef __WMUD_MENU_H__ #ifndef __WMUD_MENU_H__
#define __WMUD_MENU_H__ #define __WMUD_MENU_H__
#include "game-networking.h"
#include <glib.h> #include <glib.h>
/** /**
@ -48,11 +49,22 @@ typedef struct _wmudMenu {
gchar *func; gchar *func;
} wmudMenu; } 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() #define WMUD_MENU_ERROR wmud_menu_error_quark()
GQuark wmud_menu_error_quark(); GQuark wmud_menu_error_quark();
gboolean wmud_menu_init(GSList **menu); gboolean wmud_menu_init(GSList **menu);
gboolean wmud_menu_items_check(GSList *menu_items, GError **err); gboolean wmud_menu_items_check(GSList *menu_items, GError **err);
void wmud_menu_items_free(GSList **menu_items); 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__ */ #endif /* __WMUD_MENU_H__ */