source: network-game/server/DataAccess.cpp@ 84754c0

Last change on this file since 84754c0 was 84754c0, checked in by Dmitry Portnoy <dmp1488@…>, 10 years ago

Change updatePlayer to take a Player object and update many fields of that object

  • Property mode set to 100644
File size: 8.6 KB
Line 
1#include "DataAccess.h"
2
3#include <iostream>
4#include <sstream>
5#include <cstdlib>
6#include <crypt.h>
7
8#include "LuaLoader.h"
9
10using namespace std;
11
12DataAccess::DataAccess()
13{
14 LuaLoader luaLoader;
15
16 string database, username, password;
17
18 if (luaLoader.runScript("db_settings.lua")) {
19 cout << "Loading settings" << endl;
20
21 database = luaLoader.getValue("database");
22 username = luaLoader.getValue("username");
23 password = luaLoader.getValue("password");
24
25 cout << database << endl;
26 cout << username << endl;
27 cout << password << endl;
28 } else {
29 cout << "Failed to load settings from lua script" << endl;
30 }
31
32 mysql_init(&mysql);
33 connection = mysql_real_connect(&mysql, "localhost", "pythonAdmin", "pyMaster09*", "pythondb", 0, 0, 0);
34
35 if (connection == NULL) {
36 cout << mysql_error(&mysql) << endl;
37 }else
38 cout << "Connection successful" << endl;
39}
40
41DataAccess::~DataAccess()
42{
43 mysql_close(connection);
44 mysql_close(&mysql);
45}
46
47int DataAccess::insertPlayer(string username, string password, Player::PlayerClass playerClass)
48{
49 ostringstream oss;
50
51 string salt = "$1$";
52 int random;
53 char chr;
54 for(int i=0; i<8; i++)
55 {
56 random = rand() % 62;
57 if (random < 26)
58 chr = (char)('a'+random);
59 else if (random < 52)
60 chr = (char)('A'+random-26);
61 else
62 chr = (char)('0'+random-52);
63 salt += chr;
64 }
65 salt += '$';
66
67 string encrypted(crypt(password.c_str(), salt.c_str()));
68
69 oss << "'" << username << "', '" << encrypted << "', " << playerClass;
70
71 return insert("users", "name, password, class", oss.str());
72}
73
74int DataAccess::updatePlayer(Player* p)
75{
76 ostringstream values, where;
77
78 values << "level=" << p->level << ", experience=" << p->experience << ", honor=" << p->honor << ", wins=" << p->wins << ", losses=" << p->losses << "";
79
80 where << "id=" << p->getId() << "";
81
82 return update("users", values.str(), where.str());
83}
84
85Player *DataAccess::getPlayer(string username)
86{
87 MYSQL_RES *result;
88 MYSQL_ROW row;
89 Player *p;
90 ostringstream oss;
91
92 oss << "name='" << username << "'";
93
94 result = select("users", oss.str().c_str());
95
96 cout << "Got result" << endl;
97
98 if (result == NULL) {
99 cout << "Error occured" << endl;
100 cout << mysql_error(connection) << endl;
101 return NULL;
102 }
103
104 if ( ( row = mysql_fetch_row(result)) != NULL ) {
105 cout << "Creating a new player" << endl;
106 p = new Player(string(row[1]), string(row[2]));
107 p->setId(atoi(row[0]));
108 if (row[3] == NULL) {
109 p->setClass(Player::CLASS_NONE);
110 cout << "Class from db was NULL" << endl;
111 }else {
112 p->setClass((Player::PlayerClass)atoi(row[3]));
113 cout << "Class from db: " << atoi(row[3]) << endl;
114 }
115 p->level = atoi(row[4]);
116 p->experience = atoi(row[5]);
117 p->honor = atoi(row[6]);
118 p->wins = atoi(row[7]);
119 p->losses = atoi(row[8]);
120 cout << "Player class: " << p->playerClass << endl;
121 cout << "level: " << p->level << endl;
122 cout << "experience: " << p->experience << endl;
123 cout << "honor: " << p->honor << endl;
124 cout << "wins: " << p->wins << endl;
125 cout << "losses: " << p->losses << endl;
126 cout << "Loaded player from db" << endl;
127 }else {
128 cout << "Returned no results for some reason" << endl;
129 p = NULL;
130 }
131
132 mysql_free_result(result);
133
134 return p;
135}
136
137// need to make sure this list is freed
138// since we need to create a DataAccess class
139// when calling these functions,
140// we could free this list in the destructor
141list<Player*>* DataAccess::getPlayers()
142{
143 // This method doesn't seem to ever get used. Decide whether it's actually needed
144 MYSQL_RES *result;
145 MYSQL_ROW row;
146
147 result = select("users", "");
148
149 if (result == NULL) {
150 cout << mysql_error(connection) << endl;
151 return NULL;
152 }
153
154 list<Player*>* lstPlayers = new list<Player*>();
155 while ( ( row = mysql_fetch_row(result)) != NULL ) {
156 cout << row[0] << ", " << row[1] << ", " << row[2] << endl;
157 lstPlayers->push_back(new Player(row[1], row[2]));
158
159 // need to assign all the other db values to the player
160 }
161
162 mysql_free_result(result);
163
164 return lstPlayers;
165}
166
167bool DataAccess::verifyPassword(string password, string encrypted)
168{
169 string test(crypt(password.c_str(), encrypted.c_str()));
170
171 return encrypted.compare(test) == 0;
172}
173
174int* DataAccess::getPlayerRecord(int playerId) {
175 MYSQL_RES *result;
176 MYSQL_ROW row;
177 ostringstream oss;
178 int* record = new int[5];
179
180 oss << "id=" << playerId;
181 result = select("users", oss.str());
182
183 if ( ( row = mysql_fetch_row(result)) != NULL ) {
184 cout << "Retrieved player record successfully" << endl;
185 record[0] = atoi(row[4]); // level
186 record[1] = atoi(row[5]); // experience
187 record[2] = atoi(row[6]); // honor
188 record[3] = atoi(row[7]); // wins
189 record[4] = atoi(row[8]); // losses
190 cout << "record[0]:" << record[0] << endl;
191 cout << "record[1]:" << record[1] << endl;
192 cout << "record[2]:" << record[2] << endl;
193 cout << "record[3]:" << record[3] << endl;
194 cout << "record[4]:" << record[4] << endl;
195 }
196
197 if (result == NULL) {
198 cout << mysql_error(connection) << endl;
199 return NULL;
200 }
201
202 mysql_free_result(result);
203
204 return record;
205}
206
207int** DataAccess::getPlayerGameHistory(int playerId, unsigned int& numGames)
208{
209 // each array is the score for one game
210 // the columns are result, team, blue score, and red score
211 // for result 0 is defeat and 1 is victory
212 // for team, 0 is blue and 1 is red
213
214 MYSQL_RES *result;
215 MYSQL_ROW row;
216 ostringstream oss;
217
218 int** gameHistory;
219
220 oss << "user_id=" << playerId;
221 result = select("gameHistory", oss.str());
222
223 numGames = mysql_num_rows(result);
224 gameHistory = (int**)malloc(sizeof(int*)*numGames);
225 cout << "Result has " << numGames << " rows" << endl;
226
227 int i=0;
228 while ( ( row = mysql_fetch_row(result)) != NULL ) {
229 gameHistory[i] = new int[4];
230
231 int userTeam = atoi(row[2]);
232 int blueScore = atoi(row[4]);
233 int redScore = atoi(row[3]);
234 int gameResult = -1;
235
236 if (blueScore == 3) {
237 if (userTeam == 0)
238 gameResult = 1;
239 else
240 gameResult = 0;
241 }else if (redScore == 3) {
242 if (userTeam == 1)
243 gameResult = 1;
244 else
245 gameResult = 0;
246 }else {
247 cout << "Recorded game has no team with 3 points" << endl;
248 }
249
250 gameHistory[i][0] = gameResult;
251 gameHistory[i][1] = userTeam;
252 gameHistory[i][2] = blueScore;
253 gameHistory[i][3] = redScore;
254
255 i++;
256 }
257
258 if (result == NULL) {
259 cout << mysql_error(connection) << endl;
260 return NULL;
261 }
262
263 mysql_free_result(result);
264
265 return gameHistory;
266}
267
268int DataAccess::saveGameHistory(int playerId, int team, int blueScore, int redScore)
269{
270 ostringstream oss;
271
272 cout << "Saving game to db" << endl;
273 oss << playerId << ", " << team << ", " << blueScore << ", " << redScore;
274
275 return insert("gameHistory", "user_id, user_team, blue_score, red_score", oss.str());
276}
277
278int DataAccess::insert(string table, string columns, string values)
279{
280 int query_state;
281 ostringstream oss;
282
283 if (connection == NULL) {
284 cout << "Error: non database connection exists" << endl;
285 return -1;
286 }
287
288 oss << "INSERT into " << table << " (" << columns << ") VALUES (" << values << ")";
289 cout << "query: " << oss.str() << endl;
290
291 query_state = mysql_query(connection, oss.str().c_str());
292
293 if (query_state != 0) {
294 cout << mysql_error(connection) << endl;
295 return -1;
296 }
297
298 return 0;
299}
300
301int DataAccess::update(string table, string values, string where)
302{
303 int query_state;
304 ostringstream oss;
305
306 if (connection == NULL) {
307 cout << "Error: no database connection exists" << endl;
308 return -1;
309 }
310
311 oss << "UPDATE " << table << " SET " << values << " WHERE " << where;
312 cout << "query: " << oss.str() << endl;
313
314 query_state = mysql_query(connection, oss.str().c_str());
315
316
317 if (query_state != 0) {
318 cout << mysql_error(connection) << endl;
319 return -1;
320 }
321
322 return 0;
323}
324
325MYSQL_RES *DataAccess::select(string table, string filter)
326{
327 int query_state;
328 ostringstream oss;
329
330 if (connection == NULL) {
331 cout << "Error: non database connection exists" << endl;
332 return NULL;
333 }
334
335 oss << "SELECT * FROM " << table;
336 if (!filter.empty())
337 oss << " WHERE " << filter;
338 cout << "executing select query: " << oss.str() << endl;
339
340 query_state = mysql_query(connection, oss.str().c_str());
341
342 if (query_state != 0) {
343 cout << mysql_error(connection) << endl;
344 return NULL;
345 }
346
347 return mysql_store_result(connection);
348}
Note: See TracBrowser for help on using the repository browser.