source: network-game/server/server.cpp@ f15d6a9

Last change on this file since f15d6a9 was f15d6a9, checked in by Dmitry Portnoy <dmp1488@…>, 10 years ago

Server processes JOIN_GAME messages and broadcasts the info to all players

  • Property mode set to 100644
File size: 25.5 KB
Line 
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
41using namespace std;
42
43// from used to be const. Removed that so I could take a reference
44// and use it to send messages
45void processMessage(const NETWORK_MSG& clientMsg, struct sockaddr_in& from, MessageProcessor& msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, unsigned int& unusedPlayerId, ofstream& outputLog);
46
47void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player*>& mapPlayers);
48Player *findPlayerByName(map<unsigned int, Player*> &m, string name);
49Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr);
50
51void quit(int sig);
52
53bool done;
54
55int main(int argc, char *argv[])
56{
57 int sock, length;
58 struct sockaddr_in server;
59 struct sockaddr_in from; // info of client sending the message
60 NETWORK_MSG clientMsg, serverMsg;
61 MessageProcessor msgProcessor;
62 map<unsigned int, Player*> mapPlayers;
63 map<unsigned int, Projectile> mapProjectiles;
64 map<string, Game*> mapGames;
65 unsigned int unusedPlayerId = 1;
66 ofstream outputLog;
67
68 done = false;
69
70 signal(SIGINT, quit);
71
72 //SSL_load_error_strings();
73 //ERR_load_BIO_strings();
74 //OpenSSL_add_all_algorithms();
75
76 if (argc != 2)
77 {
78 cerr << "ERROR, expected server [domain] [port]" << endl;
79 exit(1);
80 }
81
82 outputLog.open("server.log", ios::app);
83 outputLog << "Started server on " << getCurrentDateTimeString() << endl;
84
85 sock = socket(AF_INET, SOCK_DGRAM, 0);
86 if (sock < 0)
87 error("Opening socket");
88 length = sizeof(server);
89 bzero(&server,length);
90 server.sin_family=AF_INET;
91 server.sin_port=htons(atoi(argv[1]));
92 server.sin_addr.s_addr=INADDR_ANY;
93 if ( bind(sock, (struct sockaddr *)&server, length) < 0 )
94 error("binding");
95
96 set_nonblock(sock);
97
98 msgProcessor = MessageProcessor(sock, &outputLog);
99
100 timespec ts;
101 int timeLastUpdated = 0, curTime = 0;
102 while (!done)
103 {
104 usleep(5000);
105
106 clock_gettime(CLOCK_REALTIME, &ts);
107 // make the number smaller so millis can fit in an int
108 ts.tv_sec -= 1368000000;
109 curTime = ts.tv_sec*1000 + ts.tv_nsec/1000000;
110
111 if (timeLastUpdated == 0 || (curTime-timeLastUpdated) >= 50)
112 {
113 timeLastUpdated = curTime;
114
115 msgProcessor.cleanAckedMessages();
116 msgProcessor.resendUnackedMessages();
117
118 map<unsigned int, Player*>::iterator it;
119
120 cout << "Updating player targets and respawning dead players" << endl;
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 0:// blue team
142 spawnPos = p->currentGame->getMap()->getStructureLocation(STRUCTURE_BLUE_FLAG);
143 break;
144 case 1:// 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 cout << "Processing players in a game" << endl;
182
183 // process players currently in a game
184 map<string, Game*>::iterator itGames;
185 Game* game = NULL;
186
187 for (itGames = mapGames.begin(); itGames != mapGames.end();) {
188 game = itGames->second;
189 if (game->handleGameEvents()) {
190 // send a GAME_INFO message with 0 players to force clients to delete the game
191 int numPlayers = 0;
192 serverMsg.type = MSG_TYPE_GAME_INFO;
193 memcpy(serverMsg.buffer, &numPlayers, 4);
194 strcpy(serverMsg.buffer+4, game->getName().c_str());
195 msgProcessor.broadcastMessage(serverMsg, mapPlayers);
196
197 delete itGames->second;
198 mapGames.erase(itGames++);
199 }else
200 itGames++;
201 }
202
203 cout << "Processing projectiles" << endl;
204
205 // move all projectiles
206 // see if this can be moved inside the game class
207 // this method can be moved when I add a MessageProcessor to the Game class
208 map<unsigned int, Projectile>::iterator itProj;
209 for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++) {
210 game = itGames->second;
211 for (itProj = game->getProjectiles().begin(); itProj != game->getProjectiles().end(); itProj++)
212 {
213 cout << "About to call projectile move" << endl;
214 if (itProj->second.move(game->getPlayers()))
215 {
216 // send a REMOVE_PROJECTILE message
217 cout << "send a REMOVE_PROJECTILE message" << endl;
218 serverMsg.type = MSG_TYPE_REMOVE_PROJECTILE;
219 memcpy(serverMsg.buffer, &itProj->second.id, 4);
220 game->removeProjectile(itProj->second.id);
221 msgProcessor.broadcastMessage(serverMsg, game->getPlayers());
222
223 Player* target = game->getPlayers()[itProj->second.target];
224 game->dealDamageToPlayer(target, itProj->second.damage);
225 }
226 }
227 }
228 }
229
230 if (msgProcessor.receiveMessage(&clientMsg, &from) >= 0)
231 {
232 processMessage(clientMsg, from, msgProcessor, mapPlayers, mapGames, unusedPlayerId, outputLog);
233
234 cout << "Finished processing the message" << endl;
235 }
236 }
237
238 outputLog << "Stopped server on " << getCurrentDateTimeString() << endl;
239 outputLog.close();
240
241 // delete all games
242 map<string, Game*>::iterator itGames;
243 for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++)
244 {
245 delete itGames->second;
246 }
247
248 map<unsigned int, Player*>::iterator itPlayers;
249 for (itPlayers = mapPlayers.begin(); itPlayers != mapPlayers.end(); itPlayers++)
250 {
251 delete itPlayers->second;
252 }
253
254 return 0;
255}
256
257void processMessage(const NETWORK_MSG &clientMsg, struct sockaddr_in &from, MessageProcessor &msgProcessor, map<unsigned int, Player*>& mapPlayers, map<string, Game*>& mapGames, unsigned int& unusedPlayerId, ofstream& outputLog)
258{
259 NETWORK_MSG serverMsg;
260 DataAccess da;
261
262 cout << "Inside processMessage" << endl;
263
264 cout << "Received message" << endl;
265 cout << "MSG: type: " << clientMsg.type << endl;
266 cout << "MSG contents: " << clientMsg.buffer << endl;
267
268 // Check that if an invalid message is sent, the client will correctly
269 // receive and display the response. Maybe make a special error msg type
270 switch(clientMsg.type)
271 {
272 case MSG_TYPE_REGISTER:
273 {
274 string username(clientMsg.buffer);
275 string password(strchr(clientMsg.buffer, '\0')+1);
276 Player::PlayerClass playerClass;
277
278 memcpy(&playerClass, clientMsg.buffer+username.length()+password.length()+2, 4);
279
280 cout << "username: " << username << endl;
281 cout << "password: " << password << endl;
282
283 bool validClass = false;
284
285 switch(playerClass) {
286 case Player::CLASS_WARRIOR:
287 case Player::CLASS_RANGER:
288 validClass = true;
289 break;
290 case Player::CLASS_NONE:
291 validClass = false;
292 break;
293 }
294
295 serverMsg.type = MSG_TYPE_REGISTER;
296
297 if (validClass) {
298 int error = da.insertPlayer(username, password, playerClass);
299
300 if (error)
301 strcpy(serverMsg.buffer, "Registration failed. Please try again.");
302 else
303 strcpy(serverMsg.buffer, "Registration successful.");
304 }else
305 strcpy(serverMsg.buffer, "You didn't select a class");
306
307 msgProcessor.sendMessage(&serverMsg, &from);
308
309 break;
310 }
311 case MSG_TYPE_LOGIN:
312 {
313 cout << "Got login message" << endl;
314
315 string username(clientMsg.buffer);
316 string password(strchr(clientMsg.buffer, '\0')+1);
317
318 Player* p = da.getPlayer(username);
319
320 if (p == NULL || !da.verifyPassword(password, p->password))
321 {
322 strcpy(serverMsg.buffer, "Incorrect username or password");
323 if (p != NULL)
324 delete(p);
325 }
326 else if(findPlayerByName(mapPlayers, username) != NULL)
327 {
328 strcpy(serverMsg.buffer, "Player has already logged in.");
329 delete(p);
330 }
331 else
332 {
333 updateUnusedPlayerId(unusedPlayerId, mapPlayers);
334 p->setId(unusedPlayerId);
335 cout << "new player id: " << p->getId() << endl;
336 p->setAddr(from);
337
338 serverMsg.type = MSG_TYPE_PLAYER;
339 // tell the new player about all the existing players
340 cout << "Sending other players to new player" << endl;
341
342 map<unsigned int, Player*>::iterator it;
343 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
344 {
345 it->second->serialize(serverMsg.buffer);
346
347 cout << "sending info about " << it->second->name << endl;
348 cout << "sending id " << it->second->getId() << endl;
349 msgProcessor.sendMessage(&serverMsg, &from);
350 }
351
352 // send info about existing games to new player
353 map<string, Game*>::iterator itGames;
354 Game* g;
355 int numPlayers;
356 serverMsg.type = MSG_TYPE_GAME_INFO;
357
358 for (itGames = mapGames.begin(); itGames != mapGames.end(); itGames++)
359 {
360 g = itGames->second;
361 numPlayers = g->getNumPlayers();
362 memcpy(serverMsg.buffer, &numPlayers, 4);
363 strcpy(serverMsg.buffer+4, g->getName().c_str());
364 msgProcessor.sendMessage(&serverMsg, &from);
365 }
366
367 serverMsg.type = MSG_TYPE_PLAYER;
368 p->serialize(serverMsg.buffer);
369 msgProcessor.broadcastMessage(serverMsg, mapPlayers);
370
371 mapPlayers[unusedPlayerId] = p;
372 }
373
374 serverMsg.type = MSG_TYPE_LOGIN;
375 msgProcessor.sendMessage(&serverMsg, &from);
376
377 break;
378 }
379 case MSG_TYPE_LOGOUT:
380 {
381 string name(clientMsg.buffer);
382 cout << "Player logging out: " << name << endl;
383
384 Player *p = findPlayerByName(mapPlayers, name);
385
386 if (p == NULL)
387 {
388 strcpy(serverMsg.buffer+4, "That player is not logged in. This is either a bug, or you're trying to hack the server.");
389 cout << "Player not logged in" << endl;
390 }
391 else if ( p->addr.sin_addr.s_addr != from.sin_addr.s_addr ||
392 p->addr.sin_port != from.sin_port )
393 {
394 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.");
395 cout << "Player logged in using a different connection" << endl;
396 }
397 else
398 {
399 // broadcast to all players before deleting p from the map
400 unsigned int playerId = p->getId();
401 serverMsg.type = MSG_TYPE_LOGOUT;
402 memcpy(serverMsg.buffer, &playerId, 4);
403
404 msgProcessor.broadcastMessage(serverMsg, mapPlayers);
405
406 if (p->getId() < unusedPlayerId)
407 unusedPlayerId = p->getId();
408
409 mapPlayers.erase(p->getId());
410 delete p;
411
412 strcpy(serverMsg.buffer+4, "You have successfully logged out.");
413 }
414
415 serverMsg.type = MSG_TYPE_LOGOUT;
416 msgProcessor.sendMessage(&serverMsg, &from);
417
418 break;
419 }
420 case MSG_TYPE_CHAT:
421 {
422 cout << "Got a chat message" << endl;
423
424 serverMsg.type = MSG_TYPE_CHAT;
425
426 Player *p = findPlayerByAddr(mapPlayers, from);
427
428 if (p == NULL)
429 {
430 strcpy(serverMsg.buffer, "No player is logged in using this connection. This is either a bug, or you're trying to hack the server.");
431 msgProcessor.sendMessage(&serverMsg, &from);
432 }
433 else
434 {
435 ostringstream oss;
436 oss << p->name << ": " << clientMsg.buffer;
437
438 strcpy(serverMsg.buffer, oss.str().c_str());
439 msgProcessor.broadcastMessage(serverMsg, mapPlayers);
440 }
441
442 break;
443 }
444 case MSG_TYPE_PLAYER_MOVE:
445 {
446 cout << "PLAYER_MOVE" << endl;
447
448 unsigned int id;
449 int x, y;
450
451 memcpy(&id, clientMsg.buffer, 4);
452 memcpy(&x, clientMsg.buffer+4, 4);
453 memcpy(&y, clientMsg.buffer+8, 4);
454
455 cout << "x: " << x << endl;
456 cout << "y: " << y << endl;
457 cout << "id: " << id << endl;
458
459 Player* p = mapPlayers[id];
460 bool validMessage = false;
461
462 if ( p->addr.sin_addr.s_addr == from.sin_addr.s_addr &&
463 p->addr.sin_port == from.sin_port )
464 {
465 if (p->currentGame->startPlayerMovement(id, x, y)) {
466 serverMsg.type = MSG_TYPE_PLAYER_MOVE;
467
468 memcpy(serverMsg.buffer, &id, 4);
469 memcpy(serverMsg.buffer+4, &p->target.x, 4);
470 memcpy(serverMsg.buffer+8, &p->target.y, 4);
471
472 msgProcessor.broadcastMessage(serverMsg, mapPlayers);
473
474 validMessage = true;
475 }
476 else
477 cout << "Bad terrain detected" << endl;
478 }
479 else
480 cout << "Player id (" << id << ") doesn't match sender" << endl;
481
482 if (!validMessage)
483 msgProcessor.sendMessage(&serverMsg, &from);
484
485 break;
486 }
487 case MSG_TYPE_PICKUP_FLAG:
488 {
489 // may want to check the id matches the sender, just like for PLAYER_NOVE
490 cout << "PICKUP_FLAG" << endl;
491
492 unsigned int id;
493
494 memcpy(&id, clientMsg.buffer, 4);
495 cout << "id: " << id << endl;
496
497 Player* p = mapPlayers[id];
498 unsigned int objectId = p->currentGame->processFlagPickupRequest(p);
499
500 if (objectId >= 0) {
501 map<unsigned int, Player*> players = p->currentGame->getPlayers();
502
503 serverMsg.type = MSG_TYPE_REMOVE_OBJECT;
504 memcpy(serverMsg.buffer, &objectId, 4);
505 msgProcessor.broadcastMessage(serverMsg, players);
506
507 serverMsg.type = MSG_TYPE_PLAYER;
508 p->serialize(serverMsg.buffer);
509 msgProcessor.broadcastMessage(serverMsg, players);
510 }
511
512 break;
513 }
514 case MSG_TYPE_DROP_FLAG:
515 {
516 // may want to check the id matches the sender, just like for PLAYER_NOVE
517 cout << "DROP_FLAG" << endl;
518
519 unsigned int id;
520
521 memcpy(&id, clientMsg.buffer, 4);
522 cout << "id: " << id << endl;
523
524 Player* p = mapPlayers[id];
525
526 ObjectType flagType = OBJECT_NONE;
527 if (p->hasBlueFlag)
528 flagType = OBJECT_BLUE_FLAG;
529 else if (p->hasRedFlag)
530 flagType = OBJECT_RED_FLAG;
531
532 map<unsigned int, Player*> players = p->currentGame->getPlayers();
533
534 p->currentGame->addObjectToMap(flagType, p->pos.x, p->pos.y);
535
536 p->hasBlueFlag = false;
537 p->hasRedFlag = false;
538
539 serverMsg.type = MSG_TYPE_PLAYER;
540 p->serialize(serverMsg.buffer);
541 msgProcessor.broadcastMessage(serverMsg, players);
542
543 break;
544 }
545 case MSG_TYPE_ATTACK:
546 {
547 cout << "Received a START_ATTACK message" << endl;
548
549 unsigned int id, targetId;
550
551 memcpy(&id, clientMsg.buffer, 4);
552 memcpy(&targetId, clientMsg.buffer+4, 4);
553
554 // need to make sure the target is in the sender's game
555
556 Player* p = mapPlayers[id];
557 p->setTargetPlayer(targetId);
558 p->isChasing = true;
559
560 map<unsigned int, Player*> players = p->currentGame->getPlayers();
561
562 serverMsg.type = MSG_TYPE_ATTACK;
563 memcpy(serverMsg.buffer, &id, 4);
564 memcpy(serverMsg.buffer+4, &targetId, 4);
565 msgProcessor.broadcastMessage(serverMsg, players);
566
567 break;
568 }
569 case MSG_TYPE_CREATE_GAME:
570 {
571 cout << "Received a CREATE_GAME message" << endl;
572
573 string gameName(clientMsg.buffer);
574 cout << "Game name: " << gameName << endl;
575
576 // check if this game already exists
577 if (mapGames.find(gameName) != mapGames.end()) {
578 cout << "Error: Game already exists" << endl;
579 serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
580 }else {
581 Game* g = new Game(gameName, "../data/map.txt", &msgProcessor);
582 mapGames[gameName] = g;
583
584 // add flag objects to the map
585 WorldMap* m = g->getMap();
586 m->createObjectsFromStructures();
587
588 serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS;
589 strcpy(serverMsg.buffer, gameName.c_str());
590 }
591
592 msgProcessor.sendMessage(&serverMsg, &from);
593
594 break;
595 }
596 case MSG_TYPE_JOIN_GAME:
597 {
598 cout << "Received a JOIN_GAME message" << endl;
599
600 string gameName(clientMsg.buffer);
601 cout << "Game name: " << gameName << endl;
602
603 // check if this game already exists
604 if (mapGames.find(gameName) == mapGames.end()) {
605 cout << "Error: Game does not exist" << endl;
606 serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
607 }else {
608 Game* g = mapGames[gameName];
609 map<unsigned int, Player*>& players = g->getPlayers();
610 Player* p = findPlayerByAddr(mapPlayers, from);
611
612 if (players.find(p->getId()) != players.end()) {
613 cout << "Player " << p->name << " trying to join a game he's already in" << endl;
614 serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
615 }else {
616 serverMsg.type = MSG_TYPE_JOIN_GAME_SUCCESS;
617 strcpy(serverMsg.buffer, gameName.c_str());
618 }
619 }
620
621 msgProcessor.sendMessage(&serverMsg, &from);
622
623 break;
624 }
625 case MSG_TYPE_LEAVE_GAME:
626 {
627 cout << "Received a LEAVE_GAME message" << endl;
628
629 Player* p = findPlayerByAddr(mapPlayers, from);
630 Game* g = p->currentGame;
631
632 if (g == NULL) {
633 cout << "Player " << p->name << " is trying to leave a game, but is not currently in a game." << endl;
634
635 /// should send a response back, maybe a new message type is needed
636 // not sure what to do here
637 }else {
638 cout << "Game name: " << g->getName() << endl;
639
640 if (!p->isDead) {
641 ObjectType flagType = OBJECT_NONE;
642 if (p->hasBlueFlag)
643 flagType = OBJECT_BLUE_FLAG;
644 else if (p->hasRedFlag)
645 flagType = OBJECT_RED_FLAG;
646
647 if (flagType != OBJECT_NONE)
648 g->addObjectToMap(flagType, p->pos.x, p->pos.y);
649 }
650
651 p->currentGame = NULL;
652 g->removePlayer(p->getId());
653
654 unsigned int playerId = p->getId();
655 serverMsg.type = MSG_TYPE_LEAVE_GAME;
656 memcpy(serverMsg.buffer, &playerId, 4);
657 strcpy(serverMsg.buffer+4, g->getName().c_str());
658 msgProcessor.broadcastMessage(serverMsg, g->getPlayers());
659
660 int numPlayers = g->getNumPlayers();
661
662 serverMsg.type = MSG_TYPE_GAME_INFO;
663 memcpy(serverMsg.buffer, &numPlayers, 4);
664 strcpy(serverMsg.buffer+4, g->getName().c_str());
665 msgProcessor.broadcastMessage(serverMsg, mapPlayers);
666
667 // if there are no more players in the game, remove it
668 if (numPlayers == 0) {
669 mapGames.erase(g->getName());
670 delete g;
671 }
672 }
673
674 break;
675 }
676 case MSG_TYPE_JOIN_GAME_ACK:
677 {
678 cout << "Received a JOIN_GAME_ACK message" << endl;
679
680 string gameName(clientMsg.buffer);
681 cout << "Game name: " << gameName << endl;
682
683 // check if this game already exists
684 if (mapGames.find(gameName) == mapGames.end()) {
685 serverMsg.type = MSG_TYPE_JOIN_GAME_FAILURE;
686
687 msgProcessor.sendMessage(&serverMsg, &from);
688 }
689
690 Game* g = mapGames[gameName];
691
692 Player* p = findPlayerByAddr(mapPlayers, from);
693
694 // tell the new player about all map objects
695 // (currently just the flags)
696
697 serverMsg.type = MSG_TYPE_OBJECT;
698 vector<WorldMap::Object>* vctObjects = g->getMap()->getObjects();
699 vector<WorldMap::Object>::iterator itObjects;
700 cout << "sending items" << endl;
701 for (itObjects = vctObjects->begin(); itObjects != vctObjects->end(); itObjects++) {
702 itObjects->serialize(serverMsg.buffer);
703 cout << "sending item id " << itObjects->id << endl;
704 msgProcessor.sendMessage(&serverMsg, &from);
705 }
706
707
708 // send the current score
709 serverMsg.type = MSG_TYPE_SCORE;
710
711 unsigned int blueScore = g->getBlueScore();
712 unsigned int redScore = g->getRedScore();
713 memcpy(serverMsg.buffer, &blueScore, 4);
714 memcpy(serverMsg.buffer+4, &redScore, 4);
715
716 msgProcessor.sendMessage(&serverMsg, &from);
717
718
719 map<unsigned int, Player*>& oldPlayers = g->getPlayers();
720 g->addPlayer(p, true);
721 p->team = -1;
722
723 // send info to other players
724 serverMsg.type = MSG_TYPE_PLAYER_JOIN_GAME;
725 p->serialize(serverMsg.buffer);
726 cout << "Should be broadcasting the message" << endl;
727 msgProcessor.broadcastMessage(serverMsg, oldPlayers);
728
729
730 // tell the new player about all the players in the game (including himself)
731 cout << "Sending other players to new player" << endl;
732 serverMsg.type = MSG_TYPE_PLAYER_JOIN_GAME;
733
734 map<unsigned int, Player*>& allPlayers = g->getPlayers();
735 map<unsigned int, Player*>::iterator it;
736 for (it = allPlayers.begin(); it != allPlayers.end(); it++)
737 {
738 it->second->serialize(serverMsg.buffer);
739
740 cout << "sending info about " << it->second->name << endl;
741 cout << "sending id " << it->second->getId() << endl;
742 msgProcessor.sendMessage(&serverMsg, &from);
743 }
744
745
746 int numPlayers = g->getNumPlayers();
747
748 serverMsg.type = MSG_TYPE_GAME_INFO;
749 memcpy(serverMsg.buffer, &numPlayers, 4);
750 strcpy(serverMsg.buffer+4, gameName.c_str());
751 msgProcessor.broadcastMessage(serverMsg, mapPlayers);
752
753 break;
754 }
755 case MSG_TYPE_JOIN_TEAM:
756 {
757 cout << "Received a JOIN_TEAM message" << endl;
758
759 Player* p = findPlayerByAddr(mapPlayers, from);
760 map<unsigned int, Player*> players = p->currentGame->getPlayers();
761
762 memcpy(&(p->team), clientMsg.buffer, 4);
763
764 serverMsg.type = MSG_TYPE_PLAYER;
765 p->serialize(serverMsg.buffer);
766 msgProcessor.broadcastMessage(serverMsg, players);
767 }
768 default:
769 {
770 outputLog << "Received unknown message of type " << clientMsg.type << endl;
771
772 break;
773 }
774 }
775}
776
777void updateUnusedPlayerId(unsigned int& id, map<unsigned int, Player*>& mapPlayers)
778{
779 while (mapPlayers.find(id) != mapPlayers.end())
780 id++;
781}
782
783Player *findPlayerByName(map<unsigned int, Player*> &m, string name)
784{
785 map<unsigned int, Player*>::iterator it;
786
787 for (it = m.begin(); it != m.end(); it++)
788 {
789 if ( it->second->name.compare(name) == 0 )
790 return it->second;
791 }
792
793 return NULL;
794}
795
796Player *findPlayerByAddr(map<unsigned int, Player*> &m, const sockaddr_in &addr)
797{
798 map<unsigned int, Player*>::iterator it;
799
800 for (it = m.begin(); it != m.end(); it++)
801 {
802 if ( it->second->addr.sin_addr.s_addr == addr.sin_addr.s_addr &&
803 it->second->addr.sin_port == addr.sin_port )
804 return it->second;
805 }
806
807 return NULL;
808}
809
810void quit(int sig) {
811 done = true;
812}
Note: See TracBrowser for help on using the repository browser.