]> jfr.im git - irc/gameservirc.git/blob - gameserv-2.0/libgameservgldl/src/DataLayer/File/FileItemDAO.cpp
aa0e2a2b1c5c5c69c6f2a40c45e9af73cd1c0560
[irc/gameservirc.git] / 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;
11
12 #include <GameServ/DataLayer/DataLayerExceptions.h>
13 using GameServ::DataLayer::Exceptions::ResourceException;
14 using GameServ::DataLayer::Exceptions::DataLayerException;
15
16 #include <GameServ/DataLayer/File/FileId.h>
17 using GameServ::DataLayer::File::FileId;
18
19 #include <GameServ/Types.h>
20 using GameServ::Types::ItemTypes;
21 using GameServ::Types::Modifiers;
22 using GameServ::Types::Exceptions::TypeException;
23
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;
29 using boost::format;
30 using boost::str;
31 using boost::algorithm::to_upper;
32 using boost::lexical_cast;
33 #include <boost/tokenizer.hpp>
34 typedef boost::tokenizer<boost::char_separator<char> >
35 tokenizer;
36
37 #include <string>
38 using std::string;
39
40 #include <fstream>
41 using std::ifstream;
42 using std::getline;
43 using std::ofstream;
44 using std::endl;
45
46 #include <map>
47 using std::map;
48
49 // TODO: Switch filename to be loaded from a config file
50 FileItemDAO::FileItemDAO() : mFilename("data\\items\\masteritems.dat")
51 {
52 Initialize(GetItemFilePath());
53 }
54
55 FileItemDAO::FileItemDAO(const string &filename)
56 {
57 Initialize(filename);
58 }
59
60 FileItemDAO::~FileItemDAO()
61 {
62 }
63
64 void FileItemDAO::Initialize(const string &filename)
65 {
66 mFilename = filename;
67 LoadItemCache();
68 }
69
70 string FileItemDAO::GetItemFilePath() const
71 {
72 return mFilename;
73 }
74
75 shared_ptr<ItemGO> FileItemDAO::GetById(const string &Id) const
76 {
77 map<string, shared_ptr<ItemGO> >::const_iterator iter;
78 iter = spItemCache.find(Id);
79 if (iter != spItemCache.end())
80 {
81 return shared_ptr<ItemGO>(iter->second->Clone());
82 }
83 else
84 {
85 return shared_ptr<ItemGO>();
86 }
87 }
88
89 shared_ptr<ItemGO> FileItemDAO::CreateItemFromLine(const string &line) const
90 {
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();
97
98 ItemTypes::ItemType type;
99
100 try
101 {
102 type = ItemTypes::Parse((*tok_iter));
103 }
104 catch (TypeException)
105 {
106 return shared_ptr<ItemGO>();
107 }
108
109 switch(type)
110 {
111 case ItemTypes::Weapon:
112 spItem = shared_ptr<WeaponGO>(new WeaponGO());
113 break;
114 case ItemTypes::Armor:
115 spItem = shared_ptr<ArmorGO>(new ArmorGO());
116 break;
117 case ItemTypes::Potion:
118 spItem = shared_ptr<PotionGO>(new PotionGO());
119 break;
120 default:
121 throw DataLayerException(str(format("Unknown Item type: %1%") % type), __FILE__, __LINE__);
122 }
123 tok_iter++;
124 if (tok_iter == tokens.end())
125 {
126 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
127 mFilename % line), __FILE__, __LINE__);
128 }
129 spItem->Id(FileId::CreateItemId((*tok_iter)));
130
131 tok_iter++;
132 if (tok_iter == tokens.end())
133 {
134 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
135 mFilename % line), __FILE__, __LINE__);
136 }
137 spItem->Name((*tok_iter));
138 tok_iter++;
139 if (tok_iter == tokens.end())
140 {
141 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
142 mFilename % line), __FILE__, __LINE__);
143 }
144 spItem->Price(lexical_cast<unsigned long int>((*tok_iter)));
145 tok_iter++;
146 if (tok_iter == tokens.end())
147 {
148 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
149 mFilename % line), __FILE__, __LINE__);
150 }
151 spItem->Uses(lexical_cast<int>((*tok_iter)));
152 tok_iter++;
153 if (tok_iter == tokens.end())
154 {
155 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
156 mFilename % line), __FILE__, __LINE__);
157 }
158 modifiers[Modifiers::strength] = lexical_cast<int>((*tok_iter));
159 tok_iter++;
160 if (tok_iter == tokens.end())
161 {
162 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
163 mFilename % line), __FILE__, __LINE__);
164 }
165 modifiers[Modifiers::defense] = lexical_cast<int>((*tok_iter));
166 tok_iter++;
167 if (tok_iter == tokens.end())
168 {
169 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
170 mFilename % line), __FILE__, __LINE__);
171 }
172 modifiers[Modifiers::maxhp] = lexical_cast<int>((*tok_iter));
173 tok_iter++;
174 if (tok_iter == tokens.end())
175 {
176 throw DataLayerException(str(format("Corrupt %1% file. Missing information on line %2%") %
177 mFilename % line), __FILE__, __LINE__);
178 }
179 modifiers[Modifiers::hp] = lexical_cast<int>((*tok_iter));
180 tok_iter++;
181 if (tok_iter == tokens.end())
182 {
183 return spItem;
184 }
185 modifiers[Modifiers::forestfights] = lexical_cast<int>((*tok_iter));
186 tok_iter++;
187 if (tok_iter == tokens.end())
188 {
189 return spItem;
190 }
191 modifiers[Modifiers::playerfights] = lexical_cast<int>((*tok_iter));
192 tok_iter++;
193 if (tok_iter == tokens.end())
194 {
195 return spItem;
196 }
197 modifiers[Modifiers::gold] = lexical_cast<int>((*tok_iter));
198 tok_iter++;
199 if (tok_iter == tokens.end())
200 {
201 return spItem;
202 }
203 modifiers[Modifiers::bank] = lexical_cast<int>((*tok_iter));
204 spItem->Modifiers(modifiers);
205
206 return spItem;
207 }
208 bool FileItemDAO::IdExists(const string &Id) const
209 {
210 map<string, shared_ptr<ItemGO> >::const_iterator iter;
211 iter = spItemCache.find(Id);
212 return (iter != spItemCache.end());
213 }
214
215 void FileItemDAO::LoadItemCache(void)
216 {
217 ifstream infile;
218 infile.open(mFilename.c_str());
219
220 if (infile.fail())
221 {
222 throw DataLayerException(str(format("Unable to open %1% for reading") % mFilename), __FILE__, __LINE__);
223 }
224 string line;
225 while (getline(infile, line))
226 {
227 if (line.length() == 0)
228 {
229 continue;
230 }
231 if (line[0] == '#')
232 {
233 continue;
234 }
235 shared_ptr<ItemGO> spItem = CreateItemFromLine(line);
236 if (spItem != 0)
237 {
238 spItemCache[spItem->Id()] = spItem;
239 }
240 }
241 infile.close();
242 }