187 lines
8.0 KiB
Lua
187 lines
8.0 KiB
Lua
local skynet = require "skynet"
|
||
local oo = require "Class"
|
||
local pb = require "pb"
|
||
local log = require "Log"
|
||
local json =require "json"
|
||
local errorInfo = require "ErrorInfo"
|
||
local dataType = require "DataType"
|
||
local redisKeyUrl = require "RedisKeyUrl"
|
||
local group = require "Group"
|
||
local GroupRank = oo.class(group)
|
||
|
||
--家园显示排行榜
|
||
function GroupRank:ShowRank( player , c2sData , s2cData )
|
||
c2sData.data = assert(pb.decode("C2SGroupShowRank", c2sData.data ))
|
||
local data = {}
|
||
local myPartnerId = player.gameData.partner.id
|
||
local groupId = self:GetGroupID( player )
|
||
local redisKey = string.format( redisKeyUrl.GameServerGroupContributionScoreZSet, groupId )
|
||
|
||
data.ranks = {}
|
||
data.isHaveReward = false
|
||
--获取排行榜数据
|
||
local rankList = skynet.server.redis:zrange( redisKey , 0 , -1 , "withscores")
|
||
rankList = redisKeyUrl:CovertTable( rankList )
|
||
rankList = self:SortRank( rankList )
|
||
|
||
local curRankId = 0
|
||
local rankInfo = {}
|
||
local prevScore = 0 --前一个人
|
||
local cfgSValue = skynet.server.gameConfig:GetPlayerAllCfg( player , "SValue")
|
||
|
||
for k , v in pairs( rankList ) do
|
||
--只有达到了周贡献进排行榜标准值才能进行排名统计
|
||
local isRank = false
|
||
if v.score >= cfgSValue.communityRankValue then
|
||
isRank = true
|
||
if 0 == prevScore or v.score ~= prevScore then
|
||
curRankId = curRankId + 1
|
||
prevScore = v.score
|
||
end
|
||
end
|
||
|
||
local curDetail = skynet.server.personal:GetDetail( v.partnerId )
|
||
local rankInfo = {}
|
||
if not isRank then
|
||
rankInfo.rankId = 0
|
||
else
|
||
rankInfo.rankId = curRankId
|
||
end
|
||
|
||
rankInfo.partnerId = v.partnerId
|
||
rankInfo.nickName = curDetail.nickName
|
||
rankInfo.headId = curDetail.headId
|
||
--自定义头像
|
||
if rankInfo.headId == 0 and curDetail.DIYHeadInfo ~=nil then
|
||
rankInfo.DIYHeadInfo = curDetail.DIYHeadInfo
|
||
end
|
||
rankInfo.headFrameID = curDetail.headFrameId
|
||
rankInfo.contributionScore = v.score
|
||
table.insert( data.ranks , rankInfo )
|
||
|
||
--如果自己排名在1~12并且未领取奖励
|
||
if v.partnerId == myPartnerId and curDetail.groupRandId >= 1 and curDetail.groupRandId <= 12 then
|
||
data.isHaveReward = true
|
||
end
|
||
end
|
||
|
||
self:CheckRankStatus( player , groupId )
|
||
|
||
s2cData.cmd = pb.enum("MsgType","CMD_S2C_GroupShowRank")
|
||
s2cData.data = assert(pb.encode("S2CGroupShowRank", data))
|
||
end
|
||
|
||
--家园排行榜奖励领取
|
||
function GroupRank:GetReward( player , c2sData , s2cData )
|
||
c2sData.data = assert(pb.decode("C2SGroupRankGetReward", c2sData.data ))
|
||
local data = {}
|
||
|
||
local myPartnerId = player.gameData.partner.id
|
||
local curDetail = skynet.server.personal:GetDetail( myPartnerId )
|
||
local groupId = curDetail.groupId
|
||
|
||
if not self:IsVaildParam( "groupId" , groupId ) then
|
||
s2cData.code = errorInfo.ErrorCode.NoGroupMember
|
||
else
|
||
local rankId = curDetail.groupRandId
|
||
|
||
if not rankId or rankId <=0 or rankId > 12 then
|
||
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
|
||
else
|
||
--开始根据排行榜领取奖励
|
||
local cfgSValue = skynet.server.gameConfig:GetPlayerAllCfg( player , "SValue")
|
||
local rankRewardIds = cfgSValue.communityRankReward
|
||
local curRewardId = 0
|
||
if 1 == rankId then
|
||
curRewardId = rankRewardIds[1]
|
||
elseif 2 == rankId then
|
||
curRewardId = rankRewardIds[2]
|
||
elseif 3 == rankId then
|
||
curRewardId = rankRewardIds[3]
|
||
elseif rankId >= 4 and rankId <= 6 then
|
||
curRewardId = rankRewardIds[4]
|
||
elseif rankId >= 7 and rankId <= 12 then
|
||
curRewardId = rankRewardIds[5]
|
||
end
|
||
|
||
--发放奖励
|
||
local eventId = pb.enum("EnumMoneyChangeEventID","EventID_73")
|
||
player:GiveReward( curRewardId , eventId)
|
||
skynet.server.personal:SetDetail( myPartnerId , "groupRandId" , 0 )
|
||
|
||
skynet.server.msgTips:ReduceAll( player , 39 )
|
||
data.rewardId = curRewardId
|
||
end
|
||
end
|
||
|
||
s2cData.cmd = pb.enum("MsgType","CMD_S2C_GroupRankGetReward")
|
||
s2cData.data = assert(pb.encode("S2CGroupRankGetReward", data))
|
||
end
|
||
|
||
--家园检查排行榜状态
|
||
function GroupRank:CheckRankStatus( player , groupId )
|
||
local redisKey = string.format( redisKeyUrl.GameServerGroupInfoHash , groupId )
|
||
local redisData = skynet.server.redis:hmget( redisKey , "rankStatus" , "nexRankStatusTime")
|
||
--当前时间大于状态改变时间
|
||
if skynet.GetTime() >= tonumber(redisData[2]) then
|
||
--清空贡献分数
|
||
local function ClearContributionScore()
|
||
redisKey = string.format( redisKeyUrl.GameServerGroupContributionScoreZSet, groupId )
|
||
local parentIds = skynet.server.redis:zrange( redisKey , 0 , -1 )
|
||
for k, parentId in pairs(parentIds) do
|
||
skynet.server.redis:zadd( redisKey , 0 , parentId )
|
||
end
|
||
end
|
||
|
||
if self.RankStatus_Acc == tonumber(redisData[1]) then --累加状态
|
||
--获取上一轮的贡献分数
|
||
local redisKey = string.format( redisKeyUrl.GameServerGroupContributionScoreZSet, groupId )
|
||
local rankList = skynet.server.redis:zrevrange( redisKey , 0 , -1 , "withscores")
|
||
rankList = redisKeyUrl:CovertTable( rankList )
|
||
rankList = self:SortRank( rankList )
|
||
|
||
--获取排行榜的奖励信息
|
||
local curRankId = 0
|
||
local rankInfo = {}
|
||
local prevScore = 0 --前一个人
|
||
local cfgSValue = skynet.server.gameConfig:GetPlayerAllCfg( player , "SValue")
|
||
for k, v in pairs( rankList ) do
|
||
--只有达到了周贡献进排行榜标准值才能进行排名统计
|
||
if v.score >= cfgSValue.communityRankValue then
|
||
--上一个分数为0或者上一个分数与当前分数不相等才进行排名累加
|
||
if 0 == prevScore or v.score ~= prevScore then
|
||
curRankId = curRankId + 1
|
||
prevScore = v.score
|
||
end
|
||
skynet.server.personal:SetDetail( v.partnerId , "groupRandId" , curRankId )
|
||
if curRankId >= 1 and curRankId <= 12 then
|
||
skynet.server.partner:SendMsgTips( v.partnerId , 39 , true )
|
||
end
|
||
end
|
||
end
|
||
elseif self.RankStatus_Calc == tonumber(redisData[1]) then --结算状态
|
||
ClearContributionScore()
|
||
end
|
||
|
||
--设置下一轮的状态和时间
|
||
local nextRankStatus , nexRankStatusTime = self:CalcRankStatusAndTime() --计算排行榜状态和时间
|
||
--如果当前是累加状态,并且下一个状态还是累加,说明该群未在周日20点后触发状态切换,这里就直接清空贡献分数
|
||
if self.RankStatus_Acc == tonumber(redisData[1]) and self.RankStatus_Acc == nextRankStatus then
|
||
ClearContributionScore()
|
||
end
|
||
redisKey = string.format( redisKeyUrl.GameServerGroupInfoHash , groupId )
|
||
skynet.server.redis:hmset( redisKey , "rankStatus", nextRankStatus , "nexRankStatusTime", nexRankStatusTime)
|
||
local myPartnerId = player.gameData.partner.id
|
||
skynet.server.gameRecord:Add( player.userId , dataType.GameRecordType_109 , groupId , myPartnerId , nextRankStatus , nexRankStatusTime )
|
||
end
|
||
end
|
||
|
||
--家园获取排行榜状态
|
||
function GroupRank:GetRankStatus( player )
|
||
local groupId = self:GetGroupID( player )
|
||
local redisKey = string.format( redisKeyUrl.GameServerGroupInfoHash , groupId )
|
||
return tonumber(skynet.server.redis:hget( redisKey , "rankStatus"))
|
||
end
|
||
|
||
skynet.server.groupRank = GroupRank
|
||
return GroupRank |