Added the menu loading and checking functions.

Signed-off-by: Gergely POLONKAI (W00d5t0ck) <polesz@w00d5t0ck.info>
This commit is contained in:
Polonkai Gergely 2012-03-31 09:19:57 +00:00
parent d60f0a85f1
commit db4ca68b7e
8 changed files with 244 additions and 2 deletions

View File

@ -25,6 +25,7 @@
<xi:include href="xml/interpreter.xml"/> <xi:include href="xml/interpreter.xml"/>
<xi:include href="xml/db.xml"/> <xi:include href="xml/db.xml"/>
<xi:include href="xml/player.xml"/> <xi:include href="xml/player.xml"/>
<xi:include href="xml/menu.xml"/>
<xi:include href="xml/world.xml"/> <xi:include href="xml/world.xml"/>
</chapter> </chapter>

View File

@ -79,6 +79,11 @@ wmud_player_auth
wmud_player_exists wmud_player_exists
</SECTION> </SECTION>
<SECTION>
<TITLE></TITLE>
<FILE>menu</FILE>
</SECTION>
<SECTION> <SECTION>
<TITLE></TITLE> <TITLE></TITLE>
<FILE>world</FILE> <FILE>world</FILE>

View File

@ -1,5 +1,5 @@
bin_PROGRAMS = wmud bin_PROGRAMS = wmud
AM_CPPFLAGS = -DWMUD_STATEDIR=\""$(localstatedir)"\" -DWMUD_CONFDIR=\""$(sysconfdir)"\" $(MEMCACHED_CFLAGS) $(GLIB_CFLAGS) $(GIO_CFLAGS) $(GTHREAD_CFLAGS) $(SQLITE3_CFLAGS) AM_CPPFLAGS = -DWMUD_STATEDIR=\""$(localstatedir)"\" -DWMUD_CONFDIR=\""$(sysconfdir)"\" $(MEMCACHED_CFLAGS) $(GLIB_CFLAGS) $(GIO_CFLAGS) $(GTHREAD_CFLAGS) $(SQLITE3_CFLAGS)
wmud_SOURCES = main.c game-networking.c interpreter.c db.c players.c maintenance.c game.c configuration.c world.c wmud_SOURCES = main.c game-networking.c interpreter.c db.c players.c maintenance.c game.c configuration.c world.c menu.c
wmud_LDADD = $(MEMCACHED_LIBS) $(GLIB_LIBS) $(GIO_LIBS) $(GTHREAD_LIBS) $(SQLITE3_LIBS) wmud_LDADD = $(MEMCACHED_LIBS) $(GLIB_LIBS) $(GIO_LIBS) $(GTHREAD_LIBS) $(SQLITE3_LIBS)

View File

@ -25,6 +25,7 @@
#include "db.h" #include "db.h"
#include "players.h" #include "players.h"
#include "configuration.h" #include "configuration.h"
#include "menu.h"
/** /**
* SECTION:db * SECTION:db
@ -550,3 +551,59 @@ wmud_db_load_planet_planes(GSList **planet_planes, GError **err)
return TRUE; return TRUE;
} }
gboolean
wmud_db_load_menu(GSList **menu_items, GError **err)
{
sqlite3_stmt *sth = NULL;
int sqlite_code;
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading menu items");
if (dbh == NULL)
{
if (err)
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized");
return FALSE;
}
if ((sqlite_code = sqlite3_prepare_v2(dbh, "SELECT id, menuchar, need_active_char, placement, display_text, fnctn FROM menu", -1, &sth, NULL)) != SQLITE_OK)
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad query in wmud_db_load_menu(): %s", sqlite3_errmsg(dbh));
return FALSE;
}
while (1)
{
sqlite_code = sqlite3_step(sth);
if (sqlite_code == SQLITE_ROW)
{
wmudMenu *menu_item = g_new0(wmudMenu, 1);
menu_item->id = sqlite3_column_int(sth, 0);
menu_item->menuchar = *(sqlite3_column_text(sth, 1));
menu_item->need_active_char = (sqlite3_column_int(sth, 2) != 0);
menu_item->placement = sqlite3_column_int(sth, 4);
menu_item->text = g_strdup((gchar *)sqlite3_column_text(sth, 5));
menu_item->func = g_strdup((gchar *)sqlite3_column_text(sth, 6));
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded menu item %s(%d)", menu_item->text, menu_item->id);
*menu_items = g_slist_prepend(*menu_items, menu_item);
}
else if (sqlite_code == SQLITE_DONE)
{
break;
}
else
{
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Query error in wmud_db_load_menu_items(): %s", sqlite3_errmsg(dbh));
sqlite3_finalize(sth);
return FALSE;
}
}
sqlite3_finalize(sth);
return TRUE;
return FALSE;
}

View File

@ -50,6 +50,7 @@ gboolean wmud_db_load_areas(GSList **areas, GError **err);
gboolean wmud_db_load_rooms(GSList **rooms, GError **err); gboolean wmud_db_load_rooms(GSList **rooms, GError **err);
gboolean wmud_db_load_exits(GSList **exits, GError **err); gboolean wmud_db_load_exits(GSList **exits, GError **err);
gboolean wmud_db_load_planet_planes(GSList **planet_planes, GError **err); gboolean wmud_db_load_planet_planes(GSList **planet_planes, GError **err);
gboolean wmud_db_load_menu(GSList **menu_items, GError **err);
#endif /* __WMUD__DB_H__ */ #endif /* __WMUD__DB_H__ */

