Sorted Socket.
[UnsignedByte.git] / src / DB / Savables / Room.cpp
blobef3ea4b4d1dd7493143c46b0742e81782f903eda
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 Assert(count == 1);
222 Area* p = Cache::Get()->GetArea(id);
224 for(value_type y = 1; y <= p->getHeight(); y++)
226 std::string toprow;
227 std::string thisrow;
228 std::string bottomrow;
230 for(value_type x = 1; x <= p->getWidth(); x++)
232 long rid = Cache::Get()->GetRoomID(id, x, y);
233 long count = DatabaseMgr::Get()->CountSavable(Tables::Get()->ROOMS, rid);
234 if(!count)
236 if(x == origx && y == origy)
238 toprow.append (" ");
239 thisrow.append (" o "); // Empty, current
240 bottomrow.append(" ");
242 else
244 toprow.append (" ");
245 thisrow.append (" . "); // Empty, not current
246 bottomrow.append(" ");
250 else
252 Room* room = Cache::Get()->GetRoom(rid);
254 if(!room->isClosed(Exit::NORTHWEST))
255 toprow.append("\\");
256 else
257 toprow.append(" ");
259 if(!room->isClosed(Exit::NORTH))
260 toprow.append("|");
261 else
262 toprow.append(" ");
264 if(!room->isClosed(Exit::NORTHEAST))
265 toprow.append("/");
266 else
267 toprow.append(" ");
269 if(!room->isClosed(Exit::WEST))
270 thisrow.append("-");
271 else
272 thisrow.append(" ");
274 if(x == origx && y == origy)
276 thisrow.append("*");
278 else
280 Sector* sector = Cache::Get()->GetSector(room->getSector());
281 thisrow.append(sector->getSymbol());
284 if(!room->isClosed(Exit::EAST))
285 thisrow.append("-");
286 else
287 thisrow.append(" ");
289 if(!room->isClosed(Exit::SOUTHWEST))
290 bottomrow.append("/");
291 else
292 bottomrow.append(" ");
294 if(!room->isClosed(Exit::SOUTH))
295 bottomrow.append("|");
296 else
297 bottomrow.append(" ");
299 if(!room->isClosed(Exit::SOUTHEAST))
300 bottomrow.append("\\");
301 else
302 bottomrow.append(" ");
306 result.append(toprow);
307 result.append("\n");
308 result.append(thisrow);
309 result.append("\n");
310 result.append(bottomrow);
311 result.append("\n");
314 return result;
316 #endif
318 SavableManagerPtr Room::getManager() const
320 return m_room;
323 TableImplPtr Room::getTable() const
325 return m_room->getTable();
328 std::string Room::toString() const
330 Strings result;
331 std::string line;
333 value_type roomid = getID();
334 RoomPtr room = mud::Managers::Get()->Room->GetByKey(roomid);
335 Assert(room);
337 line = room->getDescription();
338 result.push_back(line);
340 Strings details = mud::Managers::Get()->Detail->ReadableList(room);
342 // Add all details
343 for(Strings::const_iterator it = details.begin(); it != details.end(); it++) {
344 result.push_back(*it);
347 return String::Get()->unlines(result, " ", 0);
350 std::string Room::toFullString() const
352 return Savable::toString();