From 93bf11c446b2412d8f5527c0756fe9901462890d Mon Sep 17 00:00:00 2001 From: Polonkai Gergely Date: Wed, 16 May 2012 19:54:48 +0000 Subject: [PATCH] Now sending prepared menu items to logged in players Signed-off-by: Gergely POLONKAI (W00d5t0ck) --- wmud/game-networking.c | 5 ++-- wmud/menu.c | 55 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/wmud/game-networking.c b/wmud/game-networking.c index 55d89c7..8029a65 100644 --- a/wmud/game-networking.c +++ b/wmud/game-networking.c @@ -91,7 +91,8 @@ wmud_client_close(wmudClient *client, gboolean send_goodbye) void send_menu_item(wmudMenu *item, wmudClient *client) { - wmud_client_send(client, "Item\r\n"); + /* TODO: Send ANSI menu item only to ANSI players! */ + wmud_client_send(client, "%s\r\n", item->display_text_ansi); } /** @@ -228,7 +229,7 @@ wmud_client_callback(GSocket *client_socket, GIOCondition condition, wmudClient client->state = WMUD_CLIENT_STATE_MENU; /* TODO: send MOTD */ /* TODO: send menu items */ - //g_slist_foreach(game_menu, (GFunc)send_menu_item, client); + g_slist_foreach(game_menu, (GFunc)send_menu_item, client); /* TODO: send menu prologue */ } else diff --git a/wmud/menu.c b/wmud/menu.c index 1ad683a..64a196d 100644 --- a/wmud/menu.c +++ b/wmud/menu.c @@ -76,6 +76,58 @@ wmud_menu_items_free(GSList **menu_items) } } +void +menu_item_prepare(wmudMenu *item, gpointer data) +{ + gchar m1, m2; + gchar *a, + *found = NULL; + GString *ds, *dsa; + + g_debug("Preparing menu item %s", item->text); + m1 = g_ascii_tolower(item->menuchar); + m2 = g_ascii_toupper(item->menuchar); + for (a = item->text; *a; a++) + if ((*a == m1) || (*a == m2)) + { + found = a; + break; + } + + if (found) + { + gchar *tmp; + + tmp = g_ascii_strdown(item->text, -1); + ds = g_string_new(tmp); + dsa = g_string_new(tmp); + g_free(tmp); + + ds->str[found - item->text] = g_ascii_toupper(item->menuchar); + dsa->str[found - item->text] = g_ascii_toupper(item->menuchar); + } + else + { + found = item->text; + ds = g_string_new(item->text); + dsa = g_string_new(item->text); + + g_string_prepend_c(ds, ' '); + g_string_prepend_c(ds, g_ascii_toupper(item->menuchar)); + g_string_prepend_c(dsa, ' '); + g_string_prepend_c(dsa, g_ascii_toupper(item->menuchar)); + } + + g_string_insert_c(ds, found - item->text, '('); + g_string_insert_c(ds, found - item->text + 2, ')'); + + g_string_insert(dsa, found - item->text, "\x1b[31;1m"); + 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); +} + gboolean wmud_menu_init(GSList **menu) { @@ -105,8 +157,11 @@ wmud_menu_init(GSList **menu) return FALSE; } + /* TODO: free previous menu list, if *menu is not NULL */ *menu = menu_items; + g_slist_foreach(*menu, (GFunc)menu_item_prepare, NULL); + return TRUE; }