Sorted Socket.
[UnsignedByte.git] / src / DB / Savables / Chunk.cpp
blob6dc06d1eda35c0d5d9035c1bb7808c44d263590a
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 "Chunk.h"
22 #include "Global.h"
23 #include "FieldImpls.h"
24 #include "TableImpls.h"
25 #include "Managers.h"
26 #include "Character.h"
27 #include "CharacterManager.h"
28 #include "FieldValues.h"
29 #include "RoomManager.h"
30 #include "ClusterManager.h"
31 #include "DetailManager.h"
32 #include "AreaManager.h"
33 #include "Room.h"
34 #include "Cluster.h"
35 #include "Area.h"
36 #include "StringUtilities.h"
37 #include "ChunkManager.h"
39 using mud::Chunk;
41 std::map<value_type, value_types> Chunk::ms_charactersInRoom;
43 Chunk::Chunk(SavableManagerPtr chunk) :
44 m_chunk(chunk)
46 Assert(chunk);
49 Chunk::~Chunk(void)
51 Unlock();
54 value_type Chunk::getID() const
56 return m_chunk->getValue(db::TableImpls::Get()->CHUNKS->CHUNKID)->getIntegerValue();
59 const std::string& Chunk::getName() const
61 return m_chunk->getValue(db::TableImpls::Get()->CHUNKS->NAME)->getStringValue();
64 const std::string& Chunk::getDescription() const
66 return m_chunk->getValue(db::TableImpls::Get()->CHUNKS->DESCRIPTION)->getStringValue();
69 const std::string& Chunk::getTags() const
71 return m_chunk->getValue(db::TableImpls::Get()->CHUNKS->TAGS)->getStringValue();
74 value_type Chunk::getRoom() const
76 return m_chunk->getValue(db::TableImpls::Get()->CHUNKS->FKROOMS)->getIntegerValue();
79 void Chunk::setName(const std::string& name)
81 Lock();
82 ValuePtr value(new FieldValue(db::TableImpls::Get()->CHUNKS->NAME, name));
83 m_chunk->setValue(value);
86 void Chunk::setDescription(const std::string& description)
88 Lock();
89 ValuePtr value(new FieldValue(db::TableImpls::Get()->CHUNKS->DESCRIPTION, description));
90 m_chunk->setValue(value);
93 void Chunk::setTags(const std::string& tags)
95 Lock();
96 ValuePtr value(new FieldValue(db::TableImpls::Get()->CHUNKS->TAGS, tags));
97 m_chunk->setValue(value);
100 void Chunk::setRoom(value_type room)
102 Lock();
103 ValuePtr value(new FieldValue(db::TableImpls::Get()->CHUNKS->FKROOMS, room));
104 m_chunk->setValue(value);
107 void mud::Chunk::addCharacter(value_type characterid)
109 SmartPtr<mud::Character> character = mud::Managers::Get()->Character->GetByKey(characterid);
110 Assert(character->getChunk() == this->getID());
112 ms_charactersInRoom[getID()].insert(characterid);
115 void mud::Chunk::removeCharacter(value_type characterid)
117 SmartPtr<mud::Character> character = mud::Managers::Get()->Character->GetByKey(characterid);
118 Assert(character->getChunk() == this->getID());
120 value_types::const_iterator it = ms_charactersInRoom[getID()].find(characterid);
121 Assert(it != end());
122 ms_charactersInRoom[getID()].erase(it);
125 void mud::Chunk::Send(const std::string& msg)
127 for(value_types::const_iterator it = begin(); it != end(); it++)
129 CharacterPtr character = mud::Managers::Get()->Character->GetByKey(*it);
130 character->OnSend(msg);
134 void Chunk::Delete()
136 Lock();
137 m_chunk->erase();
140 void Chunk::Save()
142 Lock();
143 m_chunk->save();
146 void Chunk::Discard()
148 Lock();
149 m_chunk->discard();
152 bool Chunk::Exists()
154 return m_chunk->exists();
157 SavableManagerPtr Chunk::getManager() const
159 return m_chunk;
162 TableImplPtr Chunk::getTable() const
164 return m_chunk->getTable();
167 value_type mud::Chunk::getExitAt(Coordinate direction) const
169 Assert(direction.isDirection());
171 int x = direction.getXCoordinate();
172 int y = direction.getYCoordinate();
173 int z = direction.getZCoordinate();
175 FieldValuesPtr values(new FieldValues(db::TableImpls::Get()->EXITS));
176 values->addValue(db::TableImpls::Get()->EXITS->FKCHUNKSFROM, getID());
177 values->addValue(db::TableImpls::Get()->EXITS->X, x+1);
178 values->addValue(db::TableImpls::Get()->EXITS->Y, y+1);
179 values->addValue(db::TableImpls::Get()->EXITS->Z, z+1);
181 SavableManagersPtr result = SavableManager::getmulti(values);
182 if(result->size() > 0)
184 SavableManagerPtr manager = result->first();
185 Assert(manager);
187 FieldValuePtr value = manager->getValue(db::TableImpls::Get()->EXITS->EXITID);
188 Assert(value);
190 return value->getIntegerValue();
193 return 0;
196 value_types::const_iterator mud::Chunk::begin()
198 return ms_charactersInRoom[getID()].begin();
201 value_types::const_iterator mud::Chunk::end()
203 return ms_charactersInRoom[getID()].end();
206 value_type mud::Chunk::size()
208 return ms_charactersInRoom[getID()].size();
211 std::string mud::Chunk::toString() const
213 Strings result;
214 std::string line;
216 value_type chunkid = getID();
217 mud::ChunkPtr chunk = mud::Managers::Get()->Chunk->GetByKey(chunkid);
218 Assert(chunk);
220 value_type roomid = getRoom();
221 mud::RoomPtr room = mud::Managers::Get()->Room->GetByKey(roomid);
222 Assert(room);
224 value_type clusterid = room->getCluster();
225 mud::ClusterPtr cluster = mud::Managers::Get()->Cluster->GetByKey(clusterid);
226 Assert(cluster);
228 value_type areaid = cluster->getArea();
229 mud::AreaPtr area = mud::Managers::Get()->Area->GetByKey(areaid);
230 Assert(area);
233 // Create the header
234 line = std::string("[") + area->getName() + std::string(", ") + cluster->getName() + std::string("]");
235 result.push_back(line);
237 // Add the room description
238 line = room->toString();
239 result.push_back(line);
241 // Add our own description
242 line = getDescription();
243 result.push_back(line);
245 // Get our own details
246 Strings details = mud::Managers::Get()->Detail->ReadableList(chunk);
248 // Add all details
249 for(Strings::const_iterator it = details.begin(); it != details.end(); it++) {
250 result.push_back(*it);
253 // Unline and add a trailing newline
254 std::string unlined = String::Get()->unlines(result, "\n", 0);
255 unlined.append("\n");
257 return unlined;
260 std::string mud::Chunk::toFullString() const
262 return Savable::toString();