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 "EditorSector.h"
22 #include "EditorOLC.h"
27 #include "StringUtilities.h"
28 #include "TableImpls.h"
32 #include "SectorManager.h"
34 typedef EditorSector E
;
35 typedef CommandObject
<E
> O
;
36 typedef CommandBinding
<E
> B
;
38 static O
editName("Name", &E::editName
);
39 static O
editSymbol("Description", &E::editSymbol
);
40 static O
editMoveCost("Height", &E::editMoveCost
);
41 static O
editWater("Width", &E::editWater
);
42 static O
showSector("Show", &E::showSector
);
43 static O
saveSector("Save", &E::saveSector
);
45 static const B commands
[] = {
46 B("movecost", editMoveCost
),
48 B("save", saveSector
),
49 B("show", showSector
),
50 B("symbol", editSymbol
),
51 B("water", editWater
),
54 EditorSector::EditorSector(UBSocket
* sock
) :
56 m_commands(commands
, array_size(commands
)),
59 listCommands(Global::Get()->EmptyString
);
62 EditorSector::~EditorSector(void)
67 std::string
EditorSector::lookup(const std::string
& action
)
69 std::string name
= OLCEditor::lookup(action
);
73 const SectorCommand
* act
= m_commands
.getObject(action
);
75 return act
->getName();
77 return Global::Get()->EmptyString
;
80 void EditorSector::dispatch(const std::string
& action
, const std::string
& argument
)
82 const SectorCommand
* act
= m_commands
.getObject(action
);
86 OLCEditor::dispatch(action
, argument
);
92 m_sock
->Send("You need to be editing a sector first.\n");
93 m_sock
->Send("(Use the 'edit' command.)\n");
98 act
->Run(this, argument
);
99 } catch(SavableLocked
& e
) {
100 m_sock
->Send("The sector you are currently editing is locked (being edited by someone else), so you cannot edit it right now.\n");
101 m_sock
->Send("Please try again later.\n");
107 SavablePtr
EditorSector::getEditing()
112 TableImplPtr
EditorSector::getTable()
114 return db::TableImpls::Get()->SECTORS
;
117 KeysPtr
EditorSector::addNew()
119 return mud::SectorManager::Get()->Add();
122 std::vector
<std::string
> EditorSector::getList()
124 return mud::SectorManager::Get()->List();
127 void EditorSector::setEditing(KeysPtr keys
)
135 m_sector
= mud::SectorManager::Get()->GetByKey(keys
->first()->getIntegerValue());
139 std::vector
<std::string
> EditorSector::getCommands()
141 return m_commands
.getCommandsVector();
144 void EditorSector::editName(const std::string
& argument
)
146 if(argument
.size() == 0)
148 m_sock
->Send("Sector name can't be zero length!\n");
152 m_sock
->Sendf("Sector name changed from '%s' to '%s'.\n", m_sector
->getName().c_str(), argument
.c_str());
153 m_sector
->setName(argument
);
157 void EditorSector::editMoveCost(const std::string
& argument
)
159 long movecost
= atoi(argument
.c_str());
163 m_sock
->Sendf("%s is not a valid movecost!", argument
.c_str());
167 m_sock
->Sendf("Sector movecost changed from '%d' to '%d'.\n", m_sector
->getMoveCost(), movecost
);
168 m_sector
->setMoveCost(movecost
);
172 void EditorSector::editWater(const std::string
& argument
)
174 m_sock
->Sendf("Sector water flag toggled.\n");
175 if(m_sector
->isWater())
176 m_sector
->setWater(false);
178 m_sector
->setWater(true);
183 void EditorSector::editSymbol(const std::string
& argument
)
185 if(!m_sector
->Exists())
187 m_sock
->Send("For some reason the sector you are editing does not exist.\n");
191 if(argument
.size() != 1)
193 m_sock
->Send("Please provide one character to set as symbol!");
197 m_sock
->Sendf("Sector symbol changed from '%s' to '%s'.\n", m_sector
->getSymbol().c_str(), argument
.c_str());
198 m_sector
->setSymbol(argument
);
202 void EditorSector::showSector(const std::string
& argument
)
204 if(!argument
.compare("compact"))
206 m_sock
->Sendf("%s(%s): %d%s",
207 m_sector
->getName().c_str(),
208 m_sector
->getSymbol().c_str(),
209 m_sector
->getMoveCost(),
210 (m_sector
->isWater() ? " (water)\n" : "\n"));
214 m_sock
->Sendf("Name: '%s'.\n", m_sector
->getName().c_str());
215 m_sock
->Sendf("Symbol: '%s'.\n", m_sector
->getSymbol().c_str());
216 m_sock
->Sendf("Movecost: '%d'.\n", m_sector
->getMoveCost());
217 if(m_sector
->isWater())
218 m_sock
->Send("Sector is water type.\n");
223 void EditorSector::saveSector(const std::string
& argument
)
225 m_sock
->Sendf("Saving sector '%s'.\n", m_sector
->getName().c_str());
227 m_sock
->Send("Saved.\n");