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