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"
23 #include "EditorDetail.h"
24 #include "EditorString.h"
25 #include "EditorBool.h"
29 #include "StringUtilities.h"
30 #include "TableImpls.h"
35 #include "DetailManager.h"
37 #include "AreaManager.h"
39 #include "RoomManager.h"
41 #include "ChunkManager.h"
43 #include <boost/spirit/core.hpp>
45 typedef EditorDetail E
;
46 typedef CommandObject
<E
> O
;
47 typedef CommandBinding
<E
> B
;
49 using namespace boost::spirit
;
51 static O
editKey("Key", &E::editKey
);
52 static O
editDescription("Description", &E::editDescription
);
53 static O
showDetail("Show", &E::showDetail
);
54 static O
saveDetail("Save", &E::saveDetail
);
55 static O
quitEditor("Quit", &E::quitEditor
);
56 static O
clearParents("ClearParents", &E::clearParents
);
57 static O
setParent("SetParent", &E::setParent
);
59 static const B commands
[] = {
60 B("all", clearParents
),
61 B("description", editDescription
),
62 B("filter", setParent
),
64 B("quit", quitEditor
),
65 B("save", saveDetail
),
66 B("show", showDetail
),
69 EditorDetail::EditorDetail(UBSocket
* sock
) :
71 m_commands(commands
, array_size(commands
)),
75 listCommands(Global::Get()->EmptyString
);
78 EditorDetail::EditorDetail(UBSocket
* sock
, mud::DetailPtr detail
) :
80 m_commands(commands
, array_size(commands
)),
87 EditorDetail::EditorDetail(UBSocket
* sock
, mud::AreaPtr parentArea
) :
89 m_commands(commands
, array_size(commands
)),
91 m_parentArea(parentArea
),
94 listCommands(Global::Get()->EmptyString
);
95 m_sock
->Sendf("Only Details that belong to area '%d' are shown, to clear this restriction type 'all'.\n", parentArea
->getID());
98 EditorDetail::EditorDetail(UBSocket
* sock
, mud::RoomPtr parentRoom
) :
100 m_commands(commands
, array_size(commands
)),
102 m_parentRoom(parentRoom
),
105 listCommands(Global::Get()->EmptyString
);
106 m_sock
->Sendf("Only Details that belong to room '%d' are shown, to clear this restriction type 'all'.\n", parentRoom
->getID());
109 EditorDetail::EditorDetail(UBSocket
* sock
, mud::ChunkPtr parentChunk
) :
111 m_commands(commands
, array_size(commands
)),
113 m_parentChunk(parentChunk
),
116 listCommands(Global::Get()->EmptyString
);
117 m_sock
->Sendf("Only Details that belong to chunk '%d' are shown, to clear this restriction type 'all'.\n", parentChunk
->getID());
120 EditorDetail::~EditorDetail(void)
125 void EditorDetail::OnFocus()
133 m_detail
->setDescription(m_value
);
140 std::string
EditorDetail::lookup(const std::string
& action
)
142 const DetailCommand
* act
= m_commands
.getObject(action
);
144 return act
->getName();
146 return Global::Get()->EmptyString
;
149 void EditorDetail::dispatch(const std::string
& action
, const std::string
& argument
)
151 const DetailCommand
* act
= m_commands
.getObject(action
);
154 act
->Run(this, argument
);
157 SavablePtr
EditorDetail::getEditing()
162 TableImplPtr
EditorDetail::getTable()
164 return db::TableImpls::Get()->DETAILS
;
167 KeysPtr
EditorDetail::addNew()
169 return mud::DetailManager::Get()->Add();
172 std::vector
<std::string
> EditorDetail::getList()
175 return mud::DetailManager::Get()->List(m_parentArea
);
178 return mud::DetailManager::Get()->List(m_parentRoom
);
181 return mud::DetailManager::Get()->List(m_parentChunk
);
183 return mud::DetailManager::Get()->List();
186 void EditorDetail::setEditing(KeysPtr keys
)
194 m_detail
= mud::DetailManager::Get()->GetByKey(keys
->first()->getIntegerValue());
198 std::vector
<std::string
> EditorDetail::getCommands()
200 return m_commands
.getCommandsVector();
203 void EditorDetail::editKey(const std::string
& argument
)
205 if(argument
.size() == 0)
207 m_sock
->Send("Detail key can't be zero length!\n");
211 m_sock
->Sendf("Detail key changed from '%s' to '%s'.\n", m_detail
->getKey().c_str(), argument
.c_str());
212 m_detail
->setKey(argument
);
216 void EditorDetail::editDescription(const std::string
& argument
)
218 if(argument
.size() == 0)
220 m_sock
->Send("No argument, dropping you into the string editor!\n");
221 m_sock
->SetEditor(new EditorString(m_sock
, m_value
));
222 m_target
= M_DESCRIPTION
;
226 m_sock
->Sendf("Detail description changed from '%s' to '%s'.\n", m_detail
->getDescription().c_str(), argument
.c_str());
227 m_detail
->setDescription(argument
);
231 void EditorDetail::showDetail(const std::string
& argument
)
233 std::vector
<std::string
> result
;
234 std::string key
= "Key: '";
235 key
.append(m_detail
->getKey());
237 result
.push_back(key
);
239 std::string description
= "Description: '";
240 description
.append(m_detail
->getDescription());
241 description
.append("'.");
242 result
.push_back(description
);
244 m_sock
->Send(String::Get()->box(result
, "Detail"));
247 void EditorDetail::saveDetail(const std::string
& argument
)
249 m_sock
->Sendf("Saving detail '%s'.\n", m_detail
->getKey().c_str());
251 m_detail
->Save(m_sock
->GetAccount()->getID(), argument
);
253 m_detail
->Save(m_sock
->GetAccount()->getID(), Global::Get()->EmptyString
);
254 m_sock
->Send("Saved.\n");
258 void EditorDetail::clearParents(const std::string
& argument
)
260 m_sock
->Send("Ok.\n");
261 m_parentArea
.reset();
262 m_parentRoom
.reset();
263 m_parentChunk
.reset();
267 void EditorDetail::setParent(const std::string
& argument
)
271 m_sock
->Send("Please provide an area, room, or chunk to filter on.\n");
280 bool good
= parse(argument
.c_str(),
283 (str_p("area")[assign_a(area
, true)] |
284 str_p("chunk")[assign_a(chunk
, true)] |
285 str_p("room")[assign_a(room
, true)])
287 >> int_p
[assign_a(id
)]
295 m_sock
->Send("Please specify what area, room, or chunk to filter on in the following format:\n");
296 m_sock
->Send("area/room/chunk id\n");
300 Assert(area
|| chunk
|| room
);
305 mud::AreaPtr area
= mud::AreaManager::Get()->GetByKey(id
);
307 m_sock
->Send("Ok.\n");
308 m_sock
->Sendf("Only Details that belong to area '%d' are shown, to clear this restriction type 'all'.\n", m_parentArea
->getID());
309 } catch(RowNotFoundException
& e
) {
310 m_sock
->Sendf("'%s' is not an area.\n", argument
.c_str());
317 mud::RoomPtr room
= mud::RoomManager::Get()->GetByKey(id
);
319 m_sock
->Send("Ok.\n");
320 m_sock
->Sendf("Only Details that belong to room '%d' are shown, to clear this restriction type 'all'.\n", m_parentRoom
->getID());
321 } catch(RowNotFoundException
& e
) {
322 m_sock
->Sendf("'%s' is not a room.\n", argument
.c_str());
329 mud::ChunkPtr chunk
= mud::ChunkManager::Get()->GetByKey(id
);
330 m_parentChunk
= chunk
;
331 m_sock
->Send("Ok.\n");
332 m_sock
->Sendf("Only Details that belong to chunk '%d' are shown, to clear this restriction type 'all'.\n", m_parentChunk
->getID());
333 } catch(RowNotFoundException
& e
) {
334 m_sock
->Sendf("'%d' is not a chunk.\n", id
);