Changeset b128109 in network-game for server/DataAccess.cpp
- Timestamp:
- Jan 29, 2013, 7:34:29 PM (12 years ago)
- Branches:
- master
- Children:
- 60b77d2
- Parents:
- 5806dc2
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
server/DataAccess.cpp
r5806dc2 rb128109 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 … … 63 93 } 64 94 65 int DataAccess::printPlayers() 95 // need to make sure this list is freed 96 // since we need to create a DataAccess class 97 // when calling these functions, 98 // we could free this list in the destructor 99 list<Player*>* DataAccess::getPlayers() 66 100 { 67 101 MYSQL_RES *result; … … 73 107 if (result == NULL) { 74 108 cout << mysql_error(connection) << endl; 75 return 1;109 return NULL; 76 110 } 77 111 112 list<Player*>* lstPlayers = new list<Player*>(); 78 113 while ( ( row = mysql_fetch_row(result)) != NULL ) { 79 114 cout << row[0] << ", " << row[1] << ", " << row[2] << endl; 115 lstPlayers->push_back(new Player(row[1], row[2])); 80 116 } 81 117 82 118 mysql_free_result(result); 83 119 84 return 0;120 return lstPlayers; 85 121 } 86 122 87 int DataAccess::insert(string table, string rows, string values) 123 bool DataAccess::verifyPassword(string password, string encrypted) 124 { 125 string test(crypt(password.c_str(), encrypted.c_str())); 126 127 return encrypted.compare(test) == 0; 128 } 129 130 int DataAccess::insert(string table, string columns, string values) 88 131 { 89 132 int query_state; 90 133 ostringstream oss; 91 134 92 oss << "INSERT into " << table << " (" << rows << ") VALUES (" << values << ")"; 135 oss << "INSERT into " << table << " (" << columns << ") VALUES (" << values << ")"; 136 cout << "query: " << oss.str() << endl; 137 138 query_state = mysql_query(connection, oss.str().c_str()); 139 140 if (query_state != 0) { 141 cout << mysql_error(connection) << endl; 142 return 1; 143 } 144 145 return 0; 146 } 147 148 int DataAccess::update(string table, string values, string where) 149 { 150 int query_state; 151 ostringstream oss; 152 153 oss << "UPDATE " << table << " SET " << values << " WHERE " << where; 93 154 cout << "query: " << oss.str() << endl; 94 155
Note:
See TracChangeset
for help on using the changeset viewer.