View File

@ -136,6 +136,7 @@ main(int argc, char **argv)
GError *err = NULL; GError *err = NULL;
GThread *game_thread; GThread *game_thread;
GMainContext *game_context; GMainContext *game_context;
GSList *game_menu;
/* Initialize the thread and type system */ /* Initialize the thread and type system */
g_thread_init(NULL); g_thread_init(NULL);
@ -182,9 +183,16 @@ main(int argc, char **argv)
return 1; return 1;
} }
if (!wmud_menu_init(&game_menu))
{
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "An error occured during menu loading.");
return 1;
}
/* Initialization ends here */ /* Initialization ends here */
wmud_game_init(&game_thread, &game_context); wmud_game_init(&game_thread, &game_context, game_menu);
g_clear_error(&err); g_clear_error(&err);
if (!wmud_networking_init(active_config->port, game_context, &err)) if (!wmud_networking_init(active_config->port, game_context, &err))

112
wmud/menu.c Normal file
View File

@ -0,0 +1,112 @@
/* wMUD - Yet another MUD codebase by W00d5t0ck
* Copyright (C) 2012 - Gergely POLONKAI
*
* menu.c: menu handling routines
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
#include "menu.h"
#include "db.h"
#include "menu.h"
/**
* SECTION:menu
* @short_description: Menu handling
* @title: Menu handling routines
*
*/
GQuark
wmud_menu_error_quark()
{
return g_quark_from_static_string("wmud-menu-error");
}
gboolean
wmud_menu_items_check(GSList *menu_items, GError **err)
{
/* TODO: Check for duplicate menuchars */
/* TODO: Check for duplicate menu texts */
/* TODO: Check for duplicate placements */
return TRUE;
}
void
menu_item_free(wmudMenu *menu_item)
{
if (menu_item->text)
g_free(menu_item->text);
if (menu_item->display_text)
g_free(menu_item->display_text);
if (menu_item->display_text_ansi)
g_free(menu_item->display_text_ansi);
if (menu_item->func)
g_free(menu_item->func);
g_free(menu_item);
}
void
wmud_menu_items_free(GSList **menu_items)
{
if (menu_items)
{
#if GLIB_CHECK_VERSION(2, 28, 0)
g_slist_free_full(*menu_items, (GDestroyNotify)menu_item_free);
#else
g_slist_foreach(*menu_items, (GFunc)menu_item_free, NULL);
g_slist_free(*menu_items);
#endif
*menu_items = NULL;
}
}
gboolean
wmud_menu_init(GSList **menu)
{
GSList *menu_items = NULL;
GError *in_err = NULL;
if (!wmud_db_load_menu(&menu_items, &in_err))
{
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Unable to load menu items from the database: %s", in_err->message);
wmud_menu_items_free(&menu_items);
return FALSE;
}
if (!menu_items)
{
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "No menu items were found in the database!");
return FALSE;
}
if (!wmud_menu_items_check(menu_items, &in_err))
{
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Menu items pre-flight check error: %s", in_err->message);
wmud_menu_items_free(&menu_items);
return FALSE;
}
*menu = menu_items;
return TRUE;
}

58
wmud/menu.h Normal file
View File

@ -0,0 +1,58 @@
/* wMUD - Yet another MUD codebase by W00d5t0ck
* Copyright (C) 2012 - Gergely POLONKAI
*
* menu.h: menu handling routines
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __WMUD_MENU_H__
#define __WMUD_MENU_H__
#include <glib.h>
/**
* wmudMenu:
* @id: The databas ID of the menu item
* @menuchar: The highlighted character of the menu item. Players must enter it
* in the menu to activate the menu item
* @need_active_char: If %TRUE, the menu item will be displayed only if the
* player has a character selected
* @placement: The order number of the menu item. The whole menu is sorted
* based on this field
* @text: The text to be displayed to the player. It gets processed by
* wmud_menu_process_item() to highlight the menuchar
* @display_text: The actual text that gets displayed to the player
* @display_text_ansi: The actual text that gets displayed to the player when
* ANSI colour mode is enabled
* @func: the menu item's function. See menu.c for the list of possible values.
*/
typedef struct _wmudMenu {
guint id;
gchar menuchar;
gboolean need_active_char;
guint placement;
gchar *text;
gchar *display_text;
gchar *display_text_ansi;
gchar *func;
} wmudMenu;
#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);
#endif /* __WMUD_MENU_H__ */