]>
jfr.im git - irc/gameservirc.git/blob - gameserv-2.0/libgameservgldl/src/DataLayer/File/FileItemDAO.cpp
1 #include <GameServ/DataLayer/File/FileItemDAO.h>
2 using GameServ::DataLayer::File::FileItemDAO
;
3 #include <GameServ/GameLayer/GameObjects/ItemGO.h>
4 using GameServ::GameLayer::GameObjects::ItemGO
;
5 #include <GameServ/GameLayer/GameObjects/ArmorGO.h>
6 using GameServ::GameLayer::GameObjects::ArmorGO
;
7 #include <GameServ/GameLayer/GameObjects/WeaponGO.h>
8 using GameServ::GameLayer::GameObjects::WeaponGO
;
9 #include <GameServ/GameLayer/GameObjects/PotionGO.h>
10 using GameServ::GameLayer::GameObjects::PotionGO
;
12 #include <GameServ/DataLayer/DataLayerExceptions.h>
13 using GameServ::DataLayer::Exceptions::ResourceException
;
14 using GameServ::DataLayer::Exceptions::DataLayerException
;
16 #include <GameServ/DataLayer/File/FileId.h>
17 using GameServ::DataLayer::File::FileId
;
19 #include <GameServ/Types.h>
20 using GameServ::Types::ItemTypes
;
21 using GameServ::Types::Modifiers
;
22 using GameServ::Types::Exceptions::TypeException
;
24 #include <boost/shared_ptr.hpp>
25 #include <boost/format.hpp>
26 #include <boost/algorithm/string.hpp>
27 #include <boost/lexical_cast.hpp>
28 using boost::shared_ptr
;
31 using boost::algorithm::to_upper
;
32 using boost::lexical_cast
;
33 #include <boost/tokenizer.hpp>
34 typedef boost::tokenizer
<boost::char_separator
<char> >
49 // TODO: Switch filename to be loaded from a config file
50 FileItemDAO::FileItemDAO() : mFilename("data\\items\\masteritems.dat")
52 Initialize(GetItemFilePath());
55 FileItemDAO::FileItemDAO(const string
&filename
)
60 FileItemDAO::~FileItemDAO()
64 void FileItemDAO::Initialize(const string
&filename
)
70 string
FileItemDAO::GetItemFilePath() const
75 shared_ptr
<ItemGO
> FileItemDAO::GetById(const string
&Id
) const
77 map
<string
, shared_ptr
<ItemGO
> >::const_iterator iter
;
78 iter
= spItemCache
.find(Id
);
79 if (iter
!= spItemCache
.end())
81 return shared_ptr
<ItemGO
>(iter
->second
->Clone());
85 return shared_ptr
<ItemGO
>();
88 shared_ptr
<ItemGO
> FileItemDAO::CreatePotionFromLine(const string
&line
) const
90 assert(!line
.empty());
91 shared_ptr
<PotionGO
> spPotion
= shared_ptr
<PotionGO
>(new PotionGO());
92 map
<Modifiers::Modifier
, Range
> modifiers
;
93 boost::char_separator
<char> sep("~", 0, boost::keep_empty_tokens
);
94 tokenizer
tokens(line
, sep
);
95 tokenizer::iterator tok_iter
= tokens
.begin();
98 if (tok_iter
== tokens
.end())
100 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
101 mFilename
% line
), __FILE__
, __LINE__
);
103 spPotion
->Id(FileId::CreateItemId((*tok_iter
)));
106 if (tok_iter
== tokens
.end())
108 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
109 mFilename
% line
), __FILE__
, __LINE__
);
112 spPotion
->Name((*tok_iter
));
115 if (tok_iter
== tokens
.end())
117 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
118 mFilename
% line
), __FILE__
, __LINE__
);
120 spPotion
->Price(lexical_cast
<unsigned long int>((*tok_iter
)));
123 if (tok_iter
== tokens
.end())
125 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
126 mFilename
% line
), __FILE__
, __LINE__
);
128 spPotion
->Uses(lexical_cast
<int>((*tok_iter
)));
131 if (tok_iter
== tokens
.end())
133 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
134 mFilename
% line
), __FILE__
, __LINE__
);
136 int low
= lexical_cast
<int>((*tok_iter
));
138 if (tok_iter
== tokens
.end())
140 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
141 mFilename
% line
), __FILE__
, __LINE__
);
143 int high
= lexical_cast
<int>((*tok_iter
));
144 modifiers
[Modifiers::strength
] = Range(high
, low
);
146 if (tok_iter
== tokens
.end())
148 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
149 mFilename
% line
), __FILE__
, __LINE__
);
151 low
= lexical_cast
<int>((*tok_iter
));
153 if (tok_iter
== tokens
.end())
155 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
156 mFilename
% line
), __FILE__
, __LINE__
);
158 high
= lexical_cast
<int>((*tok_iter
));
159 modifiers
[Modifiers::defense
] = Range(high
, low
);
162 if (tok_iter
== tokens
.end())
164 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
165 mFilename
% line
), __FILE__
, __LINE__
);
167 low
= lexical_cast
<int>((*tok_iter
));
169 if (tok_iter
== tokens
.end())
171 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
172 mFilename
% line
), __FILE__
, __LINE__
);
174 high
= lexical_cast
<int>((*tok_iter
));
175 modifiers
[Modifiers::maxhealth
] = Range(high
, low
);
178 if (tok_iter
== tokens
.end())
180 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
181 mFilename
% line
), __FILE__
, __LINE__
);
183 low
= lexical_cast
<int>((*tok_iter
));
185 if (tok_iter
== tokens
.end())
187 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
188 mFilename
% line
), __FILE__
, __LINE__
);
190 high
= lexical_cast
<int>((*tok_iter
));
191 modifiers
[Modifiers::health
] = Range(high
, low
);
194 if (tok_iter
== tokens
.end())
196 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
197 mFilename
% line
), __FILE__
, __LINE__
);
199 low
= lexical_cast
<int>((*tok_iter
));
201 if (tok_iter
== tokens
.end())
203 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
204 mFilename
% line
), __FILE__
, __LINE__
);
206 high
= lexical_cast
<int>((*tok_iter
));
207 modifiers
[Modifiers::forestfights
] = Range(high
, low
);
210 if (tok_iter
== tokens
.end())
212 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
213 mFilename
% line
), __FILE__
, __LINE__
);
215 low
= lexical_cast
<int>((*tok_iter
));
217 if (tok_iter
== tokens
.end())
219 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
220 mFilename
% line
), __FILE__
, __LINE__
);
222 high
= lexical_cast
<int>((*tok_iter
));
223 modifiers
[Modifiers::playerfights
] = Range(high
, low
);
226 if (tok_iter
== tokens
.end())
228 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
229 mFilename
% line
), __FILE__
, __LINE__
);
231 low
= lexical_cast
<int>((*tok_iter
));
233 if (tok_iter
== tokens
.end())
235 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
236 mFilename
% line
), __FILE__
, __LINE__
);
238 high
= lexical_cast
<int>((*tok_iter
));
239 modifiers
[Modifiers::gold
] = Range(high
, low
);
242 if (tok_iter
== tokens
.end())
244 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
245 mFilename
% line
), __FILE__
, __LINE__
);
247 low
= lexical_cast
<int>((*tok_iter
));
249 if (tok_iter
== tokens
.end())
251 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
252 mFilename
% line
), __FILE__
, __LINE__
);
254 high
= lexical_cast
<int>((*tok_iter
));
255 modifiers
[Modifiers::bank
] = Range(high
, low
);
260 shared_ptr
<ItemGO
> FileItemDAO::CreateItemFromLine(const string
&line
) const
262 assert(!line
.empty());
263 shared_ptr
<ItemGO
> spItem
;
264 map
<Modifiers::Modifier
, Range
> modifiers
;
265 boost::char_separator
<char> sep("~", 0, boost::keep_empty_tokens
);
266 tokenizer
tokens(line
, sep
);
267 tokenizer::iterator tok_iter
= tokens
.begin();
269 ItemTypes::ItemType type
;
273 type
= ItemTypes::Parse((*tok_iter
));
275 catch (TypeException
)
277 return shared_ptr
<ItemGO
>();
282 case ItemTypes::Weapon
:
283 spItem
= shared_ptr
<WeaponGO
>(new WeaponGO());
285 case ItemTypes::Armor
:
286 spItem
= shared_ptr
<ArmorGO
>(new ArmorGO());
288 case ItemTypes::Potion
:
289 return CreatePotionFromLine(line
);
292 throw DataLayerException(str(format("Unknown Item type: %1%") % type
), __FILE__
, __LINE__
);
295 if (tok_iter
== tokens
.end())
297 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
298 mFilename
% line
), __FILE__
, __LINE__
);
300 spItem
->Id(FileId::CreateItemId((*tok_iter
)));
303 if (tok_iter
== tokens
.end())
305 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
306 mFilename
% line
), __FILE__
, __LINE__
);
308 spItem
->Name((*tok_iter
));
310 if (tok_iter
== tokens
.end())
312 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
313 mFilename
% line
), __FILE__
, __LINE__
);
315 spItem
->Price(lexical_cast
<unsigned long int>((*tok_iter
)));
317 if (tok_iter
== tokens
.end())
319 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
320 mFilename
% line
), __FILE__
, __LINE__
);
322 spItem
->Uses(lexical_cast
<int>((*tok_iter
)));
324 if (tok_iter
== tokens
.end())
326 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
327 mFilename
% line
), __FILE__
, __LINE__
);
329 modifiers
[Modifiers::strength
] = Range(lexical_cast
<int>((*tok_iter
)), lexical_cast
<int>((*tok_iter
)));
331 if (tok_iter
== tokens
.end())
333 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
334 mFilename
% line
), __FILE__
, __LINE__
);
336 modifiers
[Modifiers::defense
] = Range(lexical_cast
<int>((*tok_iter
)), lexical_cast
<int>((*tok_iter
)));
338 if (tok_iter
== tokens
.end())
340 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
341 mFilename
% line
), __FILE__
, __LINE__
);
343 modifiers
[Modifiers::maxhealth
] = Range(lexical_cast
<int>((*tok_iter
)), lexical_cast
<int>((*tok_iter
)));
345 if (tok_iter
== tokens
.end())
347 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
348 mFilename
% line
), __FILE__
, __LINE__
);
350 modifiers
[Modifiers::health
] = Range(lexical_cast
<int>((*tok_iter
)), lexical_cast
<int>((*tok_iter
)));
352 if (tok_iter
== tokens
.end())
356 modifiers
[Modifiers::forestfights
] = Range(lexical_cast
<int>((*tok_iter
)), lexical_cast
<int>((*tok_iter
)));
358 if (tok_iter
== tokens
.end())
362 modifiers
[Modifiers::playerfights
] = Range(lexical_cast
<int>((*tok_iter
)), lexical_cast
<int>((*tok_iter
)));
364 if (tok_iter
== tokens
.end())
368 modifiers
[Modifiers::gold
] = Range(lexical_cast
<int>((*tok_iter
)), lexical_cast
<int>((*tok_iter
)));
370 if (tok_iter
== tokens
.end())
374 modifiers
[Modifiers::bank
] = Range(lexical_cast
<int>((*tok_iter
)), lexical_cast
<int>((*tok_iter
)));
375 spItem
->Modifiers(modifiers
);
379 bool FileItemDAO::IdExists(const string
&Id
) const
381 map
<string
, shared_ptr
<ItemGO
> >::const_iterator iter
;
382 iter
= spItemCache
.find(Id
);
383 return (iter
!= spItemCache
.end());
386 void FileItemDAO::LoadItemCache(void)
389 infile
.open(mFilename
.c_str());
393 throw DataLayerException(str(format("Unable to open %1% for reading") % mFilename
), __FILE__
, __LINE__
);
396 while (getline(infile
, line
))
398 if (line
.length() == 0)
406 shared_ptr
<ItemGO
> spItem
= CreateItemFromLine(line
);
409 spItemCache
[spItem
->Id()] = spItem
;