Well don't that own? Sorted all the #include's.
[UnsignedByte.git] / src / Core / Editors / EditorRace.cpp
blob6b7e47dd0892beb0b06a6db59ab51c4285b461ed
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 "Account.h"
22 #include "Array.h"
23 #include "CommandBinding.h"
24 #include "CommandTable.h"
25 #include "EditorOLC.h"
26 #include "EditorRace.h"
27 #include "Managers.h"
28 #include "Race.h"
29 #include "RaceManager.h"
30 #include "StringUtilities.h"
31 #include "TableImpls.h"
32 #include "UBSocket.h"
34 typedef EditorRace E;
35 typedef CommandInfoObject<E> O;
36 typedef CommandBinding<E> B;
38 // name function need: object lock
39 static O editName ("Name", &E::editName, true, true);
40 static O saveRace ("Save", &E::saveRace, true, true);
41 static O showRace ("Show", &E::showRace, true, false);
43 static const B commands[] = {
44 B("name", editName),
45 B("save", saveRace),
46 B("show", showRace),
49 EditorRace::EditorRace(UBSocket* sock) :
50 OLCEditor(sock),
51 m_commands(commands, array_size(commands)),
52 m_race()
54 listCommands(Global::Get()->EmptyString);
57 EditorRace::~EditorRace(void)
61 std::string EditorRace::lookup(const std::string& action)
63 std::string name = OLCEditor::lookup(action);
64 if(name.size() != 0)
65 return name;
67 const RaceCommand* act = (RaceCommand*)m_commands.getObject(action);
68 if(act)
69 return act->getName();
71 return Global::Get()->EmptyString;
74 void EditorRace::dispatch(const std::string& action, const std::string& argument)
76 const RaceCommand* act = (RaceCommand*)m_commands.getObject(action);
78 if(!act)
80 OLCEditor::dispatch(action, argument);
81 return;
84 if(!m_race)
86 m_sock->Send("You need to be editing an race first.\n");
87 m_sock->Send("(Use the 'edit' command.)\n");
88 return;
91 if(act->needLock())
93 try {
94 m_race->Lock();
95 } catch(SavableLocked& e) {
96 m_sock->Send("The race you are currently editing is locked (being edited by someone else), so you cannot edit it right now.\n");
97 m_sock->Send("Please try again later.\n");
98 return;
102 act->Run(this, argument);
103 return;
106 SavablePtr EditorRace::getEditing()
108 return m_race;
111 TableImplPtr EditorRace::getTable()
113 return db::TableImpls::Get()->RACES;
116 KeysPtr EditorRace::addNew()
118 return mud::Managers::Get()->Race->Add();
121 std::vector<std::string> EditorRace::getList()
123 return mud::Managers::Get()->Race->List();
126 std::vector<std::string> EditorRace::getCommands()
128 return m_commands.getCommandsVector();
131 void EditorRace::setEditing(KeysPtr keys)
133 if(!keys->size())
135 m_race.reset();
136 return;
139 m_race = mud::Managers::Get()->Race->GetByKey(keys->first()->getIntegerValue());
140 return;
143 void EditorRace::editName(const std::string& argument)
145 if(argument.size() == 0)
147 m_sock->Send("Race name can't be zero length!\n");
148 return;
151 m_sock->Sendf("Race name changed from '%s' to '%s'.\n", m_race->getName().c_str(), argument.c_str());
152 m_race->setName(argument);
153 return;
156 void EditorRace::showRace(const std::string& argument)
158 m_sock->Send(m_race->toString());
161 void EditorRace::saveRace(const std::string& argument)
163 m_sock->Sendf("Saving race '%s'.\n", m_race->getName().c_str());
164 m_race->Save();
165 m_sock->Send("Saved.\n");
166 return;