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