Documentation and refactoring of the command handling templates.
[UnsignedByte.git] / src / Core / EditorRace.cpp
blob3a947ec2d799b6d36d29fa59653f95a1935863d6
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 "CommandBinding.h"
22 #include "CommandObject.h"
23 #include "CommandTable.h"
24 #include "EditorRace.h"
25 #include "EditorOLC.h"
27 #include "UBSocket.h"
29 #include "Array.h"
30 #include "StringUtilities.h"
31 #include "TableImpls.h"
33 #include "Account.h"
34 #include "Race.h"
35 #include "RaceManager.h"
37 typedef EditorRace E;
38 typedef CommandObject<E> O;
39 typedef CommandBinding<E> B;
41 static O editDescription("Description", NULL);
42 static O editHeight ("Height", NULL);
43 static O editName ("Name", &E::editName);
44 static O saveRace ("Save", &E::saveRace);
45 static O showRace ("Show", &E::showRace);
46 static O editWidth ("Width", NULL);
48 static const B commands[] = {
49 B("description", editDescription),
50 B("height", editHeight),
51 B("name", editName),
52 B("save", saveRace),
53 B("show", showRace),
54 B("width", editWidth),
57 EditorRace::EditorRace(UBSocket* sock) :
58 OLCEditor(sock),
59 m_commands(commands, array_size(commands)),
60 m_race()
62 listCommands(Global::Get()->EmptyString);
65 EditorRace::~EditorRace(void)
69 std::string EditorRace::lookup(const std::string& action)
71 std::string name = OLCEditor::lookup(action);
72 if(name.size() != 0)
73 return name;
75 const RaceCommand* act = m_commands.getObject(action);
76 if(act)
77 return act->getName();
79 return Global::Get()->EmptyString;
82 void EditorRace::dispatch(const std::string& action, const std::string& argument)
84 const RaceCommand* act = m_commands.getObject(action);
86 if(act && !m_race)
88 m_sock->Send("You need to be editing an race first.\n");
89 m_sock->Send("(Use the 'edit' command.)\n");
90 return;
93 if(act)
94 act->Run(this, argument);
95 else
96 OLCEditor::dispatch(action, argument);
98 return;
101 SavablePtr EditorRace::getEditing()
103 return m_race;
106 TableImplPtr EditorRace::getTable()
108 return db::TableImpls::Get()->RACES;
111 KeysPtr EditorRace::addNew()
113 return mud::RaceManager::Get()->Add();
116 std::vector<std::string> EditorRace::getList()
118 return mud::RaceManager::Get()->List();
121 std::vector<std::string> EditorRace::getCommands()
123 return m_commands.getCommandsVector();
126 void EditorRace::setEditing(KeysPtr keys)
128 if(!keys->size())
130 m_race.reset();
131 return;
134 m_race = mud::RaceManager::Get()->GetByKey(keys->first()->getValue());
135 return;
138 void EditorRace::editName(const std::string& argument)
140 if(argument.size() == 0)
142 m_sock->Send("Race name can't be zero length!\n");
143 return;
146 m_sock->Sendf("Race name changed from '%s' to '%s'.\n", m_race->getName().c_str(), argument.c_str());
147 m_race->setName(argument);
148 return;
151 void EditorRace::showRace(const std::string& argument)
153 m_sock->Send(String::Get()->box(m_race->Show(), "Race"));
156 void EditorRace::saveRace(const std::string& argument)
158 m_sock->Sendf("Saving race '%s'.\n", m_race->getName().c_str());
159 m_race->Save();
160 m_sock->Send("Saved.\n");
161 return;