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