polserver/pol-core/pol/menusel.cpp

92 lines
2.2 KiB
C++
Raw Permalink Normal View History

/** @file
*
* @par History
*/
2016-02-11 22:54:43 +01:00
#include "../clib/clib_endian.h"
#include "../clib/logfacility.h"
#include "../clib/passert.h"
#include "../clib/rawtypes.h"
2016-02-11 22:54:43 +01:00
#include "accounts/account.h"
#include "menu.h"
2016-02-11 22:54:43 +01:00
#include "mobile/charactr.h"
#include "network/client.h"
2018-12-30 21:35:54 +01:00
#include "network/pktin.h"
2016-02-11 22:54:43 +01:00
#include "ufunc.h"
namespace Pol
{
namespace Core
{
void handle_menu_selection( Network::Client* client, PKTIN_7D* msg )
{
passert_paranoid( client );
if ( !client->chr )
{
POLLOGLN( "{} tried to use a menu without being in the game.", client->acct->name() );
return;
}
Menu* active_menu = nullptr;
if ( client->chr->menu.exists() )
{
active_menu = client->chr->menu.get_weakptr();
}
if ( active_menu == nullptr )
{
POLLOGLN( "{}/{} tried to use a menu, but none was active.", client->acct->name(),
client->chr->name() );
return;
}
u16 menu_id = cfBEu16( msg->menu_id );
if ( active_menu->menu_id != menu_id )
{
INFO_PRINTLN("Client tried to use a menu he wasn't entitled to");
// LOGME illegal menu selection
client->chr->cancel_menu();
return;
}
2016-02-11 22:54:43 +01:00
client->chr->menu.clear();
2016-02-11 22:54:43 +01:00
if ( msg->choice == 0 ) // client cancelled menu
{
client->chr->cancel_menu();
return;
}
2016-02-11 22:54:43 +01:00
u16 choice = cfBEu16( msg->choice );
if ( choice == 0 || choice > active_menu->menuitems_.size() )
{
INFO_PRINTLN("Client menu choice out of range");
client->chr->cancel_menu();
return;
}
// Wow, client chose something valid from the menu, even.
// Note, the incoming message specified object type, color, and used_item_serial.
// Since we'd just have to verify these anyway, I'm just going
// to ignore them, and use the information in the MenuItem.
// If this turns out not to be workable, we'll have to validate those
// input fields, too.
2016-02-11 22:54:43 +01:00
MenuItem* mi = &active_menu->menuitems_[choice - 1];
if ( mi->submenu_id )
{
Menu* submenu = Menu::find_menu( mi->submenu_id );
client->chr->menu = submenu->getWeakPtr();
send_menu( client, submenu );
}
else
{
passert( client->chr->on_menu_selection );
client->chr->on_menu_selection( client, mi, msg );
2016-02-11 22:54:43 +01:00
}
}
2018-12-30 21:35:54 +01:00
} // namespace Core
} // namespace Pol