[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 |
|
---|
[c666518] | 50 | bool handleGameEvents(Game* game, MessageProcessor* msgProcessor, DataAccess* da);
|
---|
| 51 | bool handlePlayerEvents(Game* game, Player* p, MessageProcessor* msgProcessor, DataAccess* da);
|
---|
| 52 |
|
---|
[f3fb980] | 53 | void quit(int sig);
|
---|
| 54 |
|
---|
| 55 | bool done;
|
---|
[d05086b] | 56 |
|
---|
[e3535b3] | 57 | int main(int argc, char *argv[])
|
---|
| 58 | {
|
---|
[8554263] | 59 | int sock, length;
|
---|
[e3535b3] | 60 | struct sockaddr_in server;
|
---|
[3b1efcc] | 61 | struct sockaddr_in from; // info of client sending the message
|
---|
[e084950] | 62 | NETWORK_MSG clientMsg, serverMsg;
|
---|
[9b5d30b] | 63 | MessageProcessor msgProcessor;
|
---|
[95ffe57] | 64 | map<unsigned int, Player*> mapPlayers;
|
---|
[8dad966] | 65 | map<unsigned int, Projectile> mapProjectiles;
|
---|
[f41a7f9] | 66 | map<string, Game*> mapGames;
|
---|
[f3dfead] | 67 | DataAccess da;
|
---|
[d05086b] | 68 | ofstream outputLog;
|
---|
| 69 |
|
---|
| 70 | done = false;
|
---|
[b8601ee] | 71 |
|
---|
[d05086b] | 72 | signal(SIGINT, quit);
|
---|
| 73 |
|
---|
[edfd1d0] | 74 | //SSL_load_error_strings();
|
---|
| 75 | //ERR_load_BIO_strings();
|
---|
| 76 | //OpenSSL_add_all_algorithms();
|
---|
[e3535b3] | 77 |
|
---|
[95ffe57] | 78 | if (argc != 2)
|
---|
| 79 | {
|
---|
| 80 | cerr << "ERROR, expected server [domain] [port]" << endl;
|
---|
[73f75c1] | 81 | exit(1);
|
---|
[e3535b3] | 82 | }
|
---|
[60017fc] | 83 |
|
---|
[d05086b] | 84 | outputLog.open("server.log", ios::app);
|
---|
| 85 | outputLog << "Started server on " << getCurrentDateTimeString() << endl;
|
---|
| 86 |
|
---|
[371ce29] | 87 | sock = socket(AF_INET, SOCK_DGRAM, 0);
|
---|
[5b1e31e] | 88 | if (sock < 0)
|
---|
| 89 | error("Opening socket");
|
---|
[e3535b3] | 90 | length = sizeof(server);
|
---|
| 91 | bzero(&server,length);
|
---|
| 92 | server.sin_family=AF_INET;
|
---|
| 93 | server.sin_port=htons(atoi(argv[1]));
|
---|
[2488852] | 94 | server.sin_addr.s_addr=INADDR_ANY;
|
---|
| 95 | if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
|
---|
[e084950] | 96 | error("binding");
|
---|
[73f75c1] | 97 |
|
---|
[371ce29] | 98 | set_nonblock(sock);
|
---|
| 99 |
|
---|
[8554263] | 100 | msgProcessor = MessageProcessor(sock, &outputLog);
|
---|
| 101 |
|
---|
[d211210] | 102 | timespec ts;
|
---|
[d05c484] | 103 | int timeLastUpdated = 0, curTime = 0;
|
---|
[95ffe57] | 104 | while (!done)
|
---|
| 105 | {
|
---|
[371ce29] | 106 | usleep(5000);
|
---|
| 107 |
|
---|
[d211210] | 108 | clock_gettime(CLOCK_REALTIME, &ts);
|
---|
[430c80e] | 109 | // make the number smaller so millis can fit in an int
|
---|
[d69eb32] | 110 | ts.tv_sec -= 1368000000;
|
---|
[430c80e] | 111 | curTime = ts.tv_sec*1000 + ts.tv_nsec/1000000;
|
---|
[d211210] | 112 |
|
---|
[95ffe57] | 113 | if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50)
|
---|
| 114 | {
|
---|
[d211210] | 115 | timeLastUpdated = curTime;
|
---|
| 116 |
|
---|
[8554263] | 117 | msgProcessor.cleanAckedMessages();
|
---|
| 118 | msgProcessor.resendUnackedMessages();
|
---|
[9b5d30b] | 119 |
|
---|
[95ffe57] | 120 | map<unsigned int, Player*>::iterator it;
|
---|
[11d21ee] | 121 |
|
---|
| 122 | // set targets for all chasing players (or make them attack if they're close enough)
|
---|
[8554263] | 123 | // this should be moved into the games loop
|
---|
[95ffe57] | 124 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 125 | {
|
---|
[ffadc8e] | 126 | Player* p = it->second;
|
---|
| 127 |
|
---|
[c76134b] | 128 | // check if it's time to revive dead players
|
---|
[ffadc8e] | 129 | if (p->isDead)
|
---|
[95ffe57] | 130 | {
|
---|
[e1af80c] | 131 | cout << "Player is dead" << endl;
|
---|
[35f6097] | 132 |
|
---|
[ffadc8e] | 133 | if (getCurrentMillis() - p->timeDied >= 10000)
|
---|
[95ffe57] | 134 | {
|
---|
[ffadc8e] | 135 | p->isDead = false;
|
---|
[c76134b] | 136 |
|
---|
| 137 | POSITION spawnPos;
|
---|
| 138 |
|
---|
[ffadc8e] | 139 | switch (p->team)
|
---|
[95ffe57] | 140 | {
|
---|
[7fa452f] | 141 | case 1:// blue team
|
---|
[7f884ea] | 142 | spawnPos = p->currentGame->getMap()->getStructureLocation(STRUCTURE_BLUE_FLAG);
|
---|
[c76134b] | 143 | break;
|
---|
[7fa452f] | 144 | case 2:// red team
|
---|
[7f884ea] | 145 | spawnPos = p->currentGame->getMap()->getStructureLocation(STRUCTURE_RED_FLAG);
|
---|
[c76134b] | 146 | break;
|
---|
| 147 | default:
|
---|
| 148 | // should never go here
|
---|
| 149 | cout << "Error: Invalid team" << endl;
|
---|
| 150 | break;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | // spawn the player to the right of their flag location
|
---|
| 154 | spawnPos.x = (spawnPos.x+1) * 25 + 12;
|
---|
| 155 | spawnPos.y = spawnPos.y * 25 + 12;
|
---|
| 156 |
|
---|
[ffadc8e] | 157 | p->pos = spawnPos.toFloat();
|
---|
| 158 | p->target = spawnPos;
|
---|
| 159 | p->health = p->maxHealth;
|
---|
[c76134b] | 160 |
|
---|
| 161 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
[ffadc8e] | 162 | p->serialize(serverMsg.buffer);
|
---|
[c76134b] | 163 |
|
---|
[d05c484] | 164 | msgProcessor.broadcastMessage(serverMsg, p->currentGame->getPlayers());
|
---|
[c76134b] | 165 | }
|
---|
| 166 |
|
---|
| 167 | continue;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[ffadc8e] | 170 | if (p->currentGame != NULL) {
|
---|
| 171 | map<unsigned int, Player*> playersInGame = p->currentGame->getPlayers();
|
---|
[204edcf] | 172 | if (p->updateTarget(playersInGame))
|
---|
[5b1e31e] | 173 | {
|
---|
[ffadc8e] | 174 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
| 175 | p->serialize(serverMsg.buffer);
|
---|
[d05c484] | 176 | msgProcessor.broadcastMessage(serverMsg, playersInGame);
|
---|
[11d21ee] | 177 | }
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[402cf86] | 181 | // process players currently in a game
|
---|
[e62b56c] | 182 | map<string, Game*>::iterator itGames;
|
---|
| 183 | Game* game = NULL;
|
---|
[ce2bb87] | 184 |
|
---|
[e5b96e2] | 185 | for (itGames = mapGames.begin(); itGames != mapGames.end();) {
|
---|
[d05c484] | 186 | game = itGames->second;
|
---|
[c666518] | 187 | bool gameFinished = handleGameEvents(game, &msgProcessor, &da);
|
---|
| 188 |
|
---|
| 189 | if (gameFinished) {
|
---|
[53643ca] | 190 | // save game record
|
---|
| 191 | int winningTeam = -1;
|
---|
| 192 | if (game->getBlueScore() == 3)
|
---|
| 193 | winningTeam = 1;
|
---|
[7fa452f] | 194 | else if (game->getRedScore() == 3)
|
---|
| 195 | winningTeam = 2;
|
---|
[53643ca] | 196 |
|
---|
| 197 | if (winningTeam == -1)
|
---|
| 198 | cout << "Error: Game ended, but neither team has a score of 3" << endl;
|
---|
| 199 | else {
|
---|
| 200 | map<unsigned int, Player*>::iterator it;
|
---|
| 201 |
|
---|
| 202 | for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++) {
|
---|
[c666518] | 203 | Player* p = it->second;
|
---|
| 204 | cout << "winning team: " << winningTeam << endl;
|
---|
| 205 | cout << "player team: " << p->team << endl;
|
---|
| 206 | cout << "player id: " << p->getId() << endl;
|
---|
| 207 |
|
---|
| 208 | if (winningTeam == p->team)
|
---|
| 209 | p->wins++;
|
---|
| 210 | else
|
---|
| 211 | p->losses++;
|
---|
| 212 | da.updatePlayer(p);
|
---|
| 213 | da.saveGameHistory(p->getId(), winningTeam, game->getBlueScore(), game->getRedScore());
|
---|
[53643ca] | 214 | }
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[d05c484] | 217 | // send a GAME_INFO message with 0 players to force clients to delete the game
|
---|
| 218 | int numPlayers = 0;
|
---|
| 219 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
| 220 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
| 221 | strcpy(serverMsg.buffer+4, game->getName().c_str());
|
---|
| 222 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
| 223 |
|
---|
[f3fb980] | 224 | delete itGames->second;
|
---|
[df74597] | 225 | mapGames.erase(itGames++);
|
---|
[e5b96e2] | 226 | }else
|
---|
| 227 | itGames++;
|
---|
[8dad966] | 228 | }
|
---|
| 229 |
|
---|
| 230 | // move all projectiles
|
---|
[483a2cb] | 231 | // see if this can be moved inside the game class
|
---|
[45734ff] | 232 | // this method can be moved when I add a MessageProcessor to the Game class
|
---|
[8dad966] | 233 | map<unsigned int, Projectile>::iterator itProj;
|
---|
[5ae8dca] | 234 | for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++) {
|
---|
| 235 | game = itGames->second;
|
---|
| 236 | for (itProj = game->getProjectiles().begin(); itProj != game->getProjectiles().end(); itProj++)
|
---|
[95ffe57] | 237 | {
|
---|
[5ae8dca] | 238 | cout << "About to call projectile move" << endl;
|
---|
| 239 | if (itProj->second.move(game->getPlayers()))
|
---|
[8dad966] | 240 | {
|
---|
[5ae8dca] | 241 | // send a REMOVE_PROJECTILE message
|
---|
| 242 | cout << "send a REMOVE_PROJECTILE message" << endl;
|
---|
| 243 | serverMsg.type = MSG_TYPE_REMOVE_PROJECTILE;
|
---|
| 244 | memcpy(serverMsg.buffer, &itProj->second.id, 4);
|
---|
| 245 | game->removeProjectile(itProj->second.id);
|
---|
[d05c484] | 246 | msgProcessor.broadcastMessage(serverMsg, game->getPlayers());
|
---|
[5ae8dca] | 247 |
|
---|
| 248 | Player* target = game->getPlayers()[itProj->second.target];
|
---|
[d05c484] | 249 | game->dealDamageToPlayer(target, itProj->second.damage);
|
---|
[8dad966] | 250 | }
|
---|
| 251 | }
|
---|
[d211210] | 252 | }
|
---|
| 253 | }
|
---|
| 254 |
|
---|
[8554263] | 255 | if (msgProcessor.receiveMessage(&clientMsg, &from) >= 0)
|
---|
[95ffe57] | 256 | {
|
---|
[53643ca] | 257 | processMessage(clientMsg, from, msgProcessor, mapPlayers, mapGames, outputLog);
|
---|
[ce2bb87] | 258 |
|
---|
| 259 | cout << "Finished processing the message" << endl;
|
---|
[7b43385] | 260 | }
|
---|
[8e540f4] | 261 | }
|
---|
[371ce29] | 262 |
|
---|
[d05086b] | 263 | outputLog << "Stopped server on " << getCurrentDateTimeString() << endl;
|
---|
| 264 | outputLog.close();
|
---|
| 265 |
|
---|
[f41a7f9] | 266 | // delete all games
|
---|
| 267 | map<string, Game*>::iterator itGames;
|
---|
[95ffe57] | 268 | for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++)
|
---|
| 269 | {
|
---|
[f41a7f9] | 270 | delete itGames->second;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
[95ffe57] | 273 | map<unsigned int, Player*>::iterator itPlayers;
|
---|
| 274 | for (itPlayers = mapPlayers.begin(); itPlayers != mapPlayers.end(); itPlayers++)
|
---|
| 275 | {
|
---|
| 276 | delete itPlayers->second;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[8e540f4] | 279 | return 0;
|
---|
| 280 | }
|
---|
| 281 |
|
---|
[53643ca] | 282 | void processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, ofstream& outputLog)
|
---|
[8e540f4] | 283 | {
|
---|
[f3fb980] | 284 | NETWORK_MSG serverMsg;
|
---|
[41ad8ed] | 285 | DataAccess da;
|
---|
| 286 |
|
---|
[9a4fa04] | 287 | cout << "Inside processMessage" << endl;
|
---|
| 288 |
|
---|
[b8cb03f] | 289 | cout << "Received message" << endl;
|
---|
[8e540f4] | 290 | cout << "MSG: type: " << clientMsg.type << endl;
|
---|
| 291 | cout << "MSG contents: " << clientMsg.buffer << endl;
|
---|
| 292 |
|
---|
| 293 | // Check that if an invalid message is sent, the client will correctly
|
---|
| 294 | // receive and display the response. Maybe make a special error msg type
|
---|
| 295 | switch(clientMsg.type)
|
---|
| 296 | {
|
---|
| 297 | case MSG_TYPE_REGISTER:
|
---|
[d2b411a] | 298 | {
|
---|
[8e540f4] | 299 | string username(clientMsg.buffer);
|
---|
| 300 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[521c88b] | 301 | Player::PlayerClass playerClass;
|
---|
| 302 |
|
---|
[4509648] | 303 | memcpy(&playerClass, clientMsg.buffer+username.length()+password.length()+2, 4);
|
---|
[c4c2a3c] | 304 |
|
---|
[8e540f4] | 305 | cout << "username: " << username << endl;
|
---|
| 306 | cout << "password: " << password << endl;
|
---|
[d2b411a] | 307 |
|
---|
[8554263] | 308 | bool validClass = false;
|
---|
| 309 |
|
---|
| 310 | switch(playerClass) {
|
---|
| 311 | case Player::CLASS_WARRIOR:
|
---|
| 312 | case Player::CLASS_RANGER:
|
---|
| 313 | validClass = true;
|
---|
| 314 | break;
|
---|
[c991530] | 315 | case Player::CLASS_NONE:
|
---|
[8554263] | 316 | validClass = false;
|
---|
| 317 | break;
|
---|
[c4c2a3c] | 318 | }
|
---|
[521c88b] | 319 |
|
---|
[8554263] | 320 | serverMsg.type = MSG_TYPE_REGISTER;
|
---|
[41ad8ed] | 321 |
|
---|
[8554263] | 322 | if (validClass) {
|
---|
| 323 | int error = da.insertPlayer(username, password, playerClass);
|
---|
| 324 |
|
---|
| 325 | if (error)
|
---|
| 326 | strcpy(serverMsg.buffer, "Registration failed. Please try again.");
|
---|
| 327 | else
|
---|
| 328 | strcpy(serverMsg.buffer, "Registration successful.");
|
---|
| 329 | }else
|
---|
| 330 | strcpy(serverMsg.buffer, "You didn't select a class");
|
---|
| 331 |
|
---|
| 332 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[d2b411a] | 333 |
|
---|
[8e540f4] | 334 | break;
|
---|
| 335 | }
|
---|
| 336 | case MSG_TYPE_LOGIN:
|
---|
| 337 | {
|
---|
[60017fc] | 338 | cout << "Got login message" << endl;
|
---|
| 339 |
|
---|
[8e540f4] | 340 | string username(clientMsg.buffer);
|
---|
[41ad8ed] | 341 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
[8e540f4] | 342 |
|
---|
[41ad8ed] | 343 | Player* p = da.getPlayer(username);
|
---|
[d2b411a] | 344 |
|
---|
[b128109] | 345 | if (p == NULL || !da.verifyPassword(password, p->password))
|
---|
[41ad8ed] | 346 | {
|
---|
| 347 | strcpy(serverMsg.buffer, "Incorrect username or password");
|
---|
[95ffe57] | 348 | if (p != NULL)
|
---|
| 349 | delete(p);
|
---|
[41ad8ed] | 350 | }
|
---|
[01d0d00] | 351 | else if(findPlayerByName(mapPlayers, username) != NULL)
|
---|
[41ad8ed] | 352 | {
|
---|
| 353 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
---|
[95ffe57] | 354 | delete(p);
|
---|
[41ad8ed] | 355 | }
|
---|
| 356 | else
|
---|
[8e540f4] | 357 | {
|
---|
[204edcf] | 358 | cout << "new player id: " << p->getId() << endl;
|
---|
[df79cfd] | 359 | p->setAddr(from);
|
---|
[d211210] | 360 |
|
---|
[f203c5c] | 361 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
[d211210] | 362 | // tell the new player about all the existing players
|
---|
| 363 | cout << "Sending other players to new player" << endl;
|
---|
| 364 |
|
---|
[95ffe57] | 365 | map<unsigned int, Player*>::iterator it;
|
---|
[d211210] | 366 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
| 367 | {
|
---|
[95ffe57] | 368 | it->second->serialize(serverMsg.buffer);
|
---|
[d211210] | 369 |
|
---|
[95ffe57] | 370 | cout << "sending info about " << it->second->name << endl;
|
---|
[204edcf] | 371 | cout << "sending id " << it->second->getId() << endl;
|
---|
[8554263] | 372 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[5f868c0] | 373 | }
|
---|
| 374 |
|
---|
[d3efa1a] | 375 | // send info about existing games to new player
|
---|
| 376 | map<string, Game*>::iterator itGames;
|
---|
| 377 | Game* g;
|
---|
| 378 | int numPlayers;
|
---|
| 379 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
| 380 |
|
---|
| 381 | for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++)
|
---|
| 382 | {
|
---|
| 383 | g = itGames->second;
|
---|
| 384 | numPlayers = g->getNumPlayers();
|
---|
| 385 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
| 386 | strcpy(serverMsg.buffer+4, g->getName().c_str());
|
---|
[8554263] | 387 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[d211210] | 388 | }
|
---|
[59061f6] | 389 |
|
---|
[b8601ee] | 390 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
[594d2e9] | 391 | p->serialize(serverMsg.buffer);
|
---|
[d05c484] | 392 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
[d211210] | 393 |
|
---|
[53643ca] | 394 | mapPlayers[p->getId()] = p;
|
---|
[07028b9] | 395 | }
|
---|
| 396 |
|
---|
[f203c5c] | 397 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
[8554263] | 398 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[07028b9] | 399 |
|
---|
[8e540f4] | 400 | break;
|
---|
| 401 | }
|
---|
| 402 | case MSG_TYPE_LOGOUT:
|
---|
| 403 | {
|
---|
| 404 | string name(clientMsg.buffer);
|
---|
| 405 | cout << "Player logging out: " << name << endl;
|
---|
| 406 |
|
---|
[01d0d00] | 407 | Player *p = findPlayerByName(mapPlayers, name);
|
---|
[633f42a] | 408 |
|
---|
[8e540f4] | 409 | if (p == NULL)
|
---|
| 410 | {
|
---|
[90eaad2] | 411 | strcpy(serverMsg.buffer+4, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
|
---|
[8a3ef42] | 412 | cout << "Player not logged in" << endl;
|
---|
[07028b9] | 413 | }
|
---|
[01d0d00] | 414 | else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
---|
| 415 | p->addr.sin_port != from.sin_port )
|
---|
[07028b9] | 416 | {
|
---|
[90eaad2] | 417 | 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] | 418 | cout << "Player logged in using a different connection" << endl;
|
---|
[2488852] | 419 | }
|
---|
[8e540f4] | 420 | else
|
---|
[2488852] | 421 | {
|
---|
[1a47469] | 422 | // broadcast to all players before deleting p from the map
|
---|
[204edcf] | 423 | unsigned int playerId = p->getId();
|
---|
[4509648] | 424 | serverMsg.type = MSG_TYPE_LOGOUT;
|
---|
[204edcf] | 425 | memcpy(serverMsg.buffer, &playerId, 4);
|
---|
[4509648] | 426 |
|
---|
[d05c484] | 427 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
[1a47469] | 428 |
|
---|
[204edcf] | 429 | mapPlayers.erase(p->getId());
|
---|
[95ffe57] | 430 | delete p;
|
---|
[8554263] | 431 |
|
---|
[90eaad2] | 432 | strcpy(serverMsg.buffer+4, "You have successfully logged out.");
|
---|
[8e540f4] | 433 | }
|
---|
[07028b9] | 434 |
|
---|
[d211210] | 435 | serverMsg.type = MSG_TYPE_LOGOUT;
|
---|
[8554263] | 436 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[8a3ef42] | 437 |
|
---|
[8e540f4] | 438 | break;
|
---|
| 439 | }
|
---|
| 440 | case MSG_TYPE_CHAT:
|
---|
| 441 | {
|
---|
[da692b9] | 442 | cout << "Got a chat message" << endl;
|
---|
| 443 |
|
---|
[8554263] | 444 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
| 445 |
|
---|
[01d0d00] | 446 | Player *p = findPlayerByAddr(mapPlayers, from);
|
---|
[07028b9] | 447 |
|
---|
[8e540f4] | 448 | if (p == NULL)
|
---|
| 449 | {
|
---|
| 450 | 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] | 451 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[2488852] | 452 | }
|
---|
[8e540f4] | 453 | else
|
---|
| 454 | {
|
---|
[b128109] | 455 | ostringstream oss;
|
---|
| 456 | oss << p->name << ": " << clientMsg.buffer;
|
---|
[3b1efcc] | 457 |
|
---|
[b128109] | 458 | strcpy(serverMsg.buffer, oss.str().c_str());
|
---|
[d05c484] | 459 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
[8e540f4] | 460 | }
|
---|
| 461 |
|
---|
| 462 | break;
|
---|
[e084950] | 463 | }
|
---|
[b128109] | 464 | case MSG_TYPE_PLAYER_MOVE:
|
---|
| 465 | {
|
---|
| 466 | cout << "PLAYER_MOVE" << endl;
|
---|
| 467 |
|
---|
[9ba9b96] | 468 | unsigned int id;
|
---|
| 469 | int x, y;
|
---|
[b128109] | 470 |
|
---|
| 471 | memcpy(&id, clientMsg.buffer, 4);
|
---|
| 472 | memcpy(&x, clientMsg.buffer+4, 4);
|
---|
| 473 | memcpy(&y, clientMsg.buffer+8, 4);
|
---|
[7b43385] | 474 |
|
---|
[b128109] | 475 | cout << "x: " << x << endl;
|
---|
| 476 | cout << "y: " << y << endl;
|
---|
| 477 | cout << "id: " << id << endl;
|
---|
[7b43385] | 478 |
|
---|
[95ffe57] | 479 | Player* p = mapPlayers[id];
|
---|
[8554263] | 480 | bool validMessage = false;
|
---|
[95ffe57] | 481 |
|
---|
| 482 | if ( p->addr.sin_addr.s_addr == from.sin_addr.s_addr &&
|
---|
| 483 | p->addr.sin_port == from.sin_port )
|
---|
[b128109] | 484 | {
|
---|
[0129700] | 485 | if (p->currentGame->startPlayerMovement(id, x, y)) {
|
---|
[60017fc] | 486 | serverMsg.type = MSG_TYPE_PLAYER_MOVE;
|
---|
| 487 |
|
---|
| 488 | memcpy(serverMsg.buffer, &id, 4);
|
---|
[95ffe57] | 489 | memcpy(serverMsg.buffer+4, &p->target.x, 4);
|
---|
| 490 | memcpy(serverMsg.buffer+8, &p->target.y, 4);
|
---|
[60017fc] | 491 |
|
---|
[d05c484] | 492 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
[8554263] | 493 |
|
---|
| 494 | validMessage = true;
|
---|
[60017fc] | 495 | }
|
---|
| 496 | else
|
---|
| 497 | cout << "Bad terrain detected" << endl;
|
---|
[b128109] | 498 | }
|
---|
[8554263] | 499 | else
|
---|
[b128109] | 500 | cout << "Player id (" << id << ") doesn't match sender" << endl;
|
---|
| 501 |
|
---|
[8554263] | 502 | if (!validMessage)
|
---|
| 503 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
| 504 |
|
---|
[b128109] | 505 | break;
|
---|
| 506 | }
|
---|
[5299436] | 507 | case MSG_TYPE_PICKUP_FLAG:
|
---|
| 508 | {
|
---|
| 509 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
| 510 | cout << "PICKUP_FLAG" << endl;
|
---|
| 511 |
|
---|
[9ba9b96] | 512 | unsigned int id;
|
---|
[5299436] | 513 |
|
---|
| 514 | memcpy(&id, clientMsg.buffer, 4);
|
---|
| 515 | cout << "id: " << id << endl;
|
---|
| 516 |
|
---|
[95ffe57] | 517 | Player* p = mapPlayers[id];
|
---|
[9ba9b96] | 518 | unsigned int objectId = p->currentGame->processFlagPickupRequest(p);
|
---|
[95ffe57] | 519 |
|
---|
[ce2bb87] | 520 | if (objectId >= 0) {
|
---|
[8554263] | 521 | map<unsigned int, Player*> players = p->currentGame->getPlayers();
|
---|
| 522 |
|
---|
[ce2bb87] | 523 | serverMsg.type = MSG_TYPE_REMOVE_OBJECT;
|
---|
| 524 | memcpy(serverMsg.buffer, &objectId, 4);
|
---|
[d05c484] | 525 | msgProcessor.broadcastMessage(serverMsg, players);
|
---|
[5c84d54] | 526 |
|
---|
[8554263] | 527 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
| 528 | p->serialize(serverMsg.buffer);
|
---|
[d05c484] | 529 | msgProcessor.broadcastMessage(serverMsg, players);
|
---|
[5c84d54] | 530 | }
|
---|
| 531 |
|
---|
[5299436] | 532 | break;
|
---|
| 533 | }
|
---|
[e487381] | 534 | case MSG_TYPE_DROP_FLAG:
|
---|
| 535 | {
|
---|
| 536 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
| 537 | cout << "DROP_FLAG" << endl;
|
---|
| 538 |
|
---|
[9ba9b96] | 539 | unsigned int id;
|
---|
[e487381] | 540 |
|
---|
| 541 | memcpy(&id, clientMsg.buffer, 4);
|
---|
| 542 | cout << "id: " << id << endl;
|
---|
| 543 |
|
---|
[95ffe57] | 544 | Player* p = mapPlayers[id];
|
---|
| 545 |
|
---|
[7f884ea] | 546 | ObjectType flagType = OBJECT_NONE;
|
---|
[95ffe57] | 547 | if (p->hasBlueFlag)
|
---|
[7f884ea] | 548 | flagType = OBJECT_BLUE_FLAG;
|
---|
[95ffe57] | 549 | else if (p->hasRedFlag)
|
---|
[7f884ea] | 550 | flagType = OBJECT_RED_FLAG;
|
---|
[e487381] | 551 |
|
---|
[8554263] | 552 | map<unsigned int, Player*> players = p->currentGame->getPlayers();
|
---|
| 553 |
|
---|
[d05c484] | 554 | p->currentGame->addObjectToMap(flagType, p->pos.x, p->pos.y);
|
---|
[e487381] | 555 |
|
---|
[95ffe57] | 556 | p->hasBlueFlag = false;
|
---|
| 557 | p->hasRedFlag = false;
|
---|
[e487381] | 558 |
|
---|
| 559 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
[95ffe57] | 560 | p->serialize(serverMsg.buffer);
|
---|
[d05c484] | 561 | msgProcessor.broadcastMessage(serverMsg, players);
|
---|
[e487381] | 562 |
|
---|
| 563 | break;
|
---|
| 564 | }
|
---|
[9bfc1cb] | 565 | case MSG_TYPE_ATTACK:
|
---|
[4b4b153] | 566 | {
|
---|
| 567 | cout << "Received a START_ATTACK message" << endl;
|
---|
| 568 |
|
---|
[9ba9b96] | 569 | unsigned int id, targetId;
|
---|
[8a4ed74] | 570 |
|
---|
| 571 | memcpy(&id, clientMsg.buffer, 4);
|
---|
| 572 | memcpy(&targetId, clientMsg.buffer+4, 4);
|
---|
| 573 |
|
---|
[ffadc8e] | 574 | // need to make sure the target is in the sender's game
|
---|
| 575 |
|
---|
[8554263] | 576 | Player* p = mapPlayers[id];
|
---|
[204edcf] | 577 | p->setTargetPlayer(targetId);
|
---|
[8554263] | 578 | p->isChasing = true;
|
---|
| 579 |
|
---|
| 580 | map<unsigned int, Player*> players = p->currentGame->getPlayers();
|
---|
[8dad966] | 581 |
|
---|
[9bfc1cb] | 582 | serverMsg.type = MSG_TYPE_ATTACK;
|
---|
[8dad966] | 583 | memcpy(serverMsg.buffer, &id, 4);
|
---|
| 584 | memcpy(serverMsg.buffer+4, &targetId, 4);
|
---|
[d05c484] | 585 | msgProcessor.broadcastMessage(serverMsg, players);
|
---|
[8a4ed74] | 586 |
|
---|
| 587 | break;
|
---|
[4b4b153] | 588 | }
|
---|
[cdb0e98] | 589 | case MSG_TYPE_PROFILE:
|
---|
| 590 | {
|
---|
[53643ca] | 591 | cout << "Received a PROFILE message" << endl;
|
---|
| 592 |
|
---|
| 593 | unsigned int id;
|
---|
| 594 |
|
---|
| 595 | memcpy(&id, clientMsg.buffer, 4);
|
---|
| 596 |
|
---|
| 597 | cout << "Player id: " << id << endl;
|
---|
| 598 | unsigned int numGames = 0;
|
---|
| 599 | int** gameHistory = da.getPlayerGameHistory(id, numGames);
|
---|
| 600 | int* playerRecord = da.getPlayerRecord(id);
|
---|
| 601 |
|
---|
[c666518] | 602 | // playerRecord[0] is level
|
---|
| 603 | // playerRecord[1] is experience
|
---|
| 604 | int honorPoints = playerRecord[2];
|
---|
| 605 | int wins = playerRecord[3];
|
---|
| 606 | int losses = playerRecord[4];
|
---|
[53643ca] | 607 |
|
---|
[cdb0e98] | 608 | serverMsg.type = MSG_TYPE_PROFILE;
|
---|
| 609 |
|
---|
| 610 | memcpy(serverMsg.buffer, &honorPoints, 4);
|
---|
[53643ca] | 611 | memcpy(serverMsg.buffer+4, &wins, 4);
|
---|
| 612 | memcpy(serverMsg.buffer+8, &losses, 4);
|
---|
| 613 | memcpy(serverMsg.buffer+12, &numGames, 4);
|
---|
| 614 | for (unsigned int i=0; i<numGames; i++) {
|
---|
| 615 | memcpy(serverMsg.buffer+16+i*16, &gameHistory[i][0], 4);
|
---|
| 616 | memcpy(serverMsg.buffer+20+i*16, &gameHistory[i][1], 4);
|
---|
| 617 | memcpy(serverMsg.buffer+24+i*16, &gameHistory[i][2], 4);
|
---|
| 618 | memcpy(serverMsg.buffer+28+i*16, &gameHistory[i][3], 4);
|
---|
| 619 | delete[] gameHistory[i];
|
---|
[cdb0e98] | 620 | }
|
---|
| 621 |
|
---|
[53643ca] | 622 | //delete[] gameHistory;
|
---|
| 623 | free(gameHistory);
|
---|
| 624 | delete[] playerRecord;
|
---|
| 625 |
|
---|
[cdb0e98] | 626 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
| 627 |
|
---|
| 628 | break;
|
---|
| 629 | }
|
---|
[b8f789d] | 630 | case MSG_TYPE_CREATE_GAME:
|
---|
[8e540f4] | 631 | {
|
---|
[b8f789d] | 632 | cout << "Received a CREATE_GAME message" << endl;
|
---|
| 633 |
|
---|
| 634 | string gameName(clientMsg.buffer);
|
---|
| 635 | cout << "Game name: " << gameName << endl;
|
---|
| 636 |
|
---|
[b48ef09] | 637 | // check if this game already exists
|
---|
| 638 | if (mapGames.find(gameName) != mapGames.end()) {
|
---|
[3ef8cf4] | 639 | cout << "Error: Game already exists" << endl;
|
---|
[b48ef09] | 640 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
[8554263] | 641 | }else {
|
---|
[d05c484] | 642 | Game* g = new Game(gameName, "../data/map.txt", &msgProcessor);
|
---|
[8554263] | 643 | mapGames[gameName] = g;
|
---|
| 644 |
|
---|
| 645 | // add flag objects to the map
|
---|
| 646 | WorldMap* m = g->getMap();
|
---|
[0678d60] | 647 | m->createObjectsFromStructures();
|
---|
[8554263] | 648 |
|
---|
| 649 | serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS;
|
---|
| 650 | strcpy(serverMsg.buffer, gameName.c_str());
|
---|
[70fc3e8] | 651 | }
|
---|
| 652 |
|
---|
[8554263] | 653 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[b92e6a7] | 654 |
|
---|
[b48ef09] | 655 | break;
|
---|
| 656 | }
|
---|
| 657 | case MSG_TYPE_JOIN_GAME:
|
---|
| 658 | {
|
---|
| 659 | cout << "Received a JOIN_GAME message" << endl;
|
---|
[b92e6a7] | 660 |
|
---|
[b48ef09] | 661 | string gameName(clientMsg.buffer);
|
---|
| 662 | cout << "Game name: " << gameName << endl;
|
---|
[b92e6a7] | 663 |
|
---|
[b48ef09] | 664 | // check if this game already exists
|
---|
| 665 | if (mapGames.find(gameName) == mapGames.end()) {
|
---|
[3ef8cf4] | 666 | cout << "Error: Game does not exist" << endl;
|
---|
[b48ef09] | 667 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
[8554263] | 668 | }else {
|
---|
| 669 | Game* g = mapGames[gameName];
|
---|
| 670 | map<unsigned int, Player*>& players = g->getPlayers();
|
---|
| 671 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
| 672 |
|
---|
[204edcf] | 673 | if (players.find(p->getId()) != players.end()) {
|
---|
[8554263] | 674 | cout << "Player " << p->name << " trying to join a game he's already in" << endl;
|
---|
| 675 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
| 676 | }else {
|
---|
| 677 | serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS;
|
---|
| 678 | strcpy(serverMsg.buffer, gameName.c_str());
|
---|
| 679 | }
|
---|
[b92e6a7] | 680 | }
|
---|
| 681 |
|
---|
[8554263] | 682 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[b8f789d] | 683 |
|
---|
| 684 | break;
|
---|
| 685 | }
|
---|
[ab8fd40] | 686 | case MSG_TYPE_LEAVE_GAME:
|
---|
| 687 | {
|
---|
| 688 | cout << "Received a LEAVE_GAME message" << endl;
|
---|
| 689 |
|
---|
| 690 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
| 691 | Game* g = p->currentGame;
|
---|
| 692 |
|
---|
| 693 | if (g == NULL) {
|
---|
| 694 | cout << "Player " << p->name << " is trying to leave a game, but is not currently in a game." << endl;
|
---|
| 695 |
|
---|
| 696 | /// should send a response back, maybe a new message type is needed
|
---|
[8554263] | 697 | // not sure what to do here
|
---|
| 698 | }else {
|
---|
| 699 | cout << "Game name: " << g->getName() << endl;
|
---|
[360c0f1] | 700 |
|
---|
| 701 | if (!p->isDead) {
|
---|
[7f884ea] | 702 | ObjectType flagType = OBJECT_NONE;
|
---|
[360c0f1] | 703 | if (p->hasBlueFlag)
|
---|
[7f884ea] | 704 | flagType = OBJECT_BLUE_FLAG;
|
---|
[360c0f1] | 705 | else if (p->hasRedFlag)
|
---|
[7f884ea] | 706 | flagType = OBJECT_RED_FLAG;
|
---|
[360c0f1] | 707 |
|
---|
[7f884ea] | 708 | if (flagType != OBJECT_NONE)
|
---|
[d05c484] | 709 | g->addObjectToMap(flagType, p->pos.x, p->pos.y);
|
---|
[360c0f1] | 710 | }
|
---|
[bcfd99a] | 711 |
|
---|
| 712 | p->currentGame = NULL;
|
---|
[204edcf] | 713 | g->removePlayer(p->getId());
|
---|
[360c0f1] | 714 |
|
---|
[204edcf] | 715 | unsigned int playerId = p->getId();
|
---|
[8554263] | 716 | serverMsg.type = MSG_TYPE_LEAVE_GAME;
|
---|
[204edcf] | 717 | memcpy(serverMsg.buffer, &playerId, 4);
|
---|
[8554263] | 718 | strcpy(serverMsg.buffer+4, g->getName().c_str());
|
---|
[d05c484] | 719 | msgProcessor.broadcastMessage(serverMsg, g->getPlayers());
|
---|
[95ffe57] | 720 |
|
---|
[8554263] | 721 | int numPlayers = g->getNumPlayers();
|
---|
[ab8fd40] | 722 |
|
---|
[8554263] | 723 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
| 724 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
| 725 | strcpy(serverMsg.buffer+4, g->getName().c_str());
|
---|
[d05c484] | 726 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
[8554263] | 727 |
|
---|
| 728 | // if there are no more players in the game, remove it
|
---|
| 729 | if (numPlayers == 0) {
|
---|
| 730 | mapGames.erase(g->getName());
|
---|
| 731 | delete g;
|
---|
| 732 | }
|
---|
[1248984] | 733 | }
|
---|
| 734 |
|
---|
[ab8fd40] | 735 | break;
|
---|
| 736 | }
|
---|
[b48ef09] | 737 | case MSG_TYPE_JOIN_GAME_ACK:
|
---|
[b8f789d] | 738 | {
|
---|
[b48ef09] | 739 | cout << "Received a JOIN_GAME_ACK message" << endl;
|
---|
[e084950] | 740 |
|
---|
[b8f789d] | 741 | string gameName(clientMsg.buffer);
|
---|
| 742 | cout << "Game name: " << gameName << endl;
|
---|
| 743 |
|
---|
[b48ef09] | 744 | // check if this game already exists
|
---|
| 745 | if (mapGames.find(gameName) == mapGames.end()) {
|
---|
| 746 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
[8554263] | 747 |
|
---|
| 748 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[b48ef09] | 749 | }
|
---|
[b92e6a7] | 750 |
|
---|
[f41a7f9] | 751 | Game* g = mapGames[gameName];
|
---|
[b92e6a7] | 752 |
|
---|
[b48ef09] | 753 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
[b92e6a7] | 754 |
|
---|
| 755 | // tell the new player about all map objects
|
---|
| 756 | // (currently just the flags)
|
---|
| 757 |
|
---|
| 758 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
[f41a7f9] | 759 | vector<WorldMap::Object>* vctObjects = g->getMap()->getObjects();
|
---|
[b92e6a7] | 760 | vector<WorldMap::Object>::iterator itObjects;
|
---|
| 761 | cout << "sending items" << endl;
|
---|
| 762 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
| 763 | itObjects->serialize(serverMsg.buffer);
|
---|
| 764 | cout << "sending item id " << itObjects->id << endl;
|
---|
[8554263] | 765 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[b92e6a7] | 766 | }
|
---|
| 767 |
|
---|
| 768 |
|
---|
| 769 | // send the current score
|
---|
| 770 | serverMsg.type = MSG_TYPE_SCORE;
|
---|
| 771 |
|
---|
[9ba9b96] | 772 | unsigned int blueScore = g->getBlueScore();
|
---|
| 773 | unsigned int redScore = g->getRedScore();
|
---|
[f3fb980] | 774 | memcpy(serverMsg.buffer, &blueScore, 4);
|
---|
| 775 | memcpy(serverMsg.buffer+4, &redScore, 4);
|
---|
[b92e6a7] | 776 |
|
---|
[8554263] | 777 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[b92e6a7] | 778 |
|
---|
[64a1f4e] | 779 |
|
---|
| 780 | map<unsigned int, Player*>& oldPlayers = g->getPlayers();
|
---|
[306758e] | 781 | g->addPlayer(p);
|
---|
[7fa452f] | 782 | p->team = 0;
|
---|
[64a1f4e] | 783 |
|
---|
[8554263] | 784 | // send info to other players
|
---|
[453087e] | 785 | serverMsg.type = MSG_TYPE_PLAYER_JOIN_GAME;
|
---|
[b92e6a7] | 786 | p->serialize(serverMsg.buffer);
|
---|
| 787 | cout << "Should be broadcasting the message" << endl;
|
---|
[64a1f4e] | 788 | msgProcessor.broadcastMessage(serverMsg, oldPlayers);
|
---|
[453087e] | 789 |
|
---|
| 790 |
|
---|
| 791 | // tell the new player about all the players in the game (including himself)
|
---|
| 792 | cout << "Sending other players to new player" << endl;
|
---|
| 793 | serverMsg.type = MSG_TYPE_PLAYER_JOIN_GAME;
|
---|
[8554263] | 794 |
|
---|
| 795 | map<unsigned int, Player*>& allPlayers = g->getPlayers();
|
---|
| 796 | map<unsigned int, Player*>::iterator it;
|
---|
[453087e] | 797 | for (it = allPlayers.begin(); it != allPlayers.end(); it++)
|
---|
| 798 | {
|
---|
| 799 | it->second->serialize(serverMsg.buffer);
|
---|
| 800 |
|
---|
| 801 | cout << "sending info about " << it->second->name << endl;
|
---|
[204edcf] | 802 | cout << "sending id " << it->second->getId() << endl;
|
---|
[8554263] | 803 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
[453087e] | 804 | }
|
---|
| 805 |
|
---|
[64a1f4e] | 806 |
|
---|
[b48ef09] | 807 | int numPlayers = g->getNumPlayers();
|
---|
| 808 |
|
---|
[b8f789d] | 809 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
| 810 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
| 811 | strcpy(serverMsg.buffer+4, gameName.c_str());
|
---|
[d05c484] | 812 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
[ab8fd40] | 813 |
|
---|
[b8f789d] | 814 | break;
|
---|
| 815 | }
|
---|
[f15d6a9] | 816 | case MSG_TYPE_JOIN_TEAM:
|
---|
| 817 | {
|
---|
| 818 | cout << "Received a JOIN_TEAM message" << endl;
|
---|
| 819 |
|
---|
| 820 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
| 821 | map<unsigned int, Player*> players = p->currentGame->getPlayers();
|
---|
| 822 |
|
---|
| 823 | memcpy(&(p->team), clientMsg.buffer, 4);
|
---|
| 824 |
|
---|
| 825 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
| 826 | p->serialize(serverMsg.buffer);
|
---|
| 827 | msgProcessor.broadcastMessage(serverMsg, players);
|
---|
[abc4d56] | 828 |
|
---|
| 829 | break;
|
---|
| 830 | }
|
---|
| 831 | case MSG_TYPE_START_GAME:
|
---|
| 832 | {
|
---|
| 833 | cout << "Received a START_GAME message" << endl;
|
---|
| 834 |
|
---|
| 835 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
| 836 | map<unsigned int, Player*> players = p->currentGame->getPlayers();
|
---|
| 837 |
|
---|
| 838 | serverMsg.type = MSG_TYPE_START_GAME;
|
---|
| 839 | msgProcessor.broadcastMessage(serverMsg, players);
|
---|
| 840 |
|
---|
| 841 | break;
|
---|
[f15d6a9] | 842 | }
|
---|
[b8f789d] | 843 | default:
|
---|
| 844 | {
|
---|
[9ee50ce] | 845 | outputLog << "Received unknown message of type " << clientMsg.type << endl;
|
---|
[e084950] | 846 |
|
---|
[8e540f4] | 847 | break;
|
---|
| 848 | }
|
---|
[e3535b3] | 849 | }
|
---|
[8554263] | 850 | }
|
---|
[da692b9] | 851 |
|
---|
[95ffe57] | 852 | Player *findPlayerByName(map<unsigned int, Player*> &m, string name)
|
---|
| 853 | {
|
---|
| 854 | map<unsigned int, Player*>::iterator it;
|
---|
| 855 |
|
---|
| 856 | for (it = m.begin(); it != m.end(); it++)
|
---|
| 857 | {
|
---|
| 858 | if ( it->second->name.compare(name) == 0 )
|
---|
| 859 | return it->second;
|
---|
| 860 | }
|
---|
| 861 |
|
---|
| 862 | return NULL;
|
---|
| 863 | }
|
---|
| 864 |
|
---|
| 865 | Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr)
|
---|
| 866 | {
|
---|
| 867 | map<unsigned int, Player*>::iterator it;
|
---|
| 868 |
|
---|
| 869 | for (it = m.begin(); it != m.end(); it++)
|
---|
| 870 | {
|
---|
| 871 | if ( it->second->addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
|
---|
| 872 | it->second->addr.sin_port == addr.sin_port )
|
---|
| 873 | return it->second;
|
---|
| 874 | }
|
---|
| 875 |
|
---|
| 876 | return NULL;
|
---|
| 877 | }
|
---|
| 878 |
|
---|
[c666518] | 879 | bool handleGameEvents(Game* game, MessageProcessor* msgProcessor, DataAccess* da) {
|
---|
| 880 | map<unsigned int, Player*> players = game->getPlayers();
|
---|
| 881 | map<unsigned int, Player*>::iterator it;
|
---|
| 882 | bool gameFinished = false;
|
---|
| 883 |
|
---|
| 884 | for (it = players.begin(); it != players.end(); it++) {
|
---|
| 885 | gameFinished = gameFinished || handlePlayerEvents(game, it->second, msgProcessor, da);
|
---|
| 886 | }
|
---|
| 887 |
|
---|
| 888 | if (gameFinished) {
|
---|
| 889 | for (it = players.begin(); it != players.end(); it++) {
|
---|
| 890 | it->second->currentGame = NULL;
|
---|
| 891 | }
|
---|
| 892 | }
|
---|
| 893 |
|
---|
| 894 | return gameFinished;
|
---|
| 895 | }
|
---|
| 896 |
|
---|
| 897 | bool handlePlayerEvents(Game* game, Player* p, MessageProcessor* msgProcessor, DataAccess* da) {
|
---|
| 898 | NETWORK_MSG serverMsg;
|
---|
| 899 | FLOAT_POSITION oldPos;
|
---|
| 900 | bool gameFinished = false;
|
---|
| 901 | bool broadcastMove = false;
|
---|
| 902 |
|
---|
| 903 | cout << "moving player" << endl;
|
---|
| 904 |
|
---|
| 905 | // move player and perform associated tasks
|
---|
| 906 | oldPos = p->pos;
|
---|
| 907 | if (p->move(game->getMap())) {
|
---|
| 908 |
|
---|
| 909 | cout << "player moved" << endl;
|
---|
| 910 | if (game->processPlayerMovement(p, oldPos))
|
---|
| 911 | broadcastMove = true;
|
---|
| 912 | cout << "player move processed" << endl;
|
---|
| 913 |
|
---|
| 914 | ObjectType flagType;
|
---|
| 915 | POSITION pos;
|
---|
| 916 | bool flagTurnedIn = false;
|
---|
| 917 | bool flagReturned = false;
|
---|
| 918 | bool ownFlagAtBase = false;
|
---|
| 919 |
|
---|
| 920 | switch(game->getMap()->getStructure(p->pos.x/25, p->pos.y/25)) {
|
---|
| 921 | case STRUCTURE_BLUE_FLAG:
|
---|
| 922 | {
|
---|
[7fa452f] | 923 | if (p->team == 1 && p->hasRedFlag) {
|
---|
[c666518] | 924 | // check that your flag is at your base
|
---|
| 925 | pos = game->getMap()->getStructureLocation(STRUCTURE_BLUE_FLAG);
|
---|
| 926 |
|
---|
| 927 | vector<WorldMap::Object>* vctObjects = game->getMap()->getObjects();
|
---|
| 928 | vector<WorldMap::Object>::iterator itObjects;
|
---|
| 929 |
|
---|
| 930 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
| 931 | if (itObjects->type == OBJECT_BLUE_FLAG) {
|
---|
| 932 | if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
|
---|
| 933 | ownFlagAtBase = true;
|
---|
| 934 | break;
|
---|
| 935 | }
|
---|
| 936 | }
|
---|
| 937 | }
|
---|
| 938 |
|
---|
| 939 | if (ownFlagAtBase) {
|
---|
| 940 | p->hasRedFlag = false;
|
---|
| 941 | flagType = OBJECT_RED_FLAG;
|
---|
| 942 | pos = game->getMap()->getStructureLocation(STRUCTURE_RED_FLAG);
|
---|
| 943 | flagTurnedIn = true;
|
---|
| 944 | game->setBlueScore(game->getBlueScore()+1);
|
---|
| 945 | }
|
---|
| 946 | }
|
---|
| 947 |
|
---|
| 948 | break;
|
---|
| 949 | }
|
---|
| 950 | case STRUCTURE_RED_FLAG:
|
---|
| 951 | {
|
---|
[7fa452f] | 952 | if (p->team == 2 && p->hasBlueFlag) {
|
---|
[c666518] | 953 | // check that your flag is at your base
|
---|
| 954 | pos = game->getMap()->getStructureLocation(STRUCTURE_RED_FLAG);
|
---|
| 955 |
|
---|
| 956 | vector<WorldMap::Object>* vctObjects = game->getMap()->getObjects();
|
---|
| 957 | vector<WorldMap::Object>::iterator itObjects;
|
---|
| 958 |
|
---|
| 959 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
| 960 | if (itObjects->type == OBJECT_RED_FLAG) {
|
---|
| 961 | if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
|
---|
| 962 | ownFlagAtBase = true;
|
---|
| 963 | break;
|
---|
| 964 | }
|
---|
| 965 | }
|
---|
| 966 | }
|
---|
| 967 |
|
---|
| 968 | if (ownFlagAtBase) {
|
---|
| 969 | p->hasBlueFlag = false;
|
---|
| 970 | flagType = OBJECT_BLUE_FLAG;
|
---|
| 971 | pos = game->getMap()->getStructureLocation(STRUCTURE_BLUE_FLAG);
|
---|
| 972 | flagTurnedIn = true;
|
---|
| 973 | game->setRedScore(game->getRedScore()+1);
|
---|
| 974 | }
|
---|
| 975 | }
|
---|
| 976 |
|
---|
| 977 | break;
|
---|
| 978 | }
|
---|
| 979 | default:
|
---|
| 980 | {
|
---|
| 981 | break;
|
---|
| 982 | }
|
---|
| 983 | }
|
---|
| 984 |
|
---|
| 985 | if (flagTurnedIn) {
|
---|
| 986 | unsigned int blueScore = game->getBlueScore();
|
---|
| 987 | unsigned int redScore = game->getRedScore();
|
---|
| 988 |
|
---|
| 989 | pos.x = pos.x*25+12;
|
---|
| 990 | pos.y = pos.y*25+12;
|
---|
| 991 | game->addObjectToMap(flagType, pos.x, pos.y);
|
---|
| 992 |
|
---|
| 993 | serverMsg.type = MSG_TYPE_SCORE;
|
---|
| 994 | memcpy(serverMsg.buffer, &blueScore, 4);
|
---|
| 995 | memcpy(serverMsg.buffer+4, &redScore, 4);
|
---|
| 996 | msgProcessor->broadcastMessage(serverMsg, game->getPlayers());
|
---|
| 997 |
|
---|
| 998 | // give honor to everyone on the winning team
|
---|
| 999 | map<unsigned int, Player*>::iterator itPlayers;
|
---|
| 1000 | for (itPlayers = game->getPlayers().begin(); itPlayers != game->getPlayers().end(); itPlayers++) {
|
---|
| 1001 | if (itPlayers->second->team == p->team) {
|
---|
| 1002 | itPlayers->second->honor += 50;
|
---|
| 1003 | da->updatePlayer(itPlayers->second);
|
---|
| 1004 | }
|
---|
| 1005 | }
|
---|
| 1006 |
|
---|
| 1007 | // check to see if the game should end
|
---|
| 1008 | // move to its own method
|
---|
| 1009 | if (game->getBlueScore() == 3 || game->getRedScore() == 3) {
|
---|
| 1010 | gameFinished = true;
|
---|
| 1011 |
|
---|
| 1012 | unsigned int winningTeam;
|
---|
| 1013 | if (game->getBlueScore() == 3)
|
---|
| 1014 | winningTeam = 0;
|
---|
| 1015 | else if (game->getRedScore() == 3)
|
---|
| 1016 | winningTeam = 1;
|
---|
| 1017 |
|
---|
| 1018 | serverMsg.type = MSG_TYPE_FINISH_GAME;
|
---|
| 1019 |
|
---|
| 1020 | // I should create an instance of the GameSummary object here and just serialize it into this message
|
---|
| 1021 | memcpy(serverMsg.buffer, &winningTeam, 4);
|
---|
| 1022 | memcpy(serverMsg.buffer+4, &blueScore, 4);
|
---|
| 1023 | memcpy(serverMsg.buffer+8, &redScore, 4);
|
---|
| 1024 | strcpy(serverMsg.buffer+12, game->getName().c_str());
|
---|
| 1025 | msgProcessor->broadcastMessage(serverMsg, game->getPlayers());
|
---|
| 1026 |
|
---|
| 1027 | // give honor to everyone on the winning team
|
---|
| 1028 | for (itPlayers = game->getPlayers().begin(); itPlayers != game->getPlayers().end(); itPlayers++) {
|
---|
| 1029 | if (itPlayers->second->team == p->team) {
|
---|
| 1030 | itPlayers->second->honor += 150;
|
---|
| 1031 | da->updatePlayer(itPlayers->second);
|
---|
| 1032 | }
|
---|
| 1033 | }
|
---|
| 1034 | }
|
---|
| 1035 |
|
---|
| 1036 | // this means a PLAYER message will be sent
|
---|
| 1037 | broadcastMove = true;
|
---|
| 1038 | }
|
---|
| 1039 |
|
---|
| 1040 | // go through all objects and check if the player is close to one and if its their flag
|
---|
| 1041 | vector<WorldMap::Object>* vctObjects = game->getMap()->getObjects();
|
---|
| 1042 | vector<WorldMap::Object>::iterator itObjects;
|
---|
| 1043 | POSITION structPos;
|
---|
| 1044 |
|
---|
| 1045 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
| 1046 | POSITION pos = itObjects->pos;
|
---|
| 1047 |
|
---|
| 1048 | if (posDistance(p->pos, pos.toFloat()) < 10) {
|
---|
[7fa452f] | 1049 | if (p->team == 1 && itObjects->type == OBJECT_BLUE_FLAG) {
|
---|
[c666518] | 1050 | structPos = game->getMap()->getStructureLocation(STRUCTURE_BLUE_FLAG);
|
---|
| 1051 | flagReturned = true;
|
---|
| 1052 | break;
|
---|
[7fa452f] | 1053 | } else if (p->team == 2 && itObjects->type == OBJECT_RED_FLAG) {
|
---|
[c666518] | 1054 | structPos = game->getMap()->getStructureLocation(STRUCTURE_RED_FLAG);
|
---|
| 1055 | flagReturned = true;
|
---|
| 1056 | break;
|
---|
| 1057 | }
|
---|
| 1058 | }
|
---|
| 1059 | }
|
---|
| 1060 |
|
---|
| 1061 | if (flagReturned) {
|
---|
| 1062 | itObjects->pos.x = structPos.x*25+12;
|
---|
| 1063 | itObjects->pos.y = structPos.y*25+12;
|
---|
| 1064 |
|
---|
| 1065 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
| 1066 | itObjects->serialize(serverMsg.buffer);
|
---|
| 1067 | msgProcessor->broadcastMessage(serverMsg, game->getPlayers());
|
---|
| 1068 | }
|
---|
| 1069 |
|
---|
| 1070 | if (broadcastMove) {
|
---|
| 1071 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
| 1072 | p->serialize(serverMsg.buffer);
|
---|
| 1073 | msgProcessor->broadcastMessage(serverMsg, game->getPlayers());
|
---|
| 1074 | }
|
---|
| 1075 | }
|
---|
| 1076 |
|
---|
| 1077 | cout << "processing player attack" << endl;
|
---|
| 1078 |
|
---|
| 1079 | // check if the player's attack animation is complete
|
---|
| 1080 | if (p->isAttacking && p->timeAttackStarted+p->attackCooldown <= getCurrentMillis()) {
|
---|
| 1081 | p->isAttacking = false;
|
---|
| 1082 | cout << "Attack animation is complete" << endl;
|
---|
| 1083 |
|
---|
| 1084 | //send everyone an ATTACK message
|
---|
| 1085 | cout << "about to broadcast attack" << endl;
|
---|
| 1086 |
|
---|
| 1087 | if (p->attackType == Player::ATTACK_MELEE) {
|
---|
| 1088 | cout << "Melee attack" << endl;
|
---|
| 1089 |
|
---|
| 1090 | Player* target = game->getPlayers()[p->getTargetPlayer()];
|
---|
| 1091 | game->dealDamageToPlayer(target, p->damage);
|
---|
| 1092 | } else if (p->attackType == Player::ATTACK_RANGED) {
|
---|
| 1093 | cout << "Ranged attack" << endl;
|
---|
| 1094 |
|
---|
| 1095 | Projectile proj(p->pos.x, p->pos.y, p->getTargetPlayer(), p->damage);
|
---|
| 1096 | game->assignProjectileId(&proj);
|
---|
| 1097 | game->addProjectile(proj);
|
---|
| 1098 |
|
---|
| 1099 | int x = p->pos.x;
|
---|
| 1100 | int y = p->pos.y;
|
---|
| 1101 | unsigned int targetId = p->getTargetPlayer();
|
---|
| 1102 |
|
---|
| 1103 | serverMsg.type = MSG_TYPE_PROJECTILE;
|
---|
| 1104 | memcpy(serverMsg.buffer, &proj.id, 4);
|
---|
| 1105 | memcpy(serverMsg.buffer+4, &x, 4);
|
---|
| 1106 | memcpy(serverMsg.buffer+8, &y, 4);
|
---|
| 1107 | memcpy(serverMsg.buffer+12, &targetId, 4);
|
---|
| 1108 | msgProcessor->broadcastMessage(serverMsg, game->getPlayers());
|
---|
| 1109 | } else
|
---|
| 1110 | cout << "Invalid attack type: " << p->attackType << endl;
|
---|
| 1111 | }
|
---|
| 1112 |
|
---|
| 1113 | return gameFinished;
|
---|
| 1114 | }
|
---|
| 1115 |
|
---|
[f3fb980] | 1116 | void quit(int sig) {
|
---|
| 1117 | done = true;
|
---|
| 1118 | }
|
---|