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, DataAccess& da, 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 | time_t timeFinished = time(NULL);
|
---|
203 | for (it = game->getPlayers().begin(); it != game->getPlayers().end(); it++) {
|
---|
204 | Player* p = it->second;
|
---|
205 | cout << "winning team: " << winningTeam << endl;
|
---|
206 | cout << "player team: " << p->team << endl;
|
---|
207 | cout << "player id: " << p->getId() << endl;
|
---|
208 |
|
---|
209 | if (winningTeam == p->team)
|
---|
210 | p->wins++;
|
---|
211 | else
|
---|
212 | p->losses++;
|
---|
213 | da.updatePlayer(p);
|
---|
214 | da.saveGameHistory(p->getId(), winningTeam, game->getBlueScore(), game->getRedScore(), timeFinished);
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | // send a GAME_INFO message with 0 players to force clients to delete the game
|
---|
219 | int numPlayers = 0;
|
---|
220 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
221 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
222 | strcpy(serverMsg.buffer+4, game->getName().c_str());
|
---|
223 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
224 |
|
---|
225 | delete itGames->second;
|
---|
226 | mapGames.erase(itGames++);
|
---|
227 | }else
|
---|
228 | itGames++;
|
---|
229 | }
|
---|
230 |
|
---|
231 | // move all projectiles
|
---|
232 | // see if this can be moved inside the game class
|
---|
233 | // this method can be moved when I add a MessageProcessor to the Game class
|
---|
234 | map<unsigned int, Projectile>::iterator itProj;
|
---|
235 | for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++) {
|
---|
236 | game = itGames->second;
|
---|
237 | for (itProj = game->getProjectiles().begin(); itProj != game->getProjectiles().end(); itProj++)
|
---|
238 | {
|
---|
239 | cout << "About to call projectile move" << endl;
|
---|
240 | if (itProj->second.move(game->getPlayers()))
|
---|
241 | {
|
---|
242 | // send a REMOVE_PROJECTILE message
|
---|
243 | cout << "send a REMOVE_PROJECTILE message" << endl;
|
---|
244 | serverMsg.type = MSG_TYPE_REMOVE_PROJECTILE;
|
---|
245 | memcpy(serverMsg.buffer, &itProj->second.id, 4);
|
---|
246 | game->removeProjectile(itProj->second.id);
|
---|
247 | msgProcessor.broadcastMessage(serverMsg, game->getPlayers());
|
---|
248 |
|
---|
249 | Player* target = game->getPlayers()[itProj->second.target];
|
---|
250 | game->dealDamageToPlayer(target, itProj->second.damage);
|
---|
251 | }
|
---|
252 | }
|
---|
253 | }
|
---|
254 | }
|
---|
255 |
|
---|
256 | if (msgProcessor.receiveMessage(&clientMsg, &from) >= 0)
|
---|
257 | {
|
---|
258 | processMessage(clientMsg, from, msgProcessor, mapPlayers, mapGames, da, outputLog);
|
---|
259 |
|
---|
260 | cout << "Finished processing the message" << endl;
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | outputLog << "Stopped server on " << getCurrentDateTimeString() << endl;
|
---|
265 | outputLog.close();
|
---|
266 |
|
---|
267 | // delete all games
|
---|
268 | map<string, Game*>::iterator itGames;
|
---|
269 | for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++)
|
---|
270 | {
|
---|
271 | delete itGames->second;
|
---|
272 | }
|
---|
273 |
|
---|
274 | map<unsigned int, Player*>::iterator itPlayers;
|
---|
275 | for (itPlayers = mapPlayers.begin(); itPlayers != mapPlayers.end(); itPlayers++)
|
---|
276 | {
|
---|
277 | delete itPlayers->second;
|
---|
278 | }
|
---|
279 |
|
---|
280 | return 0;
|
---|
281 | }
|
---|
282 |
|
---|
283 | void processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, DataAccess& da, ofstream& outputLog)
|
---|
284 | {
|
---|
285 | NETWORK_MSG serverMsg;
|
---|
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*20, &gameHistory[i][0], 4);
|
---|
616 | memcpy(serverMsg.buffer+20+i*20, &gameHistory[i][1], 4);
|
---|
617 | memcpy(serverMsg.buffer+24+i*20, &gameHistory[i][2], 4);
|
---|
618 | memcpy(serverMsg.buffer+28+i*20, &gameHistory[i][3], 4);
|
---|
619 | memcpy(serverMsg.buffer+32+i*20, &gameHistory[i][4], 4);
|
---|
620 | delete[] gameHistory[i];
|
---|
621 | }
|
---|
622 |
|
---|
623 | //delete[] gameHistory;
|
---|
624 | free(gameHistory);
|
---|
625 | delete[] playerRecord;
|
---|
626 |
|
---|
627 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
628 |
|
---|
629 | break;
|
---|
630 | }
|
---|
631 | case MSG_TYPE_CREATE_GAME:
|
---|
632 | {
|
---|
633 | cout << "Received a CREATE_GAME message" << endl;
|
---|
634 |
|
---|
635 | string gameName(clientMsg.buffer);
|
---|
636 | cout << "Game name: " << gameName << endl;
|
---|
637 |
|
---|
638 | // check if this game already exists
|
---|
639 | if (mapGames.find(gameName) != mapGames.end()) {
|
---|
640 | cout << "Error: Game already exists" << endl;
|
---|
641 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
642 | }else {
|
---|
643 | Game* g = new Game(gameName, "../data/map.txt", &msgProcessor);
|
---|
644 | mapGames[gameName] = g;
|
---|
645 |
|
---|
646 | // add flag objects to the map
|
---|
647 | WorldMap* m = g->getMap();
|
---|
648 | m->createObjectsFromStructures();
|
---|
649 |
|
---|
650 | serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS;
|
---|
651 | strcpy(serverMsg.buffer, gameName.c_str());
|
---|
652 | }
|
---|
653 |
|
---|
654 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
655 |
|
---|
656 | break;
|
---|
657 | }
|
---|
658 | case MSG_TYPE_JOIN_GAME:
|
---|
659 | {
|
---|
660 | cout << "Received a JOIN_GAME message" << endl;
|
---|
661 |
|
---|
662 | string gameName(clientMsg.buffer);
|
---|
663 | cout << "Game name: " << gameName << endl;
|
---|
664 |
|
---|
665 | // check if this game already exists
|
---|
666 | if (mapGames.find(gameName) == mapGames.end()) {
|
---|
667 | cout << "Error: Game does not exist" << endl;
|
---|
668 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
669 | }else {
|
---|
670 | Game* g = mapGames[gameName];
|
---|
671 | map<unsigned int, Player*>& players = g->getPlayers();
|
---|
672 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
673 |
|
---|
674 | if (players.find(p->getId()) != players.end()) {
|
---|
675 | cout << "Player " << p->name << " trying to join a game he's already in" << endl;
|
---|
676 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
677 | }else {
|
---|
678 | serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS;
|
---|
679 | strcpy(serverMsg.buffer, gameName.c_str());
|
---|
680 | }
|
---|
681 | }
|
---|
682 |
|
---|
683 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
684 |
|
---|
685 | break;
|
---|
686 | }
|
---|
687 | case MSG_TYPE_LEAVE_GAME:
|
---|
688 | {
|
---|
689 | cout << "Received a LEAVE_GAME message" << endl;
|
---|
690 |
|
---|
691 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
692 | Game* g = p->currentGame;
|
---|
693 |
|
---|
694 | if (g == NULL) {
|
---|
695 | cout << "Player " << p->name << " is trying to leave a game, but is not currently in a game." << endl;
|
---|
696 |
|
---|
697 | /// should send a response back, maybe a new message type is needed
|
---|
698 | // not sure what to do here
|
---|
699 | }else {
|
---|
700 | cout << "Game name: " << g->getName() << endl;
|
---|
701 |
|
---|
702 | if (!p->isDead) {
|
---|
703 | ObjectType flagType = OBJECT_NONE;
|
---|
704 | if (p->hasBlueFlag)
|
---|
705 | flagType = OBJECT_BLUE_FLAG;
|
---|
706 | else if (p->hasRedFlag)
|
---|
707 | flagType = OBJECT_RED_FLAG;
|
---|
708 |
|
---|
709 | if (flagType != OBJECT_NONE)
|
---|
710 | g->addObjectToMap(flagType, p->pos.x, p->pos.y);
|
---|
711 | }
|
---|
712 |
|
---|
713 | p->currentGame = NULL;
|
---|
714 | g->removePlayer(p->getId());
|
---|
715 |
|
---|
716 | unsigned int playerId = p->getId();
|
---|
717 | serverMsg.type = MSG_TYPE_LEAVE_GAME;
|
---|
718 | memcpy(serverMsg.buffer, &playerId, 4);
|
---|
719 | strcpy(serverMsg.buffer+4, g->getName().c_str());
|
---|
720 | msgProcessor.broadcastMessage(serverMsg, g->getPlayers());
|
---|
721 |
|
---|
722 | int numPlayers = g->getNumPlayers();
|
---|
723 |
|
---|
724 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
725 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
726 | strcpy(serverMsg.buffer+4, g->getName().c_str());
|
---|
727 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
728 |
|
---|
729 | // if there are no more players in the game, remove it
|
---|
730 | if (numPlayers == 0) {
|
---|
731 | mapGames.erase(g->getName());
|
---|
732 | delete g;
|
---|
733 | }
|
---|
734 | }
|
---|
735 |
|
---|
736 | break;
|
---|
737 | }
|
---|
738 | case MSG_TYPE_JOIN_GAME_ACK:
|
---|
739 | {
|
---|
740 | cout << "Received a JOIN_GAME_ACK message" << endl;
|
---|
741 |
|
---|
742 | string gameName(clientMsg.buffer);
|
---|
743 | cout << "Game name: " << gameName << endl;
|
---|
744 |
|
---|
745 | // check if this game already exists
|
---|
746 | if (mapGames.find(gameName) == mapGames.end()) {
|
---|
747 | serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
|
---|
748 |
|
---|
749 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
750 | }
|
---|
751 |
|
---|
752 | Game* g = mapGames[gameName];
|
---|
753 |
|
---|
754 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
755 |
|
---|
756 | // tell the new player about all map objects
|
---|
757 | // (currently just the flags)
|
---|
758 |
|
---|
759 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
760 | vector<WorldMap::Object>* vctObjects = g->getMap()->getObjects();
|
---|
761 | vector<WorldMap::Object>::iterator itObjects;
|
---|
762 | cout << "sending items" << endl;
|
---|
763 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
764 | itObjects->serialize(serverMsg.buffer);
|
---|
765 | cout << "sending item id " << itObjects->id << endl;
|
---|
766 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
767 | }
|
---|
768 |
|
---|
769 |
|
---|
770 | // send the current score
|
---|
771 | serverMsg.type = MSG_TYPE_SCORE;
|
---|
772 |
|
---|
773 | unsigned int blueScore = g->getBlueScore();
|
---|
774 | unsigned int redScore = g->getRedScore();
|
---|
775 | memcpy(serverMsg.buffer, &blueScore, 4);
|
---|
776 | memcpy(serverMsg.buffer+4, &redScore, 4);
|
---|
777 |
|
---|
778 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
779 |
|
---|
780 |
|
---|
781 | map<unsigned int, Player*>& oldPlayers = g->getPlayers();
|
---|
782 | g->addPlayer(p);
|
---|
783 | p->team = 0;
|
---|
784 |
|
---|
785 | // send info to other players
|
---|
786 | serverMsg.type = MSG_TYPE_PLAYER_JOIN_GAME;
|
---|
787 | p->serialize(serverMsg.buffer);
|
---|
788 | cout << "Should be broadcasting the message" << endl;
|
---|
789 | msgProcessor.broadcastMessage(serverMsg, oldPlayers);
|
---|
790 |
|
---|
791 |
|
---|
792 | // tell the new player about all the players in the game (including himself)
|
---|
793 | cout << "Sending other players to new player" << endl;
|
---|
794 | serverMsg.type = MSG_TYPE_PLAYER_JOIN_GAME;
|
---|
795 |
|
---|
796 | map<unsigned int, Player*>& allPlayers = g->getPlayers();
|
---|
797 | map<unsigned int, Player*>::iterator it;
|
---|
798 | for (it = allPlayers.begin(); it != allPlayers.end(); it++)
|
---|
799 | {
|
---|
800 | it->second->serialize(serverMsg.buffer);
|
---|
801 |
|
---|
802 | cout << "sending info about " << it->second->name << endl;
|
---|
803 | cout << "sending id " << it->second->getId() << endl;
|
---|
804 | msgProcessor.sendMessage(&serverMsg, &from);
|
---|
805 | }
|
---|
806 |
|
---|
807 |
|
---|
808 | int numPlayers = g->getNumPlayers();
|
---|
809 |
|
---|
810 | serverMsg.type = MSG_TYPE_GAME_INFO;
|
---|
811 | memcpy(serverMsg.buffer, &numPlayers, 4);
|
---|
812 | strcpy(serverMsg.buffer+4, gameName.c_str());
|
---|
813 | msgProcessor.broadcastMessage(serverMsg, mapPlayers);
|
---|
814 |
|
---|
815 | break;
|
---|
816 | }
|
---|
817 | case MSG_TYPE_JOIN_TEAM:
|
---|
818 | {
|
---|
819 | cout << "Received a JOIN_TEAM message" << endl;
|
---|
820 |
|
---|
821 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
822 | map<unsigned int, Player*> players = p->currentGame->getPlayers();
|
---|
823 |
|
---|
824 | memcpy(&(p->team), clientMsg.buffer, 4);
|
---|
825 |
|
---|
826 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
827 | p->serialize(serverMsg.buffer);
|
---|
828 | msgProcessor.broadcastMessage(serverMsg, players);
|
---|
829 |
|
---|
830 | break;
|
---|
831 | }
|
---|
832 | case MSG_TYPE_START_GAME:
|
---|
833 | {
|
---|
834 | cout << "Received a START_GAME message" << endl;
|
---|
835 |
|
---|
836 | Player* p = findPlayerByAddr(mapPlayers, from);
|
---|
837 | map<unsigned int, Player*> players = p->currentGame->getPlayers();
|
---|
838 |
|
---|
839 | serverMsg.type = MSG_TYPE_START_GAME;
|
---|
840 | msgProcessor.broadcastMessage(serverMsg, players);
|
---|
841 |
|
---|
842 | break;
|
---|
843 | }
|
---|
844 | default:
|
---|
845 | {
|
---|
846 | outputLog << "Received unknown message of type " << clientMsg.type << endl;
|
---|
847 |
|
---|
848 | break;
|
---|
849 | }
|
---|
850 | }
|
---|
851 | }
|
---|
852 |
|
---|
853 | Player *findPlayerByName(map<unsigned int, Player*> &m, string name)
|
---|
854 | {
|
---|
855 | map<unsigned int, Player*>::iterator it;
|
---|
856 |
|
---|
857 | for (it = m.begin(); it != m.end(); it++)
|
---|
858 | {
|
---|
859 | if ( it->second->name.compare(name) == 0 )
|
---|
860 | return it->second;
|
---|
861 | }
|
---|
862 |
|
---|
863 | return NULL;
|
---|
864 | }
|
---|
865 |
|
---|
866 | Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr)
|
---|
867 | {
|
---|
868 | map<unsigned int, Player*>::iterator it;
|
---|
869 |
|
---|
870 | for (it = m.begin(); it != m.end(); it++)
|
---|
871 | {
|
---|
872 | if ( it->second->addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
|
---|
873 | it->second->addr.sin_port == addr.sin_port )
|
---|
874 | return it->second;
|
---|
875 | }
|
---|
876 |
|
---|
877 | return NULL;
|
---|
878 | }
|
---|
879 |
|
---|
880 | bool handleGameEvents(Game* game, MessageProcessor* msgProcessor, DataAccess* da) {
|
---|
881 | map<unsigned int, Player*> players = game->getPlayers();
|
---|
882 | map<unsigned int, Player*>::iterator it;
|
---|
883 | bool gameFinished = false;
|
---|
884 |
|
---|
885 | for (it = players.begin(); it != players.end(); it++) {
|
---|
886 | gameFinished = gameFinished || handlePlayerEvents(game, it->second, msgProcessor, da);
|
---|
887 | }
|
---|
888 |
|
---|
889 | if (gameFinished) {
|
---|
890 | for (it = players.begin(); it != players.end(); it++) {
|
---|
891 | it->second->currentGame = NULL;
|
---|
892 | }
|
---|
893 | }
|
---|
894 |
|
---|
895 | return gameFinished;
|
---|
896 | }
|
---|
897 |
|
---|
898 | bool handlePlayerEvents(Game* game, Player* p, MessageProcessor* msgProcessor, DataAccess* da) {
|
---|
899 | NETWORK_MSG serverMsg;
|
---|
900 | FLOAT_POSITION oldPos;
|
---|
901 | bool gameFinished = false;
|
---|
902 | bool broadcastMove = false;
|
---|
903 |
|
---|
904 | cout << "moving player" << endl;
|
---|
905 |
|
---|
906 | // move player and perform associated tasks
|
---|
907 | oldPos = p->pos;
|
---|
908 | if (p->move(game->getMap())) {
|
---|
909 |
|
---|
910 | cout << "player moved" << endl;
|
---|
911 | if (game->processPlayerMovement(p, oldPos))
|
---|
912 | broadcastMove = true;
|
---|
913 | cout << "player move processed" << endl;
|
---|
914 |
|
---|
915 | ObjectType flagType;
|
---|
916 | POSITION pos;
|
---|
917 | bool flagTurnedIn = false;
|
---|
918 | bool flagReturned = false;
|
---|
919 | bool ownFlagAtBase = false;
|
---|
920 |
|
---|
921 | switch(game->getMap()->getStructure(p->pos.x/25, p->pos.y/25)) {
|
---|
922 | case STRUCTURE_BLUE_FLAG:
|
---|
923 | {
|
---|
924 | if (p->team == 1 && p->hasRedFlag) {
|
---|
925 | // check that your flag is at your base
|
---|
926 | pos = game->getMap()->getStructureLocation(STRUCTURE_BLUE_FLAG);
|
---|
927 |
|
---|
928 | vector<WorldMap::Object>* vctObjects = game->getMap()->getObjects();
|
---|
929 | vector<WorldMap::Object>::iterator itObjects;
|
---|
930 |
|
---|
931 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
932 | if (itObjects->type == OBJECT_BLUE_FLAG) {
|
---|
933 | if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
|
---|
934 | ownFlagAtBase = true;
|
---|
935 | break;
|
---|
936 | }
|
---|
937 | }
|
---|
938 | }
|
---|
939 |
|
---|
940 | if (ownFlagAtBase) {
|
---|
941 | p->hasRedFlag = false;
|
---|
942 | flagType = OBJECT_RED_FLAG;
|
---|
943 | pos = game->getMap()->getStructureLocation(STRUCTURE_RED_FLAG);
|
---|
944 | flagTurnedIn = true;
|
---|
945 | game->setBlueScore(game->getBlueScore()+1);
|
---|
946 | }
|
---|
947 | }
|
---|
948 |
|
---|
949 | break;
|
---|
950 | }
|
---|
951 | case STRUCTURE_RED_FLAG:
|
---|
952 | {
|
---|
953 | if (p->team == 2 && p->hasBlueFlag) {
|
---|
954 | // check that your flag is at your base
|
---|
955 | pos = game->getMap()->getStructureLocation(STRUCTURE_RED_FLAG);
|
---|
956 |
|
---|
957 | vector<WorldMap::Object>* vctObjects = game->getMap()->getObjects();
|
---|
958 | vector<WorldMap::Object>::iterator itObjects;
|
---|
959 |
|
---|
960 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
961 | if (itObjects->type == OBJECT_RED_FLAG) {
|
---|
962 | if (itObjects->pos.x == pos.x*25+12 && itObjects->pos.y == pos.y*25+12) {
|
---|
963 | ownFlagAtBase = true;
|
---|
964 | break;
|
---|
965 | }
|
---|
966 | }
|
---|
967 | }
|
---|
968 |
|
---|
969 | if (ownFlagAtBase) {
|
---|
970 | p->hasBlueFlag = false;
|
---|
971 | flagType = OBJECT_BLUE_FLAG;
|
---|
972 | pos = game->getMap()->getStructureLocation(STRUCTURE_BLUE_FLAG);
|
---|
973 | flagTurnedIn = true;
|
---|
974 | game->setRedScore(game->getRedScore()+1);
|
---|
975 | }
|
---|
976 | }
|
---|
977 |
|
---|
978 | break;
|
---|
979 | }
|
---|
980 | default:
|
---|
981 | {
|
---|
982 | break;
|
---|
983 | }
|
---|
984 | }
|
---|
985 |
|
---|
986 | if (flagTurnedIn) {
|
---|
987 | unsigned int blueScore = game->getBlueScore();
|
---|
988 | unsigned int redScore = game->getRedScore();
|
---|
989 |
|
---|
990 | pos.x = pos.x*25+12;
|
---|
991 | pos.y = pos.y*25+12;
|
---|
992 | game->addObjectToMap(flagType, pos.x, pos.y);
|
---|
993 |
|
---|
994 | serverMsg.type = MSG_TYPE_SCORE;
|
---|
995 | memcpy(serverMsg.buffer, &blueScore, 4);
|
---|
996 | memcpy(serverMsg.buffer+4, &redScore, 4);
|
---|
997 | msgProcessor->broadcastMessage(serverMsg, game->getPlayers());
|
---|
998 |
|
---|
999 | // give honor to everyone on the winning team
|
---|
1000 | map<unsigned int, Player*>::iterator itPlayers;
|
---|
1001 | for (itPlayers = game->getPlayers().begin(); itPlayers != game->getPlayers().end(); itPlayers++) {
|
---|
1002 | if (itPlayers->second->team == p->team) {
|
---|
1003 | itPlayers->second->honor += 50;
|
---|
1004 | da->updatePlayer(itPlayers->second);
|
---|
1005 | }
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 | // check to see if the game should end
|
---|
1009 | // move to its own method
|
---|
1010 | if (game->getBlueScore() == 3 || game->getRedScore() == 3) {
|
---|
1011 | gameFinished = true;
|
---|
1012 |
|
---|
1013 | unsigned int winningTeam;
|
---|
1014 | if (game->getBlueScore() == 3)
|
---|
1015 | winningTeam = 0;
|
---|
1016 | else if (game->getRedScore() == 3)
|
---|
1017 | winningTeam = 1;
|
---|
1018 |
|
---|
1019 | serverMsg.type = MSG_TYPE_FINISH_GAME;
|
---|
1020 |
|
---|
1021 | // I should create an instance of the GameSummary object here and just serialize it into this message
|
---|
1022 | memcpy(serverMsg.buffer, &winningTeam, 4);
|
---|
1023 | memcpy(serverMsg.buffer+4, &blueScore, 4);
|
---|
1024 | memcpy(serverMsg.buffer+8, &redScore, 4);
|
---|
1025 | strcpy(serverMsg.buffer+12, game->getName().c_str());
|
---|
1026 | msgProcessor->broadcastMessage(serverMsg, game->getPlayers());
|
---|
1027 |
|
---|
1028 | // give honor to everyone on the winning team
|
---|
1029 | for (itPlayers = game->getPlayers().begin(); itPlayers != game->getPlayers().end(); itPlayers++) {
|
---|
1030 | if (itPlayers->second->team == p->team) {
|
---|
1031 | itPlayers->second->honor += 150;
|
---|
1032 | da->updatePlayer(itPlayers->second);
|
---|
1033 | }
|
---|
1034 | }
|
---|
1035 | }
|
---|
1036 |
|
---|
1037 | // this means a PLAYER message will be sent
|
---|
1038 | broadcastMove = true;
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | // go through all objects and check if the player is close to one and if its their flag
|
---|
1042 | vector<WorldMap::Object>* vctObjects = game->getMap()->getObjects();
|
---|
1043 | vector<WorldMap::Object>::iterator itObjects;
|
---|
1044 | POSITION structPos;
|
---|
1045 |
|
---|
1046 | for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
|
---|
1047 | POSITION pos = itObjects->pos;
|
---|
1048 |
|
---|
1049 | if (posDistance(p->pos, pos.toFloat()) < 10) {
|
---|
1050 | if (p->team == 1 && itObjects->type == OBJECT_BLUE_FLAG) {
|
---|
1051 | structPos = game->getMap()->getStructureLocation(STRUCTURE_BLUE_FLAG);
|
---|
1052 | flagReturned = true;
|
---|
1053 | break;
|
---|
1054 | } else if (p->team == 2 && itObjects->type == OBJECT_RED_FLAG) {
|
---|
1055 | structPos = game->getMap()->getStructureLocation(STRUCTURE_RED_FLAG);
|
---|
1056 | flagReturned = true;
|
---|
1057 | break;
|
---|
1058 | }
|
---|
1059 | }
|
---|
1060 | }
|
---|
1061 |
|
---|
1062 | if (flagReturned) {
|
---|
1063 | itObjects->pos.x = structPos.x*25+12;
|
---|
1064 | itObjects->pos.y = structPos.y*25+12;
|
---|
1065 |
|
---|
1066 | serverMsg.type = MSG_TYPE_OBJECT;
|
---|
1067 | itObjects->serialize(serverMsg.buffer);
|
---|
1068 | msgProcessor->broadcastMessage(serverMsg, game->getPlayers());
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | if (broadcastMove) {
|
---|
1072 | serverMsg.type = MSG_TYPE_PLAYER;
|
---|
1073 | p->serialize(serverMsg.buffer);
|
---|
1074 | msgProcessor->broadcastMessage(serverMsg, game->getPlayers());
|
---|
1075 | }
|
---|
1076 | }
|
---|
1077 |
|
---|
1078 | cout << "processing player attack" << endl;
|
---|
1079 |
|
---|
1080 | // check if the player's attack animation is complete
|
---|
1081 | if (p->isAttacking && p->timeAttackStarted+p->attackCooldown <= getCurrentMillis()) {
|
---|
1082 | p->isAttacking = false;
|
---|
1083 | cout << "Attack animation is complete" << endl;
|
---|
1084 |
|
---|
1085 | //send everyone an ATTACK message
|
---|
1086 | cout << "about to broadcast attack" << endl;
|
---|
1087 |
|
---|
1088 | if (p->attackType == Player::ATTACK_MELEE) {
|
---|
1089 | cout << "Melee attack" << endl;
|
---|
1090 |
|
---|
1091 | Player* target = game->getPlayers()[p->getTargetPlayer()];
|
---|
1092 | game->dealDamageToPlayer(target, p->damage);
|
---|
1093 | } else if (p->attackType == Player::ATTACK_RANGED) {
|
---|
1094 | cout << "Ranged attack" << endl;
|
---|
1095 |
|
---|
1096 | Projectile proj(p->pos.x, p->pos.y, p->getTargetPlayer(), p->damage);
|
---|
1097 | game->assignProjectileId(&proj);
|
---|
1098 | game->addProjectile(proj);
|
---|
1099 |
|
---|
1100 | int x = p->pos.x;
|
---|
1101 | int y = p->pos.y;
|
---|
1102 | unsigned int targetId = p->getTargetPlayer();
|
---|
1103 |
|
---|
1104 | serverMsg.type = MSG_TYPE_PROJECTILE;
|
---|
1105 | memcpy(serverMsg.buffer, &proj.id, 4);
|
---|
1106 | memcpy(serverMsg.buffer+4, &x, 4);
|
---|
1107 | memcpy(serverMsg.buffer+8, &y, 4);
|
---|
1108 | memcpy(serverMsg.buffer+12, &targetId, 4);
|
---|
1109 | msgProcessor->broadcastMessage(serverMsg, game->getPlayers());
|
---|
1110 | } else
|
---|
1111 | cout << "Invalid attack type: " << p->attackType << endl;
|
---|
1112 | }
|
---|
1113 |
|
---|
1114 | return gameFinished;
|
---|
1115 | }
|
---|
1116 |
|
---|
1117 | void quit(int sig) {
|
---|
1118 | done = true;
|
---|
1119 | }
|
---|