source: network-game/server/server.cpp@ 11d21ee

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

When a player tries to attack someone, they will now move toward the target until they are in range and then start the actual attack

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