1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
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. *
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. *
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"
26 #include "StringUtilities.h"
27 #include "TableImpls.h"
31 #include "ChunkManager.h"
33 #include "RoomManager.h"
34 #include "ChunkImporter.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
),
64 EditorChunk::EditorChunk(UBSocket
* sock
) :
66 m_commands(commands
, array_size(commands
)),
70 listCommands(Global::Get()->EmptyString
);
73 EditorChunk::EditorChunk(UBSocket
* sock
, mud::RoomPtr parentRoom
) :
75 m_commands(commands
, array_size(commands
)),
77 m_parentRoom(parentRoom
),
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()
103 importChunk(m_yesno
? "accept" : "reject");
106 case M_IMPORTSAVECHUNK
:
108 importChunk(m_yesno
? "save" : "discard");
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
);
121 const ChunkCommand
* act
= (ChunkCommand
*)m_commands
.getObject(action
);
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
);
134 OLCEditor::dispatch(action
, argument
);
140 m_sock
->Send("You need to be editing a chunk first.\n");
141 m_sock
->Send("(Use the 'edit' command.)\n");
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");
156 act
->Run(this, argument
);
160 SavablePtr
EditorChunk::getEditing()
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
)
188 m_chunk
= mud::Managers::Get()->Chunk
->GetByKey(keys
->first()->getIntegerValue());
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");
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
);
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");
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
);
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");
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
);
236 void EditorChunk::editRoom(const std::string
& argument
)
238 int id
= atoi(argument
.c_str());
241 m_sock
->Send("Please specify a room this Chunk belongs to.\n");
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"));
266 void EditorChunk::importChunk(const std::string
& argument
)
268 bool createdImporter
= false;
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
));
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
;
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");
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
;
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");
329 m_sock
->Send("Allright, import discarded.\n");
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
));
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());
354 m_sock
->Send("Saved.\n");
358 void EditorChunk::clearParentRoom(const std::string
& argument
)
360 m_sock
->Send("Ok.\n");
361 m_parentRoom
.reset();
365 void EditorChunk::setParentRoom(const std::string
& argument
)
369 m_sock
->Send("Please provide a room to filter on.\n");
374 int id
= atoi(argument
.c_str());
375 mud::RoomPtr room
= mud::Managers::Get()->Room
->GetByKey(id
);
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());