Rename MoveToRoom to MoveToChunk.
[UnsignedByte.git] / src / DB / Savables / Character.cpp
blob58f9604a14474a43f9c39a19aba6f5768ce703ce
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"
40 using mud::Character;
42 Character::Character(SavableManagerPtr character) :
43 m_character(character)
45 Assert(character);
48 Character::~Character(void)
50 Unlock();
53 value_type Character::getID() const
55 return m_character->getValue(db::TableImpls::Get()->ENTITIES->ENTITYID)->getIntegerValue();
58 const std::string& Character::getName() const
60 return m_character->getValue(db::TableImpls::Get()->ENTITIES->NAME)->getStringValue();
63 const std::string& Character::getDescription() const
65 return m_character->getValue(db::TableImpls::Get()->ENTITIES->DESCRIPTION)->getStringValue();
68 value_type Character::getRace() const
70 return m_character->getValue(db::TableImpls::Get()->ENTITIES->FKRACES)->getIntegerValue();
73 value_type Character::getChunk() const
75 return m_character->getValue(db::TableImpls::Get()->ENTITIES->FKCHUNKS)->getIntegerValue();
79 /**
80 * \brief Setters
81 */
82 void Character::setName(const std::string& name)
84 Lock();
85 ValuePtr value(new FieldValue(db::TableImpls::Get()->ENTITIES->NAME, name));
86 m_character->setValue(value);
87 Unlock();
90 void Character::setDescription(const std::string& description)
92 Lock();
93 ValuePtr value(new FieldValue(db::TableImpls::Get()->ENTITIES->DESCRIPTION, description));
94 m_character->setValue(value);
95 Unlock();
98 void Character::setRace(value_type race)
100 Lock();
101 ValuePtr value(new FieldValue(db::TableImpls::Get()->ENTITIES->FKRACES, race));
102 m_character->setValue(value);
103 Unlock();
106 void Character::setChunk(value_type chunk)
108 Lock();
109 ValuePtr value(new FieldValue(db::TableImpls::Get()->ENTITIES->FKCHUNKS, chunk));
110 m_character->setValue(value);
111 Unlock();
116 * Operations
119 void mud::Character::MoveToChunk(value_type chunkid)
121 mud::ChunkPtr inchunk = mud::Managers::Get()->Chunk->GetByKey(getChunk());
122 mud::ChunkPtr tochunk = mud::Managers::Get()->Chunk->GetByKey(chunkid);
124 value_type id = getID();
126 setChunk(chunkid);
127 inchunk->removeCharacter(id);
128 tochunk->addCharacter(id);
132 * Start of Output
134 void Character::OnSend(const std::string& msg)
136 // discard msg
137 return;
140 void Character::OnSendf(const char* format, ...)
142 va_list args;
143 va_start(args, format);
144 OnSend(Global::Get()->sprint(args, format));
145 va_end(args);
146 return;
149 * End of Output
154 * Start of Commands
157 void Character::OnLook(const std::string& msg)
159 try
161 long chunkid = getChunk();
162 mud::ChunkPtr chunk = mud::Managers::Get()->Chunk->GetByKey(chunkid);
164 OnSend(chunk->toString());
166 Characters chars = room->getCharacters();
167 for(Characters::iterator it = chars.begin(); it != chars.end(); it++)
169 OnSendf("(%s) %s.\n", (*it)->getName().c_str(), (*it)->getDescription().c_str());
173 return;
175 catch (RowNotFoundException& e)
177 Global::Get()->bug(e.what());
178 return;
182 void Character::OnScore(const std::string& msg)
184 OnSend(m_character->toString());
185 return;
188 void Character::OnSay(const std::string& msg)
190 if(msg.size() == 0)
192 OnSend("Say what?\n");
193 return;
198 long chunkid = getChunk();
199 ChunkPtr chunk = mud::Managers::Get()->Chunk->GetByKey(chunkid);
201 std::string line = getName();
202 line.append(" says '");
203 line.append(msg);
204 line.append("'\n");
206 chunk->Send(line);
207 return;
209 catch (RowNotFoundException& e)
211 Global::Get()->bug(e.what());
212 return;
217 * End of commands
220 void Character::Delete()
222 Lock();
223 m_character->erase();
224 Unlock();
227 void Character::Save()
229 Lock();
230 m_character->save();
231 Unlock();
234 void Character::Discard()
236 Lock();
237 m_character->discard();
238 Unlock();
241 bool Character::Exists()
243 return m_character->exists();
246 SavableManagerPtr Character::getManager() const
248 return m_character;
251 TableImplPtr Character::getTable() const
253 return m_character->getTable();