[2488852] | 1 | #include <cstdlib>
|
---|
[371ce29] | 2 | #include <cstdio>
|
---|
[e3535b3] | 3 | #include <unistd.h>
|
---|
[2488852] | 4 | #include <string>
|
---|
[e3535b3] | 5 | #include <iostream>
|
---|
[3b1efcc] | 6 | #include <sstream>
|
---|
[d05086b] | 7 | #include <fstream>
|
---|
[edfd1d0] | 8 | #include <cstring>
|
---|
[60017fc] | 9 | #include <cmath>
|
---|
[371ce29] | 10 |
|
---|
[01d0d00] | 11 | #include <vector>
|
---|
| 12 | #include <map>
|
---|
| 13 |
|
---|
[d05086b] | 14 | #include <csignal>
|
---|
| 15 |
|
---|
[d211210] | 16 | #include <sys/time.h>
|
---|
| 17 |
|
---|
[73f75c1] | 18 | #include <sys/socket.h>
|
---|
[371ce29] | 19 | #include <netdb.h>
|
---|
[73f75c1] | 20 | #include <netinet/in.h>
|
---|
| 21 | #include <arpa/inet.h>
|
---|
| 22 |
|
---|
[b128109] | 23 | #include <crypt.h>
|
---|
| 24 |
|
---|
[edfd1d0] | 25 | /*
|
---|
[e3535b3] | 26 | #include <openssl/bio.h>
|
---|
| 27 | #include <openssl/ssl.h>
|
---|
| 28 | #include <openssl/err.h>
|
---|
[edfd1d0] | 29 | */
|
---|
[e3535b3] | 30 |
|
---|
[b53c6b3] | 31 | #include "../common/Compiler.h"
|
---|
[3b1efcc] | 32 | #include "../common/Common.h"
|
---|
[9b5d30b] | 33 | #include "../common/MessageProcessor.h"
|
---|
[60017fc] | 34 | #include "../common/WorldMap.h"
|
---|
[edfd1d0] | 35 | #include "../common/Player.h"
|
---|
[8dad966] | 36 | #include "../common/Projectile.h"
|
---|
[99afbb8] | 37 | #include "../common/Game.h"
|
---|
[c9f6a1c] | 38 | #include "../common/GameSummary.h"
|
---|
[b53c6b3] | 39 |
|
---|
[36082e8] | 40 | #include "DataAccess.h"
|
---|
[d2b411a] | 41 |
|
---|
[e3535b3] | 42 | using namespace std;
|
---|
| 43 |
|
---|
[d211210] | 44 | // from used to be const. Removed that so I could take a reference
|
---|
| 45 | // and use it to send messages
|
---|
[2e63b64] | 46 | void processMessage(const NETWORK_MSG& clientMsg, struct sockaddr_in& from, MessageProcessor& msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, unsigned int& unusedPlayerId);
|
---|
[f3fb980] | 47 |
|
---|
[95ffe57] | 48 | void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player*>& mapPlayers);
|
---|
| 49 | Player *findPlayerByName(map<unsigned int, Player*> &m, string name);
|
---|
| 50 | Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr);
|
---|
[8e540f4] | 51 |
|
---|
[f3fb980] | 52 | void quit(int sig);
|
---|
| 53 |
|
---|
| 54 | bool done;
|
---|
[d05086b] | 55 |
|
---|
[e3535b3] | 56 | int main(int argc, char *argv[])
|
---|
| 57 | {
|
---|
[8554263] | 58 | int sock, length;
|
---|
[e3535b3] | 59 | struct sockaddr_in server;
|
---|
[3b1efcc] | 60 | struct sockaddr_in from; // info of client sending the message
|
---|
[e084950] | 61 | NETWORK_MSG clientMsg, serverMsg;
|
---|
[9b5d30b] | 62 | MessageProcessor msgProcessor;
|
---|
[95ffe57] | 63 | map<unsigned int, Player*> mapPlayers;
|
---|
[8dad966] | 64 | map<unsigned int, Projectile> mapProjectiles;
|
---|
[f41a7f9] | 65 | map<string, Game*> mapGames;
|
---|
[d05c484] | 66 | unsigned int unusedPlayerId = 1;
|
---|
[d05086b] | 67 | ofstream outputLog;
|
---|
| 68 |
|
---|
| 69 | done = false;
|
---|
[b8601ee] | 70 |
|
---|
[d05086b] | 71 | signal(SIGINT, quit);
|
---|
| 72 |
|
---|
[edfd1d0] | 73 | //SSL_load_error_strings();
|
---|
| 74 | //ERR_load_BIO_strings();
|
---|
| 75 | //OpenSSL_add_all_algorithms();
|
---|
[e3535b3] | 76 |
|
---|
[95ffe57] | 77 | if (argc != 2)
|
---|
| 78 | {
|
---|
| 79 | cerr << "ERROR, expected server [domain] [port]" << endl;
|
---|
[73f75c1] | 80 | exit(1);
|
---|
[e3535b3] | 81 | }
|
---|
[60017fc] | 82 |
|
---|
[d05086b] | 83 | outputLog.open("server.log", ios::app);
|
---|
| 84 | outputLog << "Started server on " << getCurrentDateTimeString() << endl;
|
---|
| 85 |
|
---|
[371ce29] | 86 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
[5b1e31e] | 87 | if (sock < 0)
|
---|
| 88 | error("Opening socket");
|
---|
[e3535b3] | 89 | length = sizeof(server);
|
---|
| 90 | bzero(&server,length);
|
---|
| 91 | server.sin_family=AF_INET;
|
---|
| 92 | server.sin_port=htons(atoi(argv[1]));
|
---|
[2488852] | 93 | server.sin_addr.s_addr=INADDR_ANY;
|
---|
| 94 | if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
|
---|
[e084950] | 95 | error("binding");
|
---|
[73f75c1] | 96 |
|
---|
[371ce29] | 97 | set_nonblock(sock);
|
---|
| 98 |
|
---|
[8554263] | 99 | msgProcessor = MessageProcessor(sock, &outputLog);
|
---|
| 100 |
|
---|
[d211210] | 101 | timespec ts;
|
---|
[d05c484] | 102 | int timeLastUpdated = 0, curTime = 0;
|
---|
[95ffe57] | 103 | while (!done)
|
---|
| 104 | {
|
---|
[371ce29] | 105 | usleep(5000);
|
---|
| 106 |
|
---|
[d211210] | 107 | clock_gettime(CLOCK_REALTIME, &ts);
|
---|
[430c80e] | 108 | // make the number smaller so millis can fit in an int
|
---|
[d69eb32] | 109 | ts.tv_sec -= 1368000000;
|
---|
[430c80e] | 110 | curTime = ts.tv_sec*1000 + ts.tv_nsec/1000000;
|
---|
[d211210] | 111 |
|
---|
[95ffe57] | 112 | if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50)
|
---|
| 113 | {
|
---|
[d211210] | 114 | timeLastUpdated = curTime;
|
---|
| 115 |
|
---|
[8554263] | 116 | msgProcessor.cleanAckedMessages();
|
---|
| 117 | msgProcessor.resendUnackedMessages();
|
---|
[9b5d30b] | 118 |
|
---|
[95ffe57] | 119 | map<unsigned int, Player*>::iterator it;
|
---|
[11d21ee] | 120 |
|
---|
[ce2bb87] | 121 | cout << "Updating player targets and respawning dead players" << endl;
|
---|
| 122 |
|
---|
[11d21ee] | 123 | // set targets for all chasing players (or make them attack if they're close enough)
|
---|
[8554263] | 124 | // this should be moved into the games loop
|
---|
[95ffe57] | 125 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 126 | {
|
---|
[ffadc8e] | 127 | Player* p = it->second;
|
---|
| 128 |
|
---|
[c76134b] | 129 | // check if it's time to revive dead players
|
---|
[ffadc8e] | 130 | if (p->isDead)
|
---|
[95ffe57] | 131 | {
|
---|
[e1af80c] | 132 | cout << "Player is dead" << endl;
|
---|
[35f6097] | 133 |
|
---|
[ffadc8e] | 134 | if (getCurrentMillis() - p->timeDied >= 10000)
|
---|
[95ffe57] | 135 | {
|
---|
[ffadc8e] | 136 | p->isDead = false;
|
---|
[c76134b] | 137 |
|
---|
| 138 | POSITION spawnPos;
|
---|
| 139 |
|
---|
[ffadc8e] | 140 | switch (p->team)
|
---|
[95ffe57] | 141 | {
|
---|
[c76134b] | 142 | case 0:// blue team
|
---|
[35f6097] | 143 | spawnPos = p->currentGame->getMap()->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
---|
[c76134b] | 144 | break;
|
---|
| 145 | case 1:// red team
|
---|
[35f6097] | 146 | spawnPos = p->currentGame->getMap()->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
---|
[c76134b] | 147 | break;
|
---|
| 148 | default:
|
---|
| 149 | // should never go here
|
---|
| 150 | cout << "Error: Invalid team" << endl;
|
---|
| 151 | break;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | // spawn the player to the right of their flag location
|
---|
| 155 | spawnPos.x = (spawnPos.x+1) * 25 + 12;
|
---|
| 156 | spawnPos.y = spawnPos.y * 25 + 12;
|
---|
| 157 |
|
---|
[ffadc8e] | 158 | p->pos = spawnPos.toFloat();
|
---|
| 159 | p->target = spawnPos;
|
---|
| 160 | p->health = p->maxHealth;
|
---|
[c76134b] | 161 |
|
---|
| 162 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
[ffadc8e] | 163 | p->serialize(serverMsg.buffer);
|
---|
[c76134b] | 164 |
|
---|
[d05c484] | 165 | msgProcessor.broadcastMessage(serverMsg, p->currentGame->getPlayers());
|
---|
[c76134b] | 166 | }
|
---|
| 167 |
|
---|
| 168 | continue;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[ffadc8e] | 171 | if (p->currentGame != NULL) {
|
---|
| 172 | map<unsigned int, Player*> playersInGame = p->currentGame->getPlayers();
|
---|
[d998572] | 173 | if (p->updateTarget(playersInGame[p->targetPlayer]))
|
---|
[5b1e31e] | 174 | {
|
---|
[ffadc8e] | 175 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
| 176 | p->serialize(serverMsg.buffer);
|
---|
[d05c484] | 177 | msgProcessor.broadcastMessage(serverMsg, playersInGame);
|
---|
[11d21ee] | 178 | }
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[ce2bb87] | 182 | cout << "Processing players in a game" << endl;
|
---|
| 183 |
|
---|
[402cf86] | 184 | // process players currently in a game
|
---|
[e62b56c] | 185 | map<string, Game*>::iterator itGames;
|
---|
| 186 | Game* game = NULL;
|
---|
[ce2bb87] | 187 |
|
---|
[e5b96e2] | 188 | for (itGames = mapGames.begin(); itGames != mapGames.end();) {
|
---|
[d05c484] | 189 | game = itGames->second;
|
---|
| 190 | if (game->handleGameEvents()) {
|
---|
| 191 | // send a GAME_INFO message with 0 players to force clients to delete the game
|
---|
| 192 | int numPlayers = 0;
|
---|
| 193 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
| 194 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
| 195 | strcpy(serverMsg.buffer+4, game->getName().c_str());
|
---|
| 196 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
| 197 |
|
---|
[f3fb980] | 198 | delete itGames->second;
|
---|
[df74597] | 199 | mapGames.erase(itGames++);
|
---|
[e5b96e2] | 200 | }else
|
---|
| 201 | itGames++;
|
---|
[8dad966] | 202 | }
|
---|
| 203 |
|
---|
[ce2bb87] | 204 | cout << "Processing projectiles" << endl;
|
---|
| 205 |
|
---|
[8dad966] | 206 | // move all projectiles
|
---|
[483a2cb] | 207 | // see if this can be moved inside the game class
|
---|
[45734ff] | 208 | // this method can be moved when I add a MessageProcessor to the Game class
|
---|
[8dad966] | 209 | map<unsigned int, Projectile>::iterator itProj;
|
---|
[5ae8dca] | 210 | for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++) {
|
---|
| 211 | game = itGames->second;
|
---|
| 212 | for (itProj = game->getProjectiles().begin(); itProj != game->getProjectiles().end(); itProj++)
|
---|
[95ffe57] | 213 | {
|
---|
[5ae8dca] | 214 | cout << "About to call projectile move" << endl;
|
---|
| 215 | if (itProj->second.move(game->getPlayers()))
|
---|
[8dad966] | 216 | {
|
---|
[5ae8dca] | 217 | // send a REMOVE_PROJECTILE message
|
---|
| 218 | cout << "send a REMOVE_PROJECTILE message" << endl;
|
---|
| 219 | serverMsg.type = MSG_TYPE_REMOVE_PROJECTILE;
|
---|
| 220 | memcpy(serverMsg.buffer, &itProj->second.id, 4);
|
---|
| 221 | game->removeProjectile(itProj->second.id);
|
---|
[d05c484] | 222 | msgProcessor.broadcastMessage(serverMsg, game->getPlayers());
|
---|
[5ae8dca] | 223 |
|
---|
| 224 | Player* target = game->getPlayers()[itProj->second.target];
|
---|
[d05c484] | 225 | game->dealDamageToPlayer(target, itProj->second.damage);
|
---|
[8dad966] | 226 | }
|
---|
| 227 | }
|
---|
[d211210] | 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[8554263] | 231 | if (msgProcessor.receiveMessage(&clientMsg, &from) >= 0)
|
---|
[95ffe57] | 232 | {
|
---|
[2e63b64] | 233 | processMessage(clientMsg, from, msgProcessor, mapPlayers, mapGames, unusedPlayerId);
|
---|
[ce2bb87] | 234 |
|
---|
| 235 | cout << "Finished processing the message" << endl;
|
---|
[7b43385] | 236 | }
|
---|
[8e540f4] | 237 | }
|
---|
[371ce29] | 238 |
|
---|
[d05086b] | 239 | outputLog << "Stopped server on " << getCurrentDateTimeString() << endl;
|
---|
| 240 | outputLog.close();
|
---|
| 241 |
|
---|
[f41a7f9] | 242 | // delete all games
|
---|
| 243 | map<string, Game*>::iterator itGames;
|
---|
[95ffe57] | 244 | for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++)
|
---|
| 245 | {
|
---|
[f41a7f9] | 246 | delete itGames->second;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[95ffe57] | 249 | map<unsigned int, Player*>::iterator itPlayers;
|
---|
| 250 | for (itPlayers = mapPlayers.begin(); itPlayers != mapPlayers.end(); itPlayers++)
|
---|
| 251 | {
|
---|
| 252 | delete itPlayers->second;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
[8e540f4] | 255 | return 0;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
[2e63b64] | 258 | void processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, unsigned int& unusedPlayerId)
|
---|
[8e540f4] | 259 | {
|
---|
[f3fb980] | 260 | NETWORK_MSG serverMsg;
|
---|
[41ad8ed] | 261 | DataAccess da;
|
---|
| 262 |
|
---|
[9a4fa04] | 263 | cout << "Inside processMessage" << endl;
|
---|
| 264 |
|
---|
[b8cb03f] | 265 | cout << "Received message" << endl;
|
---|
[8e540f4] | 266 | cout << "MSG: type: " << clientMsg.type << endl;
|
---|
| 267 | cout << "MSG contents: " << clientMsg.buffer << endl;
|
---|
| 268 |
|
---|
| 269 | // Check that if an invalid message is sent, the client will correctly
|
---|
| 270 | // receive and display the response. Maybe make a special error msg type
|
---|
| 271 | switch(clientMsg.type)
|
---|
| 272 | {
|
---|
| 273 | case MSG_TYPE_REGISTER:
|
---|
[d2b411a] | 274 | {
|
---|
[8e540f4] | 275 | string username(clientMsg.buffer);
|
---|
| 276 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[521c88b] | 277 | Player::PlayerClass playerClass;
|
---|
| 278 |
|
---|
[4509648] | 279 | memcpy(&playerClass, clientMsg.buffer+username.length()+password.length()+2, 4);
|
---|
[c4c2a3c] | 280 |
|
---|
[8e540f4] | 281 | cout << "username: " << username << endl;
|
---|
| 282 | cout << "password: " << password << endl;
|
---|
[d2b411a] | 283 |
|
---|
[8554263] | 284 | bool validClass = false;
|
---|
| 285 |
|
---|
| 286 | switch(playerClass) {
|
---|
| 287 | case Player::CLASS_WARRIOR:
|
---|
| 288 | case Player::CLASS_RANGER:
|
---|
| 289 | validClass = true;
|
---|
| 290 | break;
|
---|
| 291 | default:
|
---|
| 292 | validClass = false;
|
---|
| 293 | break;
|
---|
[c4c2a3c] | 294 | }
|
---|
[521c88b] | 295 |
|
---|
[8554263] | 296 | serverMsg.type = MSG_TYPE_REGISTER;
|
---|
[41ad8ed] | 297 |
|
---|
[8554263] | 298 | if (validClass) {
|
---|
| 299 | int error = da.insertPlayer(username, password, playerClass);
|
---|
| 300 |
|
---|
| 301 | if (error)
|
---|
| 302 | strcpy(serverMsg.buffer, "Registration failed. Please try again.");
|
---|
| 303 | else
|
---|
| 304 | strcpy(serverMsg.buffer, "Registration successful.");
|
---|
| 305 | }else
|
---|
| 306 | strcpy(serverMsg.buffer, "You didn't select a class");
|
---|
| 307 |
|
---|
| 308 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[d2b411a] | 309 |
|
---|
[8e540f4] | 310 | break;
|
---|
| 311 | }
|
---|
| 312 | case MSG_TYPE_LOGIN:
|
---|
| 313 | {
|
---|
[60017fc] | 314 | cout << "Got login message" << endl;
|
---|
| 315 |
|
---|
[8e540f4] | 316 | string username(clientMsg.buffer);
|
---|
[41ad8ed] | 317 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[8e540f4] | 318 |
|
---|
[41ad8ed] | 319 | Player* p = da.getPlayer(username);
|
---|
[d2b411a] | 320 |
|
---|
[b128109] | 321 | if (p == NULL || !da.verifyPassword(password, p->password))
|
---|
[41ad8ed] | 322 | {
|
---|
| 323 | strcpy(serverMsg.buffer, "Incorrect username or password");
|
---|
[95ffe57] | 324 | if (p != NULL)
|
---|
| 325 | delete(p);
|
---|
[41ad8ed] | 326 | }
|
---|
[01d0d00] | 327 | else if(findPlayerByName(mapPlayers, username) != NULL)
|
---|
[41ad8ed] | 328 | {
|
---|
| 329 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
---|
[95ffe57] | 330 | delete(p);
|
---|
[41ad8ed] | 331 | }
|
---|
| 332 | else
|
---|
[8e540f4] | 333 | {
|
---|
[8dad966] | 334 | updateUnusedPlayerId(unusedPlayerId, mapPlayers);
|
---|
| 335 | p->id = unusedPlayerId;
|
---|
[d211210] | 336 | cout << "new player id: " << p->id << endl;
|
---|
[df79cfd] | 337 | p->setAddr(from);
|
---|
[ce2bb87] | 338 | p->currentGame = NULL;
|
---|
[df79cfd] | 339 |
|
---|
| 340 | // choose a random team (either 0 or 1)
|
---|
| 341 | p->team = rand() % 2;
|
---|
[d211210] | 342 |
|
---|
[f203c5c] | 343 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
[d211210] | 344 | // tell the new player about all the existing players
|
---|
| 345 | cout << "Sending other players to new player" << endl;
|
---|
| 346 |
|
---|
[95ffe57] | 347 | map<unsigned int, Player*>::iterator it;
|
---|
[d211210] | 348 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 349 | {
|
---|
[95ffe57] | 350 | it->second->serialize(serverMsg.buffer);
|
---|
[d211210] | 351 |
|
---|
[95ffe57] | 352 | cout << "sending info about " << it->second->name << endl;
|
---|
| 353 | cout << "sending id " << it->second->id << endl;
|
---|
[8554263] | 354 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[5f868c0] | 355 | }
|
---|
| 356 |
|
---|
[d3efa1a] | 357 | // send info about existing games to new player
|
---|
| 358 | map<string, Game*>::iterator itGames;
|
---|
| 359 | Game* g;
|
---|
| 360 | int numPlayers;
|
---|
| 361 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
| 362 |
|
---|
| 363 | for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++)
|
---|
| 364 | {
|
---|
| 365 | g = itGames->second;
|
---|
| 366 | numPlayers = g->getNumPlayers();
|
---|
| 367 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
| 368 | strcpy(serverMsg.buffer+4, g->getName().c_str());
|
---|
[8554263] | 369 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[d211210] | 370 | }
|
---|
[59061f6] | 371 |
|
---|
[b8601ee] | 372 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
[594d2e9] | 373 | p->serialize(serverMsg.buffer);
|
---|
[d05c484] | 374 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
[d211210] | 375 |
|
---|
[95ffe57] | 376 | mapPlayers[unusedPlayerId] = p;
|
---|
[07028b9] | 377 | }
|
---|
| 378 |
|
---|
[f203c5c] | 379 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
[8554263] | 380 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[07028b9] | 381 |
|
---|
[8e540f4] | 382 | break;
|
---|
| 383 | }
|
---|
| 384 | case MSG_TYPE_LOGOUT:
|
---|
| 385 | {
|
---|
| 386 | string name(clientMsg.buffer);
|
---|
| 387 | cout << "Player logging out: " << name << endl;
|
---|
| 388 |
|
---|
[01d0d00] | 389 | Player *p = findPlayerByName(mapPlayers, name);
|
---|
[633f42a] | 390 |
|
---|
[8e540f4] | 391 | if (p == NULL)
|
---|
| 392 | {
|
---|
[90eaad2] | 393 | strcpy(serverMsg.buffer+4, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
|
---|
[8a3ef42] | 394 | cout << "Player not logged in" << endl;
|
---|
[07028b9] | 395 | }
|
---|
[01d0d00] | 396 | else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
---|
| 397 | p->addr.sin_port != from.sin_port )
|
---|
[07028b9] | 398 | {
|
---|
[90eaad2] | 399 | strcpy(serverMsg.buffer+4, "That player is logged in using a differemt connection. This is either a bug, or you're trying to hack the server.");
|
---|
[8a3ef42] | 400 | cout << "Player logged in using a different connection" << endl;
|
---|
[2488852] | 401 | }
|
---|
[8e540f4] | 402 | else
|
---|
[2488852] | 403 | {
|
---|
[1a47469] | 404 | // broadcast to all players before deleting p from the map
|
---|
[4509648] | 405 | serverMsg.type = MSG_TYPE_LOGOUT;
|
---|
| 406 | memcpy(serverMsg.buffer, &p->id, 4);
|
---|
| 407 |
|
---|
[d05c484] | 408 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
[1a47469] | 409 |
|
---|
[8dad966] | 410 | if (p->id < unusedPlayerId)
|
---|
| 411 | unusedPlayerId = p->id;
|
---|
[8554263] | 412 |
|
---|
[01d0d00] | 413 | mapPlayers.erase(p->id);
|
---|
[95ffe57] | 414 | delete p;
|
---|
[8554263] | 415 |
|
---|
[90eaad2] | 416 | strcpy(serverMsg.buffer+4, "You have successfully logged out.");
|
---|
[8e540f4] | 417 | }
|
---|
[07028b9] | 418 |
|
---|
[d211210] | 419 | serverMsg.type = MSG_TYPE_LOGOUT;
|
---|
[8554263] | 420 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[8a3ef42] | 421 |
|
---|
[8e540f4] | 422 | break;
|
---|
| 423 | }
|
---|
| 424 | case MSG_TYPE_CHAT:
|
---|
| 425 | {
|
---|
[da692b9] | 426 | cout << "Got a chat message" << endl;
|
---|
| 427 |
|
---|
[8554263] | 428 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
| 429 |
|
---|
[01d0d00] | 430 | Player *p = findPlayerByAddr(mapPlayers, from);
|
---|
[07028b9] | 431 |
|
---|
[8e540f4] | 432 | if (p == NULL)
|
---|
| 433 | {
|
---|
| 434 | strcpy(serverMsg.buffer, "No player is logged in using this connection. This is either a bug, or you're trying to hack the server.");
|
---|
[8554263] | 435 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[2488852] | 436 | }
|
---|
[8e540f4] | 437 | else
|
---|
| 438 | {
|
---|
[b128109] | 439 | ostringstream oss;
|
---|
| 440 | oss << p->name << ": " << clientMsg.buffer;
|
---|
[3b1efcc] | 441 |
|
---|
[b128109] | 442 | strcpy(serverMsg.buffer, oss.str().c_str());
|
---|
[d05c484] | 443 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
[8e540f4] | 444 | }
|
---|
| 445 |
|
---|
| 446 | break;
|
---|
[e084950] | 447 | }
|
---|
[b128109] | 448 | case MSG_TYPE_PLAYER_MOVE:
|
---|
| 449 | {
|
---|
| 450 | cout << "PLAYER_MOVE" << endl;
|
---|
| 451 |
|
---|
| 452 | int id, x, y;
|
---|
| 453 |
|
---|
| 454 | memcpy(&id, clientMsg.buffer, 4);
|
---|
| 455 | memcpy(&x, clientMsg.buffer+4, 4);
|
---|
| 456 | memcpy(&y, clientMsg.buffer+8, 4);
|
---|
[7b43385] | 457 |
|
---|
[b128109] | 458 | cout << "x: " << x << endl;
|
---|
| 459 | cout << "y: " << y << endl;
|
---|
| 460 | cout << "id: " << id << endl;
|
---|
[7b43385] | 461 |
|
---|
[95ffe57] | 462 | Player* p = mapPlayers[id];
|
---|
[8554263] | 463 | bool validMessage = false;
|
---|
[95ffe57] | 464 |
|
---|
| 465 | if ( p->addr.sin_addr.s_addr == from.sin_addr.s_addr &&
|
---|
| 466 | p->addr.sin_port == from.sin_port )
|
---|
[b128109] | 467 | {
|
---|
[0129700] | 468 | if (p->currentGame->startPlayerMovement(id, x, y)) {
|
---|
[60017fc] | 469 | serverMsg.type = MSG_TYPE_PLAYER_MOVE;
|
---|
| 470 |
|
---|
| 471 | memcpy(serverMsg.buffer, &id, 4);
|
---|
[95ffe57] | 472 | memcpy(serverMsg.buffer+4, &p->target.x, 4);
|
---|
| 473 | memcpy(serverMsg.buffer+8, &p->target.y, 4);
|
---|
[60017fc] | 474 |
|
---|
[d05c484] | 475 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
[8554263] | 476 |
|
---|
| 477 | validMessage = true;
|
---|
[60017fc] | 478 | }
|
---|
| 479 | else
|
---|
| 480 | cout << "Bad terrain detected" << endl;
|
---|
[b128109] | 481 | }
|
---|
[8554263] | 482 | else
|
---|
[b128109] | 483 | cout << "Player id (" << id << ") doesn't match sender" << endl;
|
---|
| 484 |
|
---|
[8554263] | 485 | if (!validMessage)
|
---|
| 486 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
| 487 |
|
---|
[b128109] | 488 | break;
|
---|
| 489 | }
|
---|
[5299436] | 490 | case MSG_TYPE_PICKUP_FLAG:
|
---|
| 491 | {
|
---|
| 492 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
| 493 | cout << "PICKUP_FLAG" << endl;
|
---|
| 494 |
|
---|
| 495 | int id;
|
---|
| 496 |
|
---|
| 497 | memcpy(&id, clientMsg.buffer, 4);
|
---|
| 498 | cout << "id: " << id << endl;
|
---|
| 499 |
|
---|
[95ffe57] | 500 | Player* p = mapPlayers[id];
|
---|
[ce2bb87] | 501 | int objectId = p->currentGame->processFlagPickupRequest(p);
|
---|
[95ffe57] | 502 |
|
---|
[ce2bb87] | 503 | if (objectId >= 0) {
|
---|
[8554263] | 504 | map<unsigned int, Player*> players = p->currentGame->getPlayers();
|
---|
| 505 |
|
---|
[ce2bb87] | 506 | serverMsg.type = MSG_TYPE_REMOVE_OBJECT;
|
---|
| 507 | memcpy(serverMsg.buffer, &objectId, 4);
|
---|
[d05c484] | 508 | msgProcessor.broadcastMessage(serverMsg, players);
|
---|
[5c84d54] | 509 |
|
---|
[8554263] | 510 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
| 511 | p->serialize(serverMsg.buffer);
|
---|
[d05c484] | 512 | msgProcessor.broadcastMessage(serverMsg, players);
|
---|
[5c84d54] | 513 | }
|
---|
| 514 |
|
---|
[5299436] | 515 | break;
|
---|
| 516 | }
|
---|
[e487381] | 517 | case MSG_TYPE_DROP_FLAG:
|
---|
| 518 | {
|
---|
| 519 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
| 520 | cout << "DROP_FLAG" << endl;
|
---|
| 521 |
|
---|
| 522 | int id;
|
---|
| 523 |
|
---|
| 524 | memcpy(&id, clientMsg.buffer, 4);
|
---|
| 525 | cout << "id: " << id << endl;
|
---|
| 526 |
|
---|
[95ffe57] | 527 | Player* p = mapPlayers[id];
|
---|
| 528 |
|
---|
[e487381] | 529 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
---|
[95ffe57] | 530 | if (p->hasBlueFlag)
|
---|
[e487381] | 531 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
[95ffe57] | 532 | else if (p->hasRedFlag)
|
---|
[e487381] | 533 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
| 534 |
|
---|
[8554263] | 535 | map<unsigned int, Player*> players = p->currentGame->getPlayers();
|
---|
| 536 |
|
---|
[d05c484] | 537 | p->currentGame->addObjectToMap(flagType, p->pos.x, p->pos.y);
|
---|
[e487381] | 538 |
|
---|
[95ffe57] | 539 | p->hasBlueFlag = false;
|
---|
| 540 | p->hasRedFlag = false;
|
---|
[e487381] | 541 |
|
---|
| 542 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
[95ffe57] | 543 | p->serialize(serverMsg.buffer);
|
---|
[d05c484] | 544 | msgProcessor.broadcastMessage(serverMsg, players);
|
---|
[e487381] | 545 |
|
---|
| 546 | break;
|
---|
| 547 | }
|
---|
[9bfc1cb] | 548 | case MSG_TYPE_ATTACK:
|
---|
[4b4b153] | 549 | {
|
---|
| 550 | cout << "Received a START_ATTACK message" << endl;
|
---|
| 551 |
|
---|
[8a4ed74] | 552 | int id, targetId;
|
---|
| 553 |
|
---|
| 554 | memcpy(&id, clientMsg.buffer, 4);
|
---|
| 555 | memcpy(&targetId, clientMsg.buffer+4, 4);
|
---|
| 556 |
|
---|
[ffadc8e] | 557 | // need to make sure the target is in the sender's game
|
---|
| 558 |
|
---|
[8554263] | 559 | Player* p = mapPlayers[id];
|
---|
| 560 | p->targetPlayer = targetId;
|
---|
| 561 | p->isChasing = true;
|
---|
| 562 |
|
---|
| 563 | map<unsigned int, Player*> players = p->currentGame->getPlayers();
|
---|
[8dad966] | 564 |
|
---|
[9bfc1cb] | 565 | serverMsg.type = MSG_TYPE_ATTACK;
|
---|
[8dad966] | 566 | memcpy(serverMsg.buffer, &id, 4);
|
---|
| 567 | memcpy(serverMsg.buffer+4, &targetId, 4);
|
---|
[d05c484] | 568 | msgProcessor.broadcastMessage(serverMsg, players);
|
---|
[8a4ed74] | 569 |
|
---|
| 570 | break;
|
---|
[4b4b153] | 571 | }
|
---|
[b8f789d] | 572 | case MSG_TYPE_CREATE_GAME:
|
---|
[8e540f4] | 573 | {
|
---|
[b8f789d] | 574 | cout << "Received a CREATE_GAME message" << endl;
|
---|
| 575 |
|
---|
| 576 | string gameName(clientMsg.buffer);
|
---|
| 577 | cout << "Game name: " << gameName << endl;
|
---|
| 578 |
|
---|
[b48ef09] | 579 | // check if this game already exists
|
---|
| 580 | if (mapGames.find(gameName) != mapGames.end()) {
|
---|
[3ef8cf4] | 581 | cout << "Error: Game already exists" << endl;
|
---|
[b48ef09] | 582 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
[8554263] | 583 | }else {
|
---|
[d05c484] | 584 | Game* g = new Game(gameName, "../data/map.txt", &msgProcessor);
|
---|
[8554263] | 585 | mapGames[gameName] = g;
|
---|
| 586 |
|
---|
| 587 | // add flag objects to the map
|
---|
| 588 | WorldMap* m = g->getMap();
|
---|
| 589 | for (int y=0; y<m->height; y++) {
|
---|
| 590 | for (int x=0; x<m->width; x++) {
|
---|
| 591 | switch (m->getStructure(x, y)) {
|
---|
| 592 | case WorldMap::STRUCTURE_BLUE_FLAG:
|
---|
| 593 | m->addObject(WorldMap::OBJECT_BLUE_FLAG, x*25+12, y*25+12);
|
---|
| 594 | break;
|
---|
| 595 | case WorldMap::STRUCTURE_RED_FLAG:
|
---|
| 596 | m->addObject(WorldMap::OBJECT_RED_FLAG, x*25+12, y*25+12);
|
---|
| 597 | break;
|
---|
[d05c484] | 598 | case WorldMap::STRUCTURE_NONE:
|
---|
| 599 | break;
|
---|
[8554263] | 600 | }
|
---|
[70fc3e8] | 601 | }
|
---|
| 602 | }
|
---|
[8554263] | 603 |
|
---|
| 604 | serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS;
|
---|
| 605 | strcpy(serverMsg.buffer, gameName.c_str());
|
---|
[70fc3e8] | 606 | }
|
---|
| 607 |
|
---|
[8554263] | 608 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[b92e6a7] | 609 |
|
---|
[b48ef09] | 610 | break;
|
---|
| 611 | }
|
---|
| 612 | case MSG_TYPE_JOIN_GAME:
|
---|
| 613 | {
|
---|
| 614 | cout << "Received a JOIN_GAME message" << endl;
|
---|
[b92e6a7] | 615 |
|
---|
[b48ef09] | 616 | string gameName(clientMsg.buffer);
|
---|
| 617 | cout << "Game name: " << gameName << endl;
|
---|
[b92e6a7] | 618 |
|
---|
[b48ef09] | 619 | // check if this game already exists
|
---|
| 620 | if (mapGames.find(gameName) == mapGames.end()) {
|
---|
[3ef8cf4] | 621 | cout << "Error: Game does not exist" << endl;
|
---|
[b48ef09] | 622 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
[8554263] | 623 | }else {
|
---|
| 624 | Game* g = mapGames[gameName];
|
---|
| 625 | map<unsigned int, Player*>& players = g->getPlayers();
|
---|
| 626 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
| 627 |
|
---|
| 628 | if (players.find(p->id) != players.end()) {
|
---|
| 629 | cout << "Player " << p->name << " trying to join a game he's already in" << endl;
|
---|
| 630 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
| 631 | }else {
|
---|
| 632 | serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS;
|
---|
| 633 | strcpy(serverMsg.buffer, gameName.c_str());
|
---|
| 634 | }
|
---|
[b92e6a7] | 635 | }
|
---|
| 636 |
|
---|
[8554263] | 637 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[b8f789d] | 638 |
|
---|
| 639 | break;
|
---|
| 640 | }
|
---|
[ab8fd40] | 641 | case MSG_TYPE_LEAVE_GAME:
|
---|
| 642 | {
|
---|
| 643 | cout << "Received a LEAVE_GAME message" << endl;
|
---|
| 644 |
|
---|
| 645 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
| 646 | Game* g = p->currentGame;
|
---|
| 647 |
|
---|
| 648 | if (g == NULL) {
|
---|
| 649 | cout << "Player " << p->name << " is trying to leave a game, but is not currently in a game." << endl;
|
---|
| 650 |
|
---|
| 651 | /// should send a response back, maybe a new message type is needed
|
---|
[8554263] | 652 | // not sure what to do here
|
---|
| 653 | }else {
|
---|
| 654 | cout << "Game name: " << g->getName() << endl;
|
---|
[360c0f1] | 655 |
|
---|
| 656 | if (!p->isDead) {
|
---|
| 657 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
---|
| 658 | if (p->hasBlueFlag)
|
---|
| 659 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
| 660 | else if (p->hasRedFlag)
|
---|
| 661 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
| 662 |
|
---|
[d05c484] | 663 | if (flagType != WorldMap::OBJECT_NONE)
|
---|
| 664 | g->addObjectToMap(flagType, p->pos.x, p->pos.y);
|
---|
[360c0f1] | 665 | }
|
---|
[bcfd99a] | 666 |
|
---|
| 667 | p->currentGame = NULL;
|
---|
| 668 | g->removePlayer(p->id);
|
---|
[360c0f1] | 669 |
|
---|
[8554263] | 670 | serverMsg.type = MSG_TYPE_LEAVE_GAME;
|
---|
| 671 | memcpy(serverMsg.buffer, &p->id, 4);
|
---|
| 672 | strcpy(serverMsg.buffer+4, g->getName().c_str());
|
---|
[d05c484] | 673 | msgProcessor.broadcastMessage(serverMsg, g->getPlayers());
|
---|
[95ffe57] | 674 |
|
---|
[8554263] | 675 | int numPlayers = g->getNumPlayers();
|
---|
[ab8fd40] | 676 |
|
---|
[8554263] | 677 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
| 678 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
| 679 | strcpy(serverMsg.buffer+4, g->getName().c_str());
|
---|
[d05c484] | 680 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
[8554263] | 681 |
|
---|
| 682 | // if there are no more players in the game, remove it
|
---|
| 683 | if (numPlayers == 0) {
|
---|
| 684 | mapGames.erase(g->getName());
|
---|
| 685 | delete g;
|
---|
| 686 | }
|
---|
[1248984] | 687 | }
|
---|
| 688 |
|
---|
[ab8fd40] | 689 | break;
|
---|
| 690 | }
|
---|
[b48ef09] | 691 | case MSG_TYPE_JOIN_GAME_ACK:
|
---|
[b8f789d] | 692 | {
|
---|
[b48ef09] | 693 | cout << "Received a JOIN_GAME_ACK message" << endl;
|
---|
[e084950] | 694 |
|
---|
[b8f789d] | 695 | string gameName(clientMsg.buffer);
|
---|
| 696 | cout << "Game name: " << gameName << endl;
|
---|
| 697 |
|
---|
[b48ef09] | 698 | // check if this game already exists
|
---|
| 699 | if (mapGames.find(gameName) == mapGames.end()) {
|
---|
| 700 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
[8554263] | 701 |
|
---|
| 702 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[b48ef09] | 703 | }
|
---|
[b92e6a7] | 704 |
|
---|
[f41a7f9] | 705 | Game* g = mapGames[gameName];
|
---|
[b92e6a7] | 706 |
|
---|
[b48ef09] | 707 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
[f41a7f9] | 708 | p->team = rand() % 2; // choose a random team (either 0 or 1)
|
---|
[e62b56c] | 709 | p->currentGame = g;
|
---|
[b92e6a7] | 710 |
|
---|
| 711 | // tell the new player about all map objects
|
---|
| 712 | // (currently just the flags)
|
---|
| 713 |
|
---|
| 714 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
[f41a7f9] | 715 | vector<WorldMap::Object>* vctObjects = g->getMap()->getObjects();
|
---|
[b92e6a7] | 716 | vector<WorldMap::Object>::iterator itObjects;
|
---|
| 717 | cout << "sending items" << endl;
|
---|
| 718 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
| 719 | itObjects->serialize(serverMsg.buffer);
|
---|
| 720 | cout << "sending item id " << itObjects->id << endl;
|
---|
[8554263] | 721 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[b92e6a7] | 722 | }
|
---|
| 723 |
|
---|
| 724 |
|
---|
| 725 | // send the current score
|
---|
| 726 | serverMsg.type = MSG_TYPE_SCORE;
|
---|
| 727 |
|
---|
[f3fb980] | 728 | int blueScore = g->getBlueScore();
|
---|
| 729 | int redScore = g->getRedScore();
|
---|
| 730 | memcpy(serverMsg.buffer, &blueScore, 4);
|
---|
| 731 | memcpy(serverMsg.buffer+4, &redScore, 4);
|
---|
[b92e6a7] | 732 |
|
---|
[8554263] | 733 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[b92e6a7] | 734 |
|
---|
[8554263] | 735 | // send info to other players
|
---|
[453087e] | 736 | serverMsg.type = MSG_TYPE_PLAYER_JOIN_GAME;
|
---|
[b92e6a7] | 737 | p->serialize(serverMsg.buffer);
|
---|
| 738 | cout << "Should be broadcasting the message" << endl;
|
---|
[d05c484] | 739 | msgProcessor.broadcastMessage(serverMsg, g->getPlayers());
|
---|
[b8f789d] | 740 |
|
---|
[b48ef09] | 741 | g->addPlayer(p);
|
---|
[453087e] | 742 |
|
---|
| 743 |
|
---|
| 744 | // tell the new player about all the players in the game (including himself)
|
---|
| 745 | cout << "Sending other players to new player" << endl;
|
---|
| 746 | serverMsg.type = MSG_TYPE_PLAYER_JOIN_GAME;
|
---|
| 747 |
|
---|
[8554263] | 748 |
|
---|
| 749 | map<unsigned int, Player*>& allPlayers = g->getPlayers();
|
---|
| 750 | map<unsigned int, Player*>::iterator it;
|
---|
[453087e] | 751 | for (it = allPlayers.begin(); it != allPlayers.end(); it++)
|
---|
| 752 | {
|
---|
| 753 | it->second->serialize(serverMsg.buffer);
|
---|
| 754 |
|
---|
| 755 | cout << "sending info about " << it->second->name << endl;
|
---|
| 756 | cout << "sending id " << it->second->id << endl;
|
---|
[8554263] | 757 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[453087e] | 758 | }
|
---|
| 759 |
|
---|
[b48ef09] | 760 | int numPlayers = g->getNumPlayers();
|
---|
| 761 |
|
---|
[b8f789d] | 762 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
| 763 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
| 764 | strcpy(serverMsg.buffer+4, gameName.c_str());
|
---|
[d05c484] | 765 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
[ab8fd40] | 766 |
|
---|
[b8f789d] | 767 | break;
|
---|
| 768 | }
|
---|
| 769 | default:
|
---|
| 770 | {
|
---|
[8554263] | 771 | // probably want to log the error rather than sending a chat message,
|
---|
| 772 | // especially since chat isn't currently visible on all screens
|
---|
| 773 |
|
---|
[8e540f4] | 774 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
[b8f789d] | 775 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
---|
[e084950] | 776 |
|
---|
[8e540f4] | 777 | break;
|
---|
| 778 | }
|
---|
[e3535b3] | 779 | }
|
---|
[8554263] | 780 | }
|
---|
[da692b9] | 781 |
|
---|
[95ffe57] | 782 | void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player*>& mapPlayers)
|
---|
[01d0d00] | 783 | {
|
---|
[1106210] | 784 | while (mapPlayers.find(id) != mapPlayers.end())
|
---|
| 785 | id++;
|
---|
[01d0d00] | 786 | }
|
---|
[8dad966] | 787 |
|
---|
[95ffe57] | 788 | Player *findPlayerByName(map<unsigned int, Player*> &m, string name)
|
---|
| 789 | {
|
---|
| 790 | map<unsigned int, Player*>::iterator it;
|
---|
| 791 |
|
---|
| 792 | for (it = m.begin(); it != m.end(); it++)
|
---|
| 793 | {
|
---|
| 794 | if ( it->second->name.compare(name) == 0 )
|
---|
| 795 | return it->second;
|
---|
| 796 | }
|
---|
| 797 |
|
---|
| 798 | return NULL;
|
---|
| 799 | }
|
---|
| 800 |
|
---|
| 801 | Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr)
|
---|
| 802 | {
|
---|
| 803 | map<unsigned int, Player*>::iterator it;
|
---|
| 804 |
|
---|
| 805 | for (it = m.begin(); it != m.end(); it++)
|
---|
| 806 | {
|
---|
| 807 | if ( it->second->addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
|
---|
| 808 | it->second->addr.sin_port == addr.sin_port )
|
---|
| 809 | return it->second;
|
---|
| 810 | }
|
---|
| 811 |
|
---|
| 812 | return NULL;
|
---|
| 813 | }
|
---|
| 814 |
|
---|
[f3fb980] | 815 | void quit(int sig) {
|
---|
| 816 | done = true;
|
---|
| 817 | }
|
---|