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

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

The server correctly handles LEAVE_GAME mesages

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