Only allow this language: ((n|s)!(w|e)!(u|d)!;)+ for directions.
[UnsignedByte.git] / src / Core / Editors / EditorCluster.cpp
blob8a57eb4a1f9ed8798d55512b9190ea5430c96106
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 "EditorCluster.h"
22 #include "EditorRoom.h"
23 #include "UBSocket.h"
24 #include "StringUtilities.h"
25 #include "TableImpls.h"
26 #include "Array.h"
27 #include "Account.h"
28 #include "Cluster.h"
29 #include "ClusterManager.h"
30 #include "Area.h"
31 #include "AreaManager.h"
32 #include "Managers.h"
34 typedef EditorCluster 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 editDescription("Description", &E::editDescription, true, true);
41 static O editArea( "Area", &E::editArea, true, true);
42 static O showCluster( "Show", &E::showCluster, true, false);
43 static O saveCluster( "Save", &E::saveCluster, true, true);
44 static O startRooms("Rooms",&E::startRooms);
45 static O clearParentArea("ClearParentArea", &E::clearParentArea);
46 static O setParentArea("SetParentArea", &E::setParentArea);
48 static const B commands[] = {
49 B("all", clearParentArea),
50 B("area", editArea),
51 B("description", editDescription),
52 B("filter", setParentArea),
53 B("name", editName),
54 B("rooms", startRooms),
55 B("save", saveCluster),
56 B("show", showCluster),
59 EditorCluster::EditorCluster(UBSocket* sock) :
60 OLCEditor(sock),
61 m_commands(commands, array_size(commands)),
62 m_cluster(),
63 m_target(M_NONE)
65 listCommands(Global::Get()->EmptyString);
68 EditorCluster::EditorCluster(UBSocket* sock, mud::AreaPtr parentArea) :
69 OLCEditor(sock),
70 m_commands(commands, array_size(commands)),
71 m_cluster(),
72 m_parentArea(parentArea),
73 m_target(M_NONE)
75 listCommands(Global::Get()->EmptyString);
76 m_sock->Sendf("Only Clusters that belong to area '%d' are shown, to clear this restriction type 'all'.\n", parentArea->getID());
79 EditorCluster::~EditorCluster(void)
84 void EditorCluster::OnFocus()
86 switch(m_target)
88 case M_NONE:
89 return;
91 case M_DESCRIPTION:
92 editDescription(m_value);
93 break;
96 m_target = M_NONE;
99 std::string EditorCluster::lookup(const std::string& action)
101 std::string name = OLCEditor::lookup(action);
102 if(name.size() != 0)
103 return name;
105 const ClusterCommand* act = (ClusterCommand*)m_commands.getObject(action);
106 if(act)
107 return act->getName();
109 return Global::Get()->EmptyString;
112 void EditorCluster::dispatch(const std::string& action, const std::string& argument)
114 const ClusterCommand* act = (ClusterCommand*)m_commands.getObject(action);
116 if(!act)
118 OLCEditor::dispatch(action, argument);
119 return;
122 if(!m_cluster)
124 m_sock->Send("You need to be editing a cluster first.\n");
125 m_sock->Send("(Use the 'edit' command.)\n");
126 return;
129 if(act->needLock())
131 try {
132 m_cluster->Lock();
133 } catch(SavableLocked& e) {
134 m_sock->Send("The cluster you are currently editing is locked (being edited by someone else), so you cannot edit it right now.\n");
135 m_sock->Send("Please try again later.\n");
136 return;
140 act->Run(this, argument);
141 return;
144 SavablePtr EditorCluster::getEditing()
146 return m_cluster;
149 TableImplPtr EditorCluster::getTable()
151 return db::TableImpls::Get()->CHUNKS;
154 KeysPtr EditorCluster::addNew()
156 return mud::Managers::Get()->Cluster->Add();
159 std::vector<std::string> EditorCluster::getList()
161 return mud::Managers::Get()->Cluster->List(m_parentArea);
164 void EditorCluster::setEditing(KeysPtr keys)
166 if(!keys->size())
168 m_cluster.reset();
169 return;
172 m_cluster = mud::Managers::Get()->Cluster->GetByKey(keys->first()->getIntegerValue());
173 return;
176 std::vector<std::string> EditorCluster::getCommands()
178 return m_commands.getCommandsVector();
181 void EditorCluster::editName(const std::string& argument)
183 if(argument.size() == 0)
185 m_sock->Send("Cluster name can't be zero length!\n");
186 return;
189 m_sock->Sendf("Cluster name changed from '%s' to '%s'.\n", m_cluster->getName().c_str(), argument.c_str());
190 m_cluster->setName(argument);
191 return;
194 void EditorCluster::editDescription(const std::string& argument)
196 if(argument.size() == 0)
198 m_sock->Send("No argument, dropping you into the string editor!\n");
199 return;
202 m_sock->Sendf("Cluster description changed from '%s' to '%s'.\n", m_cluster->getDescription().c_str(), argument.c_str());
203 m_cluster->setDescription(argument);
204 return;
207 void EditorCluster::editArea(const std::string& argument)
209 int id = atoi(argument.c_str());
210 if(id <= 0)
212 m_sock->Send("Please specify a area this Cluster belongs to.\n");
215 std::string oldname;
216 try {
217 mud::AreaPtr oldarea = mud::Managers::Get()->Area->GetByKey(m_cluster->getArea());
218 oldname = oldarea->getName();
219 } catch(RowNotFoundException& e) {
220 oldname = String::Get()->fromInt(m_cluster->getArea());
225 mud::AreaPtr area = mud::Managers::Get()->Area->GetByKey(id);
226 m_sock->Sendf("Area changed from '%s' to '%s'.\n", oldname.c_str(), area->getName().c_str());
227 m_cluster->setArea(id);
229 catch(RowNotFoundException& e)
231 m_sock->Sendf("'%s' is not a valid area!\n", argument.c_str());
232 m_sock->Send(String::Get()->box(mud::Managers::Get()->Area->List(), "Areas"));
233 return;
237 void EditorCluster::startRooms(const std::string& argument)
239 if(!argument.compare("."))
241 if(!m_cluster)
243 m_sock->Send("You can only use '.' when you are already editing a cluster.\n");
244 m_sock->Send("You may specifiy a cluster to filter the Room Editor on by it's id.\n");
245 return;
248 m_sock->Send("Dropping you into filtered Room Edit mode!\n");
249 m_sock->SetEditor(new EditorRoom(m_sock, m_cluster));
250 return;
253 if(argument.size())
255 value_type id = atoi(argument.c_str());
257 try {
258 mud::ClusterPtr cluster = mud::Managers::Get()->Cluster->GetByKey(id);
259 m_sock->Send("Dropping you into filtered Room Edit mode!\n");
260 m_sock->SetEditor(new EditorRoom(m_sock, cluster));
261 } catch(RowNotFoundException& e) {
262 m_sock->Sendf("Unknown cluster '%s'.\n", argument.c_str());
265 return;
268 m_sock->Send("Dropping you into Room Edit mode!\n");
269 m_sock->SetEditor(new EditorRoom(m_sock));
270 return;
273 void EditorCluster::showCluster(const std::string& argument)
275 m_sock->Send(m_cluster->toString());
278 void EditorCluster::saveCluster(const std::string& argument)
280 m_sock->Sendf("Saving cluster '%s'.\n", m_cluster->getName().c_str());
281 m_cluster->Save();
282 m_sock->Send("Saved.\n");
283 return;
286 void EditorCluster::clearParentArea(const std::string& argument)
288 m_sock->Send("Ok.\n");
289 m_parentArea.reset();
290 return;
293 void EditorCluster::setParentArea(const std::string& argument)
295 if(argument.size())
297 m_sock->Send("Please provide a area to filter on.\n");
298 return;
301 try {
302 int id = atoi(argument.c_str());
303 mud::AreaPtr area = mud::Managers::Get()->Area->GetByKey(id);
304 m_parentArea = area;
305 m_sock->Send("Ok.\n");
306 m_sock->Sendf("Only Clusters that belong to area '%d' are shown, to clear this restriction type 'all'.\n", m_parentArea->getID());
307 } catch(RowNotFoundException& e) {
308 m_sock->Sendf("'%s' is not a area.\n", argument.c_str());