[36082e8] | 1 | #include "DataAccess.h"
|
---|
| 2 |
|
---|
| 3 | #include <iostream>
|
---|
[59061f6] | 4 | #include <sstream>
|
---|
[b128109] | 5 | #include <cstdlib>
|
---|
[b72ed16] | 6 | #include <crypt.h>
|
---|
[36082e8] | 7 |
|
---|
[53643ca] | 8 | #include "LuaLoader.h"
|
---|
| 9 |
|
---|
[36082e8] | 10 | using namespace std;
|
---|
| 11 |
|
---|
| 12 | DataAccess::DataAccess()
|
---|
| 13 | {
|
---|
[53643ca] | 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 |
|
---|
[59061f6] | 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;
|
---|
[36082e8] | 39 | }
|
---|
| 40 |
|
---|
| 41 | DataAccess::~DataAccess()
|
---|
| 42 | {
|
---|
[59061f6] | 43 | mysql_close(connection);
|
---|
| 44 | mysql_close(&mysql);
|
---|
[36082e8] | 45 | }
|
---|
| 46 |
|
---|
[521c88b] | 47 | int DataAccess::insertPlayer(string username, string password, Player::PlayerClass playerClass)
|
---|
[59061f6] | 48 | {
|
---|
| 49 | ostringstream oss;
|
---|
| 50 |
|
---|
[b128109] | 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 |
|
---|
[521c88b] | 69 | oss << "'" << username << "', '" << encrypted << "', " << playerClass;
|
---|
[59061f6] | 70 |
|
---|
[521c88b] | 71 | return insert("users", "name, password, class", oss.str());
|
---|
[59061f6] | 72 | }
|
---|
| 73 |
|
---|
[84754c0] | 74 | int DataAccess::updatePlayer(Player* p)
|
---|
[b128109] | 75 | {
|
---|
| 76 | ostringstream values, where;
|
---|
| 77 |
|
---|
[84754c0] | 78 | values << "level=" << p->level << ", experience=" << p->experience << ", honor=" << p->honor << ", wins=" << p->wins << ", losses=" << p->losses << "";
|
---|
[b128109] | 79 |
|
---|
[84754c0] | 80 | where << "id=" << p->getId() << "";
|
---|
[b128109] | 81 |
|
---|
| 82 | return update("users", values.str(), where.str());
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[59061f6] | 85 | Player *DataAccess::getPlayer(string username)
|
---|
[36082e8] | 86 | {
|
---|
| 87 | MYSQL_RES *result;
|
---|
| 88 | MYSQL_ROW row;
|
---|
[59061f6] | 89 | Player *p;
|
---|
| 90 | ostringstream oss;
|
---|
[36082e8] | 91 |
|
---|
[59061f6] | 92 | oss << "name='" << username << "'";
|
---|
[36082e8] | 93 |
|
---|
[59061f6] | 94 | result = select("users", oss.str().c_str());
|
---|
[36082e8] | 95 |
|
---|
[41ad8ed] | 96 | cout << "Got result" << endl;
|
---|
| 97 |
|
---|
[59061f6] | 98 | if (result == NULL) {
|
---|
[41ad8ed] | 99 | cout << "Error occured" << endl;
|
---|
[59061f6] | 100 | cout << mysql_error(connection) << endl;
|
---|
| 101 | return NULL;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[60017fc] | 104 | if ( ( row = mysql_fetch_row(result)) != NULL ) {
|
---|
| 105 | cout << "Creating a new player" << endl;
|
---|
[59061f6] | 106 | p = new Player(string(row[1]), string(row[2]));
|
---|
[53643ca] | 107 | p->setId(atoi(row[0]));
|
---|
[7ca5d21] | 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 | }
|
---|
[84754c0] | 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]);
|
---|
[521c88b] | 120 | cout << "Player class: " << p->playerClass << endl;
|
---|
[84754c0] | 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;
|
---|
[53643ca] | 126 | cout << "Loaded player from db" << endl;
|
---|
[60017fc] | 127 | }else {
|
---|
[41ad8ed] | 128 | cout << "Returned no results for some reason" << endl;
|
---|
[59061f6] | 129 | p = NULL;
|
---|
[41ad8ed] | 130 | }
|
---|
[36082e8] | 131 |
|
---|
[59061f6] | 132 | mysql_free_result(result);
|
---|
[36082e8] | 133 |
|
---|
[59061f6] | 134 | return p;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[b128109] | 137 | bool DataAccess::verifyPassword(string password, string encrypted)
|
---|
| 138 | {
|
---|
| 139 | string test(crypt(password.c_str(), encrypted.c_str()));
|
---|
| 140 |
|
---|
| 141 | return encrypted.compare(test) == 0;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[53643ca] | 144 | int* DataAccess::getPlayerRecord(int playerId) {
|
---|
| 145 | MYSQL_RES *result;
|
---|
| 146 | MYSQL_ROW row;
|
---|
| 147 | ostringstream oss;
|
---|
| 148 | int* record = new int[5];
|
---|
| 149 |
|
---|
| 150 | oss << "id=" << playerId;
|
---|
| 151 | result = select("users", oss.str());
|
---|
| 152 |
|
---|
| 153 | if ( ( row = mysql_fetch_row(result)) != NULL ) {
|
---|
| 154 | cout << "Retrieved player record successfully" << endl;
|
---|
| 155 | record[0] = atoi(row[4]); // level
|
---|
| 156 | record[1] = atoi(row[5]); // experience
|
---|
| 157 | record[2] = atoi(row[6]); // honor
|
---|
| 158 | record[3] = atoi(row[7]); // wins
|
---|
| 159 | record[4] = atoi(row[8]); // losses
|
---|
| 160 | cout << "record[0]:" << record[0] << endl;
|
---|
| 161 | cout << "record[1]:" << record[1] << endl;
|
---|
| 162 | cout << "record[2]:" << record[2] << endl;
|
---|
| 163 | cout << "record[3]:" << record[3] << endl;
|
---|
| 164 | cout << "record[4]:" << record[4] << endl;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | if (result == NULL) {
|
---|
| 168 | cout << mysql_error(connection) << endl;
|
---|
| 169 | return NULL;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | mysql_free_result(result);
|
---|
| 173 |
|
---|
| 174 | return record;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | int** DataAccess::getPlayerGameHistory(int playerId, unsigned int& numGames)
|
---|
| 178 | {
|
---|
| 179 | // each array is the score for one game
|
---|
[426fb84] | 180 | // the columns are result, team, blue score, red score, and time the game ended
|
---|
[53643ca] | 181 | // for result 0 is defeat and 1 is victory
|
---|
[7fa452f] | 182 | // for team, 1 is blue and 2 is red
|
---|
[53643ca] | 183 |
|
---|
| 184 | MYSQL_RES *result;
|
---|
| 185 | MYSQL_ROW row;
|
---|
| 186 | ostringstream oss;
|
---|
| 187 |
|
---|
| 188 | int** gameHistory;
|
---|
| 189 |
|
---|
| 190 | oss << "user_id=" << playerId;
|
---|
| 191 | result = select("gameHistory", oss.str());
|
---|
| 192 |
|
---|
| 193 | numGames = mysql_num_rows(result);
|
---|
| 194 | gameHistory = (int**)malloc(sizeof(int*)*numGames);
|
---|
| 195 | cout << "Result has " << numGames << " rows" << endl;
|
---|
| 196 |
|
---|
| 197 | int i=0;
|
---|
| 198 | while ( ( row = mysql_fetch_row(result)) != NULL ) {
|
---|
[426fb84] | 199 | gameHistory[i] = new int[5];
|
---|
[53643ca] | 200 |
|
---|
| 201 | int userTeam = atoi(row[2]);
|
---|
[426fb84] | 202 | int blueScore = atoi(row[3]);
|
---|
| 203 | int redScore = atoi(row[4]);
|
---|
| 204 | time_t timeFinished = atoi(row[5]);
|
---|
[53643ca] | 205 | int gameResult = -1;
|
---|
| 206 |
|
---|
| 207 | if (blueScore == 3) {
|
---|
[7fa452f] | 208 | if (userTeam == 1)
|
---|
[53643ca] | 209 | gameResult = 1;
|
---|
| 210 | else
|
---|
| 211 | gameResult = 0;
|
---|
| 212 | }else if (redScore == 3) {
|
---|
[7fa452f] | 213 | if (userTeam == 2)
|
---|
[53643ca] | 214 | gameResult = 1;
|
---|
| 215 | else
|
---|
| 216 | gameResult = 0;
|
---|
| 217 | }else {
|
---|
| 218 | cout << "Recorded game has no team with 3 points" << endl;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | gameHistory[i][0] = gameResult;
|
---|
| 222 | gameHistory[i][1] = userTeam;
|
---|
| 223 | gameHistory[i][2] = blueScore;
|
---|
| 224 | gameHistory[i][3] = redScore;
|
---|
[426fb84] | 225 | gameHistory[i][4] = timeFinished;
|
---|
[53643ca] | 226 |
|
---|
| 227 | i++;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | if (result == NULL) {
|
---|
| 231 | cout << mysql_error(connection) << endl;
|
---|
| 232 | return NULL;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | mysql_free_result(result);
|
---|
| 236 |
|
---|
| 237 | return gameHistory;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[426fb84] | 240 | int DataAccess::saveGameHistory(int playerId, int team, int blueScore, int redScore, time_t timeFinished)
|
---|
[53643ca] | 241 | {
|
---|
| 242 | ostringstream oss;
|
---|
| 243 |
|
---|
| 244 | cout << "Saving game to db" << endl;
|
---|
[426fb84] | 245 | oss << playerId << ", " << team << ", " << blueScore << ", " << redScore << ", " << timeFinished;
|
---|
[53643ca] | 246 |
|
---|
[426fb84] | 247 | return insert("gameHistory", "user_id, user_team, blue_score, red_score, time_finished", oss.str());
|
---|
[53643ca] | 248 | }
|
---|
| 249 |
|
---|
[b128109] | 250 | int DataAccess::insert(string table, string columns, string values)
|
---|
| 251 | {
|
---|
| 252 | int query_state;
|
---|
| 253 | ostringstream oss;
|
---|
| 254 |
|
---|
[95d7add] | 255 | if (connection == NULL) {
|
---|
| 256 | cout << "Error: non database connection exists" << endl;
|
---|
| 257 | return -1;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
[b128109] | 260 | oss << "INSERT into " << table << " (" << columns << ") VALUES (" << values << ")";
|
---|
| 261 | cout << "query: " << oss.str() << endl;
|
---|
| 262 |
|
---|
| 263 | query_state = mysql_query(connection, oss.str().c_str());
|
---|
| 264 |
|
---|
| 265 | if (query_state != 0) {
|
---|
| 266 | cout << mysql_error(connection) << endl;
|
---|
[53643ca] | 267 | return -1;
|
---|
[b128109] | 268 | }
|
---|
| 269 |
|
---|
[59061f6] | 270 | return 0;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
[b128109] | 273 | int DataAccess::update(string table, string values, string where)
|
---|
[59061f6] | 274 | {
|
---|
| 275 | int query_state;
|
---|
| 276 | ostringstream oss;
|
---|
| 277 |
|
---|
[95d7add] | 278 | if (connection == NULL) {
|
---|
[53643ca] | 279 | cout << "Error: no database connection exists" << endl;
|
---|
[95d7add] | 280 | return -1;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[b128109] | 283 | oss << "UPDATE " << table << " SET " << values << " WHERE " << where;
|
---|
[59061f6] | 284 | cout << "query: " << oss.str() << endl;
|
---|
| 285 |
|
---|
| 286 | query_state = mysql_query(connection, oss.str().c_str());
|
---|
| 287 |
|
---|
[84754c0] | 288 |
|
---|
[59061f6] | 289 | if (query_state != 0) {
|
---|
| 290 | cout << mysql_error(connection) << endl;
|
---|
[53643ca] | 291 | return -1;
|
---|
[59061f6] | 292 | }
|
---|
[36082e8] | 293 |
|
---|
| 294 | return 0;
|
---|
| 295 | }
|
---|
[59061f6] | 296 |
|
---|
| 297 | MYSQL_RES *DataAccess::select(string table, string filter)
|
---|
| 298 | {
|
---|
| 299 | int query_state;
|
---|
| 300 | ostringstream oss;
|
---|
| 301 |
|
---|
[95d7add] | 302 | if (connection == NULL) {
|
---|
| 303 | cout << "Error: non database connection exists" << endl;
|
---|
| 304 | return NULL;
|
---|
| 305 | }
|
---|
| 306 |
|
---|
[59061f6] | 307 | oss << "SELECT * FROM " << table;
|
---|
| 308 | if (!filter.empty())
|
---|
| 309 | oss << " WHERE " << filter;
|
---|
[53643ca] | 310 | cout << "executing select query: " << oss.str() << endl;
|
---|
[59061f6] | 311 |
|
---|
| 312 | query_state = mysql_query(connection, oss.str().c_str());
|
---|
| 313 |
|
---|
| 314 | if (query_state != 0) {
|
---|
| 315 | cout << mysql_error(connection) << endl;
|
---|
| 316 | return NULL;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | return mysql_store_result(connection);
|
---|
| 320 | }
|
---|