Add a filter-by-from/to-chunk in ExitManager::List.
[UnsignedByte.git] / src / DB / Savables / Chunk.cpp
blob63c63775c47c56f9952564798d87f44ffde20e6e
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"
30 using mud::Chunk;
32 std::map<value_type, value_types> Chunk::ms_charactersInRoom;
34 Chunk::Chunk(SavableManagerPtr chunk) :
35 m_chunk(chunk)
37 Assert(chunk);
40 Chunk::~Chunk(void)
42 Unlock();
45 value_type Chunk::getID() const
47 return m_chunk->getValue(db::TableImpls::Get()->CHUNKS->CHUNKID)->getIntegerValue();
50 const std::string& Chunk::getName() const
52 return m_chunk->getValue(db::TableImpls::Get()->CHUNKS->NAME)->getStringValue();
55 const std::string& Chunk::getDescription() const
57 return m_chunk->getValue(db::TableImpls::Get()->CHUNKS->DESCRIPTION)->getStringValue();
60 const std::string& Chunk::getTags() const
62 return m_chunk->getValue(db::TableImpls::Get()->CHUNKS->TAGS)->getStringValue();
65 value_type Chunk::getRoom() const
67 return m_chunk->getValue(db::TableImpls::Get()->CHUNKS->FKROOMS)->getIntegerValue();
70 void Chunk::setName(const std::string& name)
72 Lock();
73 ValuePtr value(new FieldValue(db::TableImpls::Get()->CHUNKS->NAME, name));
74 m_chunk->setValue(value);
77 void Chunk::setDescription(const std::string& description)
79 Lock();
80 ValuePtr value(new FieldValue(db::TableImpls::Get()->CHUNKS->DESCRIPTION, description));
81 m_chunk->setValue(value);
84 void Chunk::setTags(const std::string& tags)
86 Lock();
87 ValuePtr value(new FieldValue(db::TableImpls::Get()->CHUNKS->TAGS, tags));
88 m_chunk->setValue(value);
91 void Chunk::setRoom(value_type room)
93 Lock();
94 ValuePtr value(new FieldValue(db::TableImpls::Get()->CHUNKS->FKROOMS, room));
95 m_chunk->setValue(value);
98 void mud::Chunk::addCharacter(value_type characterid)
100 SmartPtr<mud::Character> character = mud::Managers::Get()->Character->GetByKey(characterid);
101 Assert(character->getChunk() == this->getID());
103 ms_charactersInRoom[getID()].insert(characterid);
106 void mud::Chunk::removeCharacter(value_type characterid)
108 SmartPtr<mud::Character> character = mud::Managers::Get()->Character->GetByKey(characterid);
109 Assert(character->getChunk() == this->getID());
111 value_types::const_iterator it = ms_charactersInRoom[getID()].find(characterid);
112 Assert(it != end());
113 ms_charactersInRoom[getID()].erase(it);
116 void mud::Chunk::Send(const std::string& msg)
118 for(value_types::const_iterator it = begin(); it != end(); it++)
120 CharacterPtr character = mud::Managers::Get()->Character->GetByKey(*it);
121 character->OnSend(msg);
125 void Chunk::Delete()
127 Lock();
128 m_chunk->erase();
131 void Chunk::Save()
133 Lock();
134 m_chunk->save();
137 void Chunk::Discard()
139 Lock();
140 m_chunk->discard();
143 bool Chunk::Exists()
145 return m_chunk->exists();
148 SavableManagerPtr Chunk::getManager() const
150 return m_chunk;
153 TableImplPtr Chunk::getTable() const
155 return m_chunk->getTable();
158 value_type mud::Chunk::getExitAt(Coordinate direction) const
160 Assert(direction.isDirection());
162 int x = direction.getXCoordinate();
163 int y = direction.getYCoordinate();
164 int z = direction.getZCoordinate();
166 FieldValuesPtr values(new FieldValues(db::TableImpls::Get()->EXITS));
167 values->addValue(db::TableImpls::Get()->EXITS->FKCHUNKSFROM, getID());
168 values->addValue(db::TableImpls::Get()->EXITS->X, x);
169 values->addValue(db::TableImpls::Get()->EXITS->Y, y);
170 values->addValue(db::TableImpls::Get()->EXITS->Z, z);
172 SavableManagersPtr result = SavableManager::getmulti(values);
173 if(result->size() > 0)
175 SavableManagerPtr manager = result->first();
176 Assert(manager);
178 FieldValuePtr value = manager->getValue(db::TableImpls::Get()->EXITS->EXITID);
179 Assert(value);
181 return value->getIntegerValue();
184 return 0;
187 value_types::const_iterator mud::Chunk::begin()
189 return ms_charactersInRoom[getID()].begin();
192 value_types::const_iterator mud::Chunk::end()
194 return ms_charactersInRoom[getID()].end();
197 value_type mud::Chunk::size()
199 return ms_charactersInRoom[getID()].size();