miduo_client/Assets/ManagedResources/~Lua/Modules/Player/PrivilegeManager.lua

391 lines
13 KiB
Lua
Raw Normal View History

2020-05-09 13:31:21 +08:00
PrivilegeManager = {}
local this = PrivilegeManager
function this.Initialize()
this._PrivilegeInfoList = {}
this._PrivilegeTypeList = {}
end
-- 初始化数据
function PrivilegeManager.InitPrivilegeData(data)
this._PrivilegeInfoList = {}
for i = 1, #data do
this._PrivilegeInfoList[data[i].id] = { id = data[i].id, usedTimes = data[i].usedTimes, endTime = data[i].effectTime }
-- Log(
-- string.format("--- 特权数据 id = %s, 已使用次数 = %s, 结束时间 = %s",
-- data[i].id, data[i].usedTimes, data[i].effectTime))
end
this._PrivilegeTypeList = {}
local configList = ConfigManager.GetConfig(ConfigName.PrivilegeTypeConfig)
for _, config in ConfigPairs(configList) do
if not this._PrivilegeTypeList[config.PrivilegeType] then
this._PrivilegeTypeList[config.PrivilegeType] = {}
end
table.insert(this._PrivilegeTypeList[config.PrivilegeType], config)
end
end
function PrivilegeManager.FiveAMRefreshLocalData(privilegeList)
this.InitPrivilegeData(privilegeList)
end
function PrivilegeManager.OnPrivilegeUpdate(data)
for i = 1, #data do
this._PrivilegeInfoList[data[i].id] = { id = data[i].id, usedTimes = data[i].usedTimes, endTime = data[i].effectTime }
Log(
string.format("--- 特权数据刷新 id = %s, 已使用次数 = %s, 结束时间 = %s",
data[i].id, data[i].usedTimes, data[i].effectTime))
-- 发送特权解锁事件
Game.GlobalEvent:DispatchEvent(GameEvent.Privilege.OnPrivilegeUpdate, data[i].id)
end
end
-- 获取服务器数据
function PrivilegeManager.GetSerData(privilegeId)
local data = this._PrivilegeInfoList[privilegeId]
return data
end
-- 用于比较副本id的大小
local function _CarbonIdCompare(cId1, cId2)
local len1 = string.len(cId1)
local diff1 = tonumber(string.sub(cId1, len1, len1))
local id1 = tonumber(string.sub(cId1, 1, len1-1))
local len2 = string.len(cId2)
local diff2 = tonumber(string.sub(cId1, len2, len2))
local id2 = tonumber(string.sub(cId1, 1, len2-1))
if diff1 == diff2 then
return id1 - id2
end
return diff1 - diff2
end
-- 判断 cdv2 是否 在cdv1条件下可以解锁
--- type 解锁类型
--- condition 解锁条件
--- value 当前值
local function _CompareCondition(type, condition, value)
assert(type, "特权条件比较类型为空")
assert(condition, "特权条件比较类型 条件一 为空")
if type == 1 then -- 玩家等级解锁
value = value or PlayerManager.level
return value >= condition
elseif type == 2 then -- 关卡解锁
if value then
return _CarbonIdCompare(value, condition) >= 0
else
return FightPointPassManager.GetFightStateById(condition) == FIGHT_POINT_STATE.PASS
end
elseif type == 3 then -- vip等级解锁
value = value or VipManager.GetVipLevel()
return value >= condition
elseif type == 4 then -- 特殊类型
return true
end
end
--获取特权次数或者收益相关值(纯前端读表)
--- value 自定义比较值,为空时使用玩家自己的默认值进行比较,
function PrivilegeManager.GetPrivilegeNumberById(privilegeId, value)
local privilegeData = ConfigManager.GetConfigData(ConfigName.PrivilegeTypeConfig, privilegeId)
local unlockType = privilegeData.UnlockType
-- 特殊类型特殊处理
if unlockType == 4 then
-- 4类型没有服务器数据不解锁
local serData = this.GetSerData(privilegeId)
if not serData then return 0 end
-- 判断是否有持续时间
-- 有结束时间,但不在时间范围内
if serData.endTime ~= 0 and serData.endTime < GetTimeStamp() then return 0 end
end
-- 没有条件直接 直接永远存在
if not privilegeData.Condition then return -1 end
-- 判断解锁条件
local tValue = 0
for i = 1, #privilegeData.Condition do
local condition = privilegeData.Condition[i]
-- 没有条件了
if not condition then break end
-- 按照类型比较解锁条件
if not _CompareCondition(unlockType, condition[1], value) then break end
-- 保存当前值
tValue = condition[2]
end
-- 根据类型返回相应得数值
if privilegeData.IfFloat == 1 then
return tValue
elseif privilegeData.IfFloat == 2 then
return tValue / 10000
else
return tValue / 100
end
end
--获取特权次数或者收益相关值(纯前端读表)
--- value 自定义比较值,为空时使用玩家自己的默认值进行比较,
function PrivilegeManager.GetPrivilegeNumber(privilegeType, conValue)
local privilegeList = this._PrivilegeTypeList[privilegeType]
if not privilegeList then return 0 end
local value = 0
for _, config in ipairs(privilegeList) do
local privilegeId = config.Id
local v = this.GetPrivilegeNumberById(privilegeId, conValue)
-- -1表示一直存在无限次数
if v < 0 then return v end
-- 所有值相同类型值相加
value = value + v
end
return value
end
--获取某权益已用次数
function PrivilegeManager.GetPrivilegeUsedTimes(privilegeType)
local privilegeList = this._PrivilegeTypeList[privilegeType]
if not privilegeList then return 0 end
local usedTimes = 0
for _, config in ipairs(privilegeList) do
local privilegeId = config.Id
local serData = this._PrivilegeInfoList[privilegeId]
if serData then
usedTimes = usedTimes + serData.usedTimes
end
end
return usedTimes
end
--获取特权次数相关剩余值(前后端数值计算)
function PrivilegeManager.GetPrivilegeRemainValue(privilegeType)
local originalValue = this.GetPrivilegeNumber(privilegeType)
local usedTimes = this.GetPrivilegeUsedTimes(privilegeType)
local remainNum = originalValue - usedTimes
return remainNum
end
--刷新已用次数
function PrivilegeManager.RefreshPrivilegeUsedTimes(privilegeType, times)
local privilegeList = this._PrivilegeTypeList[privilegeType]
if not privilegeList then return end
for _, config in ipairs(privilegeList) do
local privilegeId = config.Id
local serData = this._PrivilegeInfoList[privilegeId]
if serData then
local maxTimes = this.GetPrivilegeNumberById(privilegeId)
local curTimes = serData.usedTimes
local finalTimes = curTimes + times
times = finalTimes - maxTimes
if times <= 0 then
this._PrivilegeInfoList[privilegeId].usedTimes = finalTimes
break
else
this._PrivilegeInfoList[privilegeId].usedTimes = maxTimes
end
end
end
end
--获取某权益是否解锁的状态
function PrivilegeManager.GetPrivilegeOpenStatus(privilegeType, value)
-- 判断条件是否符合
local num = this.GetPrivilegeNumber(privilegeType, value)
return num ~= 0
end
--获取某权益是否解锁的状态
function PrivilegeManager.GetPrivilegeOpenStatusById(privilegeId, value)
-- 判断条件是否符合
local num = this.GetPrivilegeNumberById(privilegeId, value)
return num ~= 0
end
--获取谋权益几级解锁
function PrivilegeManager.GetPrivilegeOpenTip(privilegeType)
local privilegeList = this._PrivilegeTypeList[privilegeType]
if not privilegeList or #privilegeList == 0 then
return "未找到的特权类型"..tostring(privilegeType)
end
--当存在多解锁类型时
if #privilegeList>1 then
local infoList={}
--解锁2倍速的处理
for i,v in ipairs(privilegeList) do
if (v.UnlockType==2 or v.UnlockType==3) and privilegeType==PRIVILEGE_TYPE.DoubleTimesFight then
for _, con in ipairs(v.Condition) do
if con[2] > 0 then
table.insert(infoList, con[1])
end
end
end
end
if privilegeType==PRIVILEGE_TYPE.DoubleTimesFight then
local fightConfig = ConfigManager.GetConfigData(ConfigName.MainLevelConfig, infoList[1])
return string.format("通关 %s 关卡解锁或玩家VIP等级达到 %s 级解锁",fightConfig.Name,infoList[2])
end
end
local privilegeData = privilegeList[1]
-- 没有条件直接返回0
if not privilegeData.Condition then
return "无解锁条件"
end
if privilegeData.Type ~= 1 then
return "非功能解锁特权"
end
local condition = privilegeData.Condition
local v = nil
for _, con in ipairs(condition) do
if con[2] > 0 then
v = con[1]
end
end
if not v then
return "未找到解锁条件"
end
if privilegeData.UnlockType == 1 then
return string.format("玩家等级达到 %s 级解锁", v)
elseif privilegeData.UnlockType == 2 then
local fightConfig = ConfigManager.GetConfigData(ConfigName.MainLevelConfig, v)
return string.format("通关 %s 关卡解锁", fightConfig.Name)
elseif privilegeData.UnlockType == 3 then
return string.format("玩家VIP等级达到 %s 级解锁", v)
end
end
--功能解锁是否在当前等级
--function PrivilegeManager.IsPrivilegeOpenedCurrentLevel(privilegeId, level)
function PrivilegeManager.IsPrivilegeOpenedCurrentValue(privilegeId, value)
local privilegeData = ConfigManager.GetConfigData(ConfigName.PrivilegeTypeConfig, privilegeId)
assert(privilegeData, string.format("ConfigName.PrivilegeTypeConfig not find Id%s", privilegeId))
if not privilegeData.Condition then
return false
end
if privilegeData.Condition[1][1] == value then
return
end
end
-- 通过vip等级获取相应等级会解锁的特权信息
-- return {
-- [1] = {
-- content = "",
-- value = 0 }
-- [2] = ...
--}
function PrivilegeManager.GetTipsByVipLv(vipLv)
local list = {}
local configList = ConfigManager.GetAllConfigsDataByKey(ConfigName.PrivilegeTypeConfig, "UnlockType", 3)
for _, config in ipairs(configList) do
if config.isShowName == 1 then
for _, condition in ipairs(config.Condition) do
if condition[1] == vipLv then
local tValue = condition[2]
-- 根据类型返回相应得数值
if config.IfFloat == 1 then
tValue = tValue
elseif config.IfFloat == 2 then
tValue = tValue / 10000
else
tValue = tValue / 100
end
-- 如果是功能解锁,且解锁
if config.Type == 1 and tValue ~= 0 then
tValue = ""
end
table.insert(list, {content = config.Name, value = tValue,id = config.Id})
break
end
end
end
end
return list
end
-- 获取特权剩余时间(非计时特权返回0)
function PrivilegeManager.GetPrivilegeLeftTime(privilegeType)
local isActive = this.GetPrivilegeOpenStatus(privilegeType)
if isActive then
local privilegeList = this._PrivilegeTypeList[privilegeType]
if not privilegeList or #privilegeList == 0 then
return 0
end
for _, pId in ipairs(privilegeList) do
local serData = this.GetSerData(pId)
if serData and serData > 0 then
local leftTime = serData.endTime - GetTimeStamp()
if leftTime > 0 then
return leftTime
end
end
end
end
return 0
end
-- 获取特权剩余时间(非计时特权返回0)
function PrivilegeManager.GetPrivilegeLeftTimeById(privilegeId)
local serData = this.GetSerData(privilegeId)
if serData and serData.endTime > 0 then
local leftTime = serData.endTime - GetTimeStamp()
if leftTime > 0 then
return leftTime
end
end
return 0
end
------- 部分特殊特权处理 -----------------------
-- 检测是否是特殊特权
--function PrivilegeManager.CheckIsSpecialPrivilege(privilegeId)
-- for _, spId in pairs(SPECIAL_PRIVILEGE) do
-- if spId == privilegeId then
-- return true
-- end
-- end
-- Log("特权id == " .. privilegeId .. ", 不是特殊特权,请检查!")
-- return false
--end
-- 开放某些特权
--function PrivilegeManager.OpenSpecialPrivilege(privilegeId)
-- -- 检测是否是特殊特权
-- if not this.CheckIsSpecialPrivilege(privilegeId) then
-- return
-- end
-- -- 检测特权是否存在
-- local serData = this._PrivilegeInfoList[privilegeId]
-- if serData then return end
-- this._PrivilegeInfoList[privilegeId] = {
-- id = privilegeId,
-- usedTimes = 0,
-- endTime = 0
-- }
--end
-- 获取特权状态(true 开启false 未开启)
--function PrivilegeManager.GetSpecialPrivilegeStatus(privilegeId)
-- -- 检测是否是特殊特权
-- if not this.CheckIsSpecialPrivilege(privilegeId) then
-- return false
-- end
-- --
-- local serData = this._PrivilegeInfoList[privilegeId]
-- if serData then
-- return true
-- end
-- return false
--end
return this