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+4, &winningTeam, 4);
|
---|
383 | memcpy(serverMsg.buffer+4, &scoreBlue, 4);
|
---|
384 | memcpy(serverMsg.buffer+8, &scoreRed, 4);
|
---|
385 |
|
---|
386 | for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++)
|
---|
387 | {
|
---|
388 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
|
---|
389 | error("sendMessage");
|
---|
390 | }
|
---|
391 | }
|
---|
392 |
|
---|
393 | // this means a PLAYER message will be sent
|
---|
394 | broadcastMove = true;
|
---|
395 | }
|
---|
396 |
|
---|
397 | // go through all objects and check if the player is close to one and if its their flag
|
---|
398 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
399 | vector<WorldMap::Object>::iterator itObjects;
|
---|
400 | POSITION structPos;
|
---|
401 |
|
---|
402 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++)
|
---|
403 | {
|
---|
404 | POSITION pos = itObjects->pos;
|
---|
405 |
|
---|
406 | if (posDistance(p->pos, pos.toFloat()) < 10)
|
---|
407 | {
|
---|
408 | if (p->team == 0 &&
|
---|
409 | itObjects->type == WorldMap::OBJECT_BLUE_FLAG)
|
---|
410 | {
|
---|
411 | structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_BLUE_FLAG);
|
---|
412 | flagReturned = true;
|
---|
413 | break;
|
---|
414 | }
|
---|
415 | else if (p->team == 1 &&
|
---|
416 | itObjects->type == WorldMap::OBJECT_RED_FLAG)
|
---|
417 | {
|
---|
418 | structPos = gameMap->getStructureLocation(WorldMap::STRUCTURE_RED_FLAG);
|
---|
419 | flagReturned = true;
|
---|
420 | break;
|
---|
421 | }
|
---|
422 | }
|
---|
423 | }
|
---|
424 |
|
---|
425 | if (flagReturned)
|
---|
426 | {
|
---|
427 | itObjects->pos.x = structPos.x*25+12;
|
---|
428 | itObjects->pos.y = structPos.y*25+12;
|
---|
429 |
|
---|
430 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
431 | itObjects->serialize(serverMsg.buffer);
|
---|
432 |
|
---|
433 | map<unsigned int, Player*>::iterator it2;
|
---|
434 | for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++)
|
---|
435 | {
|
---|
436 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
|
---|
437 | error("sendMessage");
|
---|
438 | }
|
---|
439 | }
|
---|
440 |
|
---|
441 | if (broadcastMove)
|
---|
442 | {
|
---|
443 | cout << "broadcasting player move" << endl;
|
---|
444 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
445 | p->serialize(serverMsg.buffer);
|
---|
446 |
|
---|
447 | // only broadcast message to other players in the same game
|
---|
448 | cout << "about to broadcast move" << endl;
|
---|
449 |
|
---|
450 | map<unsigned int, Player*>::iterator it2;
|
---|
451 | for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++)
|
---|
452 | {
|
---|
453 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
|
---|
454 | error("sendMessage");
|
---|
455 | }
|
---|
456 | cout << "done broadcasting player move" << endl;
|
---|
457 | }
|
---|
458 | }
|
---|
459 |
|
---|
460 | cout << "processing player attack" << endl;
|
---|
461 |
|
---|
462 | // check if the player's attack animation is complete
|
---|
463 | if (p->isAttacking && p->timeAttackStarted+p->attackCooldown <= getCurrentMillis())
|
---|
464 | {
|
---|
465 | p->isAttacking = false;
|
---|
466 | cout << "Attack animation is complete" << endl;
|
---|
467 |
|
---|
468 | //send everyone an ATTACK message
|
---|
469 | cout << "about to broadcast attack" << endl;
|
---|
470 |
|
---|
471 | serverMsg.type = MSG_TYPE_ATTACK;
|
---|
472 | memcpy(serverMsg.buffer, &p->id, 4);
|
---|
473 | memcpy(serverMsg.buffer+4, &p->targetPlayer, 4);
|
---|
474 |
|
---|
475 | map<unsigned int, Player*>::iterator it2;
|
---|
476 | for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++)
|
---|
477 | {
|
---|
478 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
|
---|
479 | error("sendMessage");
|
---|
480 | }
|
---|
481 |
|
---|
482 | if (p->attackType == Player::ATTACK_MELEE)
|
---|
483 | {
|
---|
484 | cout << "Melee attack" << endl;
|
---|
485 |
|
---|
486 | Player* target = playersInGame[p->targetPlayer];
|
---|
487 | damagePlayer(target, p->damage);
|
---|
488 |
|
---|
489 | if (target->isDead)
|
---|
490 | {
|
---|
491 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
---|
492 | if (target->hasBlueFlag)
|
---|
493 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
494 | else if (target->hasRedFlag)
|
---|
495 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
496 |
|
---|
497 | if (flagType != WorldMap::OBJECT_NONE) {
|
---|
498 | addObjectToMap(flagType, target->pos.x, target->pos.y, gameMap, playersInGame, msgProcessor, sock, outputLog);
|
---|
499 | }
|
---|
500 | }
|
---|
501 |
|
---|
502 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
503 | target->serialize(serverMsg.buffer);
|
---|
504 | }
|
---|
505 | else if (p->attackType == Player::ATTACK_RANGED)
|
---|
506 | {
|
---|
507 | cout << "Ranged attack" << endl;
|
---|
508 |
|
---|
509 | Projectile proj(p->pos.x, p->pos.y, p->targetPlayer, p->damage);
|
---|
510 | game->assignProjectileId(&proj);
|
---|
511 | game->addProjectile(proj);
|
---|
512 |
|
---|
513 | int x = p->pos.x;
|
---|
514 | int y = p->pos.y;
|
---|
515 |
|
---|
516 | serverMsg.type = MSG_TYPE_PROJECTILE;
|
---|
517 | memcpy(serverMsg.buffer, &proj.id, 4);
|
---|
518 | memcpy(serverMsg.buffer+4, &x, 4);
|
---|
519 | memcpy(serverMsg.buffer+8, &y, 4);
|
---|
520 | memcpy(serverMsg.buffer+12, &p->targetPlayer, 4);
|
---|
521 | }
|
---|
522 | else
|
---|
523 | cout << "Invalid attack type: " << p->attackType << endl;
|
---|
524 |
|
---|
525 | // broadcast either a PLAYER or PROJECTILE message
|
---|
526 | cout << "Broadcasting player or projectile message" << endl;
|
---|
527 | for (it2 = playersInGame.begin(); it2 != playersInGame.end(); it2++)
|
---|
528 | {
|
---|
529 | if (msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
|
---|
530 | error("sendMessage");
|
---|
531 | }
|
---|
532 | cout << "Done broadcasting" << endl;
|
---|
533 | }
|
---|
534 | }
|
---|
535 |
|
---|
536 | if (gameFinished) {
|
---|
537 | // send a GAME_INFO message with 0 players to force clients to delete the game
|
---|
538 | int numPlayers = 0;
|
---|
539 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
540 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
541 |
|
---|
542 | map<unsigned int, Player*>::iterator it2;
|
---|
543 | for (it2 = mapPlayers.begin(); it2 != mapPlayers.end(); it2++)
|
---|
544 | {
|
---|
545 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
|
---|
546 | error("sendMessage");
|
---|
547 | }
|
---|
548 |
|
---|
549 | // erase game from server
|
---|
550 | mapGames.erase(itGames++);
|
---|
551 | delete game;
|
---|
552 | }else
|
---|
553 | itGames++;
|
---|
554 | }
|
---|
555 |
|
---|
556 | cout << "Processing projectiles" << endl;
|
---|
557 |
|
---|
558 | // move all projectiles
|
---|
559 | // see if this can be moved inside the game class
|
---|
560 | // this method can be moved when I add a MessageProcessor to the Game class
|
---|
561 | map<unsigned int, Projectile>::iterator itProj;
|
---|
562 | for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++) {
|
---|
563 | game = itGames->second;
|
---|
564 | for (itProj = game->getProjectiles().begin(); itProj != game->getProjectiles().end(); itProj++)
|
---|
565 | {
|
---|
566 | cout << "About to call projectile move" << endl;
|
---|
567 | if (itProj->second.move(game->getPlayers()))
|
---|
568 | {
|
---|
569 | // send a REMOVE_PROJECTILE message
|
---|
570 | cout << "send a REMOVE_PROJECTILE message" << endl;
|
---|
571 | serverMsg.type = MSG_TYPE_REMOVE_PROJECTILE;
|
---|
572 | memcpy(serverMsg.buffer, &itProj->second.id, 4);
|
---|
573 | game->removeProjectile(itProj->second.id);
|
---|
574 |
|
---|
575 | map<unsigned int, Player*>::iterator it2;
|
---|
576 | cout << "Broadcasting REMOVE_PROJECTILE" << endl;
|
---|
577 | for (it2 = game->getPlayers().begin(); it2 != game->getPlayers().end(); it2++)
|
---|
578 | {
|
---|
579 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
|
---|
580 | error("sendMessage");
|
---|
581 | }
|
---|
582 |
|
---|
583 | cout << "send a PLAYER message after dealing damage" << endl;
|
---|
584 | // send a PLAYER message after dealing damage
|
---|
585 | Player* target = game->getPlayers()[itProj->second.target];
|
---|
586 |
|
---|
587 | damagePlayer(target, itProj->second.damage);
|
---|
588 |
|
---|
589 | if (target->isDead)
|
---|
590 | {
|
---|
591 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
---|
592 | if (target->hasBlueFlag)
|
---|
593 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
594 | else if (target->hasRedFlag)
|
---|
595 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
596 |
|
---|
597 | if (flagType != WorldMap::OBJECT_NONE)
|
---|
598 | addObjectToMap(flagType, target->pos.x, target->pos.y, game->getMap(), game->getPlayers(), msgProcessor, sock, outputLog);
|
---|
599 | }
|
---|
600 |
|
---|
601 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
602 | target->serialize(serverMsg.buffer);
|
---|
603 |
|
---|
604 | cout << "Sending a PLAYER message" << endl;
|
---|
605 | for (it2 = game->getPlayers().begin(); it2 != game->getPlayers().end(); it2++)
|
---|
606 | {
|
---|
607 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it2->second->addr), &outputLog) < 0 )
|
---|
608 | error("sendMessage");
|
---|
609 | }
|
---|
610 | }
|
---|
611 | cout << "Projectile was not moved" << endl;
|
---|
612 | }
|
---|
613 | }
|
---|
614 | }
|
---|
615 |
|
---|
616 | n = msgProcessor.receiveMessage(&clientMsg, sock, &from, &outputLog);
|
---|
617 |
|
---|
618 | if (n >= 0)
|
---|
619 | {
|
---|
620 | broadcastResponse = processMessage(clientMsg, from, msgProcessor, mapPlayers, mapGames, gameMap, unusedPlayerId, serverMsg, sock, scoreBlue, scoreRed, outputLog);
|
---|
621 |
|
---|
622 | if (broadcastResponse)
|
---|
623 | {
|
---|
624 | cout << "Should be broadcasting the message" << endl;
|
---|
625 |
|
---|
626 | // needs to be updated to use the players from the game
|
---|
627 | map<unsigned int, Player*>::iterator it;
|
---|
628 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
629 | {
|
---|
630 | cout << "Sent message back to " << it->second->name << endl;
|
---|
631 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
|
---|
632 | error("sendMessage");
|
---|
633 | }
|
---|
634 | }
|
---|
635 | else
|
---|
636 | {
|
---|
637 | cout << "Should be sending back the message" << endl;
|
---|
638 |
|
---|
639 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
|
---|
640 | error("sendMessage");
|
---|
641 | }
|
---|
642 |
|
---|
643 | cout << "Finished processing the message" << endl;
|
---|
644 | }
|
---|
645 | }
|
---|
646 |
|
---|
647 | outputLog << "Stopped server on " << getCurrentDateTimeString() << endl;
|
---|
648 | outputLog.close();
|
---|
649 |
|
---|
650 | // delete all games
|
---|
651 | map<string, Game*>::iterator itGames;
|
---|
652 | for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++)
|
---|
653 | {
|
---|
654 | delete itGames->second;
|
---|
655 | }
|
---|
656 |
|
---|
657 | map<unsigned int, Player*>::iterator itPlayers;
|
---|
658 | for (itPlayers = mapPlayers.begin(); itPlayers != mapPlayers.end(); itPlayers++)
|
---|
659 | {
|
---|
660 | delete itPlayers->second;
|
---|
661 | }
|
---|
662 |
|
---|
663 | return 0;
|
---|
664 | }
|
---|
665 |
|
---|
666 | 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)
|
---|
667 | {
|
---|
668 | DataAccess da;
|
---|
669 |
|
---|
670 | cout << "Inside processMessage" << endl;
|
---|
671 |
|
---|
672 | cout << "Received message" << endl;
|
---|
673 | cout << "MSG: type: " << clientMsg.type << endl;
|
---|
674 | cout << "MSG contents: " << clientMsg.buffer << endl;
|
---|
675 |
|
---|
676 | // maybe we should make a message class and have this be a member
|
---|
677 | bool broadcastResponse = false;
|
---|
678 |
|
---|
679 | // Check that if an invalid message is sent, the client will correctly
|
---|
680 | // receive and display the response. Maybe make a special error msg type
|
---|
681 | switch(clientMsg.type)
|
---|
682 | {
|
---|
683 | case MSG_TYPE_REGISTER:
|
---|
684 | {
|
---|
685 | string username(clientMsg.buffer);
|
---|
686 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
687 | Player::PlayerClass playerClass;
|
---|
688 |
|
---|
689 | serverMsg.type = MSG_TYPE_REGISTER;
|
---|
690 | memcpy(&playerClass, clientMsg.buffer+username.length()+password.length()+2, 4);
|
---|
691 |
|
---|
692 | cout << "username: " << username << endl;
|
---|
693 | cout << "password: " << password << endl;
|
---|
694 |
|
---|
695 | if (playerClass == Player::CLASS_WARRIOR)
|
---|
696 | cout << "class: WARRIOR" << endl;
|
---|
697 | else if (playerClass == Player::CLASS_RANGER)
|
---|
698 | cout << "class: RANGER" << endl;
|
---|
699 | else {
|
---|
700 | cout << "Unknown player class detected" << endl;
|
---|
701 | strcpy(serverMsg.buffer, "You didn't select a class");
|
---|
702 | break;
|
---|
703 | }
|
---|
704 |
|
---|
705 | int error = da.insertPlayer(username, password, playerClass);
|
---|
706 |
|
---|
707 | if (error)
|
---|
708 | strcpy(serverMsg.buffer, "Registration failed. Please try again.");
|
---|
709 | else
|
---|
710 | strcpy(serverMsg.buffer, "Registration successful.");
|
---|
711 |
|
---|
712 | break;
|
---|
713 | }
|
---|
714 | case MSG_TYPE_LOGIN:
|
---|
715 | {
|
---|
716 | cout << "Got login message" << endl;
|
---|
717 |
|
---|
718 | string username(clientMsg.buffer);
|
---|
719 | string password(strchr(clientMsg.buffer, '\0')+1);
|
---|
720 |
|
---|
721 | Player* p = da.getPlayer(username);
|
---|
722 |
|
---|
723 | if (p == NULL || !da.verifyPassword(password, p->password))
|
---|
724 | {
|
---|
725 | strcpy(serverMsg.buffer, "Incorrect username or password");
|
---|
726 | if (p != NULL)
|
---|
727 | delete(p);
|
---|
728 | }
|
---|
729 | else if(findPlayerByName(mapPlayers, username) != NULL)
|
---|
730 | {
|
---|
731 | strcpy(serverMsg.buffer, "Player has already logged in.");
|
---|
732 | delete(p);
|
---|
733 | }
|
---|
734 | else
|
---|
735 | {
|
---|
736 | updateUnusedPlayerId(unusedPlayerId, mapPlayers);
|
---|
737 | p->id = unusedPlayerId;
|
---|
738 | cout << "new player id: " << p->id << endl;
|
---|
739 | p->setAddr(from);
|
---|
740 | p->currentGame = NULL;
|
---|
741 |
|
---|
742 | // choose a random team (either 0 or 1)
|
---|
743 | p->team = rand() % 2;
|
---|
744 |
|
---|
745 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
746 |
|
---|
747 | // tell the new player about all the existing players
|
---|
748 | cout << "Sending other players to new player" << endl;
|
---|
749 |
|
---|
750 | map<unsigned int, Player*>::iterator it;
|
---|
751 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
752 | {
|
---|
753 | it->second->serialize(serverMsg.buffer);
|
---|
754 |
|
---|
755 | cout << "sending info about " << it->second->name << endl;
|
---|
756 | cout << "sending id " << it->second->id << endl;
|
---|
757 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
|
---|
758 | error("sendMessage");
|
---|
759 | }
|
---|
760 |
|
---|
761 | // tell the new player about all map objects
|
---|
762 | // (currently just the flags)
|
---|
763 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
764 | vector<WorldMap::Object>* vctObjects = gameMap->getObjects();
|
---|
765 | vector<WorldMap::Object>::iterator itObjects;
|
---|
766 | cout << "sending items" << endl;
|
---|
767 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
768 | itObjects->serialize(serverMsg.buffer);
|
---|
769 | cout << "sending item id " << itObjects->id << endl;
|
---|
770 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
|
---|
771 | error("sendMessage");
|
---|
772 | }
|
---|
773 |
|
---|
774 | // send info about existing games to new player
|
---|
775 | map<string, Game*>::iterator itGames;
|
---|
776 | Game* g;
|
---|
777 | int numPlayers;
|
---|
778 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
779 |
|
---|
780 | for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++)
|
---|
781 | {
|
---|
782 | g = itGames->second;
|
---|
783 | numPlayers = g->getNumPlayers();
|
---|
784 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
785 | strcpy(serverMsg.buffer+4, g->getName().c_str());
|
---|
786 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
|
---|
787 | error("sendMessage");
|
---|
788 | }
|
---|
789 |
|
---|
790 | // send the current score
|
---|
791 | serverMsg.type = MSG_TYPE_SCORE;
|
---|
792 | memcpy(serverMsg.buffer, &scoreBlue, 4);
|
---|
793 | memcpy(serverMsg.buffer+4, &scoreRed, 4);
|
---|
794 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
|
---|
795 | error("sendMessage");
|
---|
796 |
|
---|
797 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
798 | p->serialize(serverMsg.buffer);
|
---|
799 | cout << "Should be broadcasting the message" << endl;
|
---|
800 |
|
---|
801 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
802 | {
|
---|
803 | cout << "Sent message back to " << it->second->name << endl;
|
---|
804 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
|
---|
805 | error("sendMessage");
|
---|
806 | }
|
---|
807 |
|
---|
808 | mapPlayers[unusedPlayerId] = p;
|
---|
809 | }
|
---|
810 |
|
---|
811 | serverMsg.type = MSG_TYPE_LOGIN;
|
---|
812 |
|
---|
813 | break;
|
---|
814 | }
|
---|
815 | case MSG_TYPE_LOGOUT:
|
---|
816 | {
|
---|
817 | string name(clientMsg.buffer);
|
---|
818 | cout << "Player logging out: " << name << endl;
|
---|
819 |
|
---|
820 | Player *p = findPlayerByName(mapPlayers, name);
|
---|
821 |
|
---|
822 | if (p == NULL)
|
---|
823 | {
|
---|
824 | strcpy(serverMsg.buffer+4, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
|
---|
825 | cout << "Player not logged in" << endl;
|
---|
826 | }
|
---|
827 | else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
|
---|
828 | p->addr.sin_port != from.sin_port )
|
---|
829 | {
|
---|
830 | 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.");
|
---|
831 | cout << "Player logged in using a different connection" << endl;
|
---|
832 | }
|
---|
833 | else
|
---|
834 | {
|
---|
835 | if (!p->isDead) {
|
---|
836 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
---|
837 | if (p->hasBlueFlag)
|
---|
838 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
839 | else if (p->hasRedFlag)
|
---|
840 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
841 |
|
---|
842 | if (flagType != WorldMap::OBJECT_NONE) {
|
---|
843 | addObjectToMap(flagType, p->pos.x, p->pos.y, gameMap, mapPlayers, msgProcessor, sock, outputLog);
|
---|
844 | }
|
---|
845 | }
|
---|
846 |
|
---|
847 | // broadcast to all players before deleting p from the map
|
---|
848 | serverMsg.type = MSG_TYPE_LOGOUT;
|
---|
849 | memcpy(serverMsg.buffer, &p->id, 4);
|
---|
850 |
|
---|
851 | map<unsigned int, Player*>::iterator it;
|
---|
852 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
853 | {
|
---|
854 | cout << "Sent message back to " << it->second->name << endl;
|
---|
855 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
|
---|
856 | error("sendMessage");
|
---|
857 | }
|
---|
858 |
|
---|
859 | if (p->id < unusedPlayerId)
|
---|
860 | unusedPlayerId = p->id;
|
---|
861 | mapPlayers.erase(p->id);
|
---|
862 | delete p;
|
---|
863 | strcpy(serverMsg.buffer+4, "You have successfully logged out.");
|
---|
864 | }
|
---|
865 |
|
---|
866 | serverMsg.type = MSG_TYPE_LOGOUT;
|
---|
867 |
|
---|
868 | break;
|
---|
869 | }
|
---|
870 | case MSG_TYPE_CHAT:
|
---|
871 | {
|
---|
872 | cout << "Got a chat message" << endl;
|
---|
873 |
|
---|
874 | Player *p = findPlayerByAddr(mapPlayers, from);
|
---|
875 |
|
---|
876 | if (p == NULL)
|
---|
877 | {
|
---|
878 | strcpy(serverMsg.buffer, "No player is logged in using this connection. This is either a bug, or you're trying to hack the server.");
|
---|
879 | }
|
---|
880 | else
|
---|
881 | {
|
---|
882 | broadcastResponse = true;
|
---|
883 |
|
---|
884 | ostringstream oss;
|
---|
885 | oss << p->name << ": " << clientMsg.buffer;
|
---|
886 |
|
---|
887 | strcpy(serverMsg.buffer, oss.str().c_str());
|
---|
888 | }
|
---|
889 |
|
---|
890 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
891 |
|
---|
892 | break;
|
---|
893 | }
|
---|
894 | case MSG_TYPE_PLAYER_MOVE:
|
---|
895 | {
|
---|
896 | cout << "PLAYER_MOVE" << endl;
|
---|
897 |
|
---|
898 | int id, x, y;
|
---|
899 |
|
---|
900 | memcpy(&id, clientMsg.buffer, 4);
|
---|
901 | memcpy(&x, clientMsg.buffer+4, 4);
|
---|
902 | memcpy(&y, clientMsg.buffer+8, 4);
|
---|
903 |
|
---|
904 | cout << "x: " << x << endl;
|
---|
905 | cout << "y: " << y << endl;
|
---|
906 | cout << "id: " << id << endl;
|
---|
907 |
|
---|
908 | Player* p = mapPlayers[id];
|
---|
909 |
|
---|
910 | if ( p->addr.sin_addr.s_addr == from.sin_addr.s_addr &&
|
---|
911 | p->addr.sin_port == from.sin_port )
|
---|
912 | {
|
---|
913 | if (p->currentGame->startPlayerMovement(id, x, y)) {
|
---|
914 | serverMsg.type = MSG_TYPE_PLAYER_MOVE;
|
---|
915 |
|
---|
916 | memcpy(serverMsg.buffer, &id, 4);
|
---|
917 | memcpy(serverMsg.buffer+4, &p->target.x, 4);
|
---|
918 | memcpy(serverMsg.buffer+8, &p->target.y, 4);
|
---|
919 |
|
---|
920 | broadcastResponse = true;
|
---|
921 | }
|
---|
922 | else
|
---|
923 | cout << "Bad terrain detected" << endl;
|
---|
924 | }
|
---|
925 | else // nned to send back a message indicating failure
|
---|
926 | cout << "Player id (" << id << ") doesn't match sender" << endl;
|
---|
927 |
|
---|
928 | break;
|
---|
929 | }
|
---|
930 | case MSG_TYPE_PICKUP_FLAG:
|
---|
931 | {
|
---|
932 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
933 | cout << "PICKUP_FLAG" << endl;
|
---|
934 |
|
---|
935 | int id;
|
---|
936 |
|
---|
937 | memcpy(&id, clientMsg.buffer, 4);
|
---|
938 | cout << "id: " << id << endl;
|
---|
939 |
|
---|
940 | Player* p = mapPlayers[id];
|
---|
941 | int objectId = p->currentGame->processFlagPickupRequest(p);
|
---|
942 |
|
---|
943 | if (objectId >= 0) {
|
---|
944 | serverMsg.type = MSG_TYPE_REMOVE_OBJECT;
|
---|
945 | memcpy(serverMsg.buffer, &objectId, 4);
|
---|
946 |
|
---|
947 | map<unsigned int, Player*> players = p->currentGame->getPlayers();
|
---|
948 | map<unsigned int, Player*>::iterator it;
|
---|
949 | for (it = players.begin(); it != players.end(); it++)
|
---|
950 | {
|
---|
951 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
|
---|
952 | error("sendMessage");
|
---|
953 | }
|
---|
954 |
|
---|
955 | }
|
---|
956 |
|
---|
957 | // if there was no flag to pickup, we really don't need to send a message
|
---|
958 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
959 | p->serialize(serverMsg.buffer);
|
---|
960 |
|
---|
961 | broadcastResponse = true;
|
---|
962 |
|
---|
963 | break;
|
---|
964 | }
|
---|
965 | case MSG_TYPE_DROP_FLAG:
|
---|
966 | {
|
---|
967 | // may want to check the id matches the sender, just like for PLAYER_NOVE
|
---|
968 | cout << "DROP_FLAG" << endl;
|
---|
969 |
|
---|
970 | int id;
|
---|
971 |
|
---|
972 | memcpy(&id, clientMsg.buffer, 4);
|
---|
973 | cout << "id: " << id << endl;
|
---|
974 |
|
---|
975 | Player* p = mapPlayers[id];
|
---|
976 |
|
---|
977 | WorldMap::ObjectType flagType = WorldMap::OBJECT_NONE;
|
---|
978 | if (p->hasBlueFlag)
|
---|
979 | flagType = WorldMap::OBJECT_BLUE_FLAG;
|
---|
980 | else if (p->hasRedFlag)
|
---|
981 | flagType = WorldMap::OBJECT_RED_FLAG;
|
---|
982 |
|
---|
983 | addObjectToMap(flagType, p->pos.x, p->pos.y, p->currentGame->getMap(), p->currentGame->getPlayers(), msgProcessor, sock, outputLog);
|
---|
984 |
|
---|
985 | p->hasBlueFlag = false;
|
---|
986 | p->hasRedFlag = false;
|
---|
987 |
|
---|
988 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
989 | p->serialize(serverMsg.buffer);
|
---|
990 |
|
---|
991 | broadcastResponse = true;
|
---|
992 |
|
---|
993 | break;
|
---|
994 | }
|
---|
995 | case MSG_TYPE_START_ATTACK:
|
---|
996 | {
|
---|
997 | cout << "Received a START_ATTACK message" << endl;
|
---|
998 |
|
---|
999 | int id, targetId;
|
---|
1000 |
|
---|
1001 | memcpy(&id, clientMsg.buffer, 4);
|
---|
1002 | memcpy(&targetId, clientMsg.buffer+4, 4);
|
---|
1003 |
|
---|
1004 | // need to make sure the target is in the sender's game
|
---|
1005 |
|
---|
1006 | Player* source = mapPlayers[id];
|
---|
1007 | source->targetPlayer = targetId;
|
---|
1008 | source->isChasing = true;
|
---|
1009 |
|
---|
1010 | // this is irrelevant since the client doesn't even listen for START_ATTACK messages
|
---|
1011 | // actually, the client should not ignore this and should instead perform the same movement
|
---|
1012 | // algorithm on its end (following the target player until in range) that the server does.
|
---|
1013 | // Once the attacker is in range, the client should stop movement and wait for messages
|
---|
1014 | // from the server
|
---|
1015 | serverMsg.type = MSG_TYPE_START_ATTACK;
|
---|
1016 | memcpy(serverMsg.buffer, &id, 4);
|
---|
1017 | memcpy(serverMsg.buffer+4, &targetId, 4);
|
---|
1018 | broadcastResponse = true;
|
---|
1019 |
|
---|
1020 | break;
|
---|
1021 | }
|
---|
1022 | case MSG_TYPE_ATTACK:
|
---|
1023 | {
|
---|
1024 | cout << "Received am ATTACK message" << endl;
|
---|
1025 | cout << "ERROR: Clients should not send ATTACK messages" << endl;
|
---|
1026 |
|
---|
1027 | break;
|
---|
1028 | }
|
---|
1029 | case MSG_TYPE_CREATE_GAME:
|
---|
1030 | {
|
---|
1031 | cout << "Received a CREATE_GAME message" << endl;
|
---|
1032 |
|
---|
1033 | string gameName(clientMsg.buffer);
|
---|
1034 | cout << "Game name: " << gameName << endl;
|
---|
1035 |
|
---|
1036 | // check if this game already exists
|
---|
1037 | if (mapGames.find(gameName) != mapGames.end()) {
|
---|
1038 | cout << "Error: Game already exists" << endl;
|
---|
1039 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
1040 | broadcastResponse = false;
|
---|
1041 | return broadcastResponse;
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 | Game* g = new Game(gameName, "../data/map.txt");
|
---|
1045 | mapGames[gameName] = g;
|
---|
1046 |
|
---|
1047 | // add flag objects to the map
|
---|
1048 | WorldMap* m = g->getMap();
|
---|
1049 | for (int y=0; y<m->height; y++) {
|
---|
1050 | for (int x=0; x<m->width; x++) {
|
---|
1051 | switch (m->getStructure(x, y)) {
|
---|
1052 | case WorldMap::STRUCTURE_BLUE_FLAG:
|
---|
1053 | m->addObject(WorldMap::OBJECT_BLUE_FLAG, x*25+12, y*25+12);
|
---|
1054 | break;
|
---|
1055 | case WorldMap::STRUCTURE_RED_FLAG:
|
---|
1056 | m->addObject(WorldMap::OBJECT_RED_FLAG, x*25+12, y*25+12);
|
---|
1057 | break;
|
---|
1058 | }
|
---|
1059 | }
|
---|
1060 | }
|
---|
1061 |
|
---|
1062 | serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS;
|
---|
1063 | strcpy(serverMsg.buffer, gameName.c_str());
|
---|
1064 | broadcastResponse = false;
|
---|
1065 |
|
---|
1066 | break;
|
---|
1067 | }
|
---|
1068 | case MSG_TYPE_JOIN_GAME:
|
---|
1069 | {
|
---|
1070 | cout << "Received a JOIN_GAME message" << endl;
|
---|
1071 |
|
---|
1072 | string gameName(clientMsg.buffer);
|
---|
1073 | cout << "Game name: " << gameName << endl;
|
---|
1074 |
|
---|
1075 | // check if this game already exists
|
---|
1076 | if (mapGames.find(gameName) == mapGames.end()) {
|
---|
1077 | cout << "Error: Game does not exist" << endl;
|
---|
1078 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
1079 | broadcastResponse = false;
|
---|
1080 | return broadcastResponse;
|
---|
1081 | }
|
---|
1082 |
|
---|
1083 | Game* g = mapGames[gameName];
|
---|
1084 | map<unsigned int, Player*>& players = g->getPlayers();
|
---|
1085 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
1086 |
|
---|
1087 | if (players.find(p->id) != players.end()) {
|
---|
1088 | cout << "Player " << p->name << " trying to join a game he's already in" << endl;
|
---|
1089 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
1090 | broadcastResponse = false;
|
---|
1091 | return broadcastResponse;
|
---|
1092 | }
|
---|
1093 |
|
---|
1094 | serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS;
|
---|
1095 | strcpy(serverMsg.buffer, gameName.c_str());
|
---|
1096 | broadcastResponse = false;
|
---|
1097 |
|
---|
1098 | break;
|
---|
1099 | }
|
---|
1100 | case MSG_TYPE_LEAVE_GAME:
|
---|
1101 | {
|
---|
1102 | cout << "Received a LEAVE_GAME message" << endl;
|
---|
1103 |
|
---|
1104 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
1105 | Game* g = p->currentGame;
|
---|
1106 |
|
---|
1107 | if (g == NULL) {
|
---|
1108 | cout << "Player " << p->name << " is trying to leave a game, but is not currently in a game." << endl;
|
---|
1109 |
|
---|
1110 | /// should send a response back, maybe a new message type is needed
|
---|
1111 |
|
---|
1112 | break;
|
---|
1113 | }
|
---|
1114 |
|
---|
1115 | cout << "Game name: " << g->getName() << endl;
|
---|
1116 | p->currentGame = NULL;
|
---|
1117 |
|
---|
1118 | serverMsg.type = MSG_TYPE_LEAVE_GAME;
|
---|
1119 | memcpy(serverMsg.buffer, &p->id, 4);
|
---|
1120 | strcpy(serverMsg.buffer+4, g->getName().c_str());
|
---|
1121 |
|
---|
1122 | map<unsigned int, Player*>& players = g->getPlayers();
|
---|
1123 |
|
---|
1124 | map<unsigned int, Player*>::iterator it;
|
---|
1125 | for (it = players.begin(); it != players.end(); it++)
|
---|
1126 | {
|
---|
1127 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
|
---|
1128 | error("sendMessage");
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | g->removePlayer(p->id);
|
---|
1132 |
|
---|
1133 | int numPlayers = g->getNumPlayers();
|
---|
1134 |
|
---|
1135 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
1136 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
1137 | strcpy(serverMsg.buffer+4, g->getName().c_str());
|
---|
1138 | broadcastResponse = true;
|
---|
1139 |
|
---|
1140 | // if there are no more players in the game, remove it
|
---|
1141 | if (numPlayers == 0) {
|
---|
1142 | mapGames.erase(g->getName());
|
---|
1143 | delete g;
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 | break;
|
---|
1147 | }
|
---|
1148 | case MSG_TYPE_JOIN_GAME_ACK:
|
---|
1149 | {
|
---|
1150 | cout << "Received a JOIN_GAME_ACK message" << endl;
|
---|
1151 |
|
---|
1152 | string gameName(clientMsg.buffer);
|
---|
1153 | cout << "Game name: " << gameName << endl;
|
---|
1154 |
|
---|
1155 | // check if this game already exists
|
---|
1156 | if (mapGames.find(gameName) == mapGames.end()) {
|
---|
1157 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
1158 | broadcastResponse = false;
|
---|
1159 | return broadcastResponse;
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 | Game* g = mapGames[gameName];
|
---|
1163 |
|
---|
1164 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
1165 | p->team = rand() % 2; // choose a random team (either 0 or 1)
|
---|
1166 | p->currentGame = g;
|
---|
1167 |
|
---|
1168 | // tell the new player about all map objects
|
---|
1169 | // (currently just the flags)
|
---|
1170 |
|
---|
1171 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
1172 | vector<WorldMap::Object>* vctObjects = g->getMap()->getObjects();
|
---|
1173 | vector<WorldMap::Object>::iterator itObjects;
|
---|
1174 | cout << "sending items" << endl;
|
---|
1175 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
1176 | itObjects->serialize(serverMsg.buffer);
|
---|
1177 | cout << "sending item id " << itObjects->id << endl;
|
---|
1178 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
|
---|
1179 | error("sendMessage");
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 |
|
---|
1183 | // send the current score
|
---|
1184 | serverMsg.type = MSG_TYPE_SCORE;
|
---|
1185 |
|
---|
1186 | int game_blueScore = g->getBlueScore();
|
---|
1187 | int game_redScore = g->getRedScore();
|
---|
1188 | memcpy(serverMsg.buffer, &game_blueScore, 4);
|
---|
1189 | memcpy(serverMsg.buffer+4, &game_redScore, 4);
|
---|
1190 |
|
---|
1191 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
|
---|
1192 | error("sendMessage");
|
---|
1193 |
|
---|
1194 | serverMsg.type = MSG_TYPE_PLAYER_JOIN_GAME;
|
---|
1195 | p->serialize(serverMsg.buffer);
|
---|
1196 | cout << "Should be broadcasting the message" << endl;
|
---|
1197 |
|
---|
1198 | map<unsigned int, Player*>& otherPlayers = g->getPlayers();
|
---|
1199 | map<unsigned int, Player*>::iterator it;
|
---|
1200 | for (it = otherPlayers.begin(); it != otherPlayers.end(); it++)
|
---|
1201 | {
|
---|
1202 | cout << "Sent message back to " << it->second->name << endl;
|
---|
1203 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
|
---|
1204 | error("sendMessage");
|
---|
1205 | }
|
---|
1206 |
|
---|
1207 | g->addPlayer(p);
|
---|
1208 |
|
---|
1209 | map<unsigned int, Player*>& allPlayers = g->getPlayers();
|
---|
1210 |
|
---|
1211 | // tell the new player about all the players in the game (including himself)
|
---|
1212 | cout << "Sending other players to new player" << endl;
|
---|
1213 | serverMsg.type = MSG_TYPE_PLAYER_JOIN_GAME;
|
---|
1214 |
|
---|
1215 | for (it = allPlayers.begin(); it != allPlayers.end(); it++)
|
---|
1216 | {
|
---|
1217 | it->second->serialize(serverMsg.buffer);
|
---|
1218 |
|
---|
1219 | cout << "sending info about " << it->second->name << endl;
|
---|
1220 | cout << "sending id " << it->second->id << endl;
|
---|
1221 | if ( msgProcessor.sendMessage(&serverMsg, sock, &from, &outputLog) < 0 )
|
---|
1222 | error("sendMessage");
|
---|
1223 | }
|
---|
1224 |
|
---|
1225 | int numPlayers = g->getNumPlayers();
|
---|
1226 |
|
---|
1227 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
1228 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
1229 | strcpy(serverMsg.buffer+4, gameName.c_str());
|
---|
1230 | broadcastResponse = true;
|
---|
1231 |
|
---|
1232 | break;
|
---|
1233 | }
|
---|
1234 | default:
|
---|
1235 | {
|
---|
1236 | serverMsg.type = MSG_TYPE_CHAT;
|
---|
1237 | strcpy(serverMsg.buffer, "Server error occured. Report this please.");
|
---|
1238 |
|
---|
1239 | break;
|
---|
1240 | }
|
---|
1241 | }
|
---|
1242 |
|
---|
1243 | return broadcastResponse;
|
---|
1244 | }
|
---|
1245 |
|
---|
1246 | void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player*>& mapPlayers)
|
---|
1247 | {
|
---|
1248 | while (mapPlayers.find(id) != mapPlayers.end())
|
---|
1249 | id++;
|
---|
1250 | }
|
---|
1251 |
|
---|
1252 | Player *findPlayerByName(map<unsigned int, Player*> &m, string name)
|
---|
1253 | {
|
---|
1254 | map<unsigned int, Player*>::iterator it;
|
---|
1255 |
|
---|
1256 | for (it = m.begin(); it != m.end(); it++)
|
---|
1257 | {
|
---|
1258 | if ( it->second->name.compare(name) == 0 )
|
---|
1259 | return it->second;
|
---|
1260 | }
|
---|
1261 |
|
---|
1262 | return NULL;
|
---|
1263 | }
|
---|
1264 |
|
---|
1265 | Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr)
|
---|
1266 | {
|
---|
1267 | map<unsigned int, Player*>::iterator it;
|
---|
1268 |
|
---|
1269 | for (it = m.begin(); it != m.end(); it++)
|
---|
1270 | {
|
---|
1271 | if ( it->second->addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
|
---|
1272 | it->second->addr.sin_port == addr.sin_port )
|
---|
1273 | return it->second;
|
---|
1274 | }
|
---|
1275 |
|
---|
1276 | return NULL;
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 | void damagePlayer(Player *p, int damage) {
|
---|
1280 | p->health -= damage;
|
---|
1281 | if (p->health < 0)
|
---|
1282 | p->health = 0;
|
---|
1283 | if (p->health == 0) {
|
---|
1284 | cout << "Player died" << endl;
|
---|
1285 | p->isDead = true;
|
---|
1286 | p->timeDied = getCurrentMillis();
|
---|
1287 | }
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 | void addObjectToMap(WorldMap::ObjectType objectType, int x, int y, WorldMap* gameMap, map<unsigned int, Player*>& mapPlayers, MessageProcessor &msgProcessor, int sock, ofstream& outputLog) {
|
---|
1291 | NETWORK_MSG serverMsg;
|
---|
1292 |
|
---|
1293 | gameMap->addObject(objectType, x, y);
|
---|
1294 |
|
---|
1295 | // need to send the OBJECT message too
|
---|
1296 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
1297 | gameMap->getObjects()->back().serialize(serverMsg.buffer);
|
---|
1298 |
|
---|
1299 | map<unsigned int, Player*>::iterator it;
|
---|
1300 | for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
|
---|
1301 | {
|
---|
1302 | if ( msgProcessor.sendMessage(&serverMsg, sock, &(it->second->addr), &outputLog) < 0 )
|
---|
1303 | error("sendMessage");
|
---|
1304 | }
|
---|
1305 | }
|
---|