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 GroupChat = oo.class(group) GroupChat.MsgType_Text = 1 --文本消息 GroupChat.MsgType_Need = 2 --需求 --家园聊天记录(每次发送最近的50条) function GroupChat:HistoryMsg( player , c2sData , s2cData ) c2sData.data = assert(pb.decode("C2SGroupHistoryMsg", c2sData.data )) local data = {} local earliestMsgId = c2sData.data.earliestMsgId if not earliestMsgId or earliestMsgId < 0 then s2cData.code = errorInfo.ErrorCode.ErrRequestParam else data.msgs = {} local myPartnerId = player.gameData.partner.id local curDetail = skynet.server.personal:GetDetail( myPartnerId ) local groupId = curDetail.groupId local redisKey = string.format( redisKeyUrl.GameServerGroupMsgRecordZSet , groupId ) local msgList = nil if 0 == earliestMsgId then --传0就发送最近指定数目的消息 msgList = skynet.server.redis:zrevrange( redisKey , 0 , self.MaxMsgRecordToClient - 1 ) else local max = earliestMsgId - 1 local min = earliestMsgId - self.MaxMsgRecordToClient msgList = skynet.server.redis:zrevrangebyscore( redisKey , max < 0 and 0 or max , min < 0 and 0 or min ) end local tmpData = nil for k, v in pairs( msgList ) do tmpData = json:decode(v) table.insert( data.msgs , tmpData) end skynet.server.msgTips:ReduceAll(player , 66) end s2cData.cmd = pb.enum("MsgType","CMD_S2C_GroupHistoryMsg") s2cData.data = assert(pb.encode("S2CGroupHistoryMsg", data)) end --家园发送消息 function GroupChat:SendMsg( player , c2sData , s2cData ) c2sData.data = assert(pb.decode("C2SGroupSendMsg", c2sData.data )) local data = {} local msgType = c2sData.data.msgType if not msgType then s2cData.code = errorInfo.ErrorCode.ErrRequestParam else local myPartnerId = player.gameData.partner.id local curDetail = skynet.server.personal:GetDetail( myPartnerId ) local groupId = curDetail.groupId local redisKey = string.format( redisKeyUrl.GameServerGroupMsgRecordZSet , groupId ) local isNoticeOther = true --是否通知其它人 if self.MsgType_Text == msgType then local msgText = c2sData.data.msgText if not self:IsVaildParam("msgText" , msgText ) then s2cData.code = errorInfo.ErrorCode.ErrRequestParam elseif skynet.server.common:IsMaskWords( msgText ) then s2cData.code = errorInfo.ErrorCode.ExistMaskWords else local lastMsgId,msgKey = self:GetLastMsgID( groupId ) local msgData = {} msgData.msgId = lastMsgId msgData.sendPartnerId = myPartnerId msgData.sendNickName = curDetail.nickName msgData.updateTime = skynet.GetTime() msgData.msgType = msgType msgData.msgText = msgText msgData.needGoods = {} msgData.headID = curDetail.headId --自定义头像 if msgData.headID == 0 and curDetail.DIYHeadInfo ~= nil then msgData.DIYHeadInfo = curDetail.DIYHeadInfo end msgData.headFrameID = curDetail.headFrameId local redisBanChatKey = string.format( redisKeyUrl.ChatManageBanChatTimeString, myPartnerId ) local unbanChatTime = tonumber(skynet.server.redis:get( redisBanChatKey )) or 0 --获取玩家解禁的时间 if 0 == unbanChatTime then msgData.isShowMsg = true else msgData.isShowMsg = false isNoticeOther = false --禁言中,不通知其它人 end skynet.server.redis:zadd( redisKey , lastMsgId , json:encode(msgData)) data.msgs = msgData end elseif self.MsgType_Need == msgType then --获取最大的帮助次数 local cfgSValue = skynet.server.gameConfig:GetPlayerAllCfg( player , "SValue") local helpInfo = cfgSValue.communityMaterialHelp local maxHelpCount = helpInfo[2] local goodsInfo = c2sData.data.needGoods if not goodsInfo then s2cData.code = errorInfo.ErrorCode.ErrRequestParam else local helpIndex = nil if 8 == goodsInfo.goodsId then helpIndex = 1 elseif 9 == goodsInfo.goodsId then helpIndex = 2 elseif 10 == goodsInfo.goodsId then helpIndex = 3 end local groupNeedHelpCount = player.gameData.todayGain.groupNeedHelpCount[ helpIndex ] if groupNeedHelpCount >= maxHelpCount then s2cData.code = errorInfo.ErrorCode.TodayMaxLimit else local todayGain = player.gameData.todayGain todayGain.groupNeedHelpCount[ helpIndex ] = todayGain.groupNeedHelpCount[ helpIndex ] + 1 --先扣除商品(这里可能需要做个商品限制,指定商品才行) if dataType.GoodsType_Prop == goodsInfo.goodsType and goodsInfo.goodsId >= 8 and goodsInfo.goodsId <= 10 then --组装新的需要物品 local needGoods = {} needGoods.status = dataType.GiftStatus_NoGain needGoods.helperNickName = "" --请求数量 local helpInfo = cfgSValue.communityMaterialHelp goodsInfo.goodsCount = helpInfo[1] needGoods.info = goodsInfo local lastMsgId,msgKey = self:GetLastMsgID( groupId ) local msgData = {} msgData.msgId = lastMsgId msgData.sendPartnerId = myPartnerId msgData.sendNickName = curDetail.nickName msgData.updateTime = skynet.GetTime() msgData.msgType = msgType msgData.msgText = "" msgData.needGoods = needGoods msgData.headID = curDetail.headId --自定义头像 if msgData.headID == 0 and curDetail.DIYHeadInfo ~= nil then msgData.DIYHeadInfo = curDetail.DIYHeadInfo end msgData.headFrameID = curDetail.headFrameId msgData.isShowMsg = true skynet.server.redis:zadd( redisKey , lastMsgId , json:encode(msgData)) data.msgs = msgData else s2cData.code = errorInfo.ErrorCode.ErrRequestParam end end end end --向每个在线玩家发送红点 if isNoticeOther then redisKey = string.format( redisKeyUrl.GameServerGroupContributionScoreZSet, groupId ) local rankList = skynet.server.redis:zrange( redisKey , 0 , -1 ) for k, v in pairs(rankList) do if myPartnerId ~= v then self:SendMsgToFriend( myPartnerId , v , data.msgs ) skynet.server.partner:SendMsgTips( v , 66 , true ) end end end end s2cData.cmd = pb.enum("MsgType","CMD_S2C_GroupSendMsg") s2cData.data = assert(pb.encode("S2CGroupSendMsg", data)) end --家园给予别人商品 function GroupChat:GiveGoods( player , c2sData , s2cData ) c2sData.data = assert(pb.decode("C2SGroupGiveGoods", c2sData.data )) local data = {} local msgId = c2sData.data.msgId if not msgId then s2cData.code = errorInfo.ErrorCode.ErrRequestParam else local myPartnerId = player.gameData.partner.id local curDetail = skynet.server.personal:GetDetail( myPartnerId ) local groupId = curDetail.groupId local redisKey = string.format( redisKeyUrl.GameServerGroupMsgRecordZSet , groupId ) local msgData = skynet.server.redis:zrangebyscore( redisKey , msgId , msgId) if nil == msgData or nil == msgData[1] then s2cData.code = errorInfo.ErrorCode.OtherUserAlreadyHelp else msgData = json:decode( msgData[1] ) --自己今日帮助次数 local goodsInfo = msgData.needGoods.info if msgData.sendPartnerId == myPartnerId or dataType.GiftStatus_NoGain ~= msgData.needGoods.status then s2cData.code = errorInfo.ErrorCode.ErrRequestParam else if not skynet.server.bag:RemoveGoods( player , goodsInfo.goodsType , goodsInfo.goodsId , goodsInfo.goodsCount ) then s2cData.code = errorInfo.ErrorCode.NoEnoughGoods else --发货 msgData.needGoods.status = dataType.GiftStatus_AlreadyGain msgData.needGoods.helperNickName = curDetail.nickName skynet.server.redis:zremrangebyscore( redisKey , msgId , msgId) skynet.server.redis:zadd( redisKey , msgId , json:encode(msgData)) --给该玩家赠送物资 local otherDetail = skynet.server.personal:GetDetail( msgData.sendPartnerId ) local needGoodsList = json:decode(otherDetail.groupNeedGoods) table.insert( needGoodsList , msgData.needGoods.info ) skynet.server.personal:SetDetail( msgData.sendPartnerId , "groupNeedGoods" , json:encode( needGoodsList )) --增加排行榜的贡献值 local cfgSValue = skynet.server.gameConfig:GetPlayerAllCfg( player , "SValue") local helpCount = cfgSValue.communityHelpReward local curRankStatus = skynet.server.groupRank:GetRankStatus( player ) if self.RankStatus_Acc == curRankStatus then local redisKey = string.format( redisKeyUrl.GameServerGroupContributionScoreZSet, groupId ) skynet.server.redis:zincrby( redisKey , helpCount , myPartnerId ) end local eventId = pb.enum("EnumMoneyChangeEventID","EventID_30") player:MoneyChange(dataType.MoneyType_ContributeCoin , helpCount , eventId ) skynet.server.personal:AccDetail( myPartnerId ,"groupContributeScore", helpCount) self:AccGroupInfo( groupId , "contributeScore" , helpCount ) skynet.server.levelTask:Modify( player , 94 , 1 ) skynet.server.taskListEvent:Modify( player ,94, 1) skynet.server.taskListEvent:Modify( player , 33 , 1 ) data.msg = msgData self:SendMsgToFriend( myPartnerId , msgData.sendPartnerId , data.msg ) skynet.server.partner:SendMsgTips( msgData.sendPartnerId , 66 , true ) end end end end s2cData.cmd = pb.enum("MsgType","CMD_S2C_GroupGiveGoods") s2cData.data = assert(pb.encode("S2CGroupGiveGoods", data)) end --家园建材求助详情 function GroupChat:HelpDetail( player , c2sData , s2cData ) c2sData.data = assert(pb.decode("C2SGroupHelpDetail", c2sData.data )) local data = {} data.infos = {} --获取我今天能帮助的最少次数 local cfgSValue = skynet.server.gameConfig:GetPlayerAllCfg( player , "SValue") local helpInfo = cfgSValue.communityMaterialHelp local minHelpCount = {0 , 0 , 0} for i = 1, 3, 1 do if not player.gameData.todayGain.groupNeedHelpCount[ i ] then player.gameData.todayGain.groupNeedHelpCount[ i ] = 0 end end minHelpCount[1]= helpInfo[2] - player.gameData.todayGain.groupNeedHelpCount[ 1 ] minHelpCount[2]= helpInfo[2] - player.gameData.todayGain.groupNeedHelpCount[ 2 ] minHelpCount[3]= helpInfo[2] - player.gameData.todayGain.groupNeedHelpCount[ 3 ] if minHelpCount[1] < 0 then minHelpCount[1] = 0 end if minHelpCount[2] < 0 then minHelpCount[2] = 0 end if minHelpCount[3] < 0 then minHelpCount[3] = 0 end table.insert( data.infos , { goodsType = dataType.GoodsType_Prop , goodsId = 8 , goodsCount = minHelpCount[1] }) table.insert( data.infos , { goodsType = dataType.GoodsType_Prop , goodsId = 9 , goodsCount = minHelpCount[2] }) table.insert( data.infos , { goodsType = dataType.GoodsType_Prop , goodsId = 10 , goodsCount = minHelpCount[3] }) --[[ local myPartnerId = player.gameData.partner.id local groupId = self:GetGroupID( player ) local redisKey = string.format( redisKeyUrl.GameServerGroupMsgRecordZSet , groupId ) local allRecords = skynet.server.redis:zrange( redisKey , 0 , -1 ) for k, v in pairs( allRecords ) do local msgInfo = json:decode(v) --非自己请求并且未领取的消息可以获取 if myPartnerId ~= msgInfo.sendPartnerId and self.MsgType_Need == msgInfo.msgType and dataType.GiftStatus_NoGain == msgInfo.needGoods.status then msgInfo.needGoods.info.goodsCount = minHelpCount table.insert( data.infos , msgInfo.needGoods.info ) end end]] s2cData.cmd = pb.enum("MsgType","CMD_S2C_GroupHelpDetail") s2cData.data = assert(pb.encode("S2CGroupHelpDetail", data)) end --发送消息到好友 function GroupChat:SendMsgToFriend( sendPartnerId , toPartnerId , msgData ) local playerDetail = skynet.server.personal:GetDetail( toPartnerId ) --获取个人详情 if not playerDetail then return end local tmpData = {} tmpData.msgs = msgData local cmd = "CMD_S2C_GroupSendMsg" local returnValue = skynet.server.playerCenter:IsSameServer( toPartnerId ) if nil == returnValue then return elseif true ==returnValue then --在同一服务器 skynet.server.gameServer:SendMsgToUser( playerDetail.userId , cmd , tmpData ) if sendPartnerId ~= toPartnerId then local player = skynet.server.playerCenter:GetPlayer( playerDetail.userId ) if player then --发送红点信息 skynet.server.msgTips:Add( player , 66 ) end end elseif false ==returnValue then --不在同一服务器,将聊天消息推到玩家所在的游戏服 local sendData = {} sendData.userId = playerDetail.userId sendData.cmd = cmd sendData.data = tmpData local serverCmd = skynet.server.gameServer.GameToGame_NetMsg skynet.server.gameServer:SendMsgToServer( playerDetail.gameServerId , serverCmd, sendData ) end end skynet.server.groupChat = GroupChat return GroupChat