82 lines
1.9 KiB
Lua
82 lines
1.9 KiB
Lua
local ServerHeroData = class("ServerHeroData", ServerBaseData)
|
|
|
|
function ServerHeroData:init(data)
|
|
self.data.heroes = data and data.heroes or {}
|
|
self.data.unlockHeroes = data and data.unlockHeroes or {}
|
|
end
|
|
|
|
function ServerHeroData:getHeroByCfgId(id)
|
|
local idStr = tostring(id)
|
|
return self.data.heroes[idStr]
|
|
end
|
|
|
|
function ServerHeroData:getIsUnlockHero(id)
|
|
local idStr = tostring(id)
|
|
if self.data.heroes[idStr] then
|
|
return true
|
|
end
|
|
return self.data.unlockHeroes[idStr]
|
|
end
|
|
|
|
function ServerHeroData:addHero(id, lv)
|
|
local idStr = tostring(id)
|
|
if self.data.heroes[idStr] then
|
|
return
|
|
end
|
|
self.data.heroes[idStr] = {
|
|
cfg_id = id,
|
|
lv = lv,
|
|
}
|
|
end
|
|
|
|
function ServerHeroData:unlockHero(id)
|
|
local idStr = tostring(id)
|
|
if self.data.unlockHeroes[idStr] then
|
|
return
|
|
end
|
|
self.data.unlockHeroes[idStr] = true
|
|
end
|
|
|
|
function ServerHeroData:tryUnlockHeroByChapterId(maxChapterId)
|
|
if self.maxChapterId and self.maxChapterId >= maxChapterId then
|
|
return
|
|
end
|
|
self.maxChapterId = maxChapterId
|
|
if self.unlockHeroList == nil then
|
|
self.unlockHeroList = {}
|
|
local config = ConfigManager:getConfig("hero")
|
|
for k, v in pairs(config) do
|
|
if not self:getIsUnlockHero(k) then
|
|
if v.unlock_chapter then
|
|
table.insert(self.unlockHeroList, k)
|
|
end
|
|
end
|
|
end
|
|
table.sort(self.unlockHeroList, function(a, b)
|
|
return config[a].unlock_chapter > config[b].unlock_chapter
|
|
end)
|
|
self.unlockChapterList = {}
|
|
for k, v in ipairs(self.unlockHeroList) do
|
|
self.unlockChapterList[k] = config[v].unlock_chapter
|
|
end
|
|
end
|
|
for i = #self.unlockChapterList, 1, -1 do
|
|
if self.unlockChapterList[i] <= maxChapterId then
|
|
self:unlockHero(self.unlockHeroList[i])
|
|
table.remove(self.unlockChapterList, i)
|
|
table.remove(self.unlockHeroList, i)
|
|
else
|
|
break
|
|
end
|
|
end
|
|
end
|
|
|
|
function ServerHeroData:upgradeHero(heroId, lv)
|
|
local idStr = tostring(heroId)
|
|
if not self.data.heroes[idStr] then
|
|
self:addHero(heroId, 0)
|
|
end
|
|
self.data.heroes[idStr].lv = lv
|
|
end
|
|
|
|
return ServerHeroData |