Pol  Revision:cb584c9
menusel.cpp
Go to the documentation of this file.
1 
7 #include "../clib/clib_endian.h"
8 #include "../clib/logfacility.h"
9 #include "../clib/passert.h"
10 #include "../clib/rawtypes.h"
11 #include "accounts/account.h"
12 #include "menu.h"
13 #include "mobile/charactr.h"
14 #include "network/client.h"
15 #include "pktin.h"
16 #include "ufunc.h"
17 
18 namespace Pol
19 {
20 namespace Core
21 {
23 {
24  passert_paranoid( client );
25 
26  if ( !client->chr )
27  {
28  POLLOG.Format( "{} tried to use a menu without being in the game.\n" ) << client->acct->name();
29  return;
30  }
31 
32 
33  Menu* active_menu = nullptr;
34  if ( client->chr->menu.exists() )
35  {
36  active_menu = client->chr->menu.get_weakptr();
37  }
38 
39  if ( active_menu == nullptr )
40  {
41  POLLOG.Format( "{}/{} tried to use a menu, but none was active.\n" )
42  << client->acct->name() << client->chr->name();
43  return;
44  }
45 
46  u16 menu_id = cfBEu16( msg->menu_id );
47  if ( active_menu->menu_id != menu_id )
48  {
49  INFO_PRINT << "Client tried to use a menu he wasn't entitled to\n";
50  // LOGME illegal menu selection
51  client->chr->cancel_menu();
52  return;
53  }
54 
55  client->chr->menu.clear();
56 
57  if ( msg->choice == 0 ) // client cancelled menu
58  {
59  client->chr->cancel_menu();
60  return;
61  }
62 
63  u16 choice = cfBEu16( msg->choice );
64  if ( choice == 0 || choice > active_menu->menuitems_.size() )
65  {
66  INFO_PRINT << "Client menu choice out of range\n";
67  client->chr->cancel_menu();
68  return;
69  }
70  // Wow, client chose something valid from the menu, even.
71  // Note, the incoming message specified object type, color, and used_item_serial.
72  // Since we'd just have to verify these anyway, I'm just going
73  // to ignore them, and use the information in the MenuItem.
74  // If this turns out not to be workable, we'll have to validate those
75  // input fields, too.
76 
77  MenuItem* mi = &active_menu->menuitems_[choice - 1];
78  if ( mi->submenu_id )
79  {
80  Menu* submenu = Menu::find_menu( mi->submenu_id );
81  client->chr->menu = submenu->getWeakPtr();
82  send_menu( client, submenu );
83  }
84  else
85  {
86  passert( client->chr->on_menu_selection );
87  client->chr->on_menu_selection( client, mi, msg );
88  }
89 }
90 }
91 }
static Menu * find_menu(const char *name)
Definition: menu.cpp:79
unsigned short submenu_id
Definition: menu.h:39
Accounts::Account * acct
Definition: client.h:181
T * get_weakptr() const
Definition: weakptr.h:110
Mobile::Character * chr
Definition: client.h:182
std::vector< MenuItem > menuitems_
Definition: menu.h:57
unsigned short u16
Definition: rawtypes.h:26
weak_ptr< Core::Menu > menu
Definition: charactr.h:862
const char * name() const
Definition: account.cpp:193
bool exists() const
Definition: weakptr.h:115
bool send_menu(Network::Client *client, Menu *menu)
Definition: ufunc2.cpp:28
#define passert(exp)
Definition: passert.h:62
#define cfBEu16(x)
Definition: clib_endian.h:44
#define POLLOG
Definition: logfacility.h:219
unsigned short menu_id
Definition: menu.h:53
void handle_menu_selection(Network::Client *client, PKTIN_7D *msg)
Definition: menusel.cpp:22
void(* on_menu_selection)(Network::Client *client, Core::MenuItem *mi, Core::PKTIN_7D *msg)
Definition: charactr.h:863
void clear()
Definition: weakptr.h:120
#define INFO_PRINT
Definition: logfacility.h:223
virtual std::string name() const
Definition: uobject.cpp:196
#define passert_paranoid(exp)
Definition: passert.h:95
Definition: berror.cpp:12
weak_ptr< Menu > getWeakPtr() const
Definition: menu.h:64