Changeset 2ee386d in network-game


Ignore:
Timestamp:
Sep 26, 2013, 3:33:18 AM (11 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
2992b1a
Parents:
99afbb8
Message:

Clients store the total number of players in each game

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • client/Client/main.cpp

    r99afbb8 r2ee386d  
    3434#include "../../common/Player.h"
    3535#include "../../common/Projectile.h"
     36#include "../../common/Game.h"
    3637
    3738#include "Window.h"
     
    118119chat chatConsole, debugConsole;
    119120bool debugging;
    120 vector<string> games;
     121map<string, Game> mapGames;
    121122
    122123MessageProcessor msgProcessor;
     
    451452
    452453         if (wndCurrent == wndLobby) {
    453             for (int i=0; i<games.size(); i++) {
    454                al_draw_text(font, al_map_rgb(0, 255, 0), SCREEN_W*1/4-100, 120+i*15, ALLEGRO_ALIGN_LEFT, games[i].c_str());
     454            map<string, Game>::iterator it;
     455            int i=0;
     456            for (it = mapGames.begin(); it != mapGames.end(); it++) {
     457               al_draw_text(font, al_map_rgb(0, 255, 0), SCREEN_W*1/4-100, 120+i*15, ALLEGRO_ALIGN_LEFT, it->first.c_str());
     458               i++;
    455459            }
    456460         }
     
    821825                cout << "Received a GAME_INFO message" << endl;
    822826
    823                string name(msg.buffer+4);
     827               string gameName(msg.buffer+4);
    824828               int numPlayers;
    825829
    826830               memcpy(&numPlayers, msg.buffer, 4);
    827831               
    828                cout << "Received game info for " << name << " (num players: " << numPlayers << ")" << endl;
    829                games.push_back(name);
     832               cout << "Received game info for " << gameName << " (num players: " << numPlayers << ")" << endl;
     833               
     834               if (mapGames.find(gameName) == mapGames.end())
     835                  mapGames[gameName] = Game(gameName);
     836               mapGames[gameName].setNumPlayers(numPlayers);
    830837
    831838               break;
  • common/Game.cpp

    r99afbb8 r2ee386d  
    2020}
    2121
     22int Game::getNumPlayers() {
     23   return players.size();
     24}
     25
     26void Game::setNumPlayers(int numPlayers) {
     27   int numCurPlayers = this->getNumPlayers();
     28   int numNewPlayers = numPlayers-numCurPlayers;
     29
     30   for (int i=0; i<numNewPlayers; i++)
     31      this->players[numCurPlayers+i] = NULL;
     32}
     33
    2234bool Game::addPlayer(Player* p) {
    2335   if (players.count(p->id) == 0) {
     
    3547      return false;
    3648}
    37 
    38 int Game::getNumPlayers() {
    39    return players.size();
    40 }
  • common/Game.h

    r99afbb8 r2ee386d  
    3030   ~Game();
    3131
     32   int getNumPlayers();
     33
    3234   void setId(int id);
    33 
     35   void setNumPlayers(int numPlayers);
    3436   bool addPlayer(Player* p);
    3537   bool removePlayer(int id);
    36    int getNumPlayers();
    3738};
    3839
Note: See TracChangeset for help on using the changeset viewer.