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