Chunk is purely a wrapper class, any 'state variables', like 'characters in room...
[UnsignedByte.git] / src / Core / Editors / EditorDetail.cpp
blob7524b5629433f1a00a78d31d3db81679767d38c2
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 "SavableHeaders.h"
22 #include "EditorDetail.h"
23 #include "EditorString.h"
24 #include "EditorBool.h"
25 #include "Array.h"
26 #include "Assert.h"
27 #include "StringUtilities.h"
28 #include "TableImpls.h"
29 #include "UBSocket.h"
30 #include "Account.h"
31 #include "Detail.h"
32 #include "DetailManager.h"
33 #include "Area.h"
34 #include "AreaManager.h"
35 #include "Room.h"
36 #include "RoomManager.h"
37 #include "Chunk.h"
38 #include "ChunkManager.h"
39 #include "Managers.h"
41 #include <boost/spirit/core.hpp>
43 typedef EditorDetail E;
44 typedef CommandInfoObject<E> O;
45 typedef CommandBinding<E> B;
47 using namespace boost::spirit;
49 // name function need: object lock
50 static O editKey( "Key", &E::editKey, true, true);
51 static O editDescription("Description", &E::editDescription, true, true);
52 static O showDetail("Show", &E::showDetail, true, false);
53 static O saveDetail("Save", &E::saveDetail, true, true);
54 static O clearParents("ClearParents", &E::clearParents);
55 static O setParent( "SetParent",&E::setParent);
57 static const B commands[] = {
58 B("all", clearParents),
59 B("description", editDescription),
60 B("filter", setParent),
61 B("name", editKey),
62 B("save", saveDetail),
63 B("show", showDetail),
66 EditorDetail::EditorDetail(UBSocket* sock) :
67 OLCEditor(sock),
68 m_commands(commands, array_size(commands)),
69 m_detail(),
70 m_target(M_NONE)
72 listCommands(Global::Get()->EmptyString);
75 EditorDetail::EditorDetail(UBSocket* sock, mud::DetailPtr detail) :
76 OLCEditor(sock),
77 m_commands(commands, array_size(commands)),
78 m_detail(detail),
79 m_target(M_NONE)
84 EditorDetail::EditorDetail(UBSocket* sock, mud::AreaPtr parentArea) :
85 OLCEditor(sock),
86 m_commands(commands, array_size(commands)),
87 m_detail(),
88 m_parentArea(parentArea),
89 m_target(M_NONE)
91 listCommands(Global::Get()->EmptyString);
92 m_sock->Sendf("Only Details that belong to area '%d' are shown, to clear this restriction type 'all'.\n", parentArea->getID());
95 EditorDetail::EditorDetail(UBSocket* sock, mud::RoomPtr parentRoom) :
96 OLCEditor(sock),
97 m_commands(commands, array_size(commands)),
98 m_detail(),
99 m_parentRoom(parentRoom),
100 m_target(M_NONE)
102 listCommands(Global::Get()->EmptyString);
103 m_sock->Sendf("Only Details that belong to room '%d' are shown, to clear this restriction type 'all'.\n", parentRoom->getID());
106 EditorDetail::EditorDetail(UBSocket* sock, mud::ChunkPtr parentChunk) :
107 OLCEditor(sock),
108 m_commands(commands, array_size(commands)),
109 m_detail(),
110 m_parentChunk(parentChunk),
111 m_target(M_NONE)
113 listCommands(Global::Get()->EmptyString);
114 m_sock->Sendf("Only Details that belong to chunk '%d' are shown, to clear this restriction type 'all'.\n", parentChunk->getID());
117 EditorDetail::~EditorDetail(void)
122 void EditorDetail::OnFocus()
124 switch(m_target)
126 case M_NONE:
127 return;
129 case M_DESCRIPTION:
130 m_detail->setDescription(m_value);
131 break;
134 m_target = M_NONE;
137 std::string EditorDetail::lookup(const std::string& action)
139 const DetailCommand* act = (DetailCommand*)m_commands.getObject(action);
140 if(act)
141 return act->getName();
143 return Global::Get()->EmptyString;
146 void EditorDetail::dispatch(const std::string& action, const std::string& argument)
148 const DetailCommand* act = (DetailCommand*)m_commands.getObject(action);
150 Assert(act);
152 if(!m_detail)
154 m_sock->Send("You need to be editing a detail first.\n");
155 m_sock->Send("(Use the 'edit' command.)\n");
156 return;
159 if(act->needLock())
161 try {
162 m_detail->Lock();
163 } catch(SavableLocked& e) {
164 m_sock->Send("The detail you are currently editing is locked (being edited by someone else), so you cannot edit it right now.\n");
165 m_sock->Send("Please try again later.\n");
166 return;
170 act->Run(this, argument);
171 return;
174 SavablePtr EditorDetail::getEditing()
176 return m_detail;
179 TableImplPtr EditorDetail::getTable()
181 return db::TableImpls::Get()->DETAILS;
184 KeysPtr EditorDetail::addNew()
186 return mud::Managers::Get()->Detail->Add();
189 std::vector<std::string> EditorDetail::getList()
191 if(m_parentArea)
192 return mud::Managers::Get()->Detail->List(m_parentArea);
194 if(m_parentRoom)
195 return mud::Managers::Get()->Detail->List(m_parentRoom);
197 if(m_parentChunk)
198 return mud::Managers::Get()->Detail->List(m_parentChunk);
200 return mud::Managers::Get()->Detail->List();
203 void EditorDetail::setEditing(KeysPtr keys)
205 if(!keys->size())
207 m_detail.reset();
208 return;
211 m_detail = mud::Managers::Get()->Detail->GetByKey(keys->first()->getIntegerValue());
212 return;
215 std::vector<std::string> EditorDetail::getCommands()
217 return m_commands.getCommandsVector();
220 void EditorDetail::editKey(const std::string& argument)
222 if(argument.size() == 0)
224 m_sock->Send("Detail key can't be zero length!\n");
225 return;
228 m_sock->Sendf("Detail key changed from '%s' to '%s'.\n", m_detail->getKey().c_str(), argument.c_str());
229 m_detail->setKey(argument);
230 return;
233 void EditorDetail::editDescription(const std::string& argument)
235 if(argument.size() == 0)
237 m_sock->Send("No argument, dropping you into the string editor!\n");
238 m_sock->SetEditor(new EditorString(m_sock, m_value));
239 m_target = M_DESCRIPTION;
240 return;
243 m_sock->Sendf("Detail description changed from '%s' to '%s'.\n", m_detail->getDescription().c_str(), argument.c_str());
244 m_detail->setDescription(argument);
245 return;
248 void EditorDetail::showDetail(const std::string& argument)
250 std::vector<std::string> result;
251 std::string key = "Key: '";
252 key.append(m_detail->getKey());
253 key.append("'.");
254 result.push_back(key);
256 std::string description = "Description: '";
257 description.append(m_detail->getDescription());
258 description.append("'.");
259 result.push_back(description);
261 m_sock->Send(String::Get()->box(result, "Detail"));
264 void EditorDetail::saveDetail(const std::string& argument)
266 m_sock->Sendf("Saving detail '%s'.\n", m_detail->getKey().c_str());
267 if(argument.size())
268 m_detail->Save(m_sock->GetAccount()->getID(), argument);
269 else
270 m_detail->Save(m_sock->GetAccount()->getID(), Global::Get()->EmptyString);
271 m_sock->Send("Saved.\n");
272 return;
275 void EditorDetail::clearParents(const std::string& argument)
277 m_sock->Send("Ok.\n");
278 m_parentArea.reset();
279 m_parentRoom.reset();
280 m_parentChunk.reset();
281 return;
284 void EditorDetail::setParent(const std::string& argument)
286 if(!argument.size())
288 m_sock->Send("Please provide an area, room, or chunk to filter on.\n");
289 return;
292 value_type id = 0;
293 bool area = false;
294 bool chunk = false;
295 bool room = false;
297 bool good = parse(argument.c_str(),
298 // Begin grammar
300 (str_p("area")[assign_a(area, true)] |
301 str_p("chunk")[assign_a(chunk, true)] |
302 str_p("room")[assign_a(room, true)])
304 >> int_p[assign_a(id)]
307 // End grammar
308 space_p).full;
310 if(!good)
312 m_sock->Send("Please specify what area, room, or chunk to filter on in the following format:\n");
313 m_sock->Send("area/room/chunk id\n");
314 return;
317 Assert(area || chunk || room);
319 if(area)
321 try {
322 mud::AreaPtr area = mud::Managers::Get()->Area->GetByKey(id);
323 m_parentArea = area;
324 m_sock->Send("Ok.\n");
325 m_sock->Sendf("Only Details that belong to area '%d' are shown, to clear this restriction type 'all'.\n", m_parentArea->getID());
326 } catch(RowNotFoundException& e) {
327 m_sock->Sendf("'%s' is not an area.\n", argument.c_str());
331 if(room)
333 try {
334 mud::RoomPtr room = mud::Managers::Get()->Room->GetByKey(id);
335 m_parentRoom = room;
336 m_sock->Send("Ok.\n");
337 m_sock->Sendf("Only Details that belong to room '%d' are shown, to clear this restriction type 'all'.\n", m_parentRoom->getID());
338 } catch(RowNotFoundException& e) {
339 m_sock->Sendf("'%s' is not a room.\n", argument.c_str());
343 if(chunk)
345 try {
346 mud::ChunkPtr chunk = mud::Managers::Get()->Chunk->GetByKey(id);
347 m_parentChunk = chunk;
348 m_sock->Send("Ok.\n");
349 m_sock->Sendf("Only Details that belong to chunk '%d' are shown, to clear this restriction type 'all'.\n", m_parentChunk->getID());
350 } catch(RowNotFoundException& e) {
351 m_sock->Sendf("'%d' is not a chunk.\n", id);