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 std::string name
= OLCEditor::lookup(action
);
143 const DetailCommand
* act
= (DetailCommand
*)m_commands
.getObject(action
);
145 return act
->getName();
147 return Global::Get()->EmptyString
;
150 void EditorDetail::dispatch(const std::string
& action
, const std::string
& argument
)
152 const DetailCommand
* act
= (DetailCommand
*)m_commands
.getObject(action
);
156 OLCEditor::dispatch(action
, argument
);
160 if(!m_detail
&& act
->needObject())
162 m_sock
->Send("You need to be editing a detail first.\n");
163 m_sock
->Send("(Use the 'edit' command.)\n");
171 } catch(SavableLocked
& e
) {
172 m_sock
->Send("The detail you are currently editing is locked (being edited by someone else), so you cannot edit it right now.\n");
173 m_sock
->Send("Please try again later.\n");
178 act
->Run(this, argument
);
182 SavablePtr
EditorDetail::getEditing()
187 TableImplPtr
EditorDetail::getTable()
189 return db::TableImpls::Get()->DETAILS
;
192 KeysPtr
EditorDetail::addNew()
194 return mud::Managers::Get()->Detail
->Add();
197 std::vector
<std::string
> EditorDetail::getList()
200 return mud::Managers::Get()->Detail
->List(m_parentArea
);
203 return mud::Managers::Get()->Detail
->List(m_parentRoom
);
206 return mud::Managers::Get()->Detail
->List(m_parentChunk
);
208 return mud::Managers::Get()->Detail
->List();
211 void EditorDetail::setEditing(KeysPtr keys
)
219 m_detail
= mud::Managers::Get()->Detail
->GetByKey(keys
->first()->getIntegerValue());
223 std::vector
<std::string
> EditorDetail::getCommands()
225 return m_commands
.getCommandsVector();
228 void EditorDetail::editKey(const std::string
& argument
)
230 if(argument
.size() == 0)
232 m_sock
->Send("Detail key can't be zero length!\n");
236 m_sock
->Sendf("Detail key changed from '%s' to '%s'.\n", m_detail
->getKey().c_str(), argument
.c_str());
237 m_detail
->setKey(argument
);
241 void EditorDetail::editDescription(const std::string
& argument
)
243 if(argument
.size() == 0)
245 m_sock
->Send("No argument, dropping you into the string editor!\n");
246 m_sock
->SetEditor(new EditorString(m_sock
, m_value
));
247 m_target
= M_DESCRIPTION
;
251 m_sock
->Sendf("Detail description changed from '%s' to '%s'.\n", m_detail
->getDescription().c_str(), argument
.c_str());
252 m_detail
->setDescription(argument
);
256 void EditorDetail::showDetail(const std::string
& argument
)
258 std::vector
<std::string
> result
;
259 std::string key
= "Key: '";
260 key
.append(m_detail
->getKey());
262 result
.push_back(key
);
264 std::string description
= "Description: '";
265 description
.append(m_detail
->getDescription());
266 description
.append("'.");
267 result
.push_back(description
);
269 m_sock
->Send(String::Get()->box(result
, "Detail"));
272 void EditorDetail::saveDetail(const std::string
& argument
)
274 m_sock
->Sendf("Saving detail '%s'.\n", m_detail
->getKey().c_str());
276 m_detail
->Save(m_sock
->GetAccount()->getID(), argument
);
278 m_detail
->Save(m_sock
->GetAccount()->getID(), Global::Get()->EmptyString
);
279 m_sock
->Send("Saved.\n");
283 void EditorDetail::clearParents(const std::string
& argument
)
285 m_sock
->Send("Ok.\n");
286 m_parentArea
.reset();
287 m_parentRoom
.reset();
288 m_parentChunk
.reset();
292 void EditorDetail::setParent(const std::string
& argument
)
296 m_sock
->Send("Please provide an area, room, or chunk to filter on.\n");
305 bool good
= parse(argument
.c_str(),
308 (str_p("area")[assign_a(area
, true)] |
309 str_p("chunk")[assign_a(chunk
, true)] |
310 str_p("room")[assign_a(room
, true)])
312 >> int_p
[assign_a(id
)]
320 m_sock
->Send("Please specify what area, room, or chunk to filter on in the following format:\n");
321 m_sock
->Send("area/room/chunk id\n");
325 Assert(area
|| chunk
|| room
);
327 m_parentArea
.reset();
328 m_parentRoom
.reset();
329 m_parentChunk
.reset();
335 mud::AreaPtr area
= mud::Managers::Get()->Area
->GetByKey(id
);
337 m_sock
->Send("Ok.\n");
338 m_sock
->Sendf("Only Details that belong to area '%d' are shown, to clear this restriction type 'all'.\n", m_parentArea
->getID());
339 } catch(RowNotFoundException
& e
) {
340 m_sock
->Sendf("'%s' is not an area.\n", argument
.c_str());
347 mud::RoomPtr room
= mud::Managers::Get()->Room
->GetByKey(id
);
349 m_sock
->Send("Ok.\n");
350 m_sock
->Sendf("Only Details that belong to room '%d' are shown, to clear this restriction type 'all'.\n", m_parentRoom
->getID());
351 } catch(RowNotFoundException
& e
) {
352 m_sock
->Sendf("'%s' is not a room.\n", argument
.c_str());
359 mud::ChunkPtr chunk
= mud::Managers::Get()->Chunk
->GetByKey(id
);
360 m_parentChunk
= chunk
;
361 m_sock
->Send("Ok.\n");
362 m_sock
->Sendf("Only Details that belong to chunk '%d' are shown, to clear this restriction type 'all'.\n", m_parentChunk
->getID());
363 } catch(RowNotFoundException
& e
) {
364 m_sock
->Sendf("'%d' is not a chunk.\n", id
);