Only allow this language: ((n|s)!(w|e)!(u|d)!;)+ for directions.
[UnsignedByte.git] / src / Core / Editors / EditorCommand.cpp
blobc575e1e4ce0fb7e0ba177ca689f04bb11c9e9972
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 "EditorCommand.h"
22 #include "EditorOLC.h"
23 #include "UBSocket.h"
24 #include "StringUtilities.h"
25 #include "TableImpls.h"
26 #include "Array.h"
27 #include "Account.h"
28 #include "Command.h"
29 #include "CommandManager.h"
30 #include "GrantGroup.h"
31 #include "GrantGroupManager.h"
32 #include "Managers.h"
34 using mud::Command;
36 typedef EditorCommand E;
37 typedef CommandInfoObject<E> O;
38 typedef CommandBinding<E> B;
40 // name function need: object lock
41 static O editName( "Name", &E::editName, true, true);
42 static O editGrantGroups("GrantGroup",&E::editGrantGroups,true, true);
43 static O editHighForce( "HighForce", &E::editHighForce, true, true);
44 static O editForce( "Force", &E::editForce, true, true);
45 static O editLowForce( "LowForce", &E::editLowForce, true, true);
46 static O showCommand( "Show", &E::showCommand, true, false);
47 static O saveCommand( "Save", &E::saveCommand, true, true);
49 static const B commands[] = {
50 B("force", editForce),
51 B("grantgroup", editGrantGroups),
52 B("highforce", editHighForce),
53 B("lowforce", editLowForce),
54 B("name", editName),
55 B("save", saveCommand),
56 B("show", showCommand),
59 EditorCommand::EditorCommand(UBSocket* sock) :
60 OLCEditor(sock),
61 m_commands(commands, array_size(commands)),
62 m_command()
64 listCommands(Global::Get()->EmptyString);
67 EditorCommand::~EditorCommand(void)
72 std::string EditorCommand::lookup(const std::string& action)
74 std::string name = OLCEditor::lookup(action);
75 if(name.size() != 0)
76 return name;
78 const CommandCommand* act = (CommandCommand*)m_commands.getObject(action);
79 if(act)
80 return act->getName();
82 return Global::Get()->EmptyString;
85 void EditorCommand::dispatch(const std::string& action, const std::string& argument)
87 const CommandCommand* act = (CommandCommand*)m_commands.getObject(action);
89 if(!act)
91 OLCEditor::dispatch(action, argument);
92 return;
95 if(!m_command)
97 m_sock->Send("You need to be editing a command first.\n");
98 m_sock->Send("(Use the 'edit' command.)\n");
99 return;
102 if(act->needLock())
104 try {
105 m_command->Lock();
106 } catch(SavableLocked& e) {
107 m_sock->Send("The command you are currently editing is locked (being edited by someone else), so you cannot edit it right now.\n");
108 m_sock->Send("Please try again later.\n");
109 return;
113 act->Run(this, argument);
114 return;
117 SavablePtr EditorCommand::getEditing()
119 return m_command;
122 TableImplPtr EditorCommand::getTable()
124 return db::TableImpls::Get()->COMMANDS;
127 KeysPtr EditorCommand::addNew()
129 return mud::Managers::Get()->Command->Add();
132 std::vector<std::string> EditorCommand::getList()
134 return mud::Managers::Get()->Command->List();
137 void EditorCommand::setEditing(KeysPtr keys)
139 if(!keys->size())
141 m_command.reset();
142 return;
145 m_command = mud::Managers::Get()->Command->GetByKey(keys->first()->getIntegerValue());
146 return;
149 std::vector<std::string> EditorCommand::getCommands()
151 return m_commands.getCommandsVector();
154 void EditorCommand::editName(const std::string& argument)
156 if(argument.size() == 0)
158 m_sock->Send("Command name can't be zero length!\n");
159 return;
162 m_sock->Sendf("Command name changed from '%s' to '%s'.\n", m_command->getName().c_str(), argument.c_str());
163 m_command->setName(argument);
164 return;
167 void EditorCommand::editGrantGroups(const std::string& argument)
169 if(!m_command->Exists())
171 m_sock->Send("For some reason the command you are editing does not exist.\n");
172 return;
177 long id = mud::Managers::Get()->GrantGroup->lookupByName(argument);
178 m_sock->Sendf("Grantgroup changed from '%d' to '%d'.\n", m_command->getGrantGroup(), id);
179 m_command->setGrantGroup(id);
181 catch(RowNotFoundException& e)
183 m_sock->Sendf("'%s' is not a valid grantgroup!\n", argument.c_str());
184 m_sock->Send(String::Get()->box(mud::Managers::Get()->GrantGroup->List(), "GrantGroups"));
188 void EditorCommand::editHighForce(const std::string& argument)
190 if(!argument.compare("Enable"))
192 m_command->setHighForce(true);
193 return;
196 if(!argument.compare("Disable"))
198 m_command->setHighForce(false);
199 return;
202 m_sock->Send("Please specify a force level!\n");
203 m_sock->Send("Choose between 'Enable' and 'Disable'.\n");
204 return;
207 void EditorCommand::editForce(const std::string& argument)
209 if(!argument.compare("Enable"))
211 m_command->setForce(true);
212 return;
215 if(!argument.compare("Disable"))
217 m_command->setForce(false);
218 return;
221 m_sock->Send("Please specify a force level!\n");
222 m_sock->Send("Choose between 'Enable' and 'Disable'.\n");
223 return;
226 void EditorCommand::editLowForce(const std::string& argument)
228 if(!argument.compare("Enable"))
230 m_command->setLowForce(true);
231 return;
234 if(!argument.compare("Disable"))
236 m_command->setLowForce(false);
237 return;
240 m_sock->Send("Please specify a force level!\n");
241 m_sock->Send("Choose between 'Enable' and 'Disable'.\n");
242 return;
245 void EditorCommand::showCommand(const std::string& argument)
247 m_sock->Send(m_command->toString());
248 return;
251 void EditorCommand::saveCommand(const std::string& argument)
253 m_sock->Sendf("Saving command '%s'.\n", m_command->getName().c_str());
254 m_command->Save();
255 m_sock->Send("Saved.\n");
256 return;