static int npmodule_listPlayers (lua_State *L)
{
    int tindex = 0; // will push ++tindex
    lua_newtable(L);

    for ( int n = 0; n < PlayerInterface::getMaxPlayers(); n++)
    {
        PlayerState *p = PlayerInterface::getPlayer(n);
        if ( ! p->isFree() )
        {
            lua_newtable(L);
            lua_pushinteger(L, n);
            lua_setfield(L, -2, "id");

            lua_pushstring(L, p->isAllocated() ? "<preconnect>" : p->getName().c_str());
            lua_setfield(L, -2, "name");

            lua_pushstring(L, PlayerInterface::isLocalPlayer(n) ? "local" : SERVER->getIP(n).c_str());
            lua_setfield(L, -2, "ip");

            lua_rawseti(L, -2, ++tindex);
        }
    }

    return 1;
}
示例#2
0
void Game::loadSavedGame()
{
	GameState gs;
	// create and open an archive for input
	std::ifstream ifs("saved_game");
	boost::archive::text_iarchive ia(ifs);
	// read class state from archive
	ia >> gs;

	// We have a gamestate. Now, let's reset the content of our scene
	depopulateLevel();
	depopulatePlayers();

	// Allocate level
	if (myLevel == NULL) {
		// the above should hold of course
		myLevel = myLevelFactory->createLevelFromSavedGame(gs);
		// Unfortunately, we have to add all the objects to the collision manager one by one
		std::vector<LevelBlock*> objects = myLevel->getLevelObjects();
		for (std::vector<LevelBlock*>::iterator ite = objects.begin(); ite != objects.end(); ++ite) {
			GameCollisionHandler->addNewObject(*ite);
		}
	}
	
	// Allocate players
	int pn = gs.getPlayerStateNumber();
	populatePlayers(pn);
	// Set the properties of players
	for (int i=0; i < pn; i++) {
		PlayerState ps = gs.getPlayerState(i);
		local_players[i]->SetPosition(Ogre::Vector3(ps.getPosX(), ps.getPosY(), ps.getPosZ()));
		local_players[i]->SetName(ps.getName());
		// Set the scores
		//std::cout << "Size: " << scoreVector.size() << std::endl;
		ScoreManager::getSingletonPtr()->setScore(ps.getName(), gs.getScore(i));
	}

}
void tPlayerStateBox::UpdateState(bool ForceUpdate)
{
    int nbPlayer = PlayerInterface::getActivePlayerCount();
    if (GameConfig::game_teammode && ShowTeam)
        nbPlayer = TeamManager::CountPlayerinTeam(TeamNumber);

    if ((nbPlayer != Count()) || ForceUpdate) 
    {
        Clear();
        for ( int i = 0; i < PlayerInterface::getMaxPlayers(); ++i)
        {
            PlayerState* state = PlayerInterface::getPlayer(i);
            if( state->isActive() )
            {
                if (ShowTeam)
                {
                    if (state->getTeamID() == TeamNumber)
                        AddData(state->getName(), state);
                } 
                else AddData(state->getName(), state);
            }
        }
    }
}
void tPlayerStateBox::onPaint(Surface &dst, const DataItem& data)
{
    char statBuf[256];
    int StartX = 1;
    PlayerState *state = (PlayerState*)(data.Data);
    if (DrawFlags)
    {
        Surface * flag = 0;
        flag = ResourceManager::getFlag(state->getFlag());
        flag->blt(dst, 0, 0);
        StartX = flag->getWidth()+4;
    }
    snprintf(statBuf, sizeof(statBuf), "%-20s", state->getName().c_str());
    dst.bltString(StartX , 4, statBuf, ctTexteNormal);
}
bool
DedicatedGameManager::mainLoop()
{
    if ( heartbeat )
        heartbeat->checkHeartbeat();

    static NTimer aktimer(10000); //all 10 sec only
    if (aktimer.isTimeOut())
    {
        aktimer.reset();
        PlayerState * player = 0;
        unsigned long max_players;
        max_players = PlayerInterface::getMaxPlayers();
        for (unsigned long i = 0; i < max_players; i++)
        {
            player = PlayerInterface::getPlayer((unsigned short) i);
            if ( player->isActive() )
            {
                if ( player->checkAutokick() )
                {
                    char chat_string[256];
                    sprintf(chat_string, "Server kicked '%s' due to inactivity",player->getName().c_str());
                    LOGGER.info("DED: %s", chat_string);
                    ChatInterface::serversay(chat_string);
                    SERVER->kickClient((PlayerID)i);

                }
            }

        }
        if (VoteManager::checkVoteTimer())
        {
            VoteManager::checkPlayersVote();
        }
    }
    return BaseGameManager::mainLoop();
}
void
NetworkServer::onClientDisconected(ClientSocket *s, const char * msg)
{
    LOGGER.debug("NetworkServer::onClientDisconected( %d, '%s')", s->getId(), msg);
    
    bool cleandisconnect = false;
    bool sendalert = true;
    
    if ( ServerConnectDaemon::inConnectQueue(s) )
    {
        // player was connecting and dropped.
        ServerConnectDaemon::removeClientFromQueue(s);
        sendalert = false;
        LOGGER.debug("NetworkServer::onClientDisconected player was connecting");
    }
    
    if ( NetworkInterface::receive_queue.isReady() )
    {
        unsigned long frontsave = NetworkInterface::receive_queue.front;
        while ( NetworkInterface::receive_queue.isReady() )
        {
            LOGGER.debug("NetworkServer::onClientDisconected there was a packet");
            NetPacket packet;

            NetworkInterface::receive_queue.dequeue(&packet);

            if ( packet.fromClient == s )
            {
                LOGGER.debug("NetworkServer::onClientDisconected the packet was from our friend");
                const NetMessage * netmessage = packet.getNetMessage();
                if (   netmessage->message_class == _net_message_class_connect 
                    && netmessage->message_id == _net_message_id_connect_netPanzer_client_disconnect )
                {
                    LOGGER.debug("NetworkServer::onClientDisconected so is a clean disconnect");
                    cleandisconnect = true;
                }
            }
        }
        NetworkInterface::receive_queue.front = frontsave;
    }
    
    PlayerID player_index = s->getPlayerIndex();
    
    ClientList::iterator i = client_list.begin();
    while ( i != client_list.end() )
    {
        if ( (*i)->client_socket == s )
        {
            LOGGER.debug("NetworkServer:onClientDisconnected found client in list, preparing to die [%d]", player_index);
            (*i)->wannadie = true;
            break;
        }
        ++i;
    }
    
    if ( player_index != INVALID_PLAYER_ID )
    {
        PlayerState * player = PlayerInterface::getPlayer(player_index);
        if ( player && sendalert )
        {
            ConsoleInterface::postMessage(Color::cyan, true, player->getFlag(),
                                      "'%s' has left.",
                                      player->getName().c_str());
        }
        
        ObjectiveInterface::disownPlayerObjectives( player_index );

        UnitInterface::destroyPlayerUnits( player_index );

        PlayerInterface::disconnectPlayerCleanup( player_index );    

        if ( sendalert )
        {
            SystemConnectAlert msg;
            if ( cleandisconnect )
            {
                msg.set( player_index, _connect_alert_mesg_disconnect);
            }
            else
            {
                msg.set( player_index, _connect_alert_mesg_client_drop );
            }

            SERVER->broadcastMessage(&msg, sizeof(msg));
        }
    }
    

//    LOGGER.warning("NetworkServer::onClientDisconected( %d, '%s')", s->getId(), msg);
}
//-----------------------------------------------------------------
void DedicatedGameManager::inputLoop()
{
    // handle server commands
    SDL_mutexP(commandqueue_mutex);
    while(!commandqueue.empty()) {
        const ServerCommand& command = commandqueue.front();

        switch(command.type) {
            case ServerCommand::QUIT:
            {
                ActionManager::runAction("quit");
                break;
            }
            case ServerCommand::CHAT:
            {
                ChatInterface::serversay(command.argument.c_str());
                break;
            }
            case ServerCommand::STATUS:
            {
                //*Console::server
                std::cout
                    << "Server " << *GameConfig::server_name
                    << " version " << PACKAGE_VERSION << " port "
                    << GameConfig::server_port << "\n"
                    << "Map: " << *GameConfig::game_map << "\n"
                    << std::setw(3) << "ID" << " "
                    << std::setw(30) << "Name" << " "
                    << std::setw(4) << "Kill" << " "
                    << std::setw(4) << "Lost" << " "
                    << std::setw(5) << "Score" << " "
                    << std::setw(21) << "IP\n";
                PlayerID i;
                for ( i = 0; i<PlayerInterface::getMaxPlayers(); ++i)
                {
                    PlayerState* playerstate = PlayerInterface::getPlayer(i);
                    if ( playerstate->isActive() )
                    {
                        //*Console::server
                        std::cout
                            << std::setw(3) << static_cast<int>(i) << " "
                            << std::setw(30) << playerstate->getName() << " "
                            << std::setw(4) << playerstate->getKills() << " "
                            << std::setw(4) << playerstate->getLosses() << " "
                            << std::setw(5) << playerstate->getTotal() << " "
                            << std::setw(21)
                            << SERVER->getIP(playerstate->getID())
                            << "\n";
                    }
                }
                //*Console::server << std::flush;
                std::cout << std::flush;
                break;
            }
            case ServerCommand::MAPCHANGE:
                if(!MapsManager::existsMap(command.argument)) {
                    std::cout << "map '" << command.argument
                        << "' doesn't exist." << std::endl;
                    break;
                }
            
                GameControlRulesDaemon::forceMapChange(command.argument);
                std::cout << "Preparing mapchange..." << std::endl;
                break;
            case ServerCommand::KICK:
                std::stringstream idstream(command.argument);
                PlayerID id = INVALID_PLAYER_ID;
                idstream >> (int&)id; // XXX KREMOVE
                if(id >= PlayerInterface::getMaxPlayers()) {
                    std::cout << "Unknown player." << std::endl;
                    break;
                }
                SERVER->kickClient(id);
                break;
        }
        commandqueue.pop();
    }
    SDL_mutexV(commandqueue_mutex);
    BaseGameManager::inputLoop();
}
void
NetworkServer::onClientDisconected(ClientSocket *s, const char * msg)
{
    LOGGER.debug("NetworkServer::onClientDisconected( %d, '%s')", s->getId(), msg ? msg : "nice");
    
    bool cleandisconnect = false;
    bool sendalert = true;
    
    if ( ServerConnectDaemon::inConnectQueue(s) )
    {
        // player was connecting and dropped.
        ServerConnectDaemon::removeClientFromQueue(s);
        sendalert = false;
        LOGGER.debug("NetworkServer::onClientDisconected player was connecting");
    }
    
    if ( NetworkInterface::receive_queue.isReady() )
    {
        unsigned long frontsave = NetworkInterface::receive_queue.front;
        while ( NetworkInterface::receive_queue.isReady() )
        {
            LOGGER.debug("NetworkServer::onClientDisconected there was a packet");
            NetPacket packet;

            NetworkInterface::receive_queue.dequeue(&packet);

            if ( packet.fromClient == s )
            {
                LOGGER.debug("NetworkServer::onClientDisconected the packet was from our friend");
                const NetMessage * netmessage = packet.getNetMessage();
                if (   netmessage->message_class == _net_message_class_connect 
                    && netmessage->message_id == _net_message_id_connect_netPanzer_client_disconnect )
                {
                    LOGGER.debug("NetworkServer::onClientDisconected so is a clean disconnect");
                    cleandisconnect = true;
                }
            }
        }
        NetworkInterface::receive_queue.front = frontsave;
    }
    
    PlayerID player_index = s->getPlayerIndex();
    
    if ( player_index != INVALID_PLAYER_ID )
    {
        PlayerState * player = PlayerInterface::getPlayer(player_index);
        bool kicked = player ? player->isKicked() : false;

        if ( player && sendalert && ! kicked)
        {
            if (GameConfig::game_teammode == true)
            ConsoleInterface::postMessage(Color::cyan, true, player->getFlag(),
                                      _("'%s' has left."),
                                      player->getName().c_str());
        }

        ObjectiveInterface::disownPlayerObjectives( player_index );
        UnitInterface::destroyPlayerUnits( player_index );
        PlayerInterface::disconnectPlayerCleanup( player_index );    

        if ( sendalert )
        {
            SystemConnectAlert scmsg;
            if ( cleandisconnect || ! msg )
            {
                scmsg.set( player_index, _connect_alert_mesg_disconnect);
            }
            else if ( kicked )
            {
                scmsg.set( player_index, _connect_alert_mesg_client_kicked);
            }
            else
            {
                scmsg.set( player_index, _connect_alert_mesg_client_drop );
            }
            SERVER->broadcastMessage(&scmsg, sizeof(scmsg));
        }
        if (GameConfig::game_teammode == true)
        {
            TeamManager::removePlayer(player->getID(), player->getTeamID());
            TeamManager::BalancedTeam();
        }
    }
}