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