- 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. - Location:
- server
- Files:
-
- 4 deleted
- 4 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 -
server/DataAccess.h
r3a79253 rca44f82 3 3 4 4 #include <string> 5 #include <list> 5 6 6 7 #include <mysql/mysql.h> … … 16 17 17 18 int insertPlayer(string username, string password); 19 int updatePlayer(string username, string password); 18 20 19 Player *getPlayer(string username); 20 int printPlayers(); 21 Player* getPlayer(string username); 22 list<Player*>* getPlayers(); 23 bool verifyPassword(string encrypted, string password); 21 24 22 25 int insert(string table, string rows, string values); 26 int update(string table, string values, string where); 23 27 MYSQL_RES *select(string table, string filter); 24 28 -
server/makefile
r3a79253 rca44f82 1 1 CC = g++ 2 LIB_FLAGS = -lssl -lmysqlclient 2 LIB_FLAGS = -lssl -lmysqlclient -lcrypt -lrt 3 3 FLAGS = $(LIB_FLAGS) 4 4 COMMON_PATH = ../common 5 DEPENDENCIES = Common.o Message.o Player.o DataAccess.o5 DEPENDENCIES = Common.o Message.o Player.o WorldMap.o DataAccess.o 6 6 7 7 server : server.cpp $(DEPENDENCIES) … … 17 17 $(CC) -c -o $@ $? 18 18 19 WorldMap.o : $(COMMON_PATH)/WorldMap.cpp 20 $(CC) -c -o $@ $? 21 19 22 %.o : %.cpp 20 23 $(CC) -c -o $@ $? -
server/server.cpp
r3a79253 rca44f82 6 6 #include <sstream> 7 7 #include <cstring> 8 #include <cmath> 8 9 9 10 #include <vector> … … 14 15 #include <netinet/in.h> 15 16 #include <arpa/inet.h> 17 18 #include <crypt.h> 16 19 17 20 /* … … 24 27 #include "../common/Common.h" 25 28 #include "../common/Message.h" 29 #include "../common/WorldMap.h" 26 30 #include "../common/Player.h" 27 31 … … 30 34 using namespace std; 31 35 32 bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, unsigned int& unusedId, NETWORK_MSG &serverMsg);33 34 void updateUnusedId(unsigned int& id );36 bool processMessage(const NETWORK_MSG &clientMsg, const struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG &serverMsg); 37 38 void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers); 35 39 36 40 // this should probably go somewhere in the common folder … … 94 98 NETWORK_MSG clientMsg, serverMsg; 95 99 map<unsigned int, Player> mapPlayers; 96 unsigned int unusedId = 0;100 unsigned int unusedId = 1; 97 101 98 102 //SSL_load_error_strings(); … … 104 108 exit(1); 105 109 } 110 111 WorldMap* gameMap = NULL; //WorldMap::createDefaultMap(); 106 112 107 113 sock = socket(AF_INET, SOCK_DGRAM, 0); … … 127 133 cout << "Got a message" << endl; 128 134 129 broadcastResponse = processMessage(clientMsg, from, mapPlayers, unusedId, serverMsg); 130 135 broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedId, serverMsg); 136 137 // probably replace this with a function that prints based on the 138 // message type 131 139 cout << "msg: " << serverMsg.buffer << endl; 132 140 cout << "broadcastResponse: " << broadcastResponse << endl; 133 141 if (broadcastResponse) 134 142 { … … 136 144 137 145 map<unsigned int, Player>::iterator it; 138 139 146 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 140 147 { … … 151 158 } 152 159 160 // update player positions 161 map<unsigned int, Player>::iterator it; 162 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) 163 { 164 it->second.move(); 165 } 166 153 167 broadcastPlayerPositions(mapPlayers, sock); 154 168 } … … 158 172 } 159 173 160 bool processMessage(const NETWORK_MSG& clientMsg, const struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, unsigned int& unusedId, NETWORK_MSG& serverMsg)174 bool processMessage(const NETWORK_MSG& clientMsg, const struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedId, NETWORK_MSG& serverMsg) 161 175 { 162 176 DataAccess da; … … 195 209 case MSG_TYPE_LOGIN: 196 210 { 211 cout << "Got login message" << endl; 212 197 213 string username(clientMsg.buffer); 198 214 string password(strchr(clientMsg.buffer, '\0')+1); 199 cout << "Player logging in: " << username << endl;200 215 201 216 Player* p = da.getPlayer(username); 202 217 203 if (p == NULL || p->password != password)218 if (p == NULL || !da.verifyPassword(password, p->password)) 204 219 { 205 220 strcpy(serverMsg.buffer, "Incorrect username or password"); … … 212 227 { 213 228 p->setAddr(from); 229 updateUnusedId(unusedId, mapPlayers); 214 230 p->id = unusedId; 215 231 mapPlayers[unusedId] = *p; 216 updateUnusedId(unusedId); 217 218 strcpy(serverMsg.buffer, "Login successful. Enjoy chatting with other players.");232 233 // sendd back the new player info to the user 234 p->serialize(serverMsg.buffer); 219 235 } 220 236 … … 235 251 { 236 252 strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server."); 253 cout << "Player not logged in" << endl; 237 254 } 238 255 else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr || … … 240 257 { 241 258 strcpy(serverMsg.buffer, "That player is logged in using a differemt connection. This is either a bug, or you're trying to hack the server."); 259 cout << "Player logged in using a different connection" << endl; 242 260 } 243 261 else … … 247 265 mapPlayers.erase(p->id); 248 266 strcpy(serverMsg.buffer, "You have successfully logged out."); 249 } 267 cout << "Player logged out successfuly" << endl; 268 } 269 270 // should really be serverMsg.type = MSG_TYPE_LOGOUT; 271 serverMsg.type = MSG_TYPE_LOGIN; 250 272 251 273 break; … … 265 287 broadcastResponse = true; 266 288 267 stringstreamss;268 ss << p->name << ": " << clientMsg.buffer;269 270 strcpy(serverMsg.buffer, ss.str().c_str());289 ostringstream oss; 290 oss << p->name << ": " << clientMsg.buffer; 291 292 strcpy(serverMsg.buffer, oss.str().c_str()); 271 293 } 272 294 … … 275 297 break; 276 298 } 299 case MSG_TYPE_PLAYER_MOVE: 300 { 301 cout << "Got a move message" << endl; 302 303 istringstream iss; 304 iss.str(clientMsg.buffer); 305 306 cout << "PLAYER_MOVE" << endl; 307 308 int id, x, y; 309 310 memcpy(&id, clientMsg.buffer, 4); 311 memcpy(&x, clientMsg.buffer+4, 4); 312 memcpy(&y, clientMsg.buffer+8, 4); 313 314 cout << "x: " << x << endl; 315 cout << "y: " << y << endl; 316 cout << "id: " << id << endl; 317 318 if ( mapPlayers[id].addr.sin_addr.s_addr == from.sin_addr.s_addr && 319 mapPlayers[id].addr.sin_port == from.sin_port ) 320 { 321 // we need to make sure the player can move here 322 if (0 <= x && x < 300 && 0 <= y && y < 300 && 323 gameMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS) 324 { 325 // first we get the correct vector 326 mapPlayers[id].target.x = x; 327 mapPlayers[id].target.y = y; 328 int xDiff = mapPlayers[id].target.x - mapPlayers[id].pos.x; 329 int yDiff = mapPlayers[id].target.y - mapPlayers[id].pos.y; 330 cout << "xDiff: " << xDiff << endl; 331 cout << "yDiff: " << yDiff << endl; 332 333 // then we get the correct angle 334 double angle = atan2(yDiff, xDiff); 335 cout << "angle: " << angle << endl; 336 337 // finally we use the angle to determine 338 // how much the player moves 339 // the player will move 50 pixels in the correct direction 340 mapPlayers[id].pos.x += cos(angle)*50; 341 mapPlayers[id].pos.y += sin(angle)*50; 342 cout << "new x: " << mapPlayers[id].pos.x << endl; 343 cout << "new y: " << mapPlayers[id].pos.y << endl; 344 345 serverMsg.type = MSG_TYPE_PLAYER_MOVE; 346 347 memcpy(serverMsg.buffer, &id, 4); 348 memcpy(serverMsg.buffer+4, &mapPlayers[id].pos.x, 4); 349 memcpy(serverMsg.buffer+8, &mapPlayers[id].pos.y, 4); 350 //memcpy(serverMsg.buffer, clientMsg.buffer, 12); 351 352 broadcastResponse = true; 353 } 354 else 355 cout << "Bad terrain detected" << endl; 356 } 357 else // nned to send back a message indicating failure 358 cout << "Player id (" << id << ") doesn't match sender" << endl; 359 360 break; 361 } 277 362 default: 278 363 { … … 285 370 } 286 371 372 cout << "Got to the end of the switch" << endl; 373 287 374 return broadcastResponse; 288 375 } 289 376 290 void updateUnusedId(unsigned int& id) 291 { 292 id = 5; 293 } 377 void updateUnusedId(unsigned int& id, map<unsigned int, Player>& mapPlayers) 378 { 379 while (mapPlayers.find(id) != mapPlayers.end()) 380 id++; 381 }
Note:
See TracChangeset
for help on using the changeset viewer.