Only allow this language: ((n|s)!(w|e)!(u|d)!;)+ for directions.
[UnsignedByte.git] / src / Core / Editors / EditorAccount.cpp
blob72c1d03a6745ec8ea7a3cf28fa57583e5b01cd26
1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "EditorAccount.h"
22 #include "EditorOLC.h"
23 #include "EditorNewCharacter.h"
24 #include "EditorPlaying.h"
25 #include "EditorOOC.h"
26 #include "UBSocket.h"
27 #include "StringUtilities.h"
28 #include "TableImpls.h"
29 #include "Array.h"
30 #include "FieldImpls.h"
31 #include "Account.h"
32 #include "Command.h"
33 #include "Permission.h"
34 #include "PCharacter.h"
35 #include "PCharacterManager.h"
36 #include "CharacterManager.h"
37 #include "Room.h"
38 #include "Managers.h"
40 extern bool g_quit;
42 typedef EditorAccount E;
43 typedef CommandInfoObject<E> O;
44 typedef CommandBinding<E> B;
46 // name function fullname
47 static O listCommands( "Commands", &E::listCommands);
48 static O beginLogin( "Login", &E::beginLogin);
49 static O beginOLC( "OLC", &E::beginOLC);
50 static O beginOOC( "OOC", &E::beginOOC);
51 static O beginCreation( "New", &E::beginCreation, true);
52 static O quitEditor( "Quit", &E::quitEditor, true);
53 static O shutdownGame( "Shutdown", &E::shutdownGame, true);
54 static O listCharacters("List", &E::listCharacters);
56 static B commands[] = {
57 B("?", listCommands),
58 B("list", listCharacters),
59 B("login", beginLogin),
60 B("new", beginCreation),
61 B("olc", beginOLC),
62 B("ooc", beginOOC),
63 B("quit", quitEditor),
64 B("shutdown", shutdownGame),
67 EditorAccount::EditorAccount(UBSocket* sock) :
68 Editor(sock),
69 m_commands(commands, array_size(commands))
71 listCommands(Global::Get()->EmptyString);
74 EditorAccount::~EditorAccount(void)
79 std::string EditorAccount::lookup(const std::string& action)
81 const AccountCommand* act = (AccountCommand*)m_commands.getObject(action);
82 if(act)
83 return act->getName();
85 return Global::Get()->EmptyString;
88 void EditorAccount::dispatch(const std::string& action, const std::string& argument)
90 const AccountCommand* act = (AccountCommand*)m_commands.getObject(action);
92 Assert(act);
93 act->Run(this, argument);
96 void EditorAccount::beginLogin(const std::string &argument)
98 if(argument.size() == 0)
100 m_sock->Send("Please choose a character:\n");
101 listCharacters(Global::Get()->EmptyString);
102 return;
105 if(mud::PCharacterManager::Get()->isActive(argument))
107 m_sock->Send("You are already playing that character!\n");
108 return;
111 int id = 0;
115 id = mud::Managers::Get()->Character->lookupByName(argument);
117 catch(RowNotFoundException& e)
119 m_sock->Send("No such character.\n");
120 m_sock->Send("\n");
121 beginLogin(Global::Get()->EmptyString);
122 return;
125 KeysPtr keys(new Keys(db::TableImpls::Get()->CHARACTERACCOUNT));
126 KeyValuePtr key;
128 key = KeyValuePtr(new KeyValue(db::TableImpls::Get()->CHARACTERACCOUNT->FKACCOUNTS, id));
129 keys->addKey(key);
130 key = KeyValuePtr(new KeyValue(db::TableImpls::Get()->CHARACTERACCOUNT->FKENTITIES, m_sock->GetAccount()->getID()));
131 keys->addKey(key);
133 bool hasAccount = false;
136 SavableManagerPtr manager = SavableManager::bykeys(keys);
137 hasAccount = true;
138 } catch(RowNotFoundException& e) { }
140 if(!hasAccount)
142 m_sock->Sendf("You don't have a character named '%s'!\n", argument.c_str());
143 beginLogin(Global::Get()->EmptyString);
144 return;
147 mud::PCharacterPtr Ch = mud::PCharacterManager::Get()->LoadByKey(m_sock, id);
148 m_sock->Sendf("Welcome back, %s\n", argument.c_str());
149 m_sock->SetEditor(new EditorPlaying(m_sock, Ch));
150 return;
153 void EditorAccount::listCharacters(const std::string &argument)
155 Assert(m_sock->hasAccount());
156 mud::AccountPtr account = m_sock->GetAccount();
158 Strings characters = mud::Managers::Get()->Character->List(account);
160 if(characters.size() == 0)
162 m_sock->Send("You have no characters yet!\n");
163 return;
166 m_sock->Send(String::Get()->box(characters, "Characters"));
167 m_sock->Send("\n");
168 return;
171 void EditorAccount::listCommands(const std::string &argument)
173 m_sock->Send(String::Get()->box(m_commands.getCommandsVector(), "Account"));
174 m_sock->Send("\n");
175 return;
178 void EditorAccount::beginOLC(const std::string &argument)
180 m_sock->Send("Dropping you into OLC mode!\n");
181 m_sock->SetEditor(new EditorOLC(m_sock));
182 return;
185 void EditorAccount::beginOOC(const std::string& argument)
187 m_sock->Send("Dropping you into OOC mode!\n");
188 m_sock->SetEditor(new EditorOOC(m_sock));
191 void EditorAccount::beginCreation(const std::string &argument)
193 m_sock->Send("Dropping you into Character Creation mode!\n");
194 m_sock->SetEditor(new EditorNewCharacter(m_sock));
195 return;
198 void EditorAccount::quitEditor(const std::string &argument)
200 m_sock->Send("Thank you for visiting.\n");
201 m_sock->SetCloseAndDelete();
202 return;
205 void EditorAccount::shutdownGame(const std::string &argument)
207 m_sock->Send("Shutting down...\n");
208 g_quit = true;
209 m_sock->Send("Game will shut down after this loop!\n");
210 return;