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 "EditorCluster.h"
22 #include "EditorRoom.h"
24 #include "StringUtilities.h"
25 #include "TableImpls.h"
29 #include "ClusterManager.h"
31 #include "AreaManager.h"
34 typedef EditorCluster E
;
35 typedef CommandInfoObject
<E
> O
;
36 typedef CommandBinding
<E
> B
;
38 // name function need: object lock
39 static O
editName( "Name", &E::editName
, true, true);
40 static O
editDescription("Description", &E::editDescription
, true, true);
41 static O
editArea( "Area", &E::editArea
, true, true);
42 static O
showCluster( "Show", &E::showCluster
, true, false);
43 static O
saveCluster( "Save", &E::saveCluster
, true, true);
44 static O
startRooms("Rooms",&E::startRooms
);
45 static O
clearParentArea("ClearParentArea", &E::clearParentArea
);
46 static O
setParentArea("SetParentArea", &E::setParentArea
);
48 static const B commands
[] = {
49 B("all", clearParentArea
),
51 B("description", editDescription
),
52 B("filter", setParentArea
),
54 B("rooms", startRooms
),
55 B("save", saveCluster
),
56 B("show", showCluster
),
59 EditorCluster::EditorCluster(UBSocket
* sock
) :
61 m_commands(commands
, array_size(commands
)),
65 listCommands(Global::Get()->EmptyString
);
68 EditorCluster::EditorCluster(UBSocket
* sock
, mud::AreaPtr parentArea
) :
70 m_commands(commands
, array_size(commands
)),
72 m_parentArea(parentArea
),
75 listCommands(Global::Get()->EmptyString
);
76 m_sock
->Sendf("Only Clusters that belong to area '%d' are shown, to clear this restriction type 'all'.\n", parentArea
->getID());
79 EditorCluster::~EditorCluster(void)
84 void EditorCluster::OnFocus()
92 editDescription(m_value
);
99 std::string
EditorCluster::lookup(const std::string
& action
)
101 std::string name
= OLCEditor::lookup(action
);
105 const ClusterCommand
* act
= (ClusterCommand
*)m_commands
.getObject(action
);
107 return act
->getName();
109 return Global::Get()->EmptyString
;
112 void EditorCluster::dispatch(const std::string
& action
, const std::string
& argument
)
114 const ClusterCommand
* act
= (ClusterCommand
*)m_commands
.getObject(action
);
118 OLCEditor::dispatch(action
, argument
);
124 m_sock
->Send("You need to be editing a cluster first.\n");
125 m_sock
->Send("(Use the 'edit' command.)\n");
133 } catch(SavableLocked
& e
) {
134 m_sock
->Send("The cluster you are currently editing is locked (being edited by someone else), so you cannot edit it right now.\n");
135 m_sock
->Send("Please try again later.\n");
140 act
->Run(this, argument
);
144 SavablePtr
EditorCluster::getEditing()
149 TableImplPtr
EditorCluster::getTable()
151 return db::TableImpls::Get()->CHUNKS
;
154 KeysPtr
EditorCluster::addNew()
156 return mud::Managers::Get()->Cluster
->Add();
159 std::vector
<std::string
> EditorCluster::getList()
161 return mud::Managers::Get()->Cluster
->List(m_parentArea
);
164 void EditorCluster::setEditing(KeysPtr keys
)
172 m_cluster
= mud::Managers::Get()->Cluster
->GetByKey(keys
->first()->getIntegerValue());
176 std::vector
<std::string
> EditorCluster::getCommands()
178 return m_commands
.getCommandsVector();
181 void EditorCluster::editName(const std::string
& argument
)
183 if(argument
.size() == 0)
185 m_sock
->Send("Cluster name can't be zero length!\n");
189 m_sock
->Sendf("Cluster name changed from '%s' to '%s'.\n", m_cluster
->getName().c_str(), argument
.c_str());
190 m_cluster
->setName(argument
);
194 void EditorCluster::editDescription(const std::string
& argument
)
196 if(argument
.size() == 0)
198 m_sock
->Send("No argument, dropping you into the string editor!\n");
202 m_sock
->Sendf("Cluster description changed from '%s' to '%s'.\n", m_cluster
->getDescription().c_str(), argument
.c_str());
203 m_cluster
->setDescription(argument
);
207 void EditorCluster::editArea(const std::string
& argument
)
209 int id
= atoi(argument
.c_str());
212 m_sock
->Send("Please specify a area this Cluster belongs to.\n");
217 mud::AreaPtr oldarea
= mud::Managers::Get()->Area
->GetByKey(m_cluster
->getArea());
218 oldname
= oldarea
->getName();
219 } catch(RowNotFoundException
& e
) {
220 oldname
= String::Get()->fromInt(m_cluster
->getArea());
225 mud::AreaPtr area
= mud::Managers::Get()->Area
->GetByKey(id
);
226 m_sock
->Sendf("Area changed from '%s' to '%s'.\n", oldname
.c_str(), area
->getName().c_str());
227 m_cluster
->setArea(id
);
229 catch(RowNotFoundException
& e
)
231 m_sock
->Sendf("'%s' is not a valid area!\n", argument
.c_str());
232 m_sock
->Send(String::Get()->box(mud::Managers::Get()->Area
->List(), "Areas"));
237 void EditorCluster::startRooms(const std::string
& argument
)
239 if(!argument
.compare("."))
243 m_sock
->Send("You can only use '.' when you are already editing a cluster.\n");
244 m_sock
->Send("You may specifiy a cluster to filter the Room Editor on by it's id.\n");
248 m_sock
->Send("Dropping you into filtered Room Edit mode!\n");
249 m_sock
->SetEditor(new EditorRoom(m_sock
, m_cluster
));
255 value_type id
= atoi(argument
.c_str());
258 mud::ClusterPtr cluster
= mud::Managers::Get()->Cluster
->GetByKey(id
);
259 m_sock
->Send("Dropping you into filtered Room Edit mode!\n");
260 m_sock
->SetEditor(new EditorRoom(m_sock
, cluster
));
261 } catch(RowNotFoundException
& e
) {
262 m_sock
->Sendf("Unknown cluster '%s'.\n", argument
.c_str());
268 m_sock
->Send("Dropping you into Room Edit mode!\n");
269 m_sock
->SetEditor(new EditorRoom(m_sock
));
273 void EditorCluster::showCluster(const std::string
& argument
)
275 m_sock
->Send(m_cluster
->toString());
278 void EditorCluster::saveCluster(const std::string
& argument
)
280 m_sock
->Sendf("Saving cluster '%s'.\n", m_cluster
->getName().c_str());
282 m_sock
->Send("Saved.\n");
286 void EditorCluster::clearParentArea(const std::string
& argument
)
288 m_sock
->Send("Ok.\n");
289 m_parentArea
.reset();
293 void EditorCluster::setParentArea(const std::string
& argument
)
297 m_sock
->Send("Please provide a area to filter on.\n");
302 int id
= atoi(argument
.c_str());
303 mud::AreaPtr area
= mud::Managers::Get()->Area
->GetByKey(id
);
305 m_sock
->Send("Ok.\n");
306 m_sock
->Sendf("Only Clusters that belong to area '%d' are shown, to clear this restriction type 'all'.\n", m_parentArea
->getID());
307 } catch(RowNotFoundException
& e
) {
308 m_sock
->Sendf("'%s' is not a area.\n", argument
.c_str());