Added count functionality in SavableManager.
[UnsignedByte.git] / src / Core / EditorSector.cpp
blobadf9682a115165f4ed5ff79da68418cd5f0f8da1
1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
4 * *
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. *
9 * *
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. *
14 * *
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"
24 #include "UBSocket.h"
26 #include "Array.h"
27 #include "StringUtilities.h"
28 #include "TableImpls.h"
30 #include "Account.h"
31 #include "Sector.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),
47 B("name", editName),
48 B("save", saveSector),
49 B("show", showSector),
50 B("symbol", editSymbol),
51 B("water", editWater),
54 EditorSector::EditorSector(UBSocket* sock) :
55 OLCEditor(sock),
56 m_commands(commands, array_size(commands)),
57 m_sector()
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);
70 if(name.size() != 0)
71 return name;
73 const SectorCommand* act = m_commands.getObject(action);
74 if(act)
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);
84 if(!act)
86 OLCEditor::dispatch(action, argument);
87 return;
90 if(!m_sector)
92 m_sock->Send("You need to be editing a sector first.\n");
93 m_sock->Send("(Use the 'edit' command.)\n");
94 return;
97 try {
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");
104 return;
107 SavablePtr EditorSector::getEditing()
109 return m_sector;
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)
129 if(!keys->size())
131 m_sector.reset();
132 return;
135 m_sector = mud::SectorManager::Get()->GetByKey(keys->first()->getIntegerValue());
136 return;
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");
149 return;
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);
154 return;
157 void EditorSector::editMoveCost(const std::string& argument)
159 long movecost = atoi(argument.c_str());
161 if(movecost < 0)
163 m_sock->Sendf("%s is not a valid movecost!", argument.c_str());
164 return;
167 m_sock->Sendf("Sector movecost changed from '%d' to '%d'.\n", m_sector->getMoveCost(), movecost);
168 m_sector->setMoveCost(movecost);
169 return;
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);
177 else
178 m_sector->setWater(true);
179 return;
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");
188 return;
191 if(argument.size() != 1)
193 m_sock->Send("Please provide one character to set as symbol!");
194 return;
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);
199 return;
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"));
212 else
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");
220 return;
223 void EditorSector::saveSector(const std::string& argument)
225 m_sock->Sendf("Saving sector '%s'.\n", m_sector->getName().c_str());
226 m_sector->Save();
227 m_sock->Send("Saved.\n");
228 return;