]>
jfr.im git - irc/gameservirc.git/blob - gameserv-2.0/libgameservgldl/src/DataLayer/File/FilePlayerDAO.cpp
1 #include <GameServ/DataLayer/File/FilePlayerDAO.h>
2 using GameServ::DataLayer::File::FilePlayerDAO
;
3 #include <GameServ/DataLayer/File/FileItemDAO.h>
4 using GameServ::DataLayer::File::FileItemDAO
;
6 #include <GameServ/GameLayer/GameObjects/PlayerGO.h>
7 using GameServ::GameLayer::GameObjects::PlayerGO
;
9 #include <GameServ/DataLayer/DataLayerExceptions.h>
10 using GameServ::DataLayer::Exceptions::ResourceException
;
11 using GameServ::DataLayer::Exceptions::DataLayerException
;
13 #include <GameServ/DataLayer/File/FileId.h>
14 using GameServ::DataLayer::File::FileId
;
16 #include <GameServ/GameLayer/Helpers/InventoryManager.h>
17 using namespace GameServ::GameLayer::Helpers
;
19 #include <boost/smart_ptr/shared_ptr.hpp>
20 #include <boost/format.hpp>
21 #include <boost/algorithm/string.hpp>
22 #include <boost/lexical_cast.hpp>
23 using boost::shared_ptr
;
24 using boost::shared_static_cast
;
27 using boost::algorithm::to_upper
;
28 using boost::algorithm::to_lower
;
29 using boost::lexical_cast
;
30 using boost::bad_lexical_cast
;
31 #include <boost/tokenizer.hpp>
32 typedef boost::tokenizer
<boost::char_separator
<char> >
35 #include <boost/date_time/posix_time/posix_time.hpp>
36 using namespace boost::posix_time
;
52 FilePlayerDAO::FilePlayerDAO() : mFilename("data\\players.dat")
54 Initialize(GetPlayerFilePath());
57 FilePlayerDAO::FilePlayerDAO(const string
&filename
)
62 FilePlayerDAO::~FilePlayerDAO()
66 void FilePlayerDAO::Initialize(const string
&filename
)
72 void FilePlayerDAO::LoadPlayerCache(void)
74 assert(!mFilename
.empty());
75 spPlayerCache
.clear();
77 infile
.open(mFilename
.c_str());
80 throw DataLayerException(str(format("Unable to open %1% for loading Players") % mFilename
),
86 shared_ptr
<PlayerGO
> spPlayer
;
87 getline(infile
, line
);
88 spPlayer
= CreatePlayerFromLine(line
);
89 string id
= spPlayer
->Id();
91 spPlayerCache
[id
] = spPlayer
;
96 void FilePlayerDAO::WritePlayerCache(void) const
99 outfile
.open(mFilename
.c_str());
102 throw DataLayerException(str(format("Unable to open %1% for writing Players") % mFilename
),
106 map
<string
, shared_ptr
<PlayerGO
> >::const_iterator iter
;
107 for (iter
= spPlayerCache
.begin(); iter
!= spPlayerCache
.end(); iter
++)
109 outfile
<< CreateLineFromPlayer(iter
->second
) << endl
;
114 string
FilePlayerDAO::GetPlayerFilePath() const
119 shared_ptr
<PlayerGO
> FilePlayerDAO::GetById(const string
&Id
) const
121 map
<string
, shared_ptr
<PlayerGO
> >::const_iterator iter
;
124 iter
= spPlayerCache
.find(id
);
125 if (iter
!= spPlayerCache
.end())
127 return shared_ptr
<PlayerGO
>(iter
->second
->Clone());
129 return shared_ptr
<PlayerGO
>();
132 shared_ptr
<PlayerGO
> FilePlayerDAO::CreatePlayerFromLine(const string
&line
) const
134 assert(!line
.empty());
135 shared_ptr
<PlayerGO
> spPlayer
;
136 spPlayer
= shared_ptr
<PlayerGO
>(new PlayerGO());
137 boost::char_separator
<char> sep(" ", 0, boost::keep_empty_tokens
);
138 tokenizer
tokens(line
, sep
);
139 tokenizer::iterator tok_iter
= tokens
.begin();
141 spPlayer
->Name((*tok_iter
));
142 spPlayer
->Id(FileId::CreatePlayerId(spPlayer
->Name()));
144 if (tok_iter
== tokens
.end())
146 throw DataLayerException(str(format("Corrupt player line %1%. Missing information") %
147 line
), __FILE__
, __LINE__
);
149 spPlayer
->Level(lexical_cast
<int>((*tok_iter
)));
151 if (tok_iter
== tokens
.end())
153 throw DataLayerException(str(format("Corrupt player line %1%. Missing information") %
154 line
), __FILE__
, __LINE__
);
156 spPlayer
->Experience(lexical_cast
<unsigned long int>((*tok_iter
)));
158 if (tok_iter
== tokens
.end())
160 throw DataLayerException(str(format("Corrupt player line %1%. Missing information") %
161 line
), __FILE__
, __LINE__
);
163 spPlayer
->Gold(lexical_cast
<unsigned long int>((*tok_iter
)));
165 if (tok_iter
== tokens
.end())
167 throw DataLayerException(str(format("Corrupt player line %1%. Missing information") %
168 line
), __FILE__
, __LINE__
);
170 spPlayer
->Bank(lexical_cast
<unsigned long int>((*tok_iter
)));
172 if (tok_iter
== tokens
.end())
174 throw DataLayerException(str(format("Corrupt player line %1%. Missing information") %
175 line
), __FILE__
, __LINE__
);
177 spPlayer
->Health(lexical_cast
<int>((*tok_iter
)));
179 if (tok_iter
== tokens
.end())
181 throw DataLayerException(str(format("Corrupt player line %1%. Missing information") %
182 line
), __FILE__
, __LINE__
);
184 spPlayer
->MaxHealth(lexical_cast
<int>((*tok_iter
)));
186 if (tok_iter
== tokens
.end())
188 throw DataLayerException(str(format("Corrupt player line %1%. Missing information") %
189 line
), __FILE__
, __LINE__
);
191 spPlayer
->Strength(lexical_cast
<int>((*tok_iter
)));
193 if (tok_iter
== tokens
.end())
195 throw DataLayerException(str(format("Corrupt player line %1%. Missing information") %
196 line
), __FILE__
, __LINE__
);
198 spPlayer
->Defense(lexical_cast
<int>((*tok_iter
)));
200 if (tok_iter
== tokens
.end())
202 throw DataLayerException(str(format("Corrupt player line %1%. Missing information") %
203 line
), __FILE__
, __LINE__
);
205 spPlayer
->ForestFights(lexical_cast
<int>((*tok_iter
)));
207 if (tok_iter
== tokens
.end())
209 throw DataLayerException(str(format("Corrupt player line %1%. Missing information") %
210 line
), __FILE__
, __LINE__
);
212 spPlayer
->PlayerFights(lexical_cast
<int>((*tok_iter
)));
214 if (tok_iter
== tokens
.end())
216 throw DataLayerException(str(format("Corrupt player line %1%. Missing information") %
217 line
), __FILE__
, __LINE__
);
219 int flags
= lexical_cast
<int>((*tok_iter
));
220 SetFlags(spPlayer
, flags
);
222 if (tok_iter
== tokens
.end())
224 throw DataLayerException(str(format("Corrupt player line %1%. Missing information") %
225 line
), __FILE__
, __LINE__
);
227 spPlayer
->Password((*tok_iter
));
231 if (tok_iter
== tokens
.end())
233 throw DataLayerException(str(format("Corrupt player line %1%. Missing information") %
234 line
), __FILE__
, __LINE__
);
239 unsigned long int intlast
= lexical_cast
<unsigned long int>((*tok_iter
));
240 time_t t
= (time_t)intlast
;
241 spPlayer
->LastLogin(from_time_t(t
));
243 catch (bad_lexical_cast
)
245 spPlayer
->LastLogin(from_iso_string((*tok_iter
)));
251 if (tok_iter
== tokens
.end())
253 throw DataLayerException(str(format("Corrupt player line %1%. Missing information") %
254 line
), __FILE__
, __LINE__
);
257 string weaponid
= FileId::CreateItemId((*tok_iter
));
258 spPlayer
->Weapon(boost::shared_static_cast
<WeaponGO
>(idao
.GetById(weaponid
)));
262 if (tok_iter
== tokens
.end())
264 throw DataLayerException(str(format("Corrupt player line %1%. Missing information") %
265 line
), __FILE__
, __LINE__
);
267 string armorid
= FileId::CreateItemId((*tok_iter
));
268 spPlayer
->Armor(boost::shared_static_cast
<ArmorGO
>(idao
.GetById(armorid
)));
271 if (tok_iter
== tokens
.end())
273 throw DataLayerException(str(format("Corrupt player line %1%. Missing information") %
274 line
), __FILE__
, __LINE__
);
276 // Load all the items into the player's inventory
277 shared_ptr
<InventoryManager
> inventory
= InventoryManager::Instance();
279 while (tok_iter
!= tokens
.end())
281 string itemid
= FileId::CreateItemId((*tok_iter
));
282 shared_ptr
<ItemGO
> spItem
= idao
.GetById(itemid
);
284 if (tok_iter
== tokens
.end())
286 throw DataLayerException(str(format("Corrupt %1% file: Missing item uses for item %2% on player %3%") %
287 mFilename
% itemid
% spPlayer
->Name()), __FILE__
, __LINE__
);
291 spItem
->Uses(lexical_cast
<int>((*tok_iter
)));
293 catch (bad_lexical_cast
)
295 throw DataLayerException(str(format("Corrupt %1% file: Non integer item uses for item %2% on player %3%") %
296 mFilename
% itemid
% spPlayer
->Name()), __FILE__
, __LINE__
);
298 inventory
->AddItem(spPlayer
, spItem
);
305 string
FilePlayerDAO::CreateLineFromPlayer(shared_ptr
<PlayerGO
> spPlayer
) const
308 line
= str(format("%1% %2% %3% %4% %5% %6% %7% %8% %9% %10% %11% %12% %13% %14% %15% %16%") %
309 spPlayer
->Name() % spPlayer
->Level() % spPlayer
->Experience() % spPlayer
->Gold() %
310 spPlayer
->Bank() % spPlayer
->Health() % spPlayer
->MaxHealth() % spPlayer
->Strength() %
311 spPlayer
->Defense() % spPlayer
->ForestFights() % spPlayer
->PlayerFights() %
312 GetFlags(spPlayer
) % spPlayer
->Password() % (to_iso_string(spPlayer
->LastLogin())) %
313 FileId::GetItemNumberFromId(spPlayer
->Weapon()->Id()) % FileId::GetItemNumberFromId(spPlayer
->Armor()->Id()));
315 shared_ptr
<InventoryManager
> spInventory
= InventoryManager::Instance();
316 ItemList inventory
= spInventory
->GetInventory(spPlayer
);
317 ItemList::const_iterator iter
;
318 for (iter
= inventory
.begin(); iter
!= inventory
.end(); iter
++)
320 shared_ptr
<ItemGO
> spItem
= (*iter
);
321 line
+= " " + FileId::GetItemNumberFromId(spItem
->Id()) + " " + lexical_cast
<string
>(spItem
->Uses());
327 void FilePlayerDAO::SetFlags(shared_ptr
<PlayerGO
> spPlayer
, const int &flags
) const
329 if ((flags
& 0x00000001) == 0x00000001)
331 spPlayer
->FoughtMaster(true);
335 spPlayer
->FoughtMaster(false);
338 if ((flags
& 0x00000002) == 0x00000002)
340 spPlayer
->Alive(true);
344 spPlayer
->Alive(false);
348 int FilePlayerDAO::GetFlags(shared_ptr
<PlayerGO
> spPlayer
) const
352 if (spPlayer
->FoughtMaster())
356 if (spPlayer
->Alive())
363 void FilePlayerDAO::Update(shared_ptr
<PlayerGO
> spPlayer
)
365 assert(spPlayer
!= 0);
366 assert(!spPlayer
->Id().empty());
367 assert(!spPlayer
->Name().empty());
369 // TODO: Lock the file
370 if (IdExists(spPlayer
->Id()))
372 string id
= spPlayer
->Id();
374 spPlayerCache
[id
] = spPlayer
;
379 throw DataLayerException(str(format("Unable to Update Player %1%: Player does not exist") %
380 spPlayer
->Name()), __FILE__
, __LINE__
);
384 void FilePlayerDAO::Insert(shared_ptr
<PlayerGO
> spPlayer
)
386 assert(spPlayer
!= 0);
387 assert(!spPlayer
->Name().empty());
388 // TODO: Lock the file
389 if (!IdExists(FileId::CreatePlayerId(spPlayer
->Name())))
391 spPlayer
->Id(FileId::CreatePlayerId(spPlayer
->Name()));
392 string id
= spPlayer
->Id();
394 spPlayerCache
[id
] = spPlayer
;
396 outfile.open(mFilename.c_str(), std::ios::app);
399 throw ResourceException(str(format("Unable to open %1% for appending.") % mFilename), __FILE__, __LINE__);
401 outfile << CreateLineFromPlayer(spPlayer) << endl;
407 throw DataLayerException(str(format("Unable to Insert Player %1%: Player exists") %
408 spPlayer
->Name()), __FILE__
, __LINE__
);
412 bool FilePlayerDAO::IdExists(const string
&Id
) const
414 map
<string
, shared_ptr
<PlayerGO
> >::const_iterator iter
;
417 iter
= spPlayerCache
.find(id
);
418 return (iter
!= spPlayerCache
.end());
421 list
<string
> FilePlayerDAO::GetIdsByName(const string
&Name
) const
424 if (IdExists(FileId::CreatePlayerId(Name
)))
426 ids
.push_back(FileId::CreatePlayerId(Name
));