Removed 'not touching files if ok' since it's not working anyway.
[UnsignedByte.git] / src / Core / Editors / EditorMobile.cpp
blob2e976ff9f34dafb20b06524e23986fbe7bfd04bb
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 "EditorMobile.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 "MCharacter.h"
29 #include "MCharacterManager.h"
30 #include "CharacterManager.h"
31 #include "Managers.h"
33 typedef EditorMobile E;
34 typedef CommandInfoObject<E> O;
35 typedef CommandBinding<E> B;
37 // name function need: object lock
38 static O editName( "Name", &E::editName, true, true);
39 static O editDescription("Description",&E::editDescription,true,true);
40 static O showMobile("Show", &E::showMobile, true, false);
41 static O saveMobile("Save", &E::saveMobile, true, true);
43 static const B commands[] = {
44 B("description", editDescription),
45 B("name", editName),
46 B("save", saveMobile),
47 B("show", showMobile),
50 EditorMobile::EditorMobile(UBSocket* sock) :
51 OLCEditor(sock),
52 m_commands(commands, array_size(commands)),
53 m_mobile()
55 listCommands(Global::Get()->EmptyString);
58 EditorMobile::~EditorMobile(void)
63 std::string EditorMobile::lookup(const std::string& action)
65 std::string name = OLCEditor::lookup(action);
66 if(name.size() != 0)
67 return name;
69 const MobileCommand* act = (MobileCommand*)m_commands.getObject(action);
70 if(act)
71 return act->getName();
73 return Global::Get()->EmptyString;
76 void EditorMobile::dispatch(const std::string& action, const std::string& argument)
78 const MobileCommand* act = (MobileCommand*)m_commands.getObject(action);
80 if(!act)
82 OLCEditor::dispatch(action, argument);
83 return;
86 if(!m_mobile)
88 m_sock->Send("You need to be editing a mobile first.\n");
89 m_sock->Send("(Use the 'edit' command.)\n");
90 return;
93 if(act->needLock())
95 try {
96 m_mobile->Lock();
97 } catch(SavableLocked& e) {
98 m_sock->Send("The mobile you are currently editing is locked (being edited by someone else), so you cannot edit it right now.\n");
99 m_sock->Send("Please try again later.\n");
100 return;
104 act->Run(this, argument);
105 return;
108 SavablePtr EditorMobile::getEditing()
110 return m_mobile;
113 TableImplPtr EditorMobile::getTable()
115 return db::TableImpls::Get()->ENTITIES;
118 KeysPtr EditorMobile::addNew()
120 return mud::Managers::Get()->Character->Add();
123 std::vector<std::string> EditorMobile::getList()
125 return mud::Managers::Get()->Character->List();
128 void EditorMobile::setEditing(KeysPtr keys)
130 if(!keys->size())
132 m_mobile.reset();
133 return;
136 m_mobile = mud::Managers::Get()->MCharacter->GetByKey(keys->first()->getIntegerValue());
137 return;
140 std::vector<std::string> EditorMobile::getCommands()
142 return m_commands.getCommandsVector();
145 void EditorMobile::editName(const std::string& argument)
147 if(argument.size() == 0)
149 m_sock->Send("Mobile name can't be zero length!\n");
150 return;
153 m_sock->Sendf("Mobile name changed from '%s' to '%s'.\n", m_mobile->getName().c_str(), argument.c_str());
154 m_mobile->setName(argument);
155 return;
158 void EditorMobile::editDescription(const std::string& argument)
160 if(!m_mobile->Exists())
162 m_sock->Send("For some reason the mobile you are editing does not exist.\n");
163 return;
166 if(argument.size() == 0)
168 m_sock->Send("No argument, dropping you into the string editor!\n");
169 return;
172 m_sock->Sendf("Mobile description changed from '%s' to '%s'.\n", m_mobile->getDescription().c_str(), argument.c_str());
173 m_mobile->setDescription(argument);
174 return;
177 void EditorMobile::showMobile(const std::string& argument)
179 m_sock->Send(m_mobile->toString());
180 return;
183 void EditorMobile::saveMobile(const std::string& argument)
185 m_sock->Sendf("Saving mobile '%s'.\n", m_mobile->getName().c_str());
186 m_mobile->Save();
187 m_sock->Send("Saved.\n");
188 return;