/** This is called from user_config when reading the config file
*/
void UnlockManager::load()
{
    const std::string filename=file_manager->getChallengeFile("challenges.xml");
    XMLNode* root = file_manager->createXMLTree(filename);
    if(!root || root->getName() != "challenges")
    {
        std::cerr << "Challenge file '" << filename << "' will be created." 
                  << std::endl;
        
        createSlotsIfNeeded();
        save();
        
        if (root) delete root;
        return;
    }
    
    std::vector<XMLNode*> xml_game_slots;
    root->getNodes("gameslot", xml_game_slots);
    for (unsigned int n=0; n<xml_game_slots.size(); n++)
    {
        std::string player_id;
        if (!xml_game_slots[n]->get("playerID", &player_id))
        {
            fprintf(stderr, "[UnlockManager] WARNING: Found game slot without a player ID attached. Discarding it\n");
            continue;
        }
        
        GameSlot* slot = new GameSlot(player_id);
        
        std::string kart_id;
        xml_game_slots[n]->get("kart", &kart_id);
        slot->setKartIdent(kart_id);
        
        m_game_slots[player_id] = slot;
        
        bool first_time = true;
        xml_game_slots[n]->get("firstTime", &first_time);
        slot->setFirstTime(first_time);
        
        for(AllChallengesType::iterator i = m_all_challenges.begin(); 
            i!=m_all_challenges.end();  i++)
        {
            ChallengeData* curr = i->second;
            Challenge* state = new Challenge(curr);
            
            slot->m_challenges_state[curr->getId()] = state;
            state->load(xml_game_slots[n]);
        }
        slot->computeActive();
    }
    
    bool something_changed = createSlotsIfNeeded();
    if (something_changed) save();
    
    delete root;
}   // load
/** Creates a gameslot for players that don't have one yet
 *  \return true if any were created
 */
bool UnlockManager::createSlotsIfNeeded()
{
    bool something_changed = false;
    
    // make sure all players have at least one game slot associated
    PtrVector<PlayerProfile>& players = UserConfigParams::m_all_players;
    for (int n=0; n<players.size(); n++)
    {
        bool exists = false;
        
        std::map<std::string, GameSlot*>::iterator it;
        for (it = m_game_slots.begin(); it != m_game_slots.end(); it++)
        {
            GameSlot* curr_slot = it->second;
            if (curr_slot->getPlayerID() == players[n].getUniqueID())
            {
                exists = true;
                break;
            }
        }
        
        if (!exists)
        {
            GameSlot* slot = new GameSlot(players[n].getUniqueID());
            for(AllChallengesType::iterator i = m_all_challenges.begin(); 
                i!=m_all_challenges.end();  i++)
            {
                ChallengeData* cd = i->second;
                slot->m_challenges_state[cd->getId()] = new Challenge(cd);
            }
            slot->computeActive();
            
            m_game_slots[players[n].getUniqueID()] = slot;
            
            something_changed = true;
        }
    }
    
    return something_changed;
} // UnlockManager::createSlotsIfNeeded
Exemplo n.º 3
0
 /** Returns all active challenges. */
 void computeActive() { m_game_slot->computeActive(); }