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

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

Move player chasing behavior to the Player class so the same behavior can be run client-side

  • Property mode set to 100644
File size: 30.2 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>
[edfd1d0]7#include <cstring>
[60017fc]8#include <cmath>
[371ce29]9
[01d0d00]10#include <vector>
11#include <map>
12
[d211210]13#include <sys/time.h>
14
[73f75c1]15#include <sys/socket.h>
[371ce29]16#include <netdb.h>
[73f75c1]17#include <netinet/in.h>
18#include <arpa/inet.h>
19
[b128109]20#include <crypt.h>
21
[edfd1d0]22/*
[e3535b3]23#include <openssl/bio.h>
24#include <openssl/ssl.h>
25#include <openssl/err.h>
[edfd1d0]26*/
[e3535b3]27
[b53c6b3]28#include "../common/Compiler.h"
[3b1efcc]29#include "../common/Common.h"
[edfd1d0]30#include "../common/Message.h"
[60017fc]31#include "../common/WorldMap.h"
[edfd1d0]32#include "../common/Player.h"
[8dad966]33#include "../common/Projectile.h"
[b53c6b3]34
[36082e8]35#include "DataAccess.h"
[d2b411a]36
[e3535b3]37using namespace std;
38
[d211210]39// from used to be const. Removed that so I could take a reference
40// and use it to send messages
[8dad966]41bool processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG &serverMsg, int sock, int &scoreBlue, int &scoreRed);
[01d0d00]42
[8dad966]43void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers);
44void updateUnusedProjectileId(unsigned int& id, map<unsigned int, Projectile>& mapProjectiles);
[8e540f4]45
[73f75c1]46// this should probably go somewhere in the common folder
[e3535b3]47void error(const char *msg)
48{
49 perror(msg);
50 exit(0);
51}
52
[01d0d00]53Player *findPlayerByName(map<unsigned int, Player> &m, string name)
[2488852]54{
[01d0d00]55 map<unsigned int, Player>::iterator it;
[2488852]56
[01d0d00]57 for (it = m.begin(); it != m.end(); it++)
[2488852]58 {
[01d0d00]59 if ( it->second.name.compare(name) == 0 )
60 return &(it->second);
[2488852]61 }
62
63 return NULL;
64}
65
[01d0d00]66Player *findPlayerByAddr(map<unsigned int, Player> &m, const sockaddr_in &addr)
[73f75c1]67{
[01d0d00]68 map<unsigned int, Player>::iterator it;
[73f75c1]69
[01d0d00]70 for (it = m.begin(); it != m.end(); it++)
[73f75c1]71 {
[01d0d00]72 if ( it->second.addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
73 it->second.addr.sin_port == addr.sin_port )
74 return &(it->second);
[73f75c1]75 }
76
77 return NULL;
78}
79
[e3535b3]80int main(int argc, char *argv[])
81{
82 int sock, length, n;
83 struct sockaddr_in server;
[3b1efcc]84 struct sockaddr_in from; // info of client sending the message
[e084950]85 NETWORK_MSG clientMsg, serverMsg;
[01d0d00]86 map<unsigned int, Player> mapPlayers;
[8dad966]87 map<unsigned int, Projectile> mapProjectiles;
88 unsigned int unusedPlayerId = 1, unusedProjectileId = 1;
[b8601ee]89 int scoreBlue, scoreRed;
90
91 scoreBlue = 0;
92 scoreRed = 0;
[e084950]93
[edfd1d0]94 //SSL_load_error_strings();
95 //ERR_load_BIO_strings();
96 //OpenSSL_add_all_algorithms();
[e3535b3]97
98 if (argc < 2) {
[73f75c1]99 cerr << "ERROR, no port provided" << endl;
100 exit(1);
[e3535b3]101 }
[60017fc]102
[7b43385]103 WorldMap* gameMap = WorldMap::loadMapFromFile("../data/map.txt");
[6e66ffd]104
105 // add some items to the map. They will be sent out
106 // to players when they login
[5f868c0]107 for (int y=0; y<gameMap->height; y++) {
108 for (int x=0; x<gameMap->width; x++) {
109 switch (gameMap->getStructure(x, y)) {
110 case WorldMap::STRUCTURE_BLUE_FLAG:
111 gameMap->addObject(WorldMap::OBJECT_BLUE_FLAG, x*25+12, y*25+12);
112 break;
113 case WorldMap::STRUCTURE_RED_FLAG:
114 gameMap->addObject(WorldMap::OBJECT_RED_FLAG, x*25+12, y*25+12);
115 break;
116 }
117 }
118 }
[6e66ffd]119
[371ce29]120 sock = socket(AF_INET, SOCK_DGRAM, 0);
[e3535b3]121 if (sock < 0) error("Opening socket");
122 length = sizeof(server);
123 bzero(&server,length);
124 server.sin_family=AF_INET;
125 server.sin_port=htons(atoi(argv[1]));
[2488852]126 server.sin_addr.s_addr=INADDR_ANY;
127 if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
[e084950]128 error("binding");
[73f75c1]129
[371ce29]130 set_nonblock(sock);
131
[da692b9]132 bool broadcastResponse;
[d211210]133 timespec ts;
[430c80e]134 int timeLastUpdated = 0, curTime = 0, timeLastBroadcast = 0;
[cb1f288]135 while (true) {
[371ce29]136
137 usleep(5000);
138
[d211210]139 clock_gettime(CLOCK_REALTIME, &ts);
[430c80e]140 // make the number smaller so millis can fit in an int
[d69eb32]141 ts.tv_sec -= 1368000000;
[430c80e]142 curTime = ts.tv_sec*1000 + ts.tv_nsec/1000000;
[d211210]143
[430c80e]144 if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50) {
[d211210]145 timeLastUpdated = curTime;
146
[11d21ee]147 map<unsigned int, Player>::iterator it;
148
149 // set targets for all chasing players (or make them attack if they're close enough)
150 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
[ff2133a]151 //Player* p = &it->second;
152 it->second.updateTarget(mapPlayers);
[11d21ee]153
[ff2133a]154 /*
[11d21ee]155 if (p->isChasing) {
156 p->target.x = mapPlayers[p->targetPlayer].pos.x;
157 p->target.y = mapPlayers[p->targetPlayer].pos.y;
158
159 if (posDistance(p->pos, p->target.toFloat()) <= p->range) {
160 p->target.x = p->pos.x;
161 p->target.y = p->pos.y;
162
163 p->isChasing = false;
164 p->isAttacking = true;
165 p->timeAttackStarted = getCurrentMillis();
166 }
167 }
[ff2133a]168 */
[11d21ee]169 }
170
[8dad966]171 // move all players
[d211210]172 // maybe put this in a separate method
[23559e7]173 FLOAT_POSITION oldPos;
174 bool broadcastMove = false;
[d211210]175 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++) {
[ff2133a]176 cout << "Starting new for loop iteration" << endl;
[23559e7]177 oldPos = it->second.pos;
178 if (it->second.move(gameMap)) {
179
180 // check if the move needs to be canceled
181 switch(gameMap->getElement(it->second.pos.x/25, it->second.pos.y/25)) {
182 case WorldMap::TERRAIN_NONE:
183 case WorldMap::TERRAIN_OCEAN:
184 case WorldMap::TERRAIN_ROCK:
[e4c60ba]185 {
[23559e7]186 it->second.pos = oldPos;
187 it->second.target.x = it->second.pos.x;
188 it->second.target.y = it->second.pos.y;
189 broadcastMove = true;
190 break;
[e4c60ba]191 }
[23559e7]192 default:
193 // if there are no obstacles, do nothing
194 break;
195 }
196
[e4c60ba]197 WorldMap::ObjectType flagType;
198 POSITION pos;
[7553db9]199 bool flagTurnedIn = false;
[446dc65]200 bool flagReturned = false;
201 bool ownFlagAtBase = false;
202
[e4c60ba]203 switch(gameMap->getStructure(it->second.pos.x/25, it->second.pos.y/25)) {
204 case WorldMap::STRUCTURE_BLUE_FLAG:
205 {
[7553db9]206 if (it->second.team == 0 && it->second.hasRedFlag)
207 {
[446dc65]208 // check that your flag is at your base
209 pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
210
211 vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
212 vector<WorldMap::Object>::iterator itObjects;
213
214 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
215 if (itObjects->type == WorldMap::OBJECT_BLUE_FLAG) {
216 if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
217 ownFlagAtBase = true;
218 break;
219 }
220 }
221 }
222
223 if (ownFlagAtBase) {
224 it->second.hasRedFlag = false;
225 flagType = WorldMap::OBJECT_RED_FLAG;
226 pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
227 flagTurnedIn = true;
228 scoreBlue++;
229 }
[e4c60ba]230 }
[7553db9]231
232 break;
[e4c60ba]233 }
234 case WorldMap::STRUCTURE_RED_FLAG:
235 {
[7553db9]236 if (it->second.team == 1 && it->second.hasBlueFlag)
237 {
[446dc65]238 // check that your flag is at your base
239 pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
240
241 vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
242 vector<WorldMap::Object>::iterator itObjects;
243
244 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
245 if (itObjects->type == WorldMap::OBJECT_RED_FLAG) {
246 if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
247 ownFlagAtBase = true;
248 break;
249 }
250 }
251 }
252
253 if (ownFlagAtBase) {
254 it->second.hasBlueFlag = false;
255 flagType = WorldMap::OBJECT_BLUE_FLAG;
256 pos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
257 flagTurnedIn = true;
258 scoreRed++;
259 }
[e4c60ba]260 }
261
[7553db9]262 break;
263 }
264 }
[e4c60ba]265
[7553db9]266 if (flagTurnedIn) {
267 // send an OBJECT message to add the flag back to its spawn point
268 pos.x = pos.x*25+12;
269 pos.y = pos.y*25+12;
270 gameMap->addObject(flagType, pos.x, pos.y);
[e4c60ba]271
[7553db9]272 serverMsg.type = MSG_TYPE_OBJECT;
273 gameMap->getObjects()->back().serialize(serverMsg.buffer);
[e4c60ba]274
[7553db9]275 map<unsigned int, Player>::iterator it2;
276 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
277 {
278 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
279 error("sendMessage");
[e4c60ba]280 }
[7553db9]281
[b8601ee]282 serverMsg.type = MSG_TYPE_SCORE;
283 memcpy(serverMsg.buffer, &scoreBlue, 4);
284 memcpy(serverMsg.buffer+4, &scoreRed, 4);
285
286 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
287 {
288 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
289 error("sendMessage");
290 }
291
[7553db9]292 // this means a PLAYER message will be sent
293 broadcastMove = true;
[e4c60ba]294 }
295
[446dc65]296 // go through all objects and check if the player is close to one and if its their flag
297 vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
298 vector<WorldMap::Object>::iterator itObjects;
299 POSITION structPos;
300
301 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
302 POSITION pos = itObjects->pos;
303
304 if (posDistance(it->second.pos, pos.toFloat()) < 10) {
305 if (it->second.team == 0 &&
306 itObjects->type == WorldMap::OBJECT_BLUE_FLAG) {
307 structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
308 flagReturned = true;
309 break;
310 } else if (it->second.team == 1 &&
311 itObjects->type == WorldMap::OBJECT_RED_FLAG) {
312 structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
313 flagReturned = true;
314 break;
315 }
316 }
317 }
318
319 if (flagReturned) {
320 itObjects->pos.x = structPos.x*25+12;
321 itObjects->pos.y = structPos.y*25+12;
322
323 serverMsg.type = MSG_TYPE_OBJECT;
324 itObjects->serialize(serverMsg.buffer);
325
326 map<unsigned int, Player>::iterator it2;
327 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
328 {
329 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
330 error("sendMessage");
331 }
332 }
333
[23559e7]334 if (broadcastMove) {
335 serverMsg.type = MSG_TYPE_PLAYER;
336 it->second.serialize(serverMsg.buffer);
337
338 cout << "about to broadcast move" << endl;
[b07eeac]339 map<unsigned int, Player>::iterator it2;
[23559e7]340 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
341 {
342 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
343 error("sendMessage");
344 }
[d211210]345 }
346 }
[8dad966]347
348 // check if the player's attack animation is complete
349 if (it->second.isAttacking && it->second.timeAttackStarted+it->second.attackCooldown <= getCurrentMillis()) {
350 it->second.isAttacking = false;
[8795a38]351 cout << "Attack animation is complete" << endl;
[8dad966]352
353 //send everyone an ATTACK message
354 cout << "about to broadcast attack" << endl;
355
356 serverMsg.type = MSG_TYPE_ATTACK;
357 memcpy(serverMsg.buffer, &it->second.id, 4);
358 memcpy(serverMsg.buffer+4, &it->second.targetPlayer, 4);
359
360 map<unsigned int, Player>::iterator it2;
361 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
362 {
363 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
364 error("sendMessage");
365 }
366
367 if (it->second.attackType == Player::ATTACK_MELEE) {
[ff2133a]368 cout << "Melee attack" << endl;
369
[8dad966]370 Player* target = &mapPlayers[it->second.targetPlayer];
371
372 target->health -= it->second.damage;
373 if (target->health < 0)
374 target->health = 0;
375
376 serverMsg.type = MSG_TYPE_PLAYER;
377 target->serialize(serverMsg.buffer);
378 }else if (it->second.attackType == Player::ATTACK_RANGED) {
[ff2133a]379 cout << "Ranged attack" << endl;
380
[8dad966]381 Projectile proj(it->second.pos.x, it->second.pos.y, it->second.targetPlayer, it->second.damage);
382 proj.id = unusedProjectileId;
383 updateUnusedProjectileId(unusedProjectileId, mapProjectiles);
384 mapProjectiles[proj.id] = proj;
385
[8795a38]386 int x = it->second.pos.x;
387 int y = it->second.pos.y;
388
[8dad966]389 serverMsg.type = MSG_TYPE_PROJECTILE;
[8795a38]390 memcpy(serverMsg.buffer, &proj.id, 4);
391 memcpy(serverMsg.buffer+4, &x, 4);
392 memcpy(serverMsg.buffer+8, &y, 4);
393 memcpy(serverMsg.buffer+12, &it->second.targetPlayer, 4);
[8dad966]394 }else {
395 cout << "Invalid attack type: " << it->second.attackType << endl;
396 }
397
398 // broadcast either a PLAYER or PROJECTILE message
[ff2133a]399 cout << "Broadcasting player or projectile message" << endl;
[8dad966]400 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
401 {
[ff2133a]402 if (sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
[8dad966]403 error("sendMessage");
404 }
[ff2133a]405 cout << "Done broadcasting" << endl;
[8dad966]406 }
407 }
408
[ff2133a]409 cout << "Done with the for loop" << endl;
410
[8dad966]411 // move all projectiles
[ff2133a]412 cout << "Moving projectiles" << endl;
[8dad966]413 map<unsigned int, Projectile>::iterator itProj;
414 for (itProj = mapProjectiles.begin(); itProj != mapProjectiles.end(); itProj++) {
415 if (itProj->second.move(mapPlayers)) {
416 // send a REMOVE_PROJECTILE message
[ff2133a]417 cout << "send a REMOVE_PROJECTILE message" << endl;
[8dad966]418 serverMsg.type = MSG_TYPE_REMOVE_PROJECTILE;
419 memcpy(serverMsg.buffer, &itProj->second.id, 4);
420 mapProjectiles.erase(itProj->second.id);
421
422 map<unsigned int, Player>::iterator it2;
[ff2133a]423 cout << "Broadcasting REMOVE_PROJECTILE" << endl;
[8dad966]424 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
425 {
426 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
427 error("sendMessage");
428 }
429
[ff2133a]430 cout << "send a PLAYER message after dealing damage" << endl;
[8dad966]431 // send a PLAYER message after dealing damage
[8795a38]432 Player* target = &mapPlayers[itProj->second.target];
[8dad966]433
434 target->health -= itProj->second.damage;
435 if (target->health < 0)
436 target->health = 0;
437
438 serverMsg.type = MSG_TYPE_PLAYER;
439 target->serialize(serverMsg.buffer);
440
[ff2133a]441 cout << "Sending a PLAYER message" << endl;
[8dad966]442 for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
443 {
444 if ( sendMessage(&serverMsg, sock, &(it2->second.addr)) < 0 )
445 error("sendMessage");
446 }
447 }
[d211210]448 }
449 }
[ff2133a]450 cout << "Done moving projectiles" << endl;
[d211210]451
[e084950]452 n = receiveMessage(&clientMsg, sock, &from);
[8e540f4]453
[371ce29]454 if (n >= 0) {
[8dad966]455 broadcastResponse = processMessage(clientMsg, from, mapPlayers, gameMap, unusedPlayerId, serverMsg, sock, scoreBlue, scoreRed);
[371ce29]456
[da692b9]457 if (broadcastResponse)
[3b1efcc]458 {
[da692b9]459 cout << "Should be broadcasting the message" << endl;
460
[01d0d00]461 map<unsigned int, Player>::iterator it;
462 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
[3b1efcc]463 {
[d211210]464 cout << "Sent message back to " << it->second.name << endl;
[01d0d00]465 if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
[3b1efcc]466 error("sendMessage");
467 }
468 }
469 else
470 {
[da692b9]471 cout << "Should be sending back the message" << endl;
472
[3b1efcc]473 if ( sendMessage(&serverMsg, sock, &from) < 0 )
474 error("sendMessage");
475 }
[7b43385]476 }
[8e540f4]477 }
[371ce29]478
[8e540f4]479 return 0;
480}
481
[8dad966]482bool processMessage(const NETWORK_MSG& clientMsg, struct sockaddr_in& from, map<unsigned int, Player>& mapPlayers, WorldMap* gameMap, unsigned int& unusedPlayerId, NETWORK_MSG& serverMsg, int sock, int &scoreBlue, int &scoreRed)
[8e540f4]483{
[41ad8ed]484 DataAccess da;
485
[b8cb03f]486 cout << "Received message" << endl;
[8e540f4]487 cout << "MSG: type: " << clientMsg.type << endl;
488 cout << "MSG contents: " << clientMsg.buffer << endl;
489
[da692b9]490 // maybe we should make a message class and have this be a member
[3b1efcc]491 bool broadcastResponse = false;
492
[8e540f4]493 // Check that if an invalid message is sent, the client will correctly
494 // receive and display the response. Maybe make a special error msg type
495 switch(clientMsg.type)
496 {
497 case MSG_TYPE_REGISTER:
[d2b411a]498 {
[8e540f4]499 string username(clientMsg.buffer);
500 string password(strchr(clientMsg.buffer, '\0')+1);
[d2b411a]501
[8e540f4]502 cout << "username: " << username << endl;
503 cout << "password: " << password << endl;
[d2b411a]504
[3b1efcc]505 int error = da.insertPlayer(username, password);
[41ad8ed]506
[3b1efcc]507 if (!error)
508 strcpy(serverMsg.buffer, "Registration successful.");
509 else
510 strcpy(serverMsg.buffer, "Registration failed. Please try again.");
[8e540f4]511
512 serverMsg.type = MSG_TYPE_REGISTER;
[d2b411a]513
[8e540f4]514 break;
515 }
516 case MSG_TYPE_LOGIN:
517 {
[60017fc]518 cout << "Got login message" << endl;
519
[d211210]520 serverMsg.type = MSG_TYPE_LOGIN;
521
[8e540f4]522 string username(clientMsg.buffer);
[41ad8ed]523 string password(strchr(clientMsg.buffer, '\0')+1);
[8e540f4]524
[41ad8ed]525 Player* p = da.getPlayer(username);
[d2b411a]526
[b128109]527 if (p == NULL || !da.verifyPassword(password, p->password))
[41ad8ed]528 {
529 strcpy(serverMsg.buffer, "Incorrect username or password");
530 }
[01d0d00]531 else if(findPlayerByName(mapPlayers, username) != NULL)
[41ad8ed]532 {
533 strcpy(serverMsg.buffer, "Player has already logged in.");
534 }
535 else
[8e540f4]536 {
[d211210]537 serverMsg.type = MSG_TYPE_PLAYER;
538
[8dad966]539 updateUnusedPlayerId(unusedPlayerId, mapPlayers);
540 p->id = unusedPlayerId;
[d211210]541 cout << "new player id: " << p->id << endl;
[df79cfd]542 p->setAddr(from);
543
544 // choose a random team (either 0 or 1)
545 p->team = rand() % 2;
[d211210]546
[46fa35a]547 // choose a random class
548 int intClass = rand() % 2;
549 switch (intClass) {
550 case 0:
551 p->setClass(Player::CLASS_WARRIOR);
552 break;
553 case 1:
554 p->setClass(Player::CLASS_RANGER);
555 break;
556 }
557
[d211210]558 // tell the new player about all the existing players
559 cout << "Sending other players to new player" << endl;
560
561 map<unsigned int, Player>::iterator it;
562 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
563 {
564 it->second.serialize(serverMsg.buffer);
565
566 cout << "sending info about " << it->second.name << endl;
[5f868c0]567 cout << "sending id " << it->second.id << endl;
568 if ( sendMessage(&serverMsg, sock, &from) < 0 )
569 error("sendMessage");
570 }
571
572 // tell the new player about all map objects
573 // (currently just the flags)
574 serverMsg.type = MSG_TYPE_OBJECT;
[e487381]575 vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
[5f868c0]576 vector<WorldMap::Object>::iterator itObjects;
577 cout << "sending items" << endl;
[e487381]578 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
[5f868c0]579 itObjects->serialize(serverMsg.buffer);
580 cout << "sending item id " << itObjects->id << endl;
[d211210]581 if ( sendMessage(&serverMsg, sock, &from) < 0 )
582 error("sendMessage");
583 }
[59061f6]584
[b8601ee]585 // send the current score
586 serverMsg.type = MSG_TYPE_SCORE;
587 memcpy(serverMsg.buffer, &scoreBlue, 4);
588 memcpy(serverMsg.buffer+4, &scoreRed, 4);
589 if ( sendMessage(&serverMsg, sock, &from) < 0 )
590 error("sendMessage");
591
592 serverMsg.type = MSG_TYPE_PLAYER;
[594d2e9]593 p->serialize(serverMsg.buffer);
[d211210]594 cout << "Should be broadcasting the message" << endl;
595
596 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
597 {
598 cout << "Sent message back to " << it->second.name << endl;
599 if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
600 error("sendMessage");
601 }
602
603 serverMsg.type = MSG_TYPE_LOGIN;
[8dad966]604 mapPlayers[unusedPlayerId] = *p;
[07028b9]605 }
606
[41ad8ed]607 delete(p);
[07028b9]608
[8e540f4]609 break;
610 }
611 case MSG_TYPE_LOGOUT:
612 {
613 string name(clientMsg.buffer);
614 cout << "Player logging out: " << name << endl;
615
[01d0d00]616 Player *p = findPlayerByName(mapPlayers, name);
[633f42a]617
[8e540f4]618 if (p == NULL)
619 {
620 strcpy(serverMsg.buffer, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
[8a3ef42]621 cout << "Player not logged in" << endl;
[07028b9]622 }
[01d0d00]623 else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
624 p->addr.sin_port != from.sin_port )
[07028b9]625 {
[8e540f4]626 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]627 cout << "Player logged in using a different connection" << endl;
[2488852]628 }
[8e540f4]629 else
[2488852]630 {
[8dad966]631 if (p->id < unusedPlayerId)
632 unusedPlayerId = p->id;
[01d0d00]633 mapPlayers.erase(p->id);
[41ad8ed]634 strcpy(serverMsg.buffer, "You have successfully logged out.");
[8e540f4]635 }
[07028b9]636
[d211210]637 serverMsg.type = MSG_TYPE_LOGOUT;
[8a3ef42]638
[8e540f4]639 break;
640 }
641 case MSG_TYPE_CHAT:
642 {
[da692b9]643 cout << "Got a chat message" << endl;
644
[01d0d00]645 Player *p = findPlayerByAddr(mapPlayers, from);
[07028b9]646
[8e540f4]647 if (p == NULL)
648 {
649 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]650 }
[8e540f4]651 else
652 {
[3b1efcc]653 broadcastResponse = true;
654
[b128109]655 ostringstream oss;
656 oss << p->name << ": " << clientMsg.buffer;
[3b1efcc]657
[b128109]658 strcpy(serverMsg.buffer, oss.str().c_str());
[8e540f4]659 }
660
661 serverMsg.type = MSG_TYPE_CHAT;
662
663 break;
[e084950]664 }
[b128109]665 case MSG_TYPE_PLAYER_MOVE:
666 {
667 cout << "PLAYER_MOVE" << endl;
668
669 int id, x, y;
670
671 memcpy(&id, clientMsg.buffer, 4);
672 memcpy(&x, clientMsg.buffer+4, 4);
673 memcpy(&y, clientMsg.buffer+8, 4);
[7b43385]674
[b128109]675 cout << "x: " << x << endl;
676 cout << "y: " << y << endl;
677 cout << "id: " << id << endl;
[7b43385]678
[b128109]679 if ( mapPlayers[id].addr.sin_addr.s_addr == from.sin_addr.s_addr &&
680 mapPlayers[id].addr.sin_port == from.sin_port )
681 {
[60017fc]682 // we need to make sure the player can move here
683 if (0 <= x && x < 300 && 0 <= y && y < 300 &&
684 gameMap->getElement(x/25, y/25) == WorldMap::TERRAIN_GRASS)
685 {
[7b43385]686 cout << "valid terrain" << endl;
687
[60017fc]688 mapPlayers[id].target.x = x;
689 mapPlayers[id].target.y = y;
690
691 serverMsg.type = MSG_TYPE_PLAYER_MOVE;
692
693 memcpy(serverMsg.buffer, &id, 4);
[d211210]694 memcpy(serverMsg.buffer+4, &mapPlayers[id].target.x, 4);
695 memcpy(serverMsg.buffer+8, &mapPlayers[id].target.y, 4);
[60017fc]696
697 broadcastResponse = true;
698 }
699 else
700 cout << "Bad terrain detected" << endl;
[b128109]701 }
702 else // nned to send back a message indicating failure
703 cout << "Player id (" << id << ") doesn't match sender" << endl;
704
705 break;
706 }
[5299436]707 case MSG_TYPE_PICKUP_FLAG:
708 {
709 // may want to check the id matches the sender, just like for PLAYER_NOVE
710 cout << "PICKUP_FLAG" << endl;
711
712 int id;
713
714 memcpy(&id, clientMsg.buffer, 4);
715 cout << "id: " << id << endl;
716
[5c84d54]717 vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
718 vector<WorldMap::Object>::iterator itObjects;
719
720 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end();) {
721 POSITION pos = itObjects->pos;
722 bool gotFlag = false;
723
724 if (posDistance(mapPlayers[id].pos, pos.toFloat()) < 10) {
725 switch (itObjects->type) {
726 case WorldMap::OBJECT_BLUE_FLAG:
727 if (mapPlayers[id].team == 1) {
728 gotFlag = true;
729 mapPlayers[id].hasBlueFlag = true;
730 broadcastResponse = true;
731 }
732 break;
733 case WorldMap::OBJECT_RED_FLAG:
734 if (mapPlayers[id].team == 0) {
735 gotFlag = true;
736 mapPlayers[id].hasRedFlag = true;
737 broadcastResponse = true;
738 }
739 break;
740 }
741
742 if (gotFlag) {
743 serverMsg.type = MSG_TYPE_REMOVE_OBJECT;
744 memcpy(serverMsg.buffer, &itObjects->id, 4);
745
746 map<unsigned int, Player>::iterator it;
747 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
748 {
749 if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
750 error("sendMessage");
751 }
752
753 // remove the object from the server-side map
754 cout << "size before: " << gameMap->getObjects()->size() << endl;
755 itObjects = vctObjects->erase(itObjects);
756 cout << "size after: " << gameMap->getObjects()->size() << endl;
757 }
758 }
759
760 if (!gotFlag)
761 itObjects++;
762 }
763
764 serverMsg.type = MSG_TYPE_PLAYER;
765 mapPlayers[id].serialize(serverMsg.buffer);
766
[5299436]767 break;
768 }
[e487381]769 case MSG_TYPE_DROP_FLAG:
770 {
771 // may want to check the id matches the sender, just like for PLAYER_NOVE
772 cout << "DROP_FLAG" << endl;
773
774 int id;
775
776 memcpy(&id, clientMsg.buffer, 4);
777 cout << "id: " << id << endl;
778
779 WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
780 if (mapPlayers[id].hasBlueFlag)
781 flagType = WorldMap::OBJECT_BLUE_FLAG;
782 else if (mapPlayers[id].hasRedFlag)
783 flagType = WorldMap::OBJECT_RED_FLAG;
784
785 gameMap->addObject(flagType, mapPlayers[id].pos.x, mapPlayers[id].pos.y);
786
787 // need to send the OBJECT message too
788 serverMsg.type = MSG_TYPE_OBJECT;
789 gameMap->getObjects()->back().serialize(serverMsg.buffer);
790
791 map<unsigned int, Player>::iterator it;
792 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
793 {
794 if ( sendMessage(&serverMsg, sock, &(it->second.addr)) < 0 )
795 error("sendMessage");
796 }
797
798 mapPlayers[id].hasBlueFlag = false;
799 mapPlayers[id].hasRedFlag = false;
800
801 serverMsg.type = MSG_TYPE_PLAYER;
802 mapPlayers[id].serialize(serverMsg.buffer);
803
804 broadcastResponse = true;
805
806 break;
807 }
[4b4b153]808 case MSG_TYPE_START_ATTACK:
809 {
810 cout << "Received a START_ATTACK message" << endl;
811
[8a4ed74]812 int id, targetId;
813
814 memcpy(&id, clientMsg.buffer, 4);
815 memcpy(&targetId, clientMsg.buffer+4, 4);
816
[8dad966]817 Player* source = &mapPlayers[id];
818 source->targetPlayer = targetId;
[11d21ee]819 source->isChasing = true;
[8dad966]820
[11d21ee]821 // this is irrelevant since the client doesn't even listen for START_ATTACK messages
822 // actually, the client should not ignore this and should instead perform the same movement
823 // algorithm on its end (following the target player until in range) that the server does.
824 // Once the attacker is in range, the client should stop movement and wait for messages
825 // from the server
[4b4b153]826 serverMsg.type = MSG_TYPE_START_ATTACK;
[8dad966]827 memcpy(serverMsg.buffer, &id, 4);
828 memcpy(serverMsg.buffer+4, &targetId, 4);
[4b4b153]829 broadcastResponse = true;
[8a4ed74]830
831 break;
[4b4b153]832 }
833 case MSG_TYPE_ATTACK:
834 {
835 cout << "Received am ATTACK message" << endl;
[8dad966]836 cout << "ERROR: Clients should not send ATTACK messages" << endl;
[8a4ed74]837
838 break;
[4b4b153]839 }
[8e540f4]840 default:
841 {
842 strcpy(serverMsg.buffer, "Server error occured. Report this please.");
[e084950]843
[8e540f4]844 serverMsg.type = MSG_TYPE_CHAT;
[e084950]845
[8e540f4]846 break;
847 }
[e3535b3]848 }
[da692b9]849
850 return broadcastResponse;
[e3535b3]851}
[da692b9]852
[8dad966]853void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player>& mapPlayers)
[01d0d00]854{
[1106210]855 while (mapPlayers.find(id) != mapPlayers.end())
856 id++;
[01d0d00]857}
[8dad966]858
859void updateUnusedProjectileId(unsigned int& id, map<unsigned int, Projectile>& mapProjectiles)
860{
861 while (mapProjectiles.find(id) != mapProjectiles.end())
862 id++;
863}
Note: See TracBrowser for help on using the repository browser.