]>
jfr.im git - irc/gameservirc.git/blob - gameserv-2.0/libgameservgldl/src/DataLayer/File/FileItemDAO.cpp
aa0e2a2b1c5c5c69c6f2a40c45e9af73cd1c0560
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
>();
89 shared_ptr
<ItemGO
> FileItemDAO::CreateItemFromLine(const string
&line
) const
91 assert(!line
.empty());
92 shared_ptr
<ItemGO
> spItem
;
93 map
<Modifiers::Modifier
, int> modifiers
;
94 boost::char_separator
<char> sep("~", 0, boost::keep_empty_tokens
);
95 tokenizer
tokens(line
, sep
);
96 tokenizer::iterator tok_iter
= tokens
.begin();
98 ItemTypes::ItemType type
;
102 type
= ItemTypes::Parse((*tok_iter
));
104 catch (TypeException
)
106 return shared_ptr
<ItemGO
>();
111 case ItemTypes::Weapon
:
112 spItem
= shared_ptr
<WeaponGO
>(new WeaponGO());
114 case ItemTypes::Armor
:
115 spItem
= shared_ptr
<ArmorGO
>(new ArmorGO());
117 case ItemTypes::Potion
:
118 spItem
= shared_ptr
<PotionGO
>(new PotionGO());
121 throw DataLayerException(str(format("Unknown Item type: %1%") % type
), __FILE__
, __LINE__
);
124 if (tok_iter
== tokens
.end())
126 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
127 mFilename
% line
), __FILE__
, __LINE__
);
129 spItem
->Id(FileId::CreateItemId((*tok_iter
)));
132 if (tok_iter
== tokens
.end())
134 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
135 mFilename
% line
), __FILE__
, __LINE__
);
137 spItem
->Name((*tok_iter
));
139 if (tok_iter
== tokens
.end())
141 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
142 mFilename
% line
), __FILE__
, __LINE__
);
144 spItem
->Price(lexical_cast
<unsigned long int>((*tok_iter
)));
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 spItem
->Uses(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 modifiers
[Modifiers::strength
] = lexical_cast
<int>((*tok_iter
));
160 if (tok_iter
== tokens
.end())
162 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
163 mFilename
% line
), __FILE__
, __LINE__
);
165 modifiers
[Modifiers::defense
] = lexical_cast
<int>((*tok_iter
));
167 if (tok_iter
== tokens
.end())
169 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
170 mFilename
% line
), __FILE__
, __LINE__
);
172 modifiers
[Modifiers::maxhp
] = lexical_cast
<int>((*tok_iter
));
174 if (tok_iter
== tokens
.end())
176 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
177 mFilename
% line
), __FILE__
, __LINE__
);
179 modifiers
[Modifiers::hp
] = lexical_cast
<int>((*tok_iter
));
181 if (tok_iter
== tokens
.end())
185 modifiers
[Modifiers::forestfights
] = lexical_cast
<int>((*tok_iter
));
187 if (tok_iter
== tokens
.end())
191 modifiers
[Modifiers::playerfights
] = lexical_cast
<int>((*tok_iter
));
193 if (tok_iter
== tokens
.end())
197 modifiers
[Modifiers::gold
] = lexical_cast
<int>((*tok_iter
));
199 if (tok_iter
== tokens
.end())
203 modifiers
[Modifiers::bank
] = lexical_cast
<int>((*tok_iter
));
204 spItem
->Modifiers(modifiers
);
208 bool FileItemDAO::IdExists(const string
&Id
) const
210 map
<string
, shared_ptr
<ItemGO
> >::const_iterator iter
;
211 iter
= spItemCache
.find(Id
);
212 return (iter
!= spItemCache
.end());
215 void FileItemDAO::LoadItemCache(void)
218 infile
.open(mFilename
.c_str());
222 throw DataLayerException(str(format("Unable to open %1% for reading") % mFilename
), __FILE__
, __LINE__
);
225 while (getline(infile
, line
))
227 if (line
.length() == 0)
235 shared_ptr
<ItemGO
> spItem
= CreateItemFromLine(line
);
238 spItemCache
[spItem
->Id()] = spItem
;