source: network-game/server/server.cpp@ d3efa1a

Last change on this file since d3efa1a was d3efa1a, checked in by dportnoy <dmp1488@…>, 11 years ago

A player gets a list of existing games when they login

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