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