Used a more uniform of logging and added a commandline parser.
[UnsignedByte.git] / src / DB / Savables / Character.cpp
blob945d1a71ddea5deee0475da0d4b47152e6cfe970
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"
30 #include "Character.h"
31 #include "Race.h"
32 #include "RaceManager.h"
33 #include "Area.h"
34 #include "Chunk.h"
35 #include "ChunkManager.h"
36 #include "AreaManager.h"
37 #include "Managers.h"
38 #include "CharacterManager.h"
39 #include "ExitManager.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);
162 Assert(chunk);
164 OnSend(chunk->toString());
165 OnSend("\n");
166 OnCharactersInRoom(Global::Get()->EmptyString);
167 OnSend("\n");
168 OnExits(Global::Get()->EmptyString);
170 return;
173 void Character::OnCharactersInRoom(const std::string& msg)
175 long chunkid = getChunk();
176 mud::ChunkPtr chunk = mud::Managers::Get()->Chunk->GetByKey(chunkid);
177 Assert(chunk);
179 for(value_types::const_iterator it = chunk->begin(); it != chunk->end(); it++)
181 value_type characterid = *it;
183 CharacterPtr pch = mud::Managers::Get()->Character->GetByKey(characterid);
184 Assert(pch);
186 OnSendf("(%s) %s\n", pch->getName().c_str(), pch->getDescription().c_str());
190 void Character::OnScore(const std::string& msg)
192 OnSend(m_character->toString());
193 return;
196 void Character::OnSay(const std::string& msg)
198 if(msg.size() == 0)
200 OnSend("Say what?\n");
201 return;
204 long chunkid = getChunk();
205 ChunkPtr chunk = mud::Managers::Get()->Chunk->GetByKey(chunkid);
207 std::string line = getName();
208 line.append(" says '");
209 line.append(msg);
210 line.append("'\n");
212 chunk->Send(line);
213 return;
216 void Character::OnExits(const std::string& msg)
218 long chunkid = getChunk();
219 mud::ChunkPtr chunk = mud::Managers::Get()->Chunk->GetByKey(chunkid);
221 std::string result = "[";
222 result.append(String::Get()->unlines(mud::Managers::Get()->Exit->ReadableList(chunk), " ", 0));
223 result.append("]\n");
225 OnSend(result);
229 * End of commands
232 void Character::Delete()
234 Lock();
235 m_character->erase();
236 Unlock();
239 void Character::Save()
241 Lock();
242 m_character->save();
243 Unlock();
246 void Character::Discard()
248 Lock();
249 m_character->discard();
250 Unlock();
253 bool Character::Exists()
255 return m_character->exists();
258 SavableManagerPtr Character::getManager() const
260 return m_character;
263 TableImplPtr Character::getTable() const
265 return m_character->getTable();