source: network-game/server/DataAccess.cpp@ 1785314

Last change on this file since 1785314 was 7ca5d21, checked in by dportnoy <dmp1488@…>, 11 years ago

The server can handle loading a NULL class from the db

  • Property mode set to 100644
File size: 4.4 KB
Line 
1#include "DataAccess.h"
2
3#include <iostream>
4#include <sstream>
5#include <cstdlib>
6
7using namespace std;
8
9DataAccess::DataAccess()
10{
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;
18}
19
20DataAccess::~DataAccess()
21{
22 mysql_close(connection);
23 mysql_close(&mysql);
24}
25
26int DataAccess::insertPlayer(string username, string password, Player::PlayerClass playerClass)
27{
28 ostringstream oss;
29
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
48 oss << "'" << username << "', '" << encrypted << "', " << playerClass;
49
50 return insert("users", "name, password, class", oss.str());
51}
52
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
64Player *DataAccess::getPlayer(string username)
65{
66 MYSQL_RES *result;
67 MYSQL_ROW row;
68 Player *p;
69 ostringstream oss;
70
71 oss << "name='" << username << "'";
72
73 result = select("users", oss.str().c_str());
74
75 cout << "Got result" << endl;
76
77 if (result == NULL) {
78 cout << "Error occured" << endl;
79 cout << mysql_error(connection) << endl;
80 return NULL;
81 }
82
83 if ( ( row = mysql_fetch_row(result)) != NULL ) {
84 cout << "Creating a new player" << endl;
85 p = new Player(string(row[1]), string(row[2]));
86 if (row[3] == NULL) {
87 p->setClass(Player::CLASS_NONE);
88 cout << "Class from db was NULL" << endl;
89 }else {
90 p->setClass((Player::PlayerClass)atoi(row[3]));
91 cout << "Class from db: " << atoi(row[3]) << endl;
92 }
93 cout << "Player class: " << p->playerClass << endl;
94 cout << "Created new player" << endl;
95 }else {
96 cout << "Returned no results for some reason" << endl;
97 p = NULL;
98 }
99
100 mysql_free_result(result);
101
102 return p;
103}
104
105// need to make sure this list is freed
106// since we need to create a DataAccess class
107// when calling these functions,
108// we could free this list in the destructor
109list<Player*>* DataAccess::getPlayers()
110{
111 MYSQL_RES *result;
112 MYSQL_ROW row;
113 ostringstream oss;
114
115 result = select("users", "");
116
117 if (result == NULL) {
118 cout << mysql_error(connection) << endl;
119 return NULL;
120 }
121
122 list<Player*>* lstPlayers = new list<Player*>();
123 while ( ( row = mysql_fetch_row(result)) != NULL ) {
124 cout << row[0] << ", " << row[1] << ", " << row[2] << endl;
125 lstPlayers->push_back(new Player(row[1], row[2]));
126 }
127
128 mysql_free_result(result);
129
130 return lstPlayers;
131}
132
133bool DataAccess::verifyPassword(string password, string encrypted)
134{
135 string test(crypt(password.c_str(), encrypted.c_str()));
136
137 return encrypted.compare(test) == 0;
138}
139
140int DataAccess::insert(string table, string columns, string values)
141{
142 int query_state;
143 ostringstream oss;
144
145 oss << "INSERT into " << table << " (" << columns << ") VALUES (" << values << ")";
146 cout << "query: " << oss.str() << endl;
147
148 query_state = mysql_query(connection, oss.str().c_str());
149
150 if (query_state != 0) {
151 cout << mysql_error(connection) << endl;
152 return 1;
153 }
154
155 return 0;
156}
157
158int DataAccess::update(string table, string values, string where)
159{
160 int query_state;
161 ostringstream oss;
162
163 oss << "UPDATE " << table << " SET " << values << " WHERE " << where;
164 cout << "query: " << oss.str() << endl;
165
166 query_state = mysql_query(connection, oss.str().c_str());
167
168 if (query_state != 0) {
169 cout << mysql_error(connection) << endl;
170 return 1;
171 }
172
173 return 0;
174}
175
176MYSQL_RES *DataAccess::select(string table, string filter)
177{
178 MYSQL_RES *result;
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}
Note: See TracBrowser for help on using the repository browser.