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 "SavableHeaders.h"
22 #include "EditorDetail.h"
23 #include "EditorString.h"
24 #include "EditorBool.h"
27 #include "StringUtilities.h"
28 #include "TableImpls.h"
32 #include "DetailManager.h"
34 #include "AreaManager.h"
36 #include "RoomManager.h"
38 #include "ChunkManager.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
),
62 B("save", saveDetail
),
63 B("show", showDetail
),
66 EditorDetail::EditorDetail(UBSocket
* sock
) :
68 m_commands(commands
, array_size(commands
)),
72 listCommands(Global::Get()->EmptyString
);
75 EditorDetail::EditorDetail(UBSocket
* sock
, mud::DetailPtr detail
) :
77 m_commands(commands
, array_size(commands
)),
84 EditorDetail::EditorDetail(UBSocket
* sock
, mud::AreaPtr parentArea
) :
86 m_commands(commands
, array_size(commands
)),
88 m_parentArea(parentArea
),
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
) :
97 m_commands(commands
, array_size(commands
)),
99 m_parentRoom(parentRoom
),
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
) :
108 m_commands(commands
, array_size(commands
)),
110 m_parentChunk(parentChunk
),
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()
130 m_detail
->setDescription(m_value
);
137 std::string
EditorDetail::lookup(const std::string
& action
)
139 const DetailCommand
* act
= (DetailCommand
*)m_commands
.getObject(action
);
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
);
154 m_sock
->Send("You need to be editing a detail first.\n");
155 m_sock
->Send("(Use the 'edit' command.)\n");
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");
170 act
->Run(this, argument
);
174 SavablePtr
EditorDetail::getEditing()
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()
192 return mud::Managers::Get()->Detail
->List(m_parentArea
);
195 return mud::Managers::Get()->Detail
->List(m_parentRoom
);
198 return mud::Managers::Get()->Detail
->List(m_parentChunk
);
200 return mud::Managers::Get()->Detail
->List();
203 void EditorDetail::setEditing(KeysPtr keys
)
211 m_detail
= mud::Managers::Get()->Detail
->GetByKey(keys
->first()->getIntegerValue());
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");
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
);
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
;
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
);
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());
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());
268 m_detail
->Save(m_sock
->GetAccount()->getID(), argument
);
270 m_detail
->Save(m_sock
->GetAccount()->getID(), Global::Get()->EmptyString
);
271 m_sock
->Send("Saved.\n");
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();
284 void EditorDetail::setParent(const std::string
& argument
)
288 m_sock
->Send("Please provide an area, room, or chunk to filter on.\n");
297 bool good
= parse(argument
.c_str(),
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
)]
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");
317 Assert(area
|| chunk
|| room
);
322 mud::AreaPtr area
= mud::Managers::Get()->Area
->GetByKey(id
);
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());
334 mud::RoomPtr room
= mud::Managers::Get()->Room
->GetByKey(id
);
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());
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
);