Chunk is purely a wrapper class, any 'state variables', like 'characters in room...
[UnsignedByte.git] / src / Core / Editors / EditorChunk.cpp
blobf910b816e23a68dc69ebe58964d71bc00c2e9753
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 "EditorChunk.h"
22 #include "EditorString.h"
23 #include "EditorBool.h"
24 #include "EditorDetail.h"
25 #include "UBSocket.h"
26 #include "StringUtilities.h"
27 #include "TableImpls.h"
28 #include "Array.h"
29 #include "Account.h"
30 #include "Chunk.h"
31 #include "ChunkManager.h"
32 #include "Room.h"
33 #include "RoomManager.h"
34 #include "ChunkImporter.h"
35 #include "Managers.h"
37 typedef EditorChunk E;
38 typedef CommandInfoObject<E> O;
39 typedef CommandBinding<E> B;
41 // name function need: object lock
42 static O editName( "Name", &E::editName, true, true);
43 static O editDescription("Description", &E::editDescription, true, true);
44 static O editRoom( "Room", &E::editRoom, true, true);
45 static O startDetails("Details",&E::startDetails);
46 static O importChunk("Import", &E::importChunk);
47 static O showChunk( "Show", &E::showChunk, true, false);
48 static O saveChunk( "Save", &E::saveChunk, true, true);
49 static O clearParentRoom("ClearParentRoom", &E::clearParentRoom);
50 static O setParentRoom("SetParentRoom", &E::setParentRoom);
52 static const B commands[] = {
53 B("all", clearParentRoom),
54 B("description", editDescription),
55 B("details", startDetails),
56 B("filter", setParentRoom),
57 B("import", importChunk),
58 B("name", editName),
59 B("room", editRoom),
60 B("save", saveChunk),
61 B("show", showChunk),
64 EditorChunk::EditorChunk(UBSocket* sock) :
65 OLCEditor(sock),
66 m_commands(commands, array_size(commands)),
67 m_chunk(),
68 m_target(M_NONE)
70 listCommands(Global::Get()->EmptyString);
73 EditorChunk::EditorChunk(UBSocket* sock, mud::RoomPtr parentRoom) :
74 OLCEditor(sock),
75 m_commands(commands, array_size(commands)),
76 m_chunk(),
77 m_parentRoom(parentRoom),
78 m_target(M_NONE)
80 listCommands(Global::Get()->EmptyString);
81 m_sock->Sendf("Only Chunks that belong to room '%d' are shown, to clear this restriction type 'all'.\n", parentRoom->getID());
84 EditorChunk::~EditorChunk(void)
89 void EditorChunk::OnFocus()
91 switch(m_target)
93 case M_NONE:
94 return;
96 case M_IMPORT:
97 m_target = M_NONE;
98 importChunk(m_value);
99 break;
101 case M_IMPORTACCEPT:
102 m_target = M_NONE;
103 importChunk(m_yesno ? "accept" : "reject");
104 break;
106 case M_IMPORTSAVECHUNK:
107 m_target = M_NONE;
108 importChunk(m_yesno ? "save" : "discard");
109 break;
112 // m_target = M_NONE; // has to happen -before- importChunk is called since importChunk might set it to something else
115 std::string EditorChunk::lookup(const std::string& action)
117 std::string name = OLCEditor::lookup(action);
118 if(name.size() != 0)
119 return name;
121 const ChunkCommand* act = (ChunkCommand*)m_commands.getObject(action);
122 if(act)
123 return act->getName();
125 return Global::Get()->EmptyString;
128 void EditorChunk::dispatch(const std::string& action, const std::string& argument)
130 const ChunkCommand* act = (ChunkCommand*)m_commands.getObject(action);
132 if(!act)
134 OLCEditor::dispatch(action, argument);
135 return;
138 if(!m_chunk)
140 m_sock->Send("You need to be editing a chunk first.\n");
141 m_sock->Send("(Use the 'edit' command.)\n");
142 return;
145 if(act->needLock())
147 try {
148 m_chunk->Lock();
149 } catch(SavableLocked& e) {
150 m_sock->Send("The chunk you are currently editing is locked (being edited by someone else), so you cannot edit it right now.\n");
151 m_sock->Send("Please try again later.\n");
152 return;
156 act->Run(this, argument);
157 return;
160 SavablePtr EditorChunk::getEditing()
162 return m_chunk;
165 TableImplPtr EditorChunk::getTable()
167 return db::TableImpls::Get()->CHUNKS;
170 KeysPtr EditorChunk::addNew()
172 return mud::Managers::Get()->Chunk->Add();
175 std::vector<std::string> EditorChunk::getList()
177 return mud::Managers::Get()->Chunk->List(m_parentRoom);
180 void EditorChunk::setEditing(KeysPtr keys)
182 if(!keys->size())
184 m_chunk.reset();
185 return;
188 m_chunk = mud::Managers::Get()->Chunk->GetByKey(keys->first()->getIntegerValue());
189 return;
192 std::vector<std::string> EditorChunk::getCommands()
194 return m_commands.getCommandsVector();
197 void EditorChunk::editName(const std::string& argument)
199 if(argument.size() == 0)
201 m_sock->Send("Chunk name can't be zero length!\n");
202 return;
205 m_sock->Sendf("Chunk name changed from '%s' to '%s'.\n", m_chunk->getName().c_str(), argument.c_str());
206 m_chunk->setName(argument);
207 return;
210 void EditorChunk::editDescription(const std::string& argument)
212 if(argument.size() == 0)
214 m_sock->Send("No argument, dropping you into the string editor!\n");
215 return;
218 m_sock->Sendf("Chunk description changed from '%s' to '%s'.\n", m_chunk->getDescription().c_str(), argument.c_str());
219 m_chunk->setDescription(argument);
220 return;
223 void EditorChunk::editTags(const std::string& argument)
225 if(argument.size() == 0)
227 m_sock->Send("Chunk tags can't be zero length!\n");
228 return;
231 m_sock->Sendf("Chunk tag changed from '%s' to '%s'.\n", m_chunk->getTags().c_str(), argument.c_str());
232 m_chunk->setTags(argument);
233 return;
236 void EditorChunk::editRoom(const std::string& argument)
238 int id = atoi(argument.c_str());
239 if(id <= 0)
241 m_sock->Send("Please specify a room this Chunk belongs to.\n");
244 std::string oldname;
245 try {
246 mud::RoomPtr oldroom = mud::Managers::Get()->Room->GetByKey(m_chunk->getRoom());
247 oldname = oldroom->getName();
248 } catch(RowNotFoundException& e) {
249 oldname = String::Get()->fromInt(m_chunk->getRoom());
254 mud::RoomPtr room = mud::Managers::Get()->Room->GetByKey(id);
255 m_sock->Sendf("Room changed from '%s' to '%s'.\n", oldname.c_str(), room->getName().c_str());
256 m_chunk->setRoom(id);
258 catch(RowNotFoundException& e)
260 m_sock->Sendf("'%s' is not a valid room!\n", argument.c_str());
261 m_sock->Send(String::Get()->box(mud::Managers::Get()->Room->List(), "Rooms"));
262 return;
266 void EditorChunk::importChunk(const std::string& argument)
268 bool createdImporter = false;
270 /**
271 * There is no argument and no importer yes, create one
273 if(argument.size() == 0 && !m_importer)
275 m_sock->Send("No argument, dropping you into the String Editor.\n");
276 m_sock->Send("Paste your description there, when done the chunk will be imported.\n");
277 m_sock->SetEditor(new EditorString(m_sock, m_value));
278 m_target = M_IMPORT;
279 return;
282 if(!m_importer)
284 ChunkImporterPtr importer(new ChunkImporter(argument));
285 m_importer = importer;
286 createdImporter = true;
287 m_sock->Send("Import complete.\n");
290 if(argument.size() == 0 || createdImporter)
292 m_sock->Send("Importing would result in the following Chunk:\n");
293 m_sock->Send(m_importer->getResult());
294 m_sock->Send("Do you want to accept these changes?\n");
295 m_sock->SetEditor(new EditorBool(m_sock, m_yesno));
296 m_target = M_IMPORTACCEPT;
297 return;
300 if(!argument.compare("accept") || !argument.compare("reject"))
302 if(!argument.compare("accept"))
304 m_importer->Apply(m_chunk);
305 m_sock->Send("Would you like to apply this import to another chunk as well?\n");
307 else
309 m_sock->Send("Ok, canceled.\n");
310 m_sock->Send("Would you like to apply this import to another chunk instead?\n");
313 m_sock->Send("(If so, this import will be saved while you remain in the chunk editor.\n");
314 m_sock->Send("The next time you run import, even on another chunk, this import will be applied again.)\n");
315 m_sock->SetEditor(new EditorBool(m_sock, m_yesno));
316 m_target = M_IMPORTSAVECHUNK;
317 return;
320 if(!argument.compare("save") || !argument.compare("discard"))
322 if(!argument.compare("save"))
324 m_sock->Send("Allright, this import will not be deleted till you change editors (e.g., type 'quit').\n");
325 return;
327 else
329 m_sock->Send("Allright, import discarded.\n");
330 m_importer.reset();
331 return;
335 m_sock->Send("Unknown action.\n");
338 void EditorChunk::startDetails(const std::string& argument)
340 m_sock->Send("Dropping you into Detail Edit mode!\n");
341 m_sock->SetEditor(new EditorDetail(m_sock));
342 return;
345 void EditorChunk::showChunk(const std::string& argument)
347 m_sock->Send(m_chunk->toString());
350 void EditorChunk::saveChunk(const std::string& argument)
352 m_sock->Sendf("Saving chunk '%s'.\n", m_chunk->getName().c_str());
353 m_chunk->Save();
354 m_sock->Send("Saved.\n");
355 return;
358 void EditorChunk::clearParentRoom(const std::string& argument)
360 m_sock->Send("Ok.\n");
361 m_parentRoom.reset();
362 return;
365 void EditorChunk::setParentRoom(const std::string& argument)
367 if(argument.size())
369 m_sock->Send("Please provide a room to filter on.\n");
370 return;
373 try {
374 int id = atoi(argument.c_str());
375 mud::RoomPtr room = mud::Managers::Get()->Room->GetByKey(id);
376 m_parentRoom = room;
377 m_sock->Send("Ok.\n");
378 m_sock->Sendf("Only Chunks that belong to room '%d' are shown, to clear this restriction type 'all'.\n", m_parentRoom->getID());
379 } catch(RowNotFoundException& e) {
380 m_sock->Sendf("'%s' is not a room.\n", argument.c_str());