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