Used a more uniform of logging and added a commandline parser.
[UnsignedByte.git] / src / DB / Savables / Room.cpp
blobdcb8f92713b3849acdb29731bb96b34e61d25185
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 #ifdef _WIN32
22 #include <winsock2.h>
23 #endif
25 #include <stdarg.h>
27 #include "Room.h"
28 #include "Area.h"
29 #include "Sector.h"
30 #include "SectorManager.h"
31 #include "Character.h"
32 #include "CharacterManager.h"
33 #include "Trace.h"
34 #include "TraceManager.h"
35 #include "TableImpls.h"
36 #include "Managers.h"
37 #include "StringUtilities.h"
38 #include "Global.h"
39 #include "FieldImpls.h"
40 #include "Detail.h"
41 #include "DetailManager.h"
42 #include "RoomManager.h"
44 using mud::Room;
46 Room::Room(SavableManagerPtr room) :
47 m_room(room)
49 Assert(room);
52 Room::~Room(void)
54 Unlock();
57 value_type Room::getID() const
59 return m_room->getValue(db::TableImpls::Get()->ROOMS->ROOMID)->getIntegerValue();
62 const std::string& Room::getName() const
64 return m_room->getValue(db::TableImpls::Get()->ROOMS->NAME)->getStringValue();
67 const std::string& Room::getDescription() const
69 return m_room->getValue(db::TableImpls::Get()->ROOMS->DESCRIPTION)->getStringValue();
72 value_type Room::getSector() const
74 return m_room->getValue(db::TableImpls::Get()->ROOMS->FKSECTORS)->getIntegerValue();
77 value_type Room::getCluster() const
79 return m_room->getValue(db::TableImpls::Get()->ROOMS->FKCLUSTERS)->getIntegerValue();
82 value_type Room::getHeight() const
84 return m_room->getValue(db::TableImpls::Get()->ROOMS->HEIGHT)->getIntegerValue();
87 value_type Room::getWidth() const
89 return m_room->getValue(db::TableImpls::Get()->ROOMS->WIDTH)->getIntegerValue();
92 value_type Room::getLength() const
94 return m_room->getValue(db::TableImpls::Get()->ROOMS->LENGTH)->getIntegerValue();
98 void Room::setName(const std::string name)
100 Lock();
101 ValuePtr value(new FieldValue(db::TableImpls::Get()->ROOMS->NAME, name));
102 m_room->setValue(value);
105 void Room::setDescription(const std::string description)
107 Lock();
108 ValuePtr value(new FieldValue(db::TableImpls::Get()->ROOMS->DESCRIPTION, description));
109 m_room->setValue(value);
112 void Room::setSector(value_type sector)
114 Lock();
115 ValuePtr value(new FieldValue(db::TableImpls::Get()->ROOMS->FKSECTORS, sector));
116 m_room->setValue(value);
119 void Room::setCluster(value_type cluster)
121 Lock();
122 ValuePtr value(new FieldValue(db::TableImpls::Get()->ROOMS->FKCLUSTERS, cluster));
123 m_room->setValue(value);
126 void Room::setHeight(value_type height)
128 Lock();
129 ValuePtr value(new FieldValue(db::TableImpls::Get()->ROOMS->HEIGHT, height));
130 m_room->setValue(value);
133 void Room::setWidth(value_type width)
135 Lock();
136 ValuePtr value(new FieldValue(db::TableImpls::Get()->ROOMS->WIDTH, width));
137 m_room->setValue(value);
140 void Room::setLength(value_type length)
142 Lock();
143 ValuePtr value(new FieldValue(db::TableImpls::Get()->ROOMS->LENGTH, length));
144 m_room->setValue(value);
147 void Room::Delete()
149 Lock();
150 m_room->erase();
153 void Room::Save()
155 Lock();
156 m_room->save();
159 void mud::Room::Delete(value_type accountid, const std::string& description)
161 // TODO ?
162 Assert(!"Not yet implemented");
165 void mud::Room::Save(value_type accountid, const std::string& description)
167 if(!m_room->isDirty())
168 return;
170 Lock();
172 KeysPtr keys = mud::Managers::Get()->Trace->Add();
173 mud::TracePtr trace = mud::Managers::Get()->Trace->GetByKey(keys->first()->getIntegerValue());
175 if(accountid)
177 trace->setAccount(accountid);
179 if(description != Global::Get()->EmptyString)
180 trace->setDescription(description);
183 trace->setDiff(m_room->getDiff());
184 trace->setTime(time(NULL));
185 trace->Save(); // create the Trace
187 RelationPtr relation(new Relation(db::TableImpls::Get()->TRACEROOM));
188 relation->addKey(db::TableImpls::Get()->TRACEROOM->FKROOMS, getID());
189 relation->addKey(db::TableImpls::Get()->TRACEROOM->FKTRACES, keys->first()->getIntegerValue());
190 relation->save(); // create the relation
192 m_room->save(); // save the room
195 void Room::Discard()
197 Lock();
198 m_room->discard();
201 bool Room::Exists()
203 Lock();
204 return m_room->exists();
207 #if 0
208 std::string Room::CreateMap(value_type id, long origx, long origy)
210 std::string result;
212 long count = DatabaseMgr::Get()->CountSavable(Tables::Get()->AREAS, id);
214 if(count <= 0)
216 Global::Get()->bug("EditorMap::Map::Run() with areacount <= 0!\n");
217 return std::string("For some reason the area the room you are in does not belong to an area?!\n");
220 if(count >= 2)
222 Global::Get()->bugf("EditorMap::Map::Run() with areacount '%d'!\n", count);
223 return std::string(Global::Get()->sprintf("For some reason the area the room you are in exists multiple times in the database?!n"));
226 Area* p = Cache::Get()->GetArea(id);
228 for(value_type y = 1; y <= p->getHeight(); y++)
230 std::string toprow;
231 std::string thisrow;
232 std::string bottomrow;
234 for(value_type x = 1; x <= p->getWidth(); x++)
236 long rid = Cache::Get()->GetRoomID(id, x, y);
237 long count = DatabaseMgr::Get()->CountSavable(Tables::Get()->ROOMS, rid);
238 if(!count)
240 if(x == origx && y == origy)
242 toprow.append (" ");
243 thisrow.append (" o "); // Empty, current
244 bottomrow.append(" ");
246 else
248 toprow.append (" ");
249 thisrow.append (" . "); // Empty, not current
250 bottomrow.append(" ");
254 else
256 Room* room = Cache::Get()->GetRoom(rid);
258 if(!room->isClosed(Exit::NORTHWEST))
259 toprow.append("\\");
260 else
261 toprow.append(" ");
263 if(!room->isClosed(Exit::NORTH))
264 toprow.append("|");
265 else
266 toprow.append(" ");
268 if(!room->isClosed(Exit::NORTHEAST))
269 toprow.append("/");
270 else
271 toprow.append(" ");
273 if(!room->isClosed(Exit::WEST))
274 thisrow.append("-");
275 else
276 thisrow.append(" ");
278 if(x == origx && y == origy)
280 thisrow.append("*");
282 else
284 Sector* sector = Cache::Get()->GetSector(room->getSector());
285 thisrow.append(sector->getSymbol());
288 if(!room->isClosed(Exit::EAST))
289 thisrow.append("-");
290 else
291 thisrow.append(" ");
293 if(!room->isClosed(Exit::SOUTHWEST))
294 bottomrow.append("/");
295 else
296 bottomrow.append(" ");
298 if(!room->isClosed(Exit::SOUTH))
299 bottomrow.append("|");
300 else
301 bottomrow.append(" ");
303 if(!room->isClosed(Exit::SOUTHEAST))
304 bottomrow.append("\\");
305 else
306 bottomrow.append(" ");
310 result.append(toprow);
311 result.append("\n");
312 result.append(thisrow);
313 result.append("\n");
314 result.append(bottomrow);
315 result.append("\n");
318 return result;
320 #endif
322 SavableManagerPtr Room::getManager() const
324 return m_room;
327 TableImplPtr Room::getTable() const
329 return m_room->getTable();
332 std::string Room::toString()
334 Strings result;
335 std::string line;
337 value_type roomid = getID();
338 RoomPtr room = mud::Managers::Get()->Room->GetByKey(roomid);
339 Assert(room);
341 line = room->getDescription();
342 result.push_back(line);
344 Strings details = mud::Managers::Get()->Detail->ReadableList(room);
346 // Add all details
347 for(Strings::const_iterator it = details.begin(); it != details.end(); it++) {
348 result.push_back(*it);
351 return String::Get()->unlines(result, " ", 0);