Exemplo n.º 1
0
void CatanRoom::requestStartGame()
{
    CatanPlayer *player = qobject_cast<CatanPlayer*>(sender());
    Q_ASSERT(player);

    if( !player ) return;
    if( player != host ) return; //only the host can begin the game
    if( startTimer.isActive() ) return; //the game is already being started
    if( GetNumPlayers() < CATAN_MIN_PLAYERS ) return; //cannot start with less than 4 players
    //check to make sure everyone is ready.
    for( int i = 0; i < GetMaxPlayers(); i++ )
    {
        CatanPlayer *occupant = players[i];
        if( !occupant ) continue;

        if( !readyPlayers[occupant->GetID()] )
            return; //cannot start the game unless all players are ready.
    }

    startTimer.setSingleShot(true);
    startTimer.start( CATAN_ROOM_START_TIME * 1000 );

    net::Begin(NETWORK_COMMAND::SERVER_ROOM_STARTING);
    net::Send(getPlayers());
}
Exemplo n.º 2
0
void CatanRoom::RemovePlayer( CatanPlayer *player )
{
    disconnect( player, SIGNAL(requestLeaveRoom()),
                this, SLOT(onPlayerLeave()));
    disconnect( player, SIGNAL(destroyed()),
                this, SLOT(onPlayerDestroying()));

    if( players[player->GetID()] != player ) return;

    LOG_DEBUG("[Lobby " << GetID() << "] Removing player: " << player->GetName() << endl);

    //remove the player
    players[player->GetID()] = 0;
    readyPlayers[player->GetID()] = false;
    playerCount--;

    cancelStartup(player);
    emit removedPlayer(player);

    //remove this room if it is empty
    if( GetNumPlayers() == 0 )
    {
        player->deleteLater();
        this->deleteLater();
        return;
    }

    //get a list of all the players to send the message to
    QVector<CatanPlayer*> playerList = getPlayers();

    if( player == host ) //if this was the host
    {

        //remove the previous host
        disconnect( player, SIGNAL(requestStartGame()),
                    this,     SLOT(requestStartGame()));
        disconnect( player, SIGNAL(requestChangeConfig(QDataStream&)),
                    this,    SLOT(receivedChangeConfig(QDataStream&)));
        host = 0;

        //set the host to the next player in the list
        LOG_INFO("The host disconnected from lobby [" << (quint16)GetID() << "]" << endl);
        for( int i = 0; i < GetMaxPlayers(); i++ )
        {
            if( players[i] )
            {
                setHost(players[i]);
                break;
            }
        }
        if( host ) {
            LOG_DEBUG("New lobby host: " << host->GetName() << endl);
            net::Begin(NETWORK_COMMAND::SERVER_ROOM_NEW_HOST);
                net::AddByte(host->GetID());
            net::Send(playerList);
        }
    }
Exemplo n.º 3
0
QVector<CatanPlayer*> CatanRoom::getPlayers() const
{
    QVector<CatanPlayer*> playerList;
    for( int i = 0; i < GetMaxPlayers(); i++ )
    {
        if( players[i] )
            playerList.push_back(players[i]);
    }
    return playerList;
}
Exemplo n.º 4
0
int CSAMPFunctions::GetFreePlayerSlot()
{
	// Get the playerpool interface
	CSAMPServer *pSAMPServer = (CSAMPServer *)CAddress::VAR_ServerPtr;
	CSAMPPlayerPool *pPlayerPool = pSAMPServer->pPlayerPool;
	// Loop through all the players
	for(int i = (GetMaxPlayers() - 1); i != 0; i--)
	{
		// Is he not connected ?
		if(!pPlayerPool->bIsPlayerConnected[i])
			return i;
	}
	return INVALID_ENTITY_ID;
}
Exemplo n.º 5
0
bool BattleGround::HasFreeSlots() const
{
    return GetPlayersSize() < GetMaxPlayers();
}
Exemplo n.º 6
0
bool CatanRoom::AddUser( CatanUser *user )
{
    if( GetNumPlayers() >= config->GetMaxPlayers() ) return false;
    if( user->InGame() || user->InLobby() ) return false;

    int id = getNextPID( players );
    CatanPlayer* player = new CatanPlayer(id, user, this);

    //set the host
    if( !host )
        setHost( player );

    players[id] = player;
    playerCount++;

    LOG_DEBUG( "Lobby player count: " << GetNumPlayers() << endl );
    player->SetState(PLAYER_STATE::IN_LOBBY);

    //tell the current players in the room that this user has joined
    for( int i = 0; i < GetMaxPlayers(); i++ )
    {
        CatanPlayer *occupant = players[i];
        if( !occupant ) continue;

        net::Begin(NETWORK_COMMAND::SERVER_ROOM_USER_JOINED);
            net::AddShort(this->GetID());
            net::AddByte(player->GetID());
            net::AddString(player->GetName());
        net::Send(occupant->GetUser());
    }

    //tell this user about the current players in the room
    net::Begin(NETWORK_COMMAND::SERVER_ROOM_PLAYERS);
        net::AddByte(GetNumPlayers()-1); //don't include the new player
        //send the host first.
        net::AddByte(host->GetID());
        net::AddString(host->GetName());
        net::AddBool(readyPlayers[host->GetID()]);
        for( int i = 0; i < GetMaxPlayers(); i++ )
        {
            CatanPlayer *occupant = players[i];
            if( !occupant ) continue;

            if( occupant != host && occupant != player )
            {
                net::AddByte(occupant->GetID());
                net::AddString(occupant->GetName());
                net::AddBool(readyPlayers[occupant->GetID()]);
            }
        }
    net::Send(user);

    //send the game configuration
    transmitConfigFull(user);

    //if the user gets destroyed, remove him from the room
    connect( player, SIGNAL(destroying()),
             this, SLOT(onPlayerDestroying()));
    connect( player, SIGNAL(requestLeaveRoom()),
             this, SLOT(onPlayerLeave()));
    connect( player, SIGNAL(readyUp(bool)),
             this, SLOT(readyUp(bool)));

    emit addedPlayer(player);
    return true;

}