]>
jfr.im git - irc/gameservirc.git/blob - gameserv-2.0/libgameservgldl/src/DataLayer/File/FileMasterDAO.cpp
1 #include <GameServ/DataLayer/File/FileMasterDAO.h>
2 using GameServ::DataLayer::File::FileMasterDAO
;
4 #include <GameServ/GameLayer/GameObjects/MasterGO.h>
5 using GameServ::GameLayer::GameObjects::MasterGO
;
7 #include <GameServ/DataLayer/DataLayerExceptions.h>
8 using GameServ::DataLayer::Exceptions::ResourceException
;
9 using GameServ::DataLayer::Exceptions::DataLayerException
;
11 #include <GameServ/DataLayer/File/FileId.h>
12 using GameServ::DataLayer::File::FileId
;
14 #include <boost/smart_ptr/shared_ptr.hpp>
15 #include <boost/format.hpp>
16 #include <boost/algorithm/string.hpp>
17 #include <boost/lexical_cast.hpp>
18 using boost::shared_ptr
;
19 using boost::shared_static_cast
;
22 using boost::lexical_cast
;
23 using boost::bad_lexical_cast
;
24 #include <boost/tokenizer.hpp>
25 typedef boost::tokenizer
<boost::char_separator
<char> >
40 FileMasterDAO::FileMasterDAO() : mFilename("data\\masters.dat")
42 Initialize(GetMasterFilePath());
45 FileMasterDAO::FileMasterDAO(const string
&filename
)
50 FileMasterDAO::~FileMasterDAO()
54 void FileMasterDAO::Initialize(const string
&filename
)
60 void FileMasterDAO::LoadMasterCache(void)
62 assert(!mFilename
.empty());
63 spMasterCache
.clear();
65 infile
.open(mFilename
.c_str());
68 throw DataLayerException(str(format("Unable to open %1% for loading Masters") % mFilename
),
74 shared_ptr
<MasterGO
> spMaster
;
75 getline(infile
, line
);
80 else if (line
[0] == '#')
84 spMaster
= CreateMasterFromLine(line
);
85 spMasterCache
[spMaster
->Id()] = spMaster
;
90 string
FileMasterDAO::GetMasterFilePath() const
95 shared_ptr
<MasterGO
> FileMasterDAO::GetById(const string
&Id
) const
97 map
<string
, shared_ptr
<MasterGO
> >::const_iterator iter
;
98 iter
= spMasterCache
.find(Id
);
99 if (iter
!= spMasterCache
.end())
101 return shared_ptr
<MasterGO
>(iter
->second
);
103 return shared_ptr
<MasterGO
>();
106 shared_ptr
<MasterGO
> FileMasterDAO::CreateMasterFromLine(const string
&line
) const
108 assert(!line
.empty());
109 shared_ptr
<MasterGO
> spMaster
;
110 spMaster
= shared_ptr
<MasterGO
>(new MasterGO());
111 boost::char_separator
<char> sep("~", 0);
112 tokenizer
tokens(line
, sep
);
113 tokenizer::iterator tok_iter
= tokens
.begin();
118 spMaster
->Level(lexical_cast
<unsigned int>((*tok_iter
)));
120 catch (bad_lexical_cast
)
122 throw DataLayerException(str(format("Corrupt master line %1%. Invalid integer value %2%") %
123 line
% (*tok_iter
)));
128 if (tok_iter
== tokens
.end())
130 throw DataLayerException(str(format("Corrupt master line %1%. Missing information") %
131 line
), __FILE__
, __LINE__
);
133 spMaster
->Name((*tok_iter
));
134 spMaster
->Id(FileId::CreateMasterId(spMaster
->Level(), spMaster
->Name()));
138 if (tok_iter
== tokens
.end())
140 throw DataLayerException(str(format("Corrupt master line %1%. Missing information") %
141 line
), __FILE__
, __LINE__
);
143 spMaster
->WeaponName((*tok_iter
));
147 if (tok_iter
== tokens
.end())
149 throw DataLayerException(str(format("Corrupt master line %1%. Missing information") %
150 line
), __FILE__
, __LINE__
);
154 spMaster
->Strength(lexical_cast
<int>((*tok_iter
)));
156 catch (bad_lexical_cast
)
158 throw DataLayerException(str(format("Corrupt master line %1%. Invalid integer value %2%") %
159 line
% (*tok_iter
)));
164 if (tok_iter
== tokens
.end())
166 throw DataLayerException(str(format("Corrupt master line %1%. Missing information") %
167 line
), __FILE__
, __LINE__
);
171 spMaster
->Defense(lexical_cast
<int>((*tok_iter
)));
173 catch (bad_lexical_cast
)
175 throw DataLayerException(str(format("Corrupt master line %1%. Invalid integer value %2%") %
176 line
% (*tok_iter
)));
182 if (tok_iter
== tokens
.end())
184 throw DataLayerException(str(format("Corrupt master line %1%. Missing information") %
185 line
), __FILE__
, __LINE__
);
189 spMaster
->Gold(lexical_cast
<unsigned long int>((*tok_iter
)));
191 catch (bad_lexical_cast
)
193 throw DataLayerException(str(format("Corrupt master line %1%. Invalid integer value %2%") %
194 line
% (*tok_iter
)));
199 if (tok_iter
== tokens
.end())
201 throw DataLayerException(str(format("Corrupt master line %1%. Missing information") %
202 line
), __FILE__
, __LINE__
);
206 spMaster
->Experience(lexical_cast
<unsigned long int>((*tok_iter
)));
208 catch (bad_lexical_cast
)
210 throw DataLayerException(str(format("Corrupt master line %1%. Invalid integer value %2%") %
211 line
% (*tok_iter
)));
214 // Health and Max Health
216 if (tok_iter
== tokens
.end())
218 throw DataLayerException(str(format("Corrupt master line %1%. Missing information") %
219 line
), __FILE__
, __LINE__
);
223 spMaster
->Health(lexical_cast
<int>((*tok_iter
)));
224 spMaster
->MaxHealth(spMaster
->Health());
226 catch (bad_lexical_cast
)
228 throw DataLayerException(str(format("Corrupt master line %1%. Invalid integer value %2%") %
229 line
% (*tok_iter
)));
234 if (tok_iter
== tokens
.end())
236 throw DataLayerException(str(format("Corrupt master line %1%. Missing information") %
237 line
), __FILE__
, __LINE__
);
239 spMaster
->PartingWords((*tok_iter
));
240 // # level~name~weapon~strength~defense~gold~exp~hp~parting words
244 bool FileMasterDAO::IdExists(const string
&Id
) const
246 map
<string
, shared_ptr
<MasterGO
> >::const_iterator iter
;
247 iter
= spMasterCache
.find(Id
);
248 return (iter
!= spMasterCache
.end());