]> jfr.im git - irc/gameservirc.git/blob - gameserv-2.0/libgameservgldl/src/DataLayer/File/FilePlayerDAO.cpp
59e6e64755d86b1a0ebee0b3a2592e715178f87c
[irc/gameservirc.git] / 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/GameLayer/GameObjects/PlayerGO.h>
4 using GameServ::GameLayer::GameObjects::PlayerGO;
5
6 #include <GameServ/DataLayer/DataLayerExceptions.h>
7 using GameServ::DataLayer::Exceptions::ResourceException;
8 using GameServ::DataLayer::Exceptions::DataLayerException;
9
10 #include <boost/shared_ptr.hpp>
11 #include <boost/format.hpp>
12 #include <boost/algorithm/string.hpp>
13 #include <boost/lexical_cast.hpp>
14 using boost::shared_ptr;
15 using boost::format;
16 using boost::str;
17 using boost::algorithm::to_upper;
18 using boost::lexical_cast;
19 #include <boost/tokenizer.hpp>
20 typedef boost::tokenizer<boost::char_separator<char> >
21 tokenizer;
22
23 #include <string>
24 using std::string;
25
26 #include <fstream>
27 using std::ifstream;
28 using std::getline;
29
30
31
32 FilePlayerDAO::FilePlayerDAO() : mFilename("players.dat")
33 {
34 Initialize(GetPlayerFilePath());
35 }
36
37 FilePlayerDAO::FilePlayerDAO(const string &filename)
38 {
39 Initialize(filename);
40 }
41
42 FilePlayerDAO::~FilePlayerDAO()
43 {
44 }
45
46 void FilePlayerDAO::Initialize(const string &filename)
47 {
48 mFilename = filename;
49 }
50
51 string FilePlayerDAO::GetPlayerFilePath() const
52 {
53 return mFilename;
54 }
55
56 shared_ptr<PlayerGO> FilePlayerDAO::GetById(const string &Id) const
57 {
58 string line;
59 ifstream infile;
60 infile.open(GetPlayerFilePath().c_str());
61
62 if (infile.fail())
63 {
64 throw ResourceException(str(format("Unable to open file %1%") % GetPlayerFilePath()), __FILE__, __LINE__);
65 }
66
67
68 while (getline(infile, line))
69 {
70 shared_ptr<PlayerGO> spPlayer = CreatePlayerFromLine(line);
71 if (spPlayer->Id() == Id)
72 {
73 return spPlayer;
74 }
75 }
76 return shared_ptr<PlayerGO>();
77 }
78
79 shared_ptr<PlayerGO> FilePlayerDAO::CreatePlayerFromLine(const string &line) const
80 {
81 shared_ptr<PlayerGO> spPlayer;
82 spPlayer = shared_ptr<PlayerGO>(new PlayerGO());
83 boost::char_separator<char> sep(" ", 0, boost::keep_empty_tokens);
84 tokenizer tokens(line, sep);
85 tokenizer::iterator tok_iter = tokens.begin();
86
87 spPlayer->Level(lexical_cast<int>((*tok_iter)));
88 tok_iter++;
89 spPlayer->Experience(lexical_cast<unsigned long int>((*tok_iter)));
90 tok_iter++;
91 spPlayer->Gold(lexical_cast<unsigned long int>((*tok_iter)));
92 tok_iter++;
93 spPlayer->Bank(lexical_cast<unsigned long int>((*tok_iter)));
94 tok_iter++;
95 spPlayer->Health(lexical_cast<int>((*tok_iter)));
96 tok_iter++;
97 spPlayer->MaxHealth(lexical_cast<int>((*tok_iter)));
98 tok_iter++;
99 spPlayer->Strength(lexical_cast<int>((*tok_iter)));
100 tok_iter++;
101 spPlayer->Defense(lexical_cast<int>((*tok_iter)));
102 tok_iter++;
103 spPlayer->ForestFights(lexical_cast<int>((*tok_iter)));
104 tok_iter++;
105 spPlayer->PlayerFights(lexical_cast<int>((*tok_iter)));
106 tok_iter++;
107 int flags = lexical_cast<int>((*tok_iter));
108 SetFlags(spPlayer, flags);
109 tok_iter++;
110 spPlayer->Password((*tok_iter));
111
112 return spPlayer;
113 }
114
115 void FilePlayerDAO::SetFlags(shared_ptr<PlayerGO> spPlayer, const int &flags) const
116 {
117
118 }
119
120 void FilePlayerDAO::Update(shared_ptr<PlayerGO> spPlayer)
121 {
122 throw DataLayerException("Undefined Function", __FILE__, __LINE__);
123 }
124
125 void FilePlayerDAO::Insert(shared_ptr<PlayerGO> spPlayer)
126 {
127 throw DataLayerException("Undefined Function", __FILE__, __LINE__);
128 }
129
130 bool FilePlayerDAO::IdExists(const string &Id) const
131 {
132 throw DataLayerException("Undefined Function", __FILE__, __LINE__);
133 return false;
134 }
135
136 list<string> FilePlayerDAO::GetIdsByName(const string &Name) const
137 {
138 throw DataLayerException("Undefined Function", __FILE__, __LINE__);
139 }