source: network-game/server/DataAccess.cpp@ 521c88b

Last change on this file since 521c88b was 521c88b, checked in by dportnoy <dmp1488@…>, 12 years ago

Upon player registration, the server stores the selected player class and no longer generates a random player class every time the player logs in

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