source: network-game/server/DataAccess.cpp@ 7f2cef0

Last change on this file since 7f2cef0 was 60017fc, checked in by dportnoy <dmp1488@…>, 12 years ago

Added code for player movement

  • Property mode set to 100644
File size: 4.0 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)
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 << "'";
49
50 return insert("users", "name, password", 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 }else {
87 cout << "Returned no results for some reason" << endl;
88 p = NULL;
89 }
90
91 mysql_free_result(result);
92
93 return p;
94}
95
96// need to make sure this list is freed
97// since we need to create a DataAccess class
98// when calling these functions,
99// we could free this list in the destructor
100list<Player*>* DataAccess::getPlayers()
101{
102 MYSQL_RES *result;
103 MYSQL_ROW row;
104 ostringstream oss;
105
106 result = select("users", "");
107
108 if (result == NULL) {
109 cout << mysql_error(connection) << endl;
110 return NULL;
111 }
112
113 list<Player*>* lstPlayers = new list<Player*>();
114 while ( ( row = mysql_fetch_row(result)) != NULL ) {
115 cout << row[0] << ", " << row[1] << ", " << row[2] << endl;
116 lstPlayers->push_back(new Player(row[1], row[2]));
117 }
118
119 mysql_free_result(result);
120
121 return lstPlayers;
122}
123
124bool DataAccess::verifyPassword(string password, string encrypted)
125{
126 string test(crypt(password.c_str(), encrypted.c_str()));
127
128 return encrypted.compare(test) == 0;
129}
130
131int DataAccess::insert(string table, string columns, string values)
132{
133 int query_state;
134 ostringstream oss;
135
136 oss << "INSERT into " << table << " (" << columns << ") VALUES (" << values << ")";
137 cout << "query: " << oss.str() << endl;
138
139 query_state = mysql_query(connection, oss.str().c_str());
140
141 if (query_state != 0) {
142 cout << mysql_error(connection) << endl;
143 return 1;
144 }
145
146 return 0;
147}
148
149int DataAccess::update(string table, string values, string where)
150{
151 int query_state;
152 ostringstream oss;
153
154 oss << "UPDATE " << table << " SET " << values << " WHERE " << where;
155 cout << "query: " << oss.str() << endl;
156
157 query_state = mysql_query(connection, oss.str().c_str());
158
159 if (query_state != 0) {
160 cout << mysql_error(connection) << endl;
161 return 1;
162 }
163
164 return 0;
165}
166
167MYSQL_RES *DataAccess::select(string table, string filter)
168{
169 MYSQL_RES *result;
170 int query_state;
171 ostringstream oss;
172
173 oss << "SELECT * FROM " << table;
174 if (!filter.empty())
175 oss << " WHERE " << filter;
176
177 query_state = mysql_query(connection, oss.str().c_str());
178
179 if (query_state != 0) {
180 cout << mysql_error(connection) << endl;
181 return NULL;
182 }
183
184 return mysql_store_result(connection);
185}
Note: See TracBrowser for help on using the repository browser.