ScoreInfo.lua 1.56 KB
cc.exports.ScoreInfo = {}

local _map = {};

--积分配置表信息
local function init()
    local configStr = cc.FileUtils:getInstance():getStringFromFile("res/config/gd_score.txt");

    local function readLineFun(lineStr)
        local info;
        local contents = StringUtil.split(lineStr,'\t');
        if #contents > 1 then
            info = {};
            info.level = tonumber(contents[1]);
            info.score = tonumber(contents[2]);
            info.name = contents[3];
        end
        return info;
    end

    local index = 1;
    local function readLine(str)
        local a,b = string.find(str,"\r\n");
        if a then
            local lineStr = string.sub(str,1,a-1);

            if index > 2 then
                local info = readLineFun(lineStr);
                if info then
                    _map[info.level] = info;
                end
            end

            index = index + 1;
            str = string.sub(str,b+1);
            readLine(str);
        else
            local info = readLineFun(str);
            if info then
                _map[info.level] = info;
            end
        end
    end
    readLine(configStr);
end
init();

--通过级别获取信息
function ScoreInfo.getScoreInfo(level)
    return _map[level];
end

--通过积分获取积分信息
function ScoreInfo.getInfoByScore(score)
    local info = nil;
    for i,v in ipairs(_map) do
        if score <= v.score then
            info = v;
            break;
        end
    end
    if not info then
        info = _map[#_map];
    end
    return info;
end

return ScoreInfo