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 "EditorMobile.h"
22 #include "EditorOLC.h"
24 #include "StringUtilities.h"
25 #include "TableImpls.h"
28 #include "MCharacter.h"
29 #include "MCharacterManager.h"
30 #include "CharacterManager.h"
33 typedef EditorMobile E
;
34 typedef CommandInfoObject
<E
> O
;
35 typedef CommandBinding
<E
> B
;
37 // name function need: object lock
38 static O
editName( "Name", &E::editName
, true, true);
39 static O
editDescription("Description",&E::editDescription
,true,true);
40 static O
showMobile("Show", &E::showMobile
, true, false);
41 static O
saveMobile("Save", &E::saveMobile
, true, true);
43 static const B commands
[] = {
44 B("description", editDescription
),
46 B("save", saveMobile
),
47 B("show", showMobile
),
50 EditorMobile::EditorMobile(UBSocket
* sock
) :
52 m_commands(commands
, array_size(commands
)),
55 listCommands(Global::Get()->EmptyString
);
58 EditorMobile::~EditorMobile(void)
63 std::string
EditorMobile::lookup(const std::string
& action
)
65 std::string name
= OLCEditor::lookup(action
);
69 const MobileCommand
* act
= (MobileCommand
*)m_commands
.getObject(action
);
71 return act
->getName();
73 return Global::Get()->EmptyString
;
76 void EditorMobile::dispatch(const std::string
& action
, const std::string
& argument
)
78 const MobileCommand
* act
= (MobileCommand
*)m_commands
.getObject(action
);
82 OLCEditor::dispatch(action
, argument
);
88 m_sock
->Send("You need to be editing a mobile first.\n");
89 m_sock
->Send("(Use the 'edit' command.)\n");
97 } catch(SavableLocked
& e
) {
98 m_sock
->Send("The mobile you are currently editing is locked (being edited by someone else), so you cannot edit it right now.\n");
99 m_sock
->Send("Please try again later.\n");
104 act
->Run(this, argument
);
108 SavablePtr
EditorMobile::getEditing()
113 TableImplPtr
EditorMobile::getTable()
115 return db::TableImpls::Get()->ENTITIES
;
118 KeysPtr
EditorMobile::addNew()
120 return mud::Managers::Get()->Character
->Add();
123 std::vector
<std::string
> EditorMobile::getList()
125 return mud::Managers::Get()->Character
->List();
128 void EditorMobile::setEditing(KeysPtr keys
)
136 m_mobile
= mud::Managers::Get()->MCharacter
->GetByKey(keys
->first()->getIntegerValue());
140 std::vector
<std::string
> EditorMobile::getCommands()
142 return m_commands
.getCommandsVector();
145 void EditorMobile::editName(const std::string
& argument
)
147 if(argument
.size() == 0)
149 m_sock
->Send("Mobile name can't be zero length!\n");
153 m_sock
->Sendf("Mobile name changed from '%s' to '%s'.\n", m_mobile
->getName().c_str(), argument
.c_str());
154 m_mobile
->setName(argument
);
158 void EditorMobile::editDescription(const std::string
& argument
)
160 if(!m_mobile
->Exists())
162 m_sock
->Send("For some reason the mobile you are editing does not exist.\n");
166 if(argument
.size() == 0)
168 m_sock
->Send("No argument, dropping you into the string editor!\n");
172 m_sock
->Sendf("Mobile description changed from '%s' to '%s'.\n", m_mobile
->getDescription().c_str(), argument
.c_str());
173 m_mobile
->setDescription(argument
);
177 void EditorMobile::showMobile(const std::string
& argument
)
179 m_sock
->Send(m_mobile
->toString());
183 void EditorMobile::saveMobile(const std::string
& argument
)
185 m_sock
->Sendf("Saving mobile '%s'.\n", m_mobile
->getName().c_str());
187 m_sock
->Send("Saved.\n");