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

430 lines
16 KiB
Lua
Raw Normal View History

2021-04-20 13:58:00 +08:00
PrivilegeManager = {}
2020-05-09 13:31:21 +08:00
local this = PrivilegeManager
function this.Initialize()
2020-06-28 17:48:49 +08:00
this._PrivilegeInfoList = {} --接收到的后端的特权列表
this._PrivilegeTypeList = {} --从前端特权表中读取的数据以表中列PrivilegeType为键
2020-05-09 13:31:21 +08:00
end
-- 初始化数据
function PrivilegeManager.InitPrivilegeData(data)
this._PrivilegeInfoList = {}
for i = 1, #data do
2020-06-28 17:48:49 +08:00
--[[data:从后端接受到的数据 data是个列表 列表元素的结构id与priviledge的id对应 usedTimes使用次数 endTime 有效时间]]
2020-05-09 13:31:21 +08:00
this._PrivilegeInfoList[data[i].id] = { id = data[i].id, usedTimes = data[i].usedTimes, endTime = data[i].effectTime }
2021-09-18 15:04:32 +08:00
-- LogGreen(string.format("特权ID = %s, 使用了的次数 = %s, 结束时间 = %s", data[i].id, data[i].usedTimes, data[i].effectTime))
2020-05-09 13:31:21 +08:00
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
2021-07-14 21:43:03 +08:00
Game.GlobalEvent:DispatchEvent(GameEvent.Privilege.OnPrivilegeZeroUpdate)
2020-05-09 13:31:21 +08:00
end
2021-09-18 15:04:32 +08:00
--刷新从后端接收的到的特权表--零点推送用
2020-05-09 13:31:21 +08:00
function PrivilegeManager.FiveAMRefreshLocalData(privilegeList)
this.InitPrivilegeData(privilegeList)
end
2021-09-18 15:04:32 +08:00
--刷新从后端接收的到的特权表--购买时用
2020-05-09 13:31:21 +08:00
function PrivilegeManager.OnPrivilegeUpdate(data)
--LogGreen("后端特权推送:")
2020-05-09 13:31:21 +08:00
for i = 1, #data do
this._PrivilegeInfoList[data[i].id] = { id = data[i].id, usedTimes = data[i].usedTimes, endTime = data[i].effectTime }
2021-09-18 15:04:32 +08:00
-- LogGreen(string.format("特权ID = %s, 使用了的次数 = %s, 结束时间 = %s", data[i].id, data[i].usedTimes, data[i].effectTime))
2020-05-09 13:31:21 +08:00
-- 发送特权解锁事件
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(cId2, len2, len2))
local id2 = tonumber(string.sub(cId2, 1, len2-1))
2020-05-09 13:31:21 +08:00
if diff1 == diff2 then
return id1 - id2
end
return diff1 - diff2
end
-- 判断 cdv2 是否 在cdv1条件下可以解锁
--- type 解锁类型
--- condition 解锁条件
--- value 当前值
local function _CompareCondition(type, condition, value)
2021-04-09 12:26:35 +08:00
assert(type, Language[11399])
assert(condition, Language[11400])
2020-05-09 13:31:21 +08:00
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 -- 充值解锁(无论多少金额)
2020-05-09 13:31:21 +08:00
return true
2020-06-18 20:39:29 +08:00
elseif type == 5 then -- 特殊类型
return true
elseif type == 6 then -- 总好感度等级解锁
local _, tlv, _ = LikabilityManager.GetTotalHeroLikeLv(-1)
value = value or tlv
return value >= condition
elseif type == 7 then -- 充值金额解锁
value = value or VipManager.GetChargedNum()
return value >= condition
2020-05-09 13:31:21 +08:00
end
end
--获取特权次数或者收益相关值(纯前端读表)
--- value 自定义比较值,为空时使用玩家自己的默认值进行比较,
function PrivilegeManager.GetPrivilegeNumberById(privilegeId, value)
local privilegeData = ConfigManager.GetConfigData(ConfigName.PrivilegeTypeConfig, privilegeId)
local unlockType = privilegeData.UnlockType
-- 特殊类型特殊处理
2020-06-18 20:39:29 +08:00
if unlockType == 4 or unlockType == 5 then
2020-05-09 13:31:21 +08:00
-- 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]
2020-05-09 13:31:21 +08:00
if serData then
usedTimes = usedTimes + serData.usedTimes
-- LogBlue(string.format("privilegeId:%s, usedTimes:%s",tostring(privilegeId),tostring(serData.usedTimes)))
end
2020-05-09 13:31:21 +08:00
end
return usedTimes
end
--获取特权次数相关剩余值(前后端数值计算)
function PrivilegeManager.GetPrivilegeRemainValue(privilegeType)
local originalValue = this.GetPrivilegeNumber(privilegeType)
local usedTimes = this.GetPrivilegeUsedTimes(privilegeType)
-- LogBlue(string.format("originalValue:%s, usedTimes:%s",tostring(originalValue),tostring(usedTimes)))
2020-05-09 13:31:21 +08:00
local remainNum = originalValue - usedTimes
-- 剩余次数不会小于0修正
if remainNum < 0 then
remainNum = 0
end
2020-05-09 13:31:21 +08:00
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.RefreshStarPrivilege(privilegeType)
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
this._PrivilegeInfoList[privilegeId].usedTimes = 0
end
end
end
--前端移除某特权
function PrivilegeManager.RemovePrivilege(privilegeType,_privilegeId)
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 and privilegeId == _privilegeId then
this._PrivilegeInfoList[privilegeId] = nil
end
end
end
2020-05-09 13:31:21 +08:00
--获取某权益是否解锁的状态
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
2021-04-09 12:26:35 +08:00
return Language[11401]..tostring(privilegeType)
2020-05-09 13:31:21 +08:00
end
--当存在多解锁类型时
2021-03-19 11:45:08 +08:00
-- if #privilegeList>1 then
-- local infoList={}
2020-05-09 13:31:21 +08:00
--解锁2倍速的处理
2021-03-19 11:45:08 +08:00
-- for i,v in ipairs(privilegeList) do
-- if (v.UnlockType==1) 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 lvOpenTimeScaleConFig = ConfigManager.TryGetConfigDataByDoubleKey(ConfigName.PrivilegeTypeConfig,"PrivilegeType",PRIVILEGE_TYPE.DoubleTimesFight,"UnlockType",1)
-- -- local fightConfig = ConfigManager.GetConfigData(ConfigName.MainLevelConfig, infoList[1])
-- -- return string.format("通关 %s 关卡解锁或玩家特权等级达到 %s 级解锁",GetLanguageStrById(fightConfig.Name),infoList[2])
-- return string.format(Language[11406],lvOpenTimeScaleConFig.Condition[1][1])
-- end
2021-03-19 11:45:08 +08:00
-- end
2020-05-09 13:31:21 +08:00
local privilegeData = privilegeList[1]
-- 没有条件直接返回0
if not privilegeData.Condition then
2021-04-09 12:26:35 +08:00
return Language[11403]
2020-05-09 13:31:21 +08:00
end
if privilegeData.Type ~= 1 then
2021-04-09 12:26:35 +08:00
return Language[11404]
2020-05-09 13:31:21 +08:00
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
2021-04-09 12:26:35 +08:00
return Language[11405]
2020-05-09 13:31:21 +08:00
end
if privilegeData.UnlockType == 1 then
2021-04-09 12:26:35 +08:00
return string.format(Language[11406], v)
2020-05-09 13:31:21 +08:00
elseif privilegeData.UnlockType == 2 then
local fightConfig = ConfigManager.GetConfigData(ConfigName.MainLevelConfig, v)
2021-04-09 12:26:35 +08:00
return string.format(Language[11407], GetLanguageStrById(fightConfig.Name))
2020-05-09 13:31:21 +08:00
elseif privilegeData.UnlockType == 3 then
2021-04-09 12:26:35 +08:00
return string.format(Language[11408], v)
2020-05-09 13:31:21 +08:00
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
2021-01-26 17:08:39 +08:00
table.insert(list, {content = GetLanguageStrById(config.Name), value = tValue,id = config.Id, IfFloat = config.IfFloat})
2020-05-09 13:31:21 +08:00
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
2020-06-23 18:36:24 +08:00
return this