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