248 lines
11 KiB
Lua
248 lines
11 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 SuitShop = oo.class(shop)
|
|||
|
|
|
|||
|
|
SuitShop.PurchaseType_1 = 1 --逸家购买
|
|||
|
|
SuitShop.PurchaseType_2 = 2 --活动获取
|
|||
|
|
|
|||
|
|
function SuitShop:Init()
|
|||
|
|
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--显示一种类型
|
|||
|
|
function SuitShop:Show( player , c2sData , s2cData )
|
|||
|
|
c2sData.data = assert(pb.decode("C2SSuitShopShow", c2sData.data ))
|
|||
|
|
local shopType = dataType.ShopType_Suit
|
|||
|
|
|
|||
|
|
local data = {}
|
|||
|
|
data.shopInfo = {}
|
|||
|
|
|
|||
|
|
--时间超过了,刷新新的一批
|
|||
|
|
if skynet.GetTime() >= player.gameData.shop[ shopType ].nextRefreshTime then
|
|||
|
|
self:RefreshShop( player , shopType )
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--获取最新的商店数据
|
|||
|
|
data.shopInfo = self:GetShopData(player , shopType )
|
|||
|
|
s2cData.cmd = pb.enum("MsgType","CMD_S2C_SuitShopShow")
|
|||
|
|
s2cData.data = assert(pb.encode("S2CSuitShopShow", data))
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--抽奖
|
|||
|
|
function SuitShop:Lottery( player , c2sData , s2cData )
|
|||
|
|
c2sData.data = assert(pb.decode("C2SSuitShopLottery", c2sData.data ))
|
|||
|
|
local shopType = dataType.ShopType_Suit
|
|||
|
|
local opType = c2sData.data.opType
|
|||
|
|
|
|||
|
|
local data = {}
|
|||
|
|
data.goodsInfo = {}
|
|||
|
|
data.shopInfo = {}
|
|||
|
|
if self.SuitShopLotteryType_AD == opType then --广告
|
|||
|
|
table.insert(data.goodsInfo , self:RandGoods( player , shopType ))
|
|||
|
|
--[[广告购买,暂时屏蔽
|
|||
|
|
elseif self.SuitShopLotteryType_One == opType then --抽一次
|
|||
|
|
if player:MoneyChange( dataType.MoneyType_Map , -100 ) then
|
|||
|
|
table.insert(data.goodsInfo , self:RandGoods( player , shopType ))
|
|||
|
|
else
|
|||
|
|
s2cData.code = errorInfo.ErrorCode.NoEnoughMoney
|
|||
|
|
end
|
|||
|
|
elseif self.SuitShopLotteryType_Three == opType then --抽三次
|
|||
|
|
if player:MoneyChange( dataType.MoneyType_Map , -100 * 3 ) then
|
|||
|
|
table.insert(data.goodsInfo , self:RandGoods( player , shopType ))
|
|||
|
|
table.insert(data.goodsInfo , self:RandGoods( player , shopType ))
|
|||
|
|
table.insert(data.goodsInfo , self:RandGoods( player , shopType ))
|
|||
|
|
else
|
|||
|
|
s2cData.code = errorInfo.ErrorCode.NoEnoughMoney
|
|||
|
|
end
|
|||
|
|
]]
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
data.shopInfo = self:GetShopData( player , shopType )
|
|||
|
|
s2cData.cmd = pb.enum("MsgType","CMD_S2C_SuitShopLottery")
|
|||
|
|
s2cData.data = assert(pb.encode("S2CSuitShopLottery", data))
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--商店购买
|
|||
|
|
function SuitShop:Buy( player , c2sData , s2cData )
|
|||
|
|
c2sData.data = assert(pb.decode("C2SSuitShopBuy", c2sData.data ))
|
|||
|
|
local goodsType = c2sData.data.goodsType
|
|||
|
|
local goodsId = c2sData.data.goodsId
|
|||
|
|
local goodsCount = math.abs(c2sData.data.goodsCount)
|
|||
|
|
local data = {}
|
|||
|
|
local shopType = dataType.ShopType_Suit
|
|||
|
|
local curShop = player.gameData.shop[ shopType ]
|
|||
|
|
|
|||
|
|
--特殊商品购买
|
|||
|
|
if not goodsType or not goodsId or not goodsCount then
|
|||
|
|
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
|
|||
|
|
else
|
|||
|
|
--购买家具
|
|||
|
|
if dataType.GoodsType_Furniture == goodsType then
|
|||
|
|
for k, v in pairs( curShop.furnitureGoods ) do
|
|||
|
|
if goodsId == v.id then
|
|||
|
|
--存在该商品
|
|||
|
|
local moneyType , moneyCount = skynet.server.gameConfig:GetBuyMoney("Furniture" , v.id )
|
|||
|
|
moneyCount = moneyCount * goodsCount
|
|||
|
|
if moneyType and player:MoneyChange( moneyType , -moneyCount ) then
|
|||
|
|
skynet.server.bag:AddGoods( player , dataType.GoodsType_Furniture , v.id , goodsCount )
|
|||
|
|
--增加图鉴
|
|||
|
|
|
|||
|
|
--增加经验
|
|||
|
|
local exp = skynet.server.bag:CalcExp( dataType.GoodsType_Furniture , v.id , goodsCount )
|
|||
|
|
player:AddExp( exp )
|
|||
|
|
player.gameData.shop[ shopType ].furnitureGoods[k].buyCount = 1
|
|||
|
|
--skynet.server.generalTask:Modify( player , dataType.GeneralTaskType_BuySuit , goodsCount )
|
|||
|
|
log.info(string.format("玩家 %d 套间商店 购买家具 商品 ID %d 数量 %d 扣除货币类型 %d 金额 %f" , player.basicInfo.userID , goodsId, goodsCount , moneyType , moneyCount ))
|
|||
|
|
break
|
|||
|
|
else
|
|||
|
|
s2cData.code = errorInfo.ErrorCode.NoEnoughMoney
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--购买装修
|
|||
|
|
if dataType.GoodsType_Decorate == goodsType then
|
|||
|
|
for k, v in pairs( curShop.decorateGoods ) do
|
|||
|
|
if goodsId == v.id then
|
|||
|
|
--存在该商品
|
|||
|
|
local moneyType , moneyCount = skynet.server.gameConfig:GetBuyMoney("currencyBuy" , v.id ) --self:GetDecoratePrice( v.id )
|
|||
|
|
goodsCount = 1
|
|||
|
|
if moneyType and player:MoneyChange( moneyType , -moneyCount ) then
|
|||
|
|
skynet.server.bag:AddGoods( player , dataType.GoodsType_Decorate , v.id , goodsCount )
|
|||
|
|
--增加图鉴
|
|||
|
|
|
|||
|
|
--增加经验
|
|||
|
|
local exp = skynet.server.bag:CalcExp( dataType.GoodsType_Decorate , v.id , goodsCount )
|
|||
|
|
player:AddExp( exp )
|
|||
|
|
player.gameData.shop[ shopType ].decorateGoods[k].buyCount = 1
|
|||
|
|
local cfgDecoration = skynet.server.gameConfig:GetDecorationCfg( v.id )
|
|||
|
|
--skynet.server.generalTask:Modify( player , dataType.GeneralTaskType_BuySuit , goodsCount )
|
|||
|
|
log.info(string.format("玩家 %d 套间商店 购买装修 商品 ID %d 数量 %d 扣除货币类型 %d 金额 %f" , player.basicInfo.userID , goodsId , goodsCount , moneyType , moneyCount ))
|
|||
|
|
break
|
|||
|
|
else
|
|||
|
|
s2cData.code = errorInfo.ErrorCode.NoEnoughMoney
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
data.shopInfo = self:GetShopData(player , shopType )
|
|||
|
|
s2cData.cmd = pb.enum("MsgType","CMD_S2C_SuitShopBuy")
|
|||
|
|
s2cData.data = assert(pb.encode("S2CSuitShopBuy", data))
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--随机商品
|
|||
|
|
function SuitShop:RandGoods( player , shopType )
|
|||
|
|
--[[
|
|||
|
|
随机策略
|
|||
|
|
1.分别计算出当前套间的家具和装修数量。
|
|||
|
|
2.在1到总数量的范围内随机
|
|||
|
|
3.根据随机值到范围,是属于家具还是装修
|
|||
|
|
]]
|
|||
|
|
local goodsInfo = {}
|
|||
|
|
local furnitureCount = #player.gameData.shop[ shopType ].furnitureGoods
|
|||
|
|
local decorateCount = #player.gameData.shop[ shopType ].decorateGoods
|
|||
|
|
local count = furnitureCount + decorateCount
|
|||
|
|
local rand1 = math.random( 1 , count ) --在1到总数量的范围内随机
|
|||
|
|
local rand2 = 0 --在1到家具或装修范围内随机
|
|||
|
|
local randId = 0 --随机生成的ID
|
|||
|
|
|
|||
|
|
if rand1<= furnitureCount then
|
|||
|
|
--随机家具
|
|||
|
|
rand2 = math.random( 1 , furnitureCount )
|
|||
|
|
randId = player.gameData.shop[ shopType ].furnitureGoods[ rand2 ].id
|
|||
|
|
skynet.server.bag:AddGoods( player , dataType.GoodsType_Furniture , randId , 1 )
|
|||
|
|
player.gameData.shop[ shopType ].furnitureGoods[ rand2 ].buyCount = 1
|
|||
|
|
goodsInfo.type = dataType.GoodsType_Furniture
|
|||
|
|
goodsInfo.id = randId
|
|||
|
|
goodsInfo.count = 1
|
|||
|
|
goodsInfo.gainTime = skynet.GetTime()
|
|||
|
|
else
|
|||
|
|
--随机装修
|
|||
|
|
rand2 = math.random( 1 , decorateCount )
|
|||
|
|
randId = player.gameData.shop[ shopType ].decorateGoods[ rand2 ].id
|
|||
|
|
skynet.server.bag:AddGoods( player , dataType.GoodsType_Decorate , randId , 1 )
|
|||
|
|
player.gameData.shop[ shopType ].decorateGoods[ rand2 ].buyCount = 1
|
|||
|
|
goodsInfo.type = dataType.GoodsType_Decorate
|
|||
|
|
goodsInfo.id = randId
|
|||
|
|
goodsInfo.count = 1
|
|||
|
|
goodsInfo.gainTime = skynet.GetTime()
|
|||
|
|
end
|
|||
|
|
--skynet.server.generalTask:Modify( player , dataType.GeneralTaskType_BuySuit , 1 )
|
|||
|
|
|
|||
|
|
log.info(string.format("玩家 %d 套间商店 随机商品 家具数量 %d 装修数量 %d 随机总范围 [ %d , %d ] 随机值1为 %d 随机值2为 %d 随机商品ID %d" ,
|
|||
|
|
player.basicInfo.userID , furnitureCount , decorateCount , 1, count , rand1 , rand2 , randId))
|
|||
|
|
return goodsInfo
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--刷新商店类型
|
|||
|
|
function SuitShop:RefreshShop( player , shopType )
|
|||
|
|
local suitCount = 0
|
|||
|
|
for k, v in pairs( skynet.server.gameConfig.Suit ) do
|
|||
|
|
if self.PurchaseType_1 == v.purchaseType then
|
|||
|
|
suitCount = suitCount + 1
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
player.gameData.shop[ shopType ].curSuitType = player.gameData.shop[ shopType ].curSuitType + 1
|
|||
|
|
|
|||
|
|
--套装ID超过套间数量时,又从第一套开始
|
|||
|
|
if player.gameData.shop[ shopType ].curSuitType > suitCount then
|
|||
|
|
player.gameData.shop[ shopType ].curSuitType = 1
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
local curSuitType = player.gameData.shop[ shopType ].curSuitType
|
|||
|
|
local goodsList = {}
|
|||
|
|
goodsList = self:GetSuitFurniture( self.FurnitureBuyType_Suit , curSuitType )
|
|||
|
|
self:SetShopData( player , shopType , dataType.GoodsType_Furniture , goodsList )
|
|||
|
|
goodsList = self:GetSuitDecoration( self.DecorateBuyType_Suit , curSuitType )
|
|||
|
|
self:SetShopData( player , shopType , dataType.GoodsType_Decorate , goodsList )
|
|||
|
|
|
|||
|
|
--刷新时间
|
|||
|
|
player.gameData.shop[ shopType ].nextRefreshTime = skynet.server.common:GetAfterSomeDay(1)
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--设置商店数据
|
|||
|
|
function SuitShop:SetShopData( player , shopType , goodsType , goodsList )
|
|||
|
|
if dataType.GoodsType_Furniture == goodsType then
|
|||
|
|
player.gameData.shop[ shopType ].furnitureGoods = {}
|
|||
|
|
elseif dataType.GoodsType_Decorate == goodsType then
|
|||
|
|
player.gameData.shop[ shopType ].decorateGoods = {}
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
for k, v in pairs( goodsList ) do
|
|||
|
|
local byCount = 0 --是否购买
|
|||
|
|
if dataType.GoodsType_Furniture == goodsType then
|
|||
|
|
if player:IsBuyGoods( dataType.GoodsType_Furniture , v ) then
|
|||
|
|
byCount = 1
|
|||
|
|
end
|
|||
|
|
table.insert( player.gameData.shop[ shopType ].furnitureGoods , { id = v , byCount = byCount } )
|
|||
|
|
log.info(string.format("玩家 %d 套间商店 设置商店数据 最新家具 ID %d 是否购买 %d" , player.basicInfo.userID , v , byCount ))
|
|||
|
|
elseif dataType.GoodsType_Decorate == goodsType then
|
|||
|
|
if player:IsBuyGoods( dataType.GoodsType_Decorate , v ) then
|
|||
|
|
byCount = 1
|
|||
|
|
end
|
|||
|
|
table.insert( player.gameData.shop[ shopType ].decorateGoods , { id = v , buyCount = byCount } )
|
|||
|
|
log.info(string.format("玩家 %d 套间商店 设置商店数据 最新装修 ID %d 是否购买 %d" , player.basicInfo.userID , v , byCount ))
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--获取商店数据
|
|||
|
|
function SuitShop:GetShopData( player , shopType )
|
|||
|
|
local shopInfo = {}
|
|||
|
|
shopInfo.curSuitType = player.gameData.shop[ shopType ].curSuitType
|
|||
|
|
shopInfo.nextRefreshTime = player.gameData.shop[ shopType ].nextRefreshTime
|
|||
|
|
shopInfo.furnitureGoods = player.gameData.shop[ shopType ].furnitureGoods
|
|||
|
|
shopInfo.decorateGoods = player.gameData.shop[ shopType ].decorateGoods
|
|||
|
|
return shopInfo
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
skynet.server.suitShop = SuitShop
|
|||
|
|
return SuitShop
|