148 lines
5.4 KiB
Lua
148 lines
5.4 KiB
Lua
local skynet = require "skynet"
|
|
local oo = require "Class"
|
|
local log = require "Log"
|
|
local pb = require "pb"
|
|
local errorInfo = require "ErrorInfo"
|
|
local dataType = require "DataType"
|
|
local Gashapon = oo.class()
|
|
|
|
function Gashapon:Init()
|
|
end
|
|
|
|
--扭蛋展示
|
|
function Gashapon:Show( player , c2sData , s2cData )
|
|
c2sData.data = assert(pb.decode("C2SGashaponShow", c2sData.data ))
|
|
local data = {}
|
|
|
|
--刷新一下
|
|
self:Refresh( player )
|
|
|
|
local gashapon = player.gameData.gashapon
|
|
data.serialId = gashapon.serialId
|
|
data.isFreeLottery = gashapon.isFreeLottery
|
|
|
|
s2cData.cmd = pb.enum("MsgType","CMD_S2C_GashaponShow")
|
|
s2cData.data = assert(pb.encode("S2CGashaponShow", data))
|
|
end
|
|
|
|
--扭蛋抽奖
|
|
function Gashapon:Lottery( player , c2sData , s2cData )
|
|
c2sData.data = assert(pb.decode("C2SGashaponLottery", c2sData.data ))
|
|
local data = {}
|
|
local gashapon = player.gameData.gashapon
|
|
if gashapon.isFreeLottery then
|
|
gashapon.isFreeLottery = false
|
|
end
|
|
|
|
local lotteryLevel = 1 --蛋的等级
|
|
local rand = math.random(1, 100)
|
|
if rand >= 1 and rand <= 5 then
|
|
lotteryLevel = 3 --紫色扭蛋
|
|
elseif rand > 6 and rand <= 45 then
|
|
lotteryLevel = 2 --蓝色扭蛋
|
|
elseif rand > 45 and rand <= 100 then
|
|
lotteryLevel = 1 --绿色扭蛋
|
|
end
|
|
|
|
gashapon.serialId = gashapon.serialId
|
|
gashapon.isFreeLottery = gashapon.isFreeLottery
|
|
gashapon.level = lotteryLevel
|
|
log.info(string.format("玩家 %d 获取成功扭蛋系列 %d 蛋的等级 %d", player.userId , gashapon.serialId , lotteryLevel ))
|
|
|
|
data.serialId = gashapon.serialId
|
|
data.isFreeLottery = gashapon.isFreeLottery
|
|
data.level = lotteryLevel
|
|
s2cData.cmd = pb.enum("MsgType","CMD_S2C_GashaponLottery")
|
|
s2cData.data = assert(pb.encode("S2CGashaponLottery", data))
|
|
end
|
|
|
|
--扭蛋获取奖品
|
|
function Gashapon:GainPrize( player , c2sData , s2cData )
|
|
c2sData.data = assert(pb.decode("C2SGashaponGainPrize", c2sData.data ))
|
|
local gashapon = player.gameData.gashapon
|
|
|
|
local data = {}
|
|
|
|
if 0 == gashapon.lotteryLevel then
|
|
s2cData.code = errorInfo.ErrorCode.GetGoodsFailed
|
|
else
|
|
local goodsType = 0
|
|
local goodsId = 0
|
|
local goodsCount = 0
|
|
|
|
--找到该系列下的奖品
|
|
local allItem = {}
|
|
local cfgGashapon = skynet.server.gameConfig.Gashapon
|
|
for k, v in pairs( cfgGashapon ) do
|
|
if gashapon.serialId == v.id then
|
|
allItem = v.allItemId
|
|
break
|
|
end
|
|
end
|
|
|
|
if 1 == gashapon.level then --绿色扭蛋
|
|
local adDiamond = skynet.server.gameConfig.SValue.ADDiamond
|
|
local level = player.gameData.level
|
|
--等级领取上限
|
|
local earningUpper = skynet.server.gameConfig.Level[ level ].earningUpper
|
|
goodsType = dataType.GoodsType_Clovers
|
|
goodsId = 0
|
|
--计算四叶草数量
|
|
goodsCount = math.ceil(adDiamond * earningUpper * 0.5)
|
|
elseif 2 == gashapon.level then --蓝色扭蛋
|
|
goodsType = dataType.GoodsType_Furniture
|
|
goodsId = allItem[ math.random(2,6) ]
|
|
goodsCount = 1
|
|
elseif 3 == gashapon.level then --紫色扭蛋
|
|
goodsType = dataType.GoodsType_Furniture
|
|
goodsId = allItem[ 1 ]
|
|
goodsCount = 1
|
|
end
|
|
|
|
--发放奖励
|
|
if dataType.GoodsType_Clovers == goodsType then
|
|
player:MoneyChange( dataType.MoneyType_Map , goodsCount ) --发放四叶草
|
|
elseif dataType.GoodsType_Furniture == goodsType then
|
|
skynet.server.bag:AddGoods( player , dataType.GoodsType_Furniture , goodsId , goodsCount ) --新增商品
|
|
end
|
|
|
|
log.info(string.format("玩家 %d 获取成功扭蛋系列 %d 蛋的等级 %d 商品类型 %d 商品ID %d 商品数量 %d", player.userId , gashapon.serialId , gashapon.level ,goodsType , goodsId , goodsCount))
|
|
|
|
gashapon.level = 0
|
|
data.goodsType = goodsType
|
|
data.goodsId = goodsId
|
|
data.goodsCount = goodsCount
|
|
end
|
|
|
|
s2cData.cmd = pb.enum("MsgType","CMD_S2C_GashaponGainPrize")
|
|
s2cData.data = assert(pb.encode("S2CGashaponGainPrize", data))
|
|
end
|
|
|
|
--刷新
|
|
function Gashapon:Refresh( player )
|
|
local gashapon = player.gameData.gashapon
|
|
--需要刷新扭蛋系列
|
|
if 0 == gashapon.refreshTime or not skynet.server.common:IsSameDay( skynet.GetTime() , gashapon.refreshTime ) then
|
|
local cfgGashapon = skynet.server.gameConfig.Gashapon
|
|
gashapon.serialId = math.random(1 , #cfgGashapon)
|
|
gashapon.refreshTime = skynet.GetTime()
|
|
gashapon.isFreeLottery = true
|
|
end
|
|
end
|
|
|
|
--是否为扭蛋
|
|
function Gashapon:IsGashapon( goodsId )
|
|
local cfgAllGashapon = skynet.server.gameConfig.Gashapon
|
|
for k1, v1 in pairs( cfgAllGashapon ) do
|
|
for k2, v2 in pairs( v1.allItemId ) do
|
|
if goodsId == v2 then
|
|
return true
|
|
end
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
|
|
skynet.server.gashapon = Gashapon
|
|
return Gashapon
|