source: network-game/server/server.cpp@ 66c4ec4

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

Restore the player's health and move him next to his base when he respawns

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