446 lines
19 KiB
Lua
446 lines
19 KiB
Lua
local skynet = require "skynet"
|
||
local oo = require "Class"
|
||
local log = require "Log"
|
||
local pb = require "pb"
|
||
local dataType = require "DataType"
|
||
local errorInfo = require "ErrorInfo"
|
||
local shop = require "Shop"
|
||
local StyleShop = oo.class(shop)
|
||
--(推送所有格子信息,单个格子status是未翻开,content必为)
|
||
StyleShop.shopType = dataType.ShopType_Style
|
||
|
||
StyleShop.GridStatus_NoOpen = 1 --未翻开
|
||
StyleShop.GridStatus_AlreadyOpen = 2 --已翻开
|
||
StyleShop.GridStatus_AlreadyGain = 3 --已领取
|
||
|
||
StyleShop.ResourceType_Init = 0
|
||
StyleShop.ResourceType_1 = 1 --卷发棒
|
||
StyleShop.ResourceType_2 = 2 --粉底刷
|
||
StyleShop.ResourceType_3 = 3 --梳子
|
||
StyleShop.ResourceType_4 = 4 --刷新
|
||
StyleShop.ResourceType_5 = 5 --剪刀
|
||
|
||
StyleShop.RestoreType_AD = 1 --广告
|
||
StyleShop.RestoreType_Volute = 2 --蜗壳币
|
||
|
||
--格子展示
|
||
function StyleShop:GridShow( player , c2sData , s2cData )
|
||
c2sData.data = assert(pb.decode("C2SStylingRoomMiniGameShow", c2sData.data ))
|
||
local data = {}
|
||
|
||
--检查下是否能恢复
|
||
self:CheckRestoreEnergy( player )
|
||
|
||
local cfgSValue = skynet.server.gameConfig.SValue
|
||
local cfgStyleProps = skynet.server.gameConfig.StyleProps
|
||
local curShop = player.gameData.shop[ self.shopType ]
|
||
--curShop.money.resource1 = 100
|
||
--curShop.money.resource2 = 100
|
||
--curShop.money.resource3 = 100
|
||
--能量恢复时间到达,重置能量数量
|
||
--[[
|
||
if 0 ~= curShop.energy.nextRefillTime and skynet.GetTime() >= curShop.energy.nextRefillTime then
|
||
curShop.energy.energy = cfgSValue.styleEnergy
|
||
curShop.energy.nextRefillTime = 0
|
||
end
|
||
]]
|
||
skynet.server.levelTask:Modify( player , 28 , 1 )
|
||
data.grids = self:GetClientAllGrid( player )
|
||
data.energy = curShop.energy
|
||
data.money = curShop.money
|
||
data.refillAdTimes = player.gameData.todayGain.styleShopADRefreshCount --这里配置还有问题
|
||
s2cData.cmd = pb.enum("MsgType","CMD_S2C_StylingRoomMiniGameShow")
|
||
s2cData.data = assert(pb.encode("S2CStylingRoomMiniGameShow", data))
|
||
end
|
||
|
||
--格子刷新
|
||
function StyleShop:GridRefresh( player , c2sData , s2cData )
|
||
c2sData.data = assert(pb.decode("C2SStylingRoomMiniGameRefresh", c2sData.data ))
|
||
local data = {}
|
||
|
||
local curShop = player.gameData.shop[ self.shopType ]
|
||
local isExist = false
|
||
for k, v in pairs( curShop.grids ) do
|
||
if self.GridStatus_AlreadyOpen == v.status and self.ResourceType_4 == v.content then
|
||
--找到对应的才会刷新
|
||
curShop.grids = self:GenerateGrid( player )
|
||
isExist = true
|
||
break
|
||
end
|
||
end
|
||
|
||
if not isExist then
|
||
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
|
||
else
|
||
data.grids = self:GetClientAllGrid( player )
|
||
log.info(string.format("玩家 %d 造型间 刷新格子", player.userId ))
|
||
end
|
||
|
||
s2cData.cmd = pb.enum("MsgType","CMD_S2C_StylingRoomMiniGameRefresh")
|
||
s2cData.data = assert(pb.encode("S2CStylingRoomMiniGameRefresh", data))
|
||
end
|
||
|
||
--格子点击
|
||
function StyleShop:GridClick( player , c2sData , s2cData )
|
||
c2sData.data = assert(pb.decode("C2SStylingRoomMiniGameClick", c2sData.data ))
|
||
local data = {}
|
||
|
||
local id = c2sData.data.id
|
||
id = id + 1
|
||
|
||
if not id or not self:IsVaildGrid( id ) then
|
||
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
|
||
else
|
||
local curShop = player.gameData.shop[ self.shopType ]
|
||
local isExist = false
|
||
for k, v in pairs( curShop.grids ) do
|
||
if id == v.id and self.GridStatus_NoOpen == v.status then --显示资源
|
||
local curEnergy = curShop.energy.energy --当前能量
|
||
if curEnergy <= 0 then
|
||
s2cData.code = errorInfo.ErrorCode.NoEnoughMoney
|
||
else
|
||
local cfgSValue = skynet.server.gameConfig.SValue
|
||
curShop.energy.energy = curShop.energy.energy - 1 --能量扣除
|
||
curShop.energy.nextRefillTime = skynet.GetTime() + cfgSValue.styleEnergyCdTime --能量恢复时间
|
||
v.status = self.GridStatus_AlreadyOpen
|
||
isExist = true
|
||
if v.content >= self.ResourceType_1 and v.content <=self.ResourceType_3 then
|
||
skynet.server.passCheck:Modify( player , 14 , 1 )
|
||
end
|
||
log.info(string.format("玩家 %d 造型间 打开格子 %d 最新能量 %d 下一次刷新时间 %s", player.userId , id , curShop.energy.energy , skynet.server.common:GetStrTime( curShop.energy.nextRefillTime)))
|
||
end
|
||
elseif id == v.id and self.GridStatus_AlreadyOpen == v.status and ((v.content >= self.ResourceType_1 and v.content <= self.ResourceType_3) or v.content == self.ResourceType_5) then --搜集资源
|
||
v.status = self.GridStatus_AlreadyGain
|
||
--根据资源类型进行搜集
|
||
if self.ResourceType_1 == v.content then
|
||
curShop.money.resource1 = curShop.money.resource1 + 1
|
||
elseif self.ResourceType_2 == v.content then
|
||
curShop.money.resource2 = curShop.money.resource2 + 1
|
||
elseif self.ResourceType_3 == v.content then
|
||
curShop.money.resource3 = curShop.money.resource3 + 1
|
||
elseif self.ResourceType_5 == v.content then
|
||
local cfgStyleProps = skynet.server.gameConfig.StyleProps
|
||
local cfgSValue = skynet.server.gameConfig.SValue
|
||
curShop.energy.energy = curShop.energy.energy + cfgStyleProps[ self.ResourceType_5 ].num --剪刀按配置中的数量增加
|
||
if curShop.energy.energy > cfgSValue.styleEnergy then
|
||
--大于默认值就按最大值给
|
||
curShop.energy.energy = cfgSValue.styleEnergy
|
||
curShop.energy.nextRefillTime = 0
|
||
end
|
||
end
|
||
|
||
if v.content >= self.ResourceType_1 and v.content <=self.ResourceType_3 then
|
||
skynet.server.levelTask:Modify( player , 54 , 1 )
|
||
end
|
||
--主动推货币变化信息
|
||
local tmpData = {}
|
||
tmpData.money = {}
|
||
tmpData.money.resource1 = curShop.money.resource1
|
||
tmpData.money.resource2 = curShop.money.resource2
|
||
tmpData.money.resource3 = curShop.money.resource3
|
||
skynet.server.gameServer:SendMsgToUser( player.userId , "CMD_S2C_StylingRoomMiniGameRes" , tmpData )
|
||
isExist = true
|
||
log.info(string.format("玩家 %d 造型间 收集格子 %d 物品 物品类型 %d 最新资源 %d %d %d", player.userId , id , v.content , curShop.money.resource1 , curShop.money.resource2 , curShop.money.resource3 ))
|
||
end
|
||
end
|
||
|
||
if not isExist then
|
||
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
|
||
else
|
||
data.grid = self:GetClientOneGrid( player , id )
|
||
data.energy = curShop.energy
|
||
end
|
||
|
||
--检查下是否能恢复
|
||
self:CheckRestoreEnergy( player )
|
||
end
|
||
|
||
s2cData.cmd = pb.enum("MsgType","CMD_S2C_StylingRoomMiniGameClick")
|
||
s2cData.data = assert(pb.encode("S2CStylingRoomMiniGameClick", data))
|
||
end
|
||
|
||
--恢复能量
|
||
function StyleShop:RestoreEnergy( player , c2sData , s2cData )
|
||
c2sData.data = assert(pb.decode("C2SStylingRoomMiniGameEnergyRefill", c2sData.data ))
|
||
local data = {}
|
||
|
||
local methodType = c2sData.data.methodType
|
||
if not methodType then
|
||
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
|
||
else
|
||
local curShop = player.gameData.shop[ self.shopType ]
|
||
local cfgStyleShop = skynet.server.gameConfig.StyleShop
|
||
local cfgSValue = skynet.server.gameConfig.SValue
|
||
|
||
if self.RestoreType_AD == methodType then --广告恢复能量
|
||
player.gameData.todayGain.styleShopADRefreshCount = player.gameData.todayGain.styleShopADRefreshCount + 1
|
||
|
||
local count = 300 - player.gameData.todayGain.styleShopADRefreshCount
|
||
if count >= 1 then
|
||
player.gameData.todayGain.styleShopADRefreshCount = player.gameData.todayGain.styleShopADRefreshCount + 1
|
||
else
|
||
s2cData.code = errorInfo.ErrorCode.TodayMaxLimit
|
||
end
|
||
|
||
elseif self.RestoreType_Volute == methodType then --蜗壳币恢复能量
|
||
if not player:MoneyChange( dataType.MoneyType_Volute , -cfgSValue.styleEnergyVolutePrice ) then
|
||
s2cData.code = errorInfo.ErrorCode.NoEnoughMoney
|
||
end
|
||
end
|
||
|
||
if errorInfo.Suc == s2cData.code then
|
||
--恢复能量
|
||
curShop.energy.energy = cfgSValue.styleEnergy
|
||
curShop.energy.nextRefillTime = 0
|
||
|
||
data.energy = curShop.energy
|
||
data.refillAdTimes = player.gameData.todayGain.styleShopADRefreshCount
|
||
log.info(string.format("玩家 %d 造型间 恢复能量 恢复类型 %d ", player.userId , methodType ))
|
||
end
|
||
end
|
||
|
||
s2cData.cmd = pb.enum("MsgType","CMD_S2C_StylingRoomMiniGameEnergyRefill")
|
||
s2cData.data = assert(pb.encode("S2CStylingRoomMiniGameEnergyRefill", data))
|
||
end
|
||
|
||
--能量展示
|
||
function StyleShop:EnergyShow( player , c2sData , s2cData )
|
||
c2sData.data = assert(pb.decode("C2SStylingRoomMiniGameEnergyShow", c2sData.data ))
|
||
local data = {}
|
||
local curShop = player.gameData.shop[ self.shopType ]
|
||
data.energy = curShop.energy
|
||
s2cData.cmd = pb.enum("MsgType","CMD_S2C_StylingRoomMiniGameEnergyShow")
|
||
s2cData.data = assert(pb.encode("S2CStylingRoomMiniGameEnergyShow", data))
|
||
end
|
||
|
||
--造型间展示
|
||
function StyleShop:RoomShow( player , c2sData , s2cData )
|
||
c2sData.data = assert(pb.decode("C2SStylingDesignRoomShow", c2sData.data ))
|
||
local data = {}
|
||
|
||
self:CheckRestoreEnergy( player )
|
||
skynet.server.levelTask:Modify( player , 28 , 1 )
|
||
local curShop = player.gameData.shop[ self.shopType ]
|
||
data.currentStyleId = curShop.curStyleId
|
||
data.rewards = curShop.gainRewards
|
||
data.money = curShop.money
|
||
data.energy = curShop.energy
|
||
s2cData.cmd = pb.enum("MsgType","CMD_S2C_StylingDesignRoomShow")
|
||
s2cData.data = assert(pb.encode("S2CStylingDesignRoomShow", data))
|
||
end
|
||
|
||
--造型间设计
|
||
function StyleShop:RoomAttempt( player , c2sData , s2cData )
|
||
c2sData.data = assert(pb.decode("C2SStylingDesignRoomAttempt", c2sData.data ))
|
||
local data = {}
|
||
local styleId = c2sData.data.styleId
|
||
if not styleId then
|
||
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
|
||
else
|
||
local curCfgStyleList = skynet.server.gameConfig:GetCurCfg( "StyleList" , styleId )
|
||
local curShop = player.gameData.shop[ self.shopType ]
|
||
local curStyleId = curShop.curStyleId
|
||
local money = curShop.money
|
||
if styleId ~= curStyleId then
|
||
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
|
||
elseif money.resource1 < curCfgStyleList.res1 or money.resource2 < curCfgStyleList.res2 or money.resource3 < curCfgStyleList.res3 then
|
||
s2cData.code = errorInfo.ErrorCode.NoEnoughMoney
|
||
else
|
||
money.resource1 = money.resource1 - curCfgStyleList.res1
|
||
money.resource2 = money.resource2 - curCfgStyleList.res2
|
||
money.resource3 = money.resource3 - curCfgStyleList.res3
|
||
curShop.curStyleId = self:GetStyleID( player )
|
||
data.currentStyleId = curShop.curStyleId
|
||
data.money = curShop.money
|
||
skynet.server.levelTask:Modify( player , 32 , 1 )
|
||
skynet.server.passCheck:Modify( player , 12 , 1 )
|
||
log.info(string.format("玩家 %d 造型间 开始设计 造型ID %d 设计后最新造型ID", player.userId , styleId , curShop.curStyleId))
|
||
end
|
||
end
|
||
s2cData.cmd = pb.enum("MsgType","CMD_S2C_StylingDesignRoomAttempt")
|
||
s2cData.data = assert(pb.encode("S2CStylingDesignRoomAttempt", data))
|
||
end
|
||
|
||
--领取设计奖励
|
||
function StyleShop:RoomReward( player , c2sData , s2cData )
|
||
c2sData.data = assert(pb.decode("C2SStylingDesignRewardPrize", c2sData.data ))
|
||
local data = {}
|
||
local styleId = c2sData.data.styleId
|
||
if not styleId or styleId <= 0 then
|
||
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
|
||
else
|
||
local isGain = false
|
||
local curShop = player.gameData.shop[ self.shopType ]
|
||
--先检查下领取过该奖励
|
||
for k, v in pairs( curShop.gainRewards ) do
|
||
if styleId == v then
|
||
isGain = true
|
||
break
|
||
end
|
||
end
|
||
|
||
if isGain then
|
||
s2cData.code = errorInfo.ErrorCode.AlreadyGet
|
||
elseif styleId > curShop.curStyleId then
|
||
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
|
||
else
|
||
table.insert( curShop.gainRewards , styleId)
|
||
|
||
--发放奖励
|
||
local curCfgStyleList = skynet.server.gameConfig:GetCurCfg( "StyleList" , styleId )
|
||
player:GiveRewardNpc( curCfgStyleList.reward , 5)
|
||
data.styleId = styleId
|
||
end
|
||
end
|
||
s2cData.cmd = pb.enum("MsgType","CMD_S2C_StylingDesignRewardPrize")
|
||
s2cData.data = assert(pb.encode("S2CStylingDesignRewardPrize", data))
|
||
end
|
||
|
||
--检查下能不能恢复体力
|
||
function StyleShop:CheckRestoreEnergy( player )
|
||
local cfgSValue = skynet.server.gameConfig.SValue
|
||
local curShop = player.gameData.shop[ self.shopType ]
|
||
local diffTime = skynet.GetTime() - curShop.energy.nextRefillTime --skynet.GetTime() - 最初的时间
|
||
if 0 == curShop.energy.nextRefillTime or diffTime < 0 then
|
||
return
|
||
end
|
||
|
||
local count = math.ceil(diffTime / cfgSValue.styleEnergyCdTime) --计算恢复数量
|
||
curShop.energy.energy = curShop.energy.energy + count
|
||
if curShop.energy.energy > cfgSValue.styleEnergy then
|
||
curShop.energy.energy = cfgSValue.styleEnergy
|
||
curShop.energy.nextRefillTime = 0
|
||
else
|
||
curShop.energy.nextRefillTime = skynet.GetTime() + cfgSValue.styleEnergyCdTime --能量恢复时间
|
||
end
|
||
log.info(string.format("玩家 %d 造型间 检查恢复能量 时间差 %d 恢复时间 %d 秒 恢复数量 %d 最终数量 %d", player.userId , diffTime , cfgSValue.styleEnergyCdTime , count , curShop.energy.energy))
|
||
end
|
||
|
||
--生成格子
|
||
function StyleShop:GenerateGrid( player )
|
||
local cfgStyleProps = skynet.server.gameConfig.StyleProps
|
||
local cfgSValue = skynet.server.gameConfig.SValue
|
||
|
||
local rand = math.random(1,3) --随机权重
|
||
--获取三种道具的权重
|
||
local curWeight = {}
|
||
curWeight[ 1 ] = cfgStyleProps[ 1 ].updateWeight[ rand ]
|
||
curWeight[ 2 ] = cfgStyleProps[ 2 ].updateWeight[ rand ]
|
||
curWeight[ 3 ] = cfgStyleProps[ 3 ].updateWeight[ rand ]
|
||
local allWeightCount = curWeight[1] + curWeight[2] + curWeight[3] --计算总权重
|
||
local materialCount = cfgSValue.styleProbsUpdateNum
|
||
local goodsCount = materialCount[1] + materialCount[ 2 ] --总共道具数量
|
||
local material = {} --材料
|
||
|
||
--根据权重随机16种材料
|
||
local sText = ""
|
||
for i = 1, materialCount[ 1 ], 1 do
|
||
rand = math.random(1,allWeightCount)
|
||
local tmp = 0
|
||
local index = 0 --当前生成的材料
|
||
for j = 1, 3, 1 do
|
||
tmp = tmp + curWeight[ j ]
|
||
if rand <= tmp then
|
||
index = j
|
||
break
|
||
end
|
||
end
|
||
|
||
sText = sText.." "..index
|
||
table.insert( material , index)
|
||
end
|
||
|
||
--根据配置数量将刷新材料放进去
|
||
for i = 1, materialCount[ 2 ], 1 do
|
||
table.insert( material , self.ResourceType_4 )
|
||
sText = sText.." "
|
||
sText = sText..self.ResourceType_4
|
||
end
|
||
|
||
--剪刀需要一定的概率刷出
|
||
local prob = cfgStyleProps[ 5 ].updateProbability * 100
|
||
rand = math.random(1,100)
|
||
if rand <= prob then
|
||
table.insert( material , self.ResourceType_5 )
|
||
sText = sText.." "
|
||
sText = sText..self.ResourceType_5
|
||
goodsCount = goodsCount + 1
|
||
end
|
||
|
||
log.info(string.format("玩家 %d 造型间 生成道具信息 %s", player.userId , sText ))
|
||
|
||
--默认42个格子
|
||
local grids = {}
|
||
local tmpGrids = {}
|
||
for i = 1, 42, 1 do
|
||
grids[ i ] = {}
|
||
grids[ i ].id = i
|
||
grids[ i ].content = 0
|
||
grids[ i ].status = self.GridStatus_NoOpen
|
||
table.insert( tmpGrids , {id = i})
|
||
end
|
||
|
||
--生成17个格子位置
|
||
sText = ""
|
||
local randGridsIndex = skynet.server.shop:GetNoRepeatGoods( goodsCount , tmpGrids )
|
||
for k, v in pairs( randGridsIndex ) do
|
||
grids[ v ].content = material[ k ]
|
||
sText = sText.." 格子ID "..v.." 道具ID "..grids[ k ].content
|
||
end
|
||
log.info(string.format("玩家 %d 造型间 最终生成格子信息 %s", player.userId , sText ))
|
||
return grids
|
||
end
|
||
|
||
--是否有效的格子
|
||
function StyleShop:IsVaildGrid( id )
|
||
if id < 1 or id > 42 then
|
||
return false
|
||
end
|
||
return true
|
||
end
|
||
|
||
--获取单个格子
|
||
function StyleShop:GetClientOneGrid( player , id )
|
||
local curShop = player.gameData.shop[ self.shopType ]
|
||
local data = {}
|
||
local curGrid = curShop.grids[ id ]
|
||
if self.GridStatus_AlreadyOpen == curGrid.status then
|
||
--打开状态才发真实数据
|
||
data = { id = curGrid.id - 1, content = curGrid.content , status = curGrid.status }
|
||
else
|
||
--未打开状态内容填0
|
||
data = { id = curGrid.id - 1, content = 0 , status = curGrid.status }
|
||
end
|
||
return data
|
||
end
|
||
|
||
--获取所有格子
|
||
function StyleShop:GetClientAllGrid( player )
|
||
local curShop = player.gameData.shop[ self.shopType ]
|
||
local data = {}
|
||
for k, v in pairs( curShop.grids ) do
|
||
if self.GridStatus_AlreadyOpen == v.status then
|
||
--打开状态才发真实数据
|
||
table.insert( data , { id = v.id -1, content = v.content , status = v.status })
|
||
else
|
||
--未打开状态内容填0
|
||
table.insert( data , { id = v.id -1, content = 0 , status = v.status })
|
||
end
|
||
end
|
||
return data
|
||
end
|
||
|
||
--获取当前的造型ID
|
||
function StyleShop:GetStyleID( player )
|
||
local cfgStyleList = skynet.server.gameConfig.StyleList
|
||
local cfgCount = #cfgStyleList
|
||
local curShop = player.gameData.shop[ self.shopType ]
|
||
curShop.curStyleId = curShop.curStyleId + 1
|
||
if curShop.curStyleId > cfgCount then
|
||
curShop.curStyleId = cfgCount + 1
|
||
end
|
||
return curShop.curStyleId
|
||
end
|
||
|
||
skynet.server.styleShop = StyleShop
|
||
return StyleShop |