[36082e8] | 1 | #include "DataAccess.h"
|
---|
| 2 |
|
---|
| 3 | #include <iostream>
|
---|
[59061f6] | 4 | #include <sstream>
|
---|
[b128109] | 5 | #include <cstdlib>
|
---|
[b72ed16] | 6 | #include <crypt.h>
|
---|
[36082e8] | 7 |
|
---|
| 8 | using namespace std;
|
---|
| 9 |
|
---|
| 10 | DataAccess::DataAccess()
|
---|
| 11 | {
|
---|
[59061f6] | 12 | mysql_init(&mysql);
|
---|
| 13 | connection = mysql_real_connect(&mysql, "localhost", "pythonAdmin", "pyMaster09*", "pythondb", 0, 0, 0);
|
---|
| 14 |
|
---|
| 15 | if (connection == NULL) {
|
---|
| 16 | cout << mysql_error(&mysql) << endl;
|
---|
| 17 | }else
|
---|
| 18 | cout << "Connection successful" << endl;
|
---|
[36082e8] | 19 | }
|
---|
| 20 |
|
---|
| 21 | DataAccess::~DataAccess()
|
---|
| 22 | {
|
---|
[59061f6] | 23 | mysql_close(connection);
|
---|
| 24 | mysql_close(&mysql);
|
---|
[36082e8] | 25 | }
|
---|
| 26 |
|
---|
[521c88b] | 27 | int DataAccess::insertPlayer(string username, string password, Player::PlayerClass playerClass)
|
---|
[59061f6] | 28 | {
|
---|
| 29 | ostringstream oss;
|
---|
| 30 |
|
---|
[b128109] | 31 | string salt = "$1$";
|
---|
| 32 | int random;
|
---|
| 33 | char chr;
|
---|
| 34 | for(int i=0; i<8; i++)
|
---|
| 35 | {
|
---|
| 36 | random = rand() % 62;
|
---|
| 37 | if (random < 26)
|
---|
| 38 | chr = (char)('a'+random);
|
---|
| 39 | else if (random < 52)
|
---|
| 40 | chr = (char)('A'+random-26);
|
---|
| 41 | else
|
---|
| 42 | chr = (char)('0'+random-52);
|
---|
| 43 | salt += chr;
|
---|
| 44 | }
|
---|
| 45 | salt += '$';
|
---|
| 46 |
|
---|
| 47 | string encrypted(crypt(password.c_str(), salt.c_str()));
|
---|
| 48 |
|
---|
[521c88b] | 49 | oss << "'" << username << "', '" << encrypted << "', " << playerClass;
|
---|
[59061f6] | 50 |
|
---|
[521c88b] | 51 | return insert("users", "name, password, class", oss.str());
|
---|
[59061f6] | 52 | }
|
---|
| 53 |
|
---|
[b128109] | 54 | int DataAccess::updatePlayer(string username, string password)
|
---|
| 55 | {
|
---|
| 56 | ostringstream values, where;
|
---|
| 57 |
|
---|
| 58 | values << "password='" << password << "'";
|
---|
| 59 |
|
---|
| 60 | where << "name='" << username << "'";
|
---|
| 61 |
|
---|
| 62 | return update("users", values.str(), where.str());
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[59061f6] | 65 | Player *DataAccess::getPlayer(string username)
|
---|
[36082e8] | 66 | {
|
---|
| 67 | MYSQL_RES *result;
|
---|
| 68 | MYSQL_ROW row;
|
---|
[59061f6] | 69 | Player *p;
|
---|
| 70 | ostringstream oss;
|
---|
[36082e8] | 71 |
|
---|
[59061f6] | 72 | oss << "name='" << username << "'";
|
---|
[36082e8] | 73 |
|
---|
[59061f6] | 74 | result = select("users", oss.str().c_str());
|
---|
[36082e8] | 75 |
|
---|
[41ad8ed] | 76 | cout << "Got result" << endl;
|
---|
| 77 |
|
---|
[59061f6] | 78 | if (result == NULL) {
|
---|
[41ad8ed] | 79 | cout << "Error occured" << endl;
|
---|
[59061f6] | 80 | cout << mysql_error(connection) << endl;
|
---|
| 81 | return NULL;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[60017fc] | 84 | if ( ( row = mysql_fetch_row(result)) != NULL ) {
|
---|
| 85 | cout << "Creating a new player" << endl;
|
---|
[59061f6] | 86 | p = new Player(string(row[1]), string(row[2]));
|
---|
[7ca5d21] | 87 | if (row[3] == NULL) {
|
---|
| 88 | p->setClass(Player::CLASS_NONE);
|
---|
| 89 | cout << "Class from db was NULL" << endl;
|
---|
| 90 | }else {
|
---|
| 91 | p->setClass((Player::PlayerClass)atoi(row[3]));
|
---|
| 92 | cout << "Class from db: " << atoi(row[3]) << endl;
|
---|
| 93 | }
|
---|
[521c88b] | 94 | cout << "Player class: " << p->playerClass << endl;
|
---|
[c76134b] | 95 | cout << "Created new player" << endl;
|
---|
[60017fc] | 96 | }else {
|
---|
[41ad8ed] | 97 | cout << "Returned no results for some reason" << endl;
|
---|
[59061f6] | 98 | p = NULL;
|
---|
[41ad8ed] | 99 | }
|
---|
[36082e8] | 100 |
|
---|
[59061f6] | 101 | mysql_free_result(result);
|
---|
[36082e8] | 102 |
|
---|
[59061f6] | 103 | return p;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[b128109] | 106 | // need to make sure this list is freed
|
---|
| 107 | // since we need to create a DataAccess class
|
---|
| 108 | // when calling these functions,
|
---|
| 109 | // we could free this list in the destructor
|
---|
| 110 | list<Player*>* DataAccess::getPlayers()
|
---|
[59061f6] | 111 | {
|
---|
| 112 | MYSQL_RES *result;
|
---|
| 113 | MYSQL_ROW row;
|
---|
| 114 | ostringstream oss;
|
---|
| 115 |
|
---|
| 116 | result = select("users", "");
|
---|
| 117 |
|
---|
| 118 | if (result == NULL) {
|
---|
[36082e8] | 119 | cout << mysql_error(connection) << endl;
|
---|
[b128109] | 120 | return NULL;
|
---|
[36082e8] | 121 | }
|
---|
| 122 |
|
---|
[b128109] | 123 | list<Player*>* lstPlayers = new list<Player*>();
|
---|
[36082e8] | 124 | while ( ( row = mysql_fetch_row(result)) != NULL ) {
|
---|
| 125 | cout << row[0] << ", " << row[1] << ", " << row[2] << endl;
|
---|
[b128109] | 126 | lstPlayers->push_back(new Player(row[1], row[2]));
|
---|
[36082e8] | 127 | }
|
---|
| 128 |
|
---|
| 129 | mysql_free_result(result);
|
---|
| 130 |
|
---|
[b128109] | 131 | return lstPlayers;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | bool DataAccess::verifyPassword(string password, string encrypted)
|
---|
| 135 | {
|
---|
| 136 | string test(crypt(password.c_str(), encrypted.c_str()));
|
---|
| 137 |
|
---|
| 138 | return encrypted.compare(test) == 0;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | int DataAccess::insert(string table, string columns, string values)
|
---|
| 142 | {
|
---|
| 143 | int query_state;
|
---|
| 144 | ostringstream oss;
|
---|
| 145 |
|
---|
| 146 | oss << "INSERT into " << table << " (" << columns << ") VALUES (" << values << ")";
|
---|
| 147 | cout << "query: " << oss.str() << endl;
|
---|
| 148 |
|
---|
| 149 | query_state = mysql_query(connection, oss.str().c_str());
|
---|
| 150 |
|
---|
| 151 | if (query_state != 0) {
|
---|
| 152 | cout << mysql_error(connection) << endl;
|
---|
| 153 | return 1;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[59061f6] | 156 | return 0;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[b128109] | 159 | int DataAccess::update(string table, string values, string where)
|
---|
[59061f6] | 160 | {
|
---|
| 161 | int query_state;
|
---|
| 162 | ostringstream oss;
|
---|
| 163 |
|
---|
[b128109] | 164 | oss << "UPDATE " << table << " SET " << values << " WHERE " << where;
|
---|
[59061f6] | 165 | cout << "query: " << oss.str() << endl;
|
---|
| 166 |
|
---|
| 167 | query_state = mysql_query(connection, oss.str().c_str());
|
---|
| 168 |
|
---|
| 169 | if (query_state != 0) {
|
---|
| 170 | cout << mysql_error(connection) << endl;
|
---|
| 171 | return 1;
|
---|
| 172 | }
|
---|
[36082e8] | 173 |
|
---|
| 174 | return 0;
|
---|
| 175 | }
|
---|
[59061f6] | 176 |
|
---|
| 177 | MYSQL_RES *DataAccess::select(string table, string filter)
|
---|
| 178 | {
|
---|
| 179 | int query_state;
|
---|
| 180 | ostringstream oss;
|
---|
| 181 |
|
---|
| 182 | oss << "SELECT * FROM " << table;
|
---|
| 183 | if (!filter.empty())
|
---|
| 184 | oss << " WHERE " << filter;
|
---|
| 185 |
|
---|
| 186 | query_state = mysql_query(connection, oss.str().c_str());
|
---|
| 187 |
|
---|
| 188 | if (query_state != 0) {
|
---|
| 189 | cout << mysql_error(connection) << endl;
|
---|
| 190 | return NULL;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | return mysql_store_result(connection);
|
---|
| 194 | }
|
---|