Changeset ca44f82 in network-game for server/DataAccess.cpp
- Timestamp:
- Feb 24, 2013, 1:28:32 AM (12 years ago)
- Branches:
- master
- Children:
- 7b43385
- Parents:
- 3a79253 (diff), 8f85180 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
server/DataAccess.cpp
r3a79253 rca44f82 3 3 #include <iostream> 4 4 #include <sstream> 5 #include <cstdlib> 5 6 6 7 using namespace std; … … 27 28 ostringstream oss; 28 29 29 oss << "'" << username << "', '" << password << "'"; 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 << "'"; 30 49 31 50 return insert("users", "name, password", oss.str()); 51 } 52 53 int 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()); 32 62 } 33 63 … … 51 81 } 52 82 53 if ( ( row = mysql_fetch_row(result)) != NULL ) 83 if ( ( row = mysql_fetch_row(result)) != NULL ) { 84 cout << "Creating a new player" << endl; 54 85 p = new Player(string(row[1]), string(row[2])); 55 else {86 }else { 56 87 cout << "Returned no results for some reason" << endl; 57 88 p = NULL; … … 63 94 } 64 95 65 int DataAccess::printPlayers() 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 100 list<Player*>* DataAccess::getPlayers() 66 101 { 67 102 MYSQL_RES *result; … … 73 108 if (result == NULL) { 74 109 cout << mysql_error(connection) << endl; 75 return 1;110 return NULL; 76 111 } 77 112 113 list<Player*>* lstPlayers = new list<Player*>(); 78 114 while ( ( row = mysql_fetch_row(result)) != NULL ) { 79 115 cout << row[0] << ", " << row[1] << ", " << row[2] << endl; 116 lstPlayers->push_back(new Player(row[1], row[2])); 80 117 } 81 118 82 119 mysql_free_result(result); 83 120 84 return 0;121 return lstPlayers; 85 122 } 86 123 87 int DataAccess::insert(string table, string rows, string values) 124 bool 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 131 int DataAccess::insert(string table, string columns, string values) 88 132 { 89 133 int query_state; 90 134 ostringstream oss; 91 135 92 oss << "INSERT into " << table << " (" << rows << ") VALUES (" << values << ")"; 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 149 int 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; 93 155 cout << "query: " << oss.str() << endl; 94 156
Note:
See TracChangeset
for help on using the changeset viewer.