254 lines
11 KiB
Lua
254 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 FestivalShop = oo.class(shop)
|
|||
|
|
|
|||
|
|
|
|||
|
|
--[[
|
|||
|
|
CMD_C2S_FestivalShopShow = 1129; //节日商店展示
|
|||
|
|
CMD_S2C_FestivalShopShow = 1130;
|
|||
|
|
CMD_C2S_FestivalShopLottery = 1131; //节日商店抽奖
|
|||
|
|
CMD_S2C_FestivalShopLottery = 1132;
|
|||
|
|
CMD_C2S_FestivalShopBuy = 1133; //节日商店购买
|
|||
|
|
CMD_S2C_FestivalShopBuy = 1134;
|
|||
|
|
|
|||
|
|
|
|||
|
|
//节日商店信息
|
|||
|
|
message PBFestivalShopInfo
|
|||
|
|
{
|
|||
|
|
int32 curSuitType = 1; //当前套间ID
|
|||
|
|
repeated PBShopGoodsInfo furnitureGoods = 2; //套间家具信息
|
|||
|
|
repeated PBShopGoodsInfo decorateGoods = 3; //套间装修信息
|
|||
|
|
int32 nextRefreshTime = 4; //下一次刷新时间
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//节日商店展示
|
|||
|
|
message C2SFestivalShopShow
|
|||
|
|
{
|
|||
|
|
int32 shopType = 1; //10---圣诞商店 11-春节商店
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//节日商店展示
|
|||
|
|
message S2CFestivalShopShow
|
|||
|
|
{
|
|||
|
|
PBFestivalShopInfo shopInfo = 1; //商店信息
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//节日商店购买
|
|||
|
|
message C2SFestivalShopBuy
|
|||
|
|
{
|
|||
|
|
int32 shopType = 1; //10---圣诞商店 11-春节商店
|
|||
|
|
int32 goodsType = 2; //商品类型 11-家具 12-装修
|
|||
|
|
int32 goodsId = 3; //商品ID
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//节日商店购买
|
|||
|
|
message S2CFestivalShopBuy
|
|||
|
|
{
|
|||
|
|
int32 shopType = 1; //10---圣诞商店 11-春节商店
|
|||
|
|
PBFestivalShopInfo shopInfo = 2; //商店信息
|
|||
|
|
}
|
|||
|
|
]]
|
|||
|
|
function FestivalShop:Init()
|
|||
|
|
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--显示一种类型
|
|||
|
|
function FestivalShop:Show( player , c2sData , s2cData )
|
|||
|
|
c2sData.data = assert(pb.decode("C2SFestivalShopShow", c2sData.data ))
|
|||
|
|
local shopType = c2sData.data.shopType
|
|||
|
|
local data = {}
|
|||
|
|
|
|||
|
|
if not shopType or shopType ~= dataType.ShopType_Christmas and shopType ~= dataType.ShopType_SpringFestival then
|
|||
|
|
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
|
|||
|
|
else
|
|||
|
|
data.shopInfo = {}
|
|||
|
|
if not player.gameData.shop[ shopType ] then
|
|||
|
|
player.gameData.shop[ shopType ] = {}
|
|||
|
|
player.gameData.shop[ shopType ].type = shopType
|
|||
|
|
player.gameData.shop[ shopType ].curSuitType = 0 --当前套装类型
|
|||
|
|
player.gameData.shop[ shopType ].furnitureGoods = {} --套装家具
|
|||
|
|
player.gameData.shop[ shopType ].decorateGoods = {} --套装家具
|
|||
|
|
player.gameData.shop[ shopType ].nextRefreshTime = 0
|
|||
|
|
self:RefreshShop( player , shopType )
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--时间超过了,刷新新的一批
|
|||
|
|
--if skynet.GetTime() >= player.gameData.shop[ shopType ].nextRefreshTime then
|
|||
|
|
-- self:RefreshShop( player , shopType )
|
|||
|
|
--end
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--获取最新的商店数据
|
|||
|
|
data.shopInfo = self:GetShopData(player , shopType )
|
|||
|
|
s2cData.cmd = pb.enum("MsgType","CMD_S2C_FestivalShopShow")
|
|||
|
|
s2cData.data = assert(pb.encode("S2CFestivalShopShow", data))
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--商店购买
|
|||
|
|
function FestivalShop:Buy( player , c2sData , s2cData )
|
|||
|
|
c2sData.data = assert(pb.decode("C2SFestivalShopBuy", c2sData.data ))
|
|||
|
|
local shopType = c2sData.data.shopType
|
|||
|
|
local goodsType = c2sData.data.goodsType
|
|||
|
|
local goodsId = c2sData.data.goodsId
|
|||
|
|
local data = {}
|
|||
|
|
local shopType = dataType.ShopType_Suit
|
|||
|
|
local curShop = player.gameData.shop[ shopType ]
|
|||
|
|
|
|||
|
|
if not goodsType or not goodsId or not shopType or shopType ~= dataType.ShopType_Christmas and shopType ~= dataType.ShopType_SpringFestival 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 )
|
|||
|
|
if moneyType and player:MoneyChange( moneyType , -moneyCount ) then
|
|||
|
|
skynet.server.bag:AddGoods( player , dataType.GoodsType_Furniture , v.id , 1 )
|
|||
|
|
--增加经验
|
|||
|
|
local exp = skynet.server.bag:CalcExp( dataType.GoodsType_Furniture , v.id , 1 )
|
|||
|
|
player:AddExp( exp )
|
|||
|
|
player.gameData.shop[ shopType ].furnitureGoods[k].buyCount = 1
|
|||
|
|
log.info(string.format("玩家 %d 套间商店 购买家具 商品 ID %d 扣除货币类型 %d 金额 %f" , player.basicInfo.userID , goodsId , 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 = self:GetDecoratePrice( v.id )
|
|||
|
|
if moneyType and player:MoneyChange( moneyType , -moneyCount ) then
|
|||
|
|
skynet.server.bag:AddGoods( player , dataType.GoodsType_Decorate , v.id , 1 )
|
|||
|
|
--增加经验
|
|||
|
|
local exp = skynet.server.bag:CalcExp( dataType.GoodsType_Decorate , v.id , 1 )
|
|||
|
|
player:AddExp( exp )
|
|||
|
|
player.gameData.shop[ shopType ].decorateGoods[k].buyCount = 1
|
|||
|
|
log.info(string.format("玩家 %d 套间商店 购买装修 商品 ID %d 扣除货币类型 %d 金额 %f" , player.basicInfo.userID , goodsId , 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_FestivalShopBuy")
|
|||
|
|
s2cData.data = assert(pb.encode("S2CFestivalShopBuy", data))
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--随机商品
|
|||
|
|
function FestivalShop: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.goodsType = dataType.GoodsType_Furniture
|
|||
|
|
goodsInfo.id = randId
|
|||
|
|
goodsInfo.count = 1
|
|||
|
|
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.goodsType = dataType.GoodsType_Decorate
|
|||
|
|
goodsInfo.id = randId
|
|||
|
|
goodsInfo.count = 1
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
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 FestivalShop:RefreshShop( player , shopType )
|
|||
|
|
local suitCount = 1 --套间总数量
|
|||
|
|
if dataType.ShopType_Christmas == shopType then
|
|||
|
|
player.gameData.shop[ shopType ].curSuitType = 1
|
|||
|
|
elseif dataType.ShopType_SpringFestival == shopType then
|
|||
|
|
player.gameData.shop[ shopType ].curSuitType = 2
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
local curSuitType = player.gameData.shop[ shopType ].curSuitType
|
|||
|
|
|
|||
|
|
local goodsList = {}
|
|||
|
|
goodsList = self:GetSuitFurniture( self.FurnitureBuyType_Festival , curSuitType )
|
|||
|
|
self:SetShopData( player , shopType , dataType.GoodsType_Furniture , goodsList )
|
|||
|
|
goodsList = self:GetSuitDecoration( self.FurnitureBuyType_Festival , curSuitType )
|
|||
|
|
self:SetShopData( player , shopType , dataType.GoodsType_Decorate , goodsList )
|
|||
|
|
|
|||
|
|
--刷新时间
|
|||
|
|
--player.gameData.shop[ shopType ].nextRefreshTime = skynet.server.common:GetAfterSomeDay(1)
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--设置商店数据
|
|||
|
|
function FestivalShop: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
|
|||
|
|
|
|||
|
|
local isBuy = false --是否购买
|
|||
|
|
for k, v in pairs( goodsList ) do
|
|||
|
|
isBuy = false
|
|||
|
|
if dataType.GoodsType_Furniture == goodsType then
|
|||
|
|
if player:IsBuyGoods( dataType.GoodsType_Furniture , v ) then
|
|||
|
|
isBuy = true
|
|||
|
|
end
|
|||
|
|
table.insert( player.gameData.shop[ shopType ].furnitureGoods , { id = v , isBuy =isBuy } )
|
|||
|
|
log.info(string.format("套间商店 设置商店数据 玩家 %d 最新家具 ID %d 是否购买 %s" , player.basicInfo.userID , v , isBuy ))
|
|||
|
|
elseif dataType.GoodsType_Decorate == goodsType then
|
|||
|
|
if player:IsBuyGoods( dataType.GoodsType_Decorate , v ) then
|
|||
|
|
isBuy = true
|
|||
|
|
end
|
|||
|
|
table.insert( player.gameData.shop[ shopType ].decorateGoods , { id = v , isBuy =isBuy } )
|
|||
|
|
log.info(string.format("套间商店 设置商店数据 玩家 %d 最新装修 ID %d 是否购买 %s" , player.basicInfo.userID , v , isBuy ))
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
end
|
|||
|
|
|
|||
|
|
--获取商店数据
|
|||
|
|
function FestivalShop: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.festivalShop = FestivalShop
|
|||
|
|
return FestivalShop
|