Only allow this language: ((n|s)!(w|e)!(u|d)!;)+ for directions.
[UnsignedByte.git] / src / Core / Editors / EditorArea.cpp
blobb6ace4309c17886f0333e64049b57971b6757882
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 "EditorArea.h"
22 #include "EditorCluster.h"
23 #include "EditorDetail.h"
24 #include "UBSocket.h"
25 #include "StringUtilities.h"
26 #include "TableImpls.h"
27 #include "Array.h"
28 #include "Account.h"
29 #include "Area.h"
30 #include "AreaManager.h"
31 #include "Managers.h"
33 typedef EditorArea 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 editHeight("Height", &E::editHeight, true, true);
41 static O editWidth( "Width", &E::editWidth, true, true);
42 static O showArea( "Show", &E::showArea, true, false);
43 static O saveArea( "Save", &E::saveArea, true, true);
44 static O startClusters("Clusters", &E::startClusters);
45 static O startDetails("Details",&E::startDetails);
47 static B commands[] = {
48 B("clusters", startClusters),
49 B("description", editDescription),
50 B("details", startDetails),
51 B("height", editHeight),
52 B("name", editName),
53 B("save", saveArea),
54 B("show", showArea),
55 B("width", editWidth),
59 EditorArea::EditorArea(UBSocket* sock) :
60 OLCEditor(sock),
61 m_commands(commands, array_size(commands)),
62 m_area()
64 listCommands(Global::Get()->EmptyString);
67 EditorArea::~EditorArea(void)
72 std::string EditorArea::lookup(const std::string& action)
74 std::string name = OLCEditor::lookup(action);
75 if(name.size() != 0)
76 return name;
78 const AreaCommand* act = (AreaCommand*)m_commands.getObject(action);
79 if(act)
80 return act->getName();
82 return Global::Get()->EmptyString;
85 void EditorArea::dispatch(const std::string& action, const std::string& argument)
87 const AreaCommand* act = (AreaCommand*)m_commands.getObject(action);
89 if(!act)
91 OLCEditor::dispatch(action, argument);
92 return;
95 if(!m_area)
97 m_sock->Send("You need to be editing an area first.\n");
98 m_sock->Send("(Use the 'edit' command.)\n");
99 return;
102 if(act->needLock())
104 try {
105 m_area->Lock();
106 } catch(SavableLocked& e) {
107 m_sock->Send("The area 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 EditorArea::getEditing()
119 return m_area;
122 TableImplPtr EditorArea::getTable()
124 return db::TableImpls::Get()->AREAS;
127 KeysPtr EditorArea::addNew()
129 return mud::Managers::Get()->Area->Add();
132 std::vector<std::string> EditorArea::getList()
134 return mud::Managers::Get()->Area->List();
137 void EditorArea::setEditing(KeysPtr keys)
139 if(!keys->size())
141 m_area.reset();
142 return;
145 m_area = mud::Managers::Get()->Area->GetByKey(keys->first()->getIntegerValue());
146 return;
149 std::vector<std::string> EditorArea::getCommands()
151 return m_commands.getCommandsVector();
154 void EditorArea::editName(const std::string& argument)
156 if(argument.size() == 0)
158 m_sock->Send("Area name can't be zero length!\n");
159 return;
162 m_sock->Sendf("Area name changed from '%s' to '%s'.\n", m_area->getName().c_str(), argument.c_str());
163 m_area->setName(argument);
164 return;
167 void EditorArea::editDescription(const std::string& argument)
169 if(argument.size() == 0)
171 m_sock->Send("No argument, dropping you into the string editor!\n");
172 return;
175 m_sock->Sendf("Area description changed from '%s' to '%s'.\n", m_area->getDescription().c_str(), argument.c_str());
176 m_area->setDescription(argument);
177 return;
180 void EditorArea::editHeight(const std::string& argument)
182 if(argument.size() == 0)
184 m_sock->Send("Please specify the area's height!\n");
185 return;
188 int height = atoi(argument.c_str());
189 if(height <= 0)
191 m_sock->Sendf("Please specify a height > 0!\n");
192 return;
195 m_sock->Sendf("Area height changed from '%d' to '%d'.\n", m_area->getHeight(), height);
196 m_area->setHeight(height);
197 return;
200 void EditorArea::editWidth(const std::string& argument)
202 if(argument.size() == 0)
204 m_sock->Send("Please specify the area's width!\n");
205 return;
208 int width = atoi(argument.c_str());
209 if(width <= 0)
211 m_sock->Sendf("Please specify a width > 0!\n");
212 return;
215 m_sock->Sendf("Area width changed from '%d' to '%d'.\n", m_area->getWidth(), width);
216 m_area->setWidth(width);
217 return;
220 void EditorArea::showArea(const std::string& argument)
222 m_sock->Send(m_area->toString());
225 void EditorArea::saveArea(const std::string& argument)
227 m_sock->Sendf("Saving area '%s'.\n", m_area->getName().c_str());
228 m_area->Save();
229 m_sock->Send("Saved.\n");
230 return;
233 void EditorArea::startClusters(const std::string& argument)
235 if(!argument.compare("."))
237 if(!m_area)
239 m_sock->Send("You can only use '.' when you are already editing an area.\n");
240 m_sock->Send("You may specifiy an area to filter the Cluster Editor on by it's id.\n");
241 return;
244 m_sock->Send("Dropping you into filtered Cluster Edit mode!\n");
245 m_sock->SetEditor(new EditorCluster(m_sock, m_area));
246 return;
249 if(argument.size())
251 value_type id = atoi(argument.c_str());
253 try {
254 mud::AreaPtr area = mud::Managers::Get()->Area->GetByKey(id);
255 m_sock->Send("dropping you into filtered Cluster Edit mode!\n");
256 m_sock->SetEditor(new EditorCluster(m_sock, area));
257 } catch(RowNotFoundException& e) {
258 m_sock->Sendf("Unknown cluster '%s'.\n", argument.c_str());
261 return;
264 m_sock->Send("Dropping you into Cluster Edit mode!\n");
265 m_sock->SetEditor(new EditorCluster(m_sock));
266 return;
269 void EditorArea::startDetails(const std::string& argument)
271 m_sock->Send("Dropping you into Detail Edit mode!\n");
272 m_sock->SetEditor(new EditorDetail(m_sock));
273 return;