Add a filter-by-from/to-chunk in ExitManager::List.
[UnsignedByte.git] / src / DB / Savables / Character.cpp
blobe142a75025d94080f1098ccef9e9b269489bbbbb
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 "StringUtilities.h"
28 #include "FieldImpls.h"
29 #include "TableImpls.h"
31 #include "Character.h"
32 #include "Race.h"
33 #include "RaceManager.h"
34 #include "Area.h"
35 #include "Chunk.h"
36 #include "ChunkManager.h"
37 #include "AreaManager.h"
38 #include "Managers.h"
39 #include "CharacterManager.h"
41 using mud::Character;
43 Character::Character(SavableManagerPtr character) :
44 m_character(character)
46 Assert(character);
49 Character::~Character(void)
51 Unlock();
54 value_type Character::getID() const
56 return m_character->getValue(db::TableImpls::Get()->ENTITIES->ENTITYID)->getIntegerValue();
59 const std::string& Character::getName() const
61 return m_character->getValue(db::TableImpls::Get()->ENTITIES->NAME)->getStringValue();
64 const std::string& Character::getDescription() const
66 return m_character->getValue(db::TableImpls::Get()->ENTITIES->DESCRIPTION)->getStringValue();
69 value_type Character::getRace() const
71 return m_character->getValue(db::TableImpls::Get()->ENTITIES->FKRACES)->getIntegerValue();
74 value_type Character::getChunk() const
76 return m_character->getValue(db::TableImpls::Get()->ENTITIES->FKCHUNKS)->getIntegerValue();
80 /**
81 * \brief Setters
82 */
83 void Character::setName(const std::string& name)
85 Lock();
86 ValuePtr value(new FieldValue(db::TableImpls::Get()->ENTITIES->NAME, name));
87 m_character->setValue(value);
88 Unlock();
91 void Character::setDescription(const std::string& description)
93 Lock();
94 ValuePtr value(new FieldValue(db::TableImpls::Get()->ENTITIES->DESCRIPTION, description));
95 m_character->setValue(value);
96 Unlock();
99 void Character::setRace(value_type race)
101 Lock();
102 ValuePtr value(new FieldValue(db::TableImpls::Get()->ENTITIES->FKRACES, race));
103 m_character->setValue(value);
104 Unlock();
107 void Character::setChunk(value_type chunk)
109 Lock();
110 ValuePtr value(new FieldValue(db::TableImpls::Get()->ENTITIES->FKCHUNKS, chunk));
111 m_character->setValue(value);
112 Unlock();
117 * Operations
120 void mud::Character::MoveToChunk(value_type chunkid)
122 mud::ChunkPtr inchunk = mud::Managers::Get()->Chunk->GetByKey(getChunk());
123 mud::ChunkPtr tochunk = mud::Managers::Get()->Chunk->GetByKey(chunkid);
125 value_type id = getID();
127 inchunk->removeCharacter(id);
128 setChunk(chunkid);
129 tochunk->addCharacter(id);
133 * Start of Output
135 void Character::OnSend(const std::string& msg)
137 // discard msg
138 return;
141 void Character::OnSendf(const char* format, ...)
143 va_list args;
144 va_start(args, format);
145 OnSend(Global::Get()->sprint(args, format));
146 va_end(args);
147 return;
150 * End of Output
155 * Start of Commands
158 void Character::OnLook(const std::string& msg)
160 long chunkid = getChunk();
161 mud::ChunkPtr chunk = mud::Managers::Get()->Chunk->GetByKey(chunkid);
163 OnSend(chunk->toString());
165 for(value_types::const_iterator it = chunk->begin(); it != chunk->end(); it++)
167 value_type characterid = *it;
169 CharacterPtr pch = mud::Managers::Get()->Character->GetByKey(characterid);
170 Assert(pch);
172 OnSendf("(%s) %s.\n", pch->getName().c_str(), pch->getDescription().c_str());
175 return;
178 void Character::OnScore(const std::string& msg)
180 OnSend(m_character->toString());
181 return;
184 void Character::OnSay(const std::string& msg)
186 if(msg.size() == 0)
188 OnSend("Say what?\n");
189 return;
192 long chunkid = getChunk();
193 ChunkPtr chunk = mud::Managers::Get()->Chunk->GetByKey(chunkid);
195 std::string line = getName();
196 line.append(" says '");
197 line.append(msg);
198 line.append("'\n");
200 chunk->Send(line);
201 return;
205 * End of commands
208 void Character::Delete()
210 Lock();
211 m_character->erase();
212 Unlock();
215 void Character::Save()
217 Lock();
218 m_character->save();
219 Unlock();
222 void Character::Discard()
224 Lock();
225 m_character->discard();
226 Unlock();
229 bool Character::Exists()
231 return m_character->exists();
234 SavableManagerPtr Character::getManager() const
236 return m_character;
239 TableImplPtr Character::getTable() const
241 return m_character->getTable();