1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
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. *
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. *
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"
24 #include "StringUtilities.h"
25 #include "TableImpls.h"
29 #include "CommandManager.h"
30 #include "GrantGroup.h"
31 #include "GrantGroupManager.h"
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
),
55 B("save", saveCommand
),
56 B("show", showCommand
),
59 EditorCommand::EditorCommand(UBSocket
* sock
) :
61 m_commands(commands
, array_size(commands
)),
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
);
78 const CommandCommand
* act
= (CommandCommand
*)m_commands
.getObject(action
);
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
);
91 OLCEditor::dispatch(action
, argument
);
97 m_sock
->Send("You need to be editing a command first.\n");
98 m_sock
->Send("(Use the 'edit' command.)\n");
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");
113 act
->Run(this, argument
);
117 SavablePtr
EditorCommand::getEditing()
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
)
145 m_command
= mud::Managers::Get()->Command
->GetByKey(keys
->first()->getIntegerValue());
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");
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
);
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");
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);
196 if(!argument
.compare("Disable"))
198 m_command
->setHighForce(false);
202 m_sock
->Send("Please specify a force level!\n");
203 m_sock
->Send("Choose between 'Enable' and 'Disable'.\n");
207 void EditorCommand::editForce(const std::string
& argument
)
209 if(!argument
.compare("Enable"))
211 m_command
->setForce(true);
215 if(!argument
.compare("Disable"))
217 m_command
->setForce(false);
221 m_sock
->Send("Please specify a force level!\n");
222 m_sock
->Send("Choose between 'Enable' and 'Disable'.\n");
226 void EditorCommand::editLowForce(const std::string
& argument
)
228 if(!argument
.compare("Enable"))
230 m_command
->setLowForce(true);
234 if(!argument
.compare("Disable"))
236 m_command
->setLowForce(false);
240 m_sock
->Send("Please specify a force level!\n");
241 m_sock
->Send("Choose between 'Enable' and 'Disable'.\n");
245 void EditorCommand::showCommand(const std::string
& argument
)
247 m_sock
->Send(m_command
->toString());
251 void EditorCommand::saveCommand(const std::string
& argument
)
253 m_sock
->Sendf("Saving command '%s'.\n", m_command
->getName().c_str());
255 m_sock
->Send("Saved.\n");