Exemplo n.º 1
0
bool TournamentConfig::Load(const std::string &fileName)
{
    JsonValue json;

    bool ret = JsonReader::ParseFile(json, fileName);
    if (ret)
    {
        std::string value;
        if (json.GetValue("version", value))
        {
            if (value == TOURNAMENT_CONFIG_VERSION)
            {
                // Setup tournament configuration
                mOptions.turns.clear();
                JsonValue tournament = json.FindValue("tournament");
                if (tournament.GetArray().Size() > 0U)
                {
                    for (JsonArray::Iterator iter = tournament.GetArray().Begin(); iter != tournament.GetArray().End(); ++iter)
                    {
                        if (iter->IsObject())
                        {
                            JsonValue value = iter->GetObj().GetValue("type");
                            Tarot::Distribution shuffle;
                            if (value.IsString())
                            {
                                if (value.GetString() == "custom")
                                {
                                    shuffle.mType = Tarot::Distribution::CUSTOM_DEAL;
                                    value = iter->GetObj().GetValue("file");
                                    if (value.IsString())
                                    {
                                        shuffle.mFile = value.GetString();
                                    }
                                    else
                                    {
                                        ret = false;
                                    }
                                }
                                else if (value.GetString() == "random")
                                {
                                    shuffle.mType = Tarot::Distribution::RANDOM_DEAL;
                                }
                                else  if (value.GetString() == "numbered")
                                {
                                    shuffle.mType = Tarot::Distribution::NUMBERED_DEAL;
                                    shuffle.mSeed = iter->GetObj().GetValue("number").GetInteger();
                                    // FIXME we can add a test on the type here before setting the seed
                                }
                                else
                                {
                                    TLogError("Unsupported deal type value");
                                    ret = false;
                                }
                            }

                            if (ret)
                            {
                                mOptions.turns.push_back(shuffle);
                            }
                        }
                    }
                }
                else
                {
                    TLogError("No tournament details");
                    ret = false;
                }
            }
            else
            {
                TLogError("Wrong tournament configuration file version");
                ret = false;
            }
        }
        else
        {
            TLogError("Cannot read tournament configuration file version");
            ret = false;
        }
    }
    else
    {
        TLogError("Cannot open tournament configuration file" + fileName);
    }

    if (!ret)
    {
        // Overwrite old file with default value
        mOptions = GetDefault();
        ret = Save(fileName);
    }

    mLoaded = true;
    return ret;
}