YAPOG  0.0.1
Yet Another Pokemon Online Game
AccountManager.cpp
Go to the documentation of this file.
7 
16 
23 
27 
29 
30 #include "Pokemon/PokemonTeam.hpp"
31 #include "Pokemon/Pokemon.hpp"
32 
33 namespace yse
34 {
36  {
37  yap::ID staticID = yap::ID (yap::RandomHelper::GetNext (1, 10));
38 
39  if (staticID == yap::ID (10))
40  staticID = yap::ID (16);
41 
42  int level = yap::RandomHelper::GetNext (1, 100);
43  level = 1;
44  Pokemon* p = new Pokemon (staticID, level, false);
46 
47  return p;
48  }
49 
51  : databaseManager_ (dm)
52  , accounts_ ()
53  {
54  }
55 
57  {
58  for (const auto& it : accounts_)
59  delete it.second;
60  }
61 
63  const yap::String& name,
64  const yap::String& password,
65  const yap::String& email,
66  const yap::String& creationIP)
67  {
68  AccountTable accountTable;
69 
70  // Hash password
71  yap::String hashedPassword = EncodePassword (password);
72 
73  accountTable.SetName (name);
74  accountTable.SetPassword (hashedPassword);
75  accountTable.SetEmail (email);
76  accountTable.SetCreationIP (creationIP);
77  AccountInsertRequest ia (accountTable);
78 
79  try
80  {
82 
83  if (ia.Insert (databaseManager_))
84  {
85  std::cout << "A new accout has been created ! ("
86  << name << ")" << std::endl;
87 
88  PlayerDataTable playerDataTable (ia.GetID ());
89  PlayerDataInsertRequest ipd (playerDataTable);
90 
91  if (ipd.Insert (databaseManager_))
92  {
93  std::cout << "Player data have been created !" << std::endl;
94 
95  // Add first Pokemon
96  for (int i = 1; i <= 5; i++)
97  {
99 
100  // Insertion of the current Pokemon's basics information
101  PokemonTable pokemonTable;
102  pokemonTable.accountID_ = ia.GetID ();
103  pokemonTable.LoadFromPokemon (*p);
104  pokemonTable.boxIndex_ = yap::ID (i);
105  PokemonInsertRequest pokemonInsert (pokemonTable);
106 
107  if (pokemonInsert.Insert (databaseManager_))
108  {
109  // Insertion of the current Pokemon's EV
110  PokemonEVTable pokemonEVTable;
111  pokemonEVTable.LoadFromPokemon (*p);
112  pokemonEVTable.pokemonID_ = pokemonInsert.GetID ();
113  PokemonEVInsertRequest pokemonEVInsert (pokemonEVTable);
114 
115  // Insertion of the current Pokemon's IV
116  PokemonIVTable pokemonIVTable;
117  pokemonIVTable.LoadFromPokemon (*p);
118  pokemonIVTable.pokemonID_ = pokemonInsert.GetID ();
119  PokemonIVInsertRequest pokemonIVInsert (pokemonIVTable);
120 
121  if (pokemonIVInsert.Insert (databaseManager_)
122  && pokemonEVInsert.Insert (databaseManager_))
123  {
124  for (int i = 0; i < 4; i++)
125  {
126  // Insertion of the current Pokemon's moves
127  PokemonMoveTable pokemonMoveTable;
128  pokemonMoveTable.LoadFromPokemon (*p, yap::ID(i));
129  pokemonMoveTable.pokemonID_ = pokemonInsert.GetID ();
130  PokemonMoveInsertRequest pokemonStatsInsert (pokemonMoveTable);
131 
132  pokemonStatsInsert.Insert (databaseManager_);
133  }
134 
135  std::cout << "A random Pokemon have been added to the player !"
136  << std::endl;
137  }
138  }
139  }
140 
141  trans.Commit ();
142 
143  return true;
144  }
145  }
146  }
147  catch (yap::Exception e)
148  {
149  e.GetMessage (std::cout);
150  }
151 
152  return false;
153  }
154 
156  const yap::String& name,
157  const yap::String& password,
158  const yap::String& current_ip)
159  {
160  std::cout << "Login of \"" << name
161  << "\" (pass: \"" << password << "\") !" << std::endl;
162 
163  AccountTable accountTable;
164  AccountSelectRequest asr (databaseManager_, name, accountTable);
165 
166  accountTable.DisplayData ();
167 
168  yap::String hashPassword = EncodePassword (password);
169 
170  // Check if this is the corresponding password
171  if (accountTable.GetPassword () != hashPassword)
172  throw yap::Exception ("Wrong password !");
173 
174  /*
175  // Check if the account is already logged in
176  if (accountTable.IsLogged ())
177  throw yap::Exception ("A person is already using this account !");
178  */
179 
180  // Get player data
181  PlayerDataTable playerDataTable (accountTable.GetID ());
182  PlayerDataSelectRequest pdsr (databaseManager_, playerDataTable);
183 
184  playerDataTable.DisplayData ();
185 
186  // Record the login IP
187  /*
188  accountTable.SetCurrentIP (current_ip);
189 
190  yap::String queryString =
191  "UPDATE account SET "
192  "account_current_ip = :currentIp, "
193  "account_last_login_date = NOW () "
194  " WHERE account_name = :name";
195 
196  yap::DatabaseStream queryUpdateCurrentIp (
197  queryString,
198  databaseManager_.GetConnection ());
199 
200  queryUpdateCurrentIp.Write (current_ip);
201  queryUpdateCurrentIp.Write (name);
202 
203  std::cout
204  << "This account is now in use for the "
205  << "server and the database !" << std::endl;
206  */
207 
208  Account* account = new Account ();
209  account->LoadFromTable (accountTable, playerDataTable);
210 
211  PokemonTeam* pokemonTeam;
212  PokemonSelectRequest selectPokemon (databaseManager_);
213  pokemonTeam = selectPokemon.SelectPokemonTeam (
214  account->GetName (), account->GetID ());
215 
216  account->SetTeam (pokemonTeam);
217 
218  return account;
219  /*
220  if (account->IsLogged ())
221  std::cout << "This account is logged !" << std::endl;
222 
223  accounts_.Add (name, account);
224  */
225  }
226 
228  {
229  yap::String current_account;
230 
231  try
232  {
233  yap::DatabaseStream accounts (
234  "SELECT account_name "
235  "FROM account",
237 
238  while (!accounts.EndOfStream ())
239  {
240  current_account = accounts.ReadString ();
241  std::cout << current_account << std::endl;
242  }
243 
244  std::cout << accounts.AffectedRows ()
245  << " account(s) found !" << std::endl;
246  }
247  catch (pgs::pg_excpt e)
248  {
249  std::cerr << e.errmsg () << std::endl;
250  }
251  }
252 
254  {
255  /*
256  for (const auto& sa : accounts_)
257  std::cout << sa.second->GetName () << std::endl;
258  */
259  }
260 
262  {
263  if (accounts_.Contains (name))
264  return *accounts_[name];
265  else
266  YAPOG_THROW("This account doesn't exist !");
267  }
268 
270  const yap::String& password)
271  {
272  yap::Md5 md5;
273  yap::String hashedPassword = md5.Calculate (password);
274 
275  return hashedPassword;
276  }
277 
279  {
280  if (!accounts_.Contains (name))
281  throw yap::Exception ("This account doesn't log in !");
282 
283  yap::String queryString =
284  "UPDATE account "
285  "SET account_current_ip = NULL "
286  "WHERE account_name = :name";
287 
288  yap::DatabaseStream update
289  (queryString, databaseManager_.GetConnection ());
290 
291  update.Write (name);
292 
293  accounts_.Remove (name);
294  std::cout << name << " is now disconnected !" << std::endl;
295  }
296 } // namespace yse