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