329 lines
16 KiB
Lua
329 lines
16 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 json = require "json"
|
|
local PetClothesShop = oo.class(shop)
|
|
|
|
function PetClothesShop:InitData( player , shopType )
|
|
if not player.gameData.shop[ shopType ] then
|
|
player.gameData.shop[ shopType ] = {}
|
|
local curShop = player.gameData.shop[ shopType ]
|
|
curShop.type = shopType
|
|
curShop.catClothes = {} --猫的服装
|
|
curShop.catHat = {} --猫的帽子
|
|
curShop.catDecoration = {} --猫的装饰品
|
|
curShop.dogClothes = {} --狗的服装
|
|
curShop.dogHat = {} --狗的帽子
|
|
curShop.dogDecoration = {} --狗的装饰品
|
|
curShop.nextRefreshTime = 0
|
|
curShop.catHistoryGoods = {} --猫的历史服装
|
|
curShop.dogHistoryGoods = {} --狗的历史服装
|
|
|
|
for i = 1, self.PetClothesType_Max, 1 do
|
|
curShop.catHistoryGoods[ i ] = {}
|
|
curShop.dogHistoryGoods[ i ] = {}
|
|
end
|
|
self:RefreshShop( player , shopType )
|
|
else
|
|
local curShop = player.gameData.shop[ shopType ]
|
|
--时间超过了,刷新新的一批
|
|
if skynet.GetTime() >= curShop.nextRefreshTime then
|
|
self:RefreshShop( player , shopType )
|
|
end
|
|
end
|
|
end
|
|
|
|
--显示一种类型
|
|
function PetClothesShop:Show( player , c2sData , s2cData )
|
|
c2sData.data = assert(pb.decode("C2SPetClothesShopShow", c2sData.data ))
|
|
local shopType = dataType.ShopType_PetClothes
|
|
|
|
local data = {}
|
|
data.shopInfo = {}
|
|
|
|
--[[if not player.gameData.shop[ shopType ] then
|
|
player.gameData.shop[ shopType ] = {}
|
|
local curShop = player.gameData.shop[ shopType ]
|
|
curShop.type = shopType
|
|
curShop.catClothes = {} --猫的服装
|
|
curShop.catHat = {} --猫的帽子
|
|
curShop.catDecoration = {} --猫的装饰品
|
|
curShop.dogClothes = {} --狗的服装
|
|
curShop.dogHat = {} --狗的帽子
|
|
curShop.dogDecoration = {} --狗的装饰品
|
|
curShop.nextRefreshTime = 0
|
|
curShop.catHistoryGoods = {} --猫的历史服装
|
|
curShop.dogHistoryGoods = {} --狗的历史服装
|
|
for i = 1, dataType.PetClothesType_Max, 1 do
|
|
curShop.catHistoryGoods[ i ] = {}
|
|
curShop.dogHistoryGoods[ i ] = {}
|
|
end
|
|
self:RefreshShop( player , shopType )
|
|
end
|
|
|
|
if nil == player.gameData.shop[ shopType ].catHistoryGoods then
|
|
player.gameData.shop[ shopType ].catHistoryGoods = {} --猫的历史服装
|
|
player.gameData.shop[ shopType ].dogHistoryGoods = {} --狗的历史服装
|
|
for i = 1, self.PetClothesType_Max, 1 do
|
|
player.gameData.shop[ shopType ].catHistoryGoods[ i ] = {}
|
|
player.gameData.shop[ shopType ].dogHistoryGoods[ i ] = {}
|
|
end
|
|
end
|
|
]]
|
|
|
|
--获取玩家数据
|
|
local curShop = player.gameData.shop[ shopType ]
|
|
--时间超过了,刷新新的一批
|
|
if skynet.GetTime() >= curShop.nextRefreshTime or not next(curShop.catDecoration) then
|
|
self:RefreshShop( player , shopType )
|
|
end
|
|
|
|
--获取最新的商店数据
|
|
data.shopInfo = self:GetShopData(player , shopType )
|
|
s2cData.cmd = pb.enum("MsgType","CMD_S2C_PetClothesShopShow")
|
|
s2cData.data = assert(pb.encode("S2CPetClothesShopShow", data))
|
|
end
|
|
|
|
--刷新
|
|
function PetClothesShop:Refresh( player , c2sData , s2cData )
|
|
c2sData.data = assert(pb.decode("C2SPetClothesShopRefresh", c2sData.data ))
|
|
local shopType = dataType.ShopType_PetClothes
|
|
local methodType = c2sData.data.methodType -- 加速类型
|
|
local todayGain = player.gameData.todayGain
|
|
local cfgSValue = skynet.server.gameConfig:GetPlayerAllCfg( player , "SValue")
|
|
local data = {}
|
|
data.shopInfo = {}
|
|
|
|
--暂时不开发这个功能,检测宠物衣服数量
|
|
--local petClothesCount = skynet.server.bag:GetGoodsStat( player , dataType.GoodsType_PetClothes ) --获取玩家当前有的宠物服装
|
|
--local cfgAllClothes = skynet.server.gameConfig:GetPlayerAllCfg( player , "PetClothes")
|
|
--local cfgClothesCount = #cfgAllClothes --当前配置有的宠物服装数量
|
|
--if petClothesCount >= cfgClothesCount then
|
|
--s2cData.code = errorInfo.ErrorCode.AlreadyBuyPetClothes
|
|
|
|
local isSuc = false
|
|
if methodType == dataType.AccelerateType_Volute then
|
|
-- 消耗玩家蜗壳币
|
|
local refreshPrice = self:GetRefreshShopPrice( player , shopType )
|
|
local eventId = pb.enum("EnumMoneyChangeEventID","EventID_4")
|
|
if refreshPrice then
|
|
if player:MoneyChange(dataType.MoneyType_Volute, -refreshPrice, eventId) then
|
|
--使用蜗壳币进行刷新 修改相关数据
|
|
todayGain.updateUseCount = todayGain.updateUseCount + 1
|
|
if todayGain.updateUseCount == cfgSValue.triggerUpdatePack[ 1 ] then
|
|
skynet.server.store:TriggerPack(player , skynet.server.store.TriggerPack_Update)
|
|
end
|
|
isSuc = true
|
|
else
|
|
s2cData.code = errorInfo.ErrorCode.NoEnoughMoney
|
|
end
|
|
else
|
|
s2cData.code = errorInfo.ErrorCode.NoExistShopType
|
|
end
|
|
elseif methodType == dataType.AccelerateType_AccTicket and skynet.server.bag:GetGoodsCount(player, dataType.GoodsType_Prop, 4) > 0 then
|
|
--消耗玩家一个刷新券
|
|
skynet.server.bag:RemoveGoods(player , dataType.GoodsType_Prop, 4 , 1)
|
|
isSuc = true
|
|
elseif methodType == dataType.AccelerateType_ADTicket and skynet.server.ad:CanWatch(player, "AppPetClothesRefresh") and
|
|
skynet.server.ad:PayADTicket(player, "AppPetClothesRefresh") then
|
|
isSuc = true
|
|
elseif methodType == dataType.AccelerateType_WatchAD and skynet.server.ad:CanWatch(player, "AppPetClothesRefresh") then
|
|
skynet.server.ad:Update(player, "AppPetClothesRefresh")
|
|
isSuc = true
|
|
end
|
|
|
|
if isSuc then
|
|
self:RefreshShop( player , shopType )
|
|
data.shopInfo = self:GetShopData( player , shopType )
|
|
|
|
skynet.server.achieveTask:Modify( player , 34 , 1)
|
|
skynet.server.taskListEvent:Modify( player , 34 , 1)
|
|
end
|
|
|
|
s2cData.cmd = pb.enum("MsgType","CMD_S2C_PetClothesShopRefresh")
|
|
s2cData.data = assert(pb.encode("S2CPetClothesShopRefresh", data))
|
|
end
|
|
|
|
--商店购买
|
|
function PetClothesShop:Buy( player , c2sData , s2cData )
|
|
c2sData.data = assert(pb.decode("C2SPetClothesShopBuy", c2sData.data ))
|
|
local goodsId = c2sData.data.goodsId
|
|
local data = {}
|
|
local shopType = dataType.ShopType_PetClothes
|
|
local curShop = player.gameData.shop[ shopType ]
|
|
|
|
--特殊商品购买
|
|
if not goodsId then
|
|
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
|
|
else
|
|
--检查是商店中是否存在该商品
|
|
local isExistGoods = false
|
|
if goodsId == curShop.catClothes.id then --猫的服装
|
|
isExistGoods = true
|
|
elseif goodsId == curShop.catHat.id then --猫的帽子
|
|
isExistGoods = true
|
|
elseif goodsId == curShop.catDecoration.id then --猫的装饰品
|
|
isExistGoods = true
|
|
elseif goodsId == curShop.dogClothes.id then --狗的服装
|
|
isExistGoods = true
|
|
elseif goodsId == curShop.dogHat.id then --狗的帽子
|
|
isExistGoods = true
|
|
elseif goodsId == curShop.dogDecoration.id then --狗的装饰品
|
|
isExistGoods = true
|
|
end
|
|
|
|
--检查该商品是否存在背包
|
|
local isBagGoods = player:IsBuyGoods( dataType.GoodsType_PetClothes , goodsId )
|
|
if not isExistGoods then
|
|
s2cData.code = errorInfo.ErrorCode.NoGoodsID
|
|
elseif isBagGoods then
|
|
s2cData.code = errorInfo.ErrorCode.ExistGoodsInBag
|
|
else
|
|
--先扣钱,再发货
|
|
local moneyType , moneyCount = skynet.server.gameConfig:GetBuyMoney( player , "PetClothes" , goodsId ) --self:GetPetClothesPrice( goodsId )
|
|
local eventId = pb.enum("EnumMoneyChangeEventID","EventID_55" )
|
|
if moneyType and player:MoneyChange( moneyType , -moneyCount , eventId) then
|
|
if goodsId == curShop.catClothes.id then --猫的服装
|
|
player.gameData.shop[ shopType ].catClothes.buyCount = 1
|
|
elseif goodsId == curShop.catHat.id then --猫的帽子
|
|
player.gameData.shop[ shopType ].catHat.buyCount = 1
|
|
elseif goodsId == curShop.catDecoration.id then --猫的装饰品
|
|
player.gameData.shop[ shopType ].catDecoration.buyCount = 1
|
|
elseif goodsId == curShop.dogClothes.id then --狗的服装
|
|
player.gameData.shop[ shopType ].dogClothes.buyCount = 1
|
|
elseif goodsId == curShop.dogHat.id then --狗的帽子
|
|
player.gameData.shop[ shopType ].dogHat.buyCount = 1
|
|
elseif goodsId == curShop.dogDecoration.id then --狗的装饰品
|
|
player.gameData.shop[ shopType ].dogDecoration.buyCount = 1
|
|
end
|
|
skynet.server.bag:AddGoods( player , dataType.GoodsType_PetClothes , goodsId , 1)
|
|
|
|
skynet.server.taskListEvent:Modify( player , 5 , moneyCount )
|
|
skynet.server.taskListEvent:Modify( player , 6 , 1 )
|
|
log.debug(string.format("玩家 %d 宠物服装商店 购买成功 商品 %d 扣除四叶草 %d" , player.basicInfo.userID , goodsId , moneyCount))
|
|
else
|
|
s2cData.code = errorInfo.ErrorCode.NoEnoughMoney
|
|
log.warning(string.format("玩家 %d 宠物服装商店 购买失败 商品 %d " , player.basicInfo.userID , goodsId))
|
|
end
|
|
end
|
|
end
|
|
|
|
data.shopInfo = self:GetShopData(player , shopType )
|
|
s2cData.cmd = pb.enum("MsgType","CMD_S2C_PetClothesShopBuy")
|
|
s2cData.data = assert(pb.encode("S2CPetClothesShopBuy", data))
|
|
end
|
|
|
|
--刷新商店类型
|
|
function PetClothesShop:RefreshShop( player , shopType )
|
|
local goodsId= 0
|
|
|
|
local curShop = player.gameData.shop[ shopType ]
|
|
--猫的服装
|
|
goodsId = self:GetRandPetClothes( player , 1 , dataType.PetType_Cat , self.PetClothesType_Clothes )
|
|
self:SetShopData( player , shopType , dataType.PetType_Cat , self.PetClothesType_Clothes , goodsId[1] )
|
|
|
|
--猫的帽子
|
|
goodsId = self:GetRandPetClothes( player , 1 , dataType.PetType_Cat , self.PetClothesType_Hat )
|
|
self:SetShopData( player , shopType , dataType.PetType_Cat , self.PetClothesType_Hat , goodsId[1] )
|
|
|
|
--随机产生尾饰和面饰
|
|
local random = math.random(1,2)
|
|
|
|
if random ==1 then
|
|
--猫的尾饰
|
|
goodsId = self:GetRandPetClothes( player , 1 , dataType.PetType_Cat , self.PetClothesType_Decoration )
|
|
self:SetShopData( player , shopType , dataType.PetType_Cat , self.PetClothesType_Decoration , goodsId[1] )
|
|
else
|
|
--猫的面饰
|
|
goodsId = self:GetRandPetClothes( player , 1 , dataType.PetType_Cat , self.PetClothesType_TailDecoration )
|
|
self:SetShopData( player , shopType , dataType.PetType_Cat , self.PetClothesType_TailDecoration , goodsId[1] )
|
|
end
|
|
|
|
--狗的服装
|
|
goodsId = self:GetRandPetClothes( player , 1 , dataType.PetType_Dog , self.PetClothesType_Clothes )
|
|
self:SetShopData( player , shopType , dataType.PetType_Dog , self.PetClothesType_Clothes , goodsId[1] )
|
|
|
|
--狗的帽子
|
|
goodsId = self:GetRandPetClothes( player , 1 , dataType.PetType_Dog , self.PetClothesType_Hat )
|
|
self:SetShopData( player , shopType , dataType.PetType_Dog , self.PetClothesType_Hat , goodsId[1] )
|
|
|
|
--狗的装饰品
|
|
goodsId = self:GetRandPetClothes( player , 1 , dataType.PetType_Dog , self.PetClothesType_Decoration )
|
|
self:SetShopData( player , shopType , dataType.PetType_Dog , self.PetClothesType_Decoration , goodsId[1] )
|
|
|
|
--刷新时间
|
|
local timeCoefficient = skynet.server.store:GetTimeCoefficient(player , 2) --月卡权益 时间系数
|
|
curShop.nextRefreshTime =math.ceil(self:GetRefreshTime(player,shopType) * timeCoefficient) + skynet.GetTime()
|
|
end
|
|
|
|
--设置商店数据
|
|
function PetClothesShop:SetShopData( player , shopType , petType , clothesType , goodsId )
|
|
if not goodsId then
|
|
return
|
|
end
|
|
|
|
local buyCount = 0 --是否购买
|
|
if player:IsBuyGoods( dataType.GoodsType_PetClothes , goodsId ) then
|
|
buyCount = 1
|
|
end
|
|
|
|
local curShop = player.gameData.shop[ shopType ]
|
|
|
|
if dataType.PetType_Cat == petType then
|
|
if self.PetClothesType_Clothes == clothesType then
|
|
curShop.catClothes.id = goodsId
|
|
curShop.catClothes.buyCount = buyCount
|
|
log.debug(string.format("玩家 %d 宠物服饰商店 设置商店数据 最新猫的服装 ID %d 是否购买 %d" , player.basicInfo.userID , goodsId , buyCount ))
|
|
elseif self.PetClothesType_Hat == clothesType then
|
|
curShop.catHat.id = goodsId
|
|
curShop.catHat.buyCount = buyCount
|
|
log.debug(string.format("玩家 %d 宠物服饰商店 设置商店数据 最新猫的帽子 ID %d 是否购买 %d" , player.basicInfo.userID , goodsId , buyCount ))
|
|
elseif self.PetClothesType_Decoration == clothesType then
|
|
curShop.catDecoration.id = goodsId
|
|
curShop.catDecoration.buyCount = buyCount
|
|
log.debug(string.format("玩家 %d 宠物服饰商店 设置商店数据 最新猫的装饰品 ID %d 是否购买 %d" , player.basicInfo.userID , goodsId , buyCount ))
|
|
elseif self.PetClothesType_TailDecoration == clothesType then
|
|
curShop.catDecoration.id = goodsId
|
|
curShop.catDecoration.buyCount = buyCount
|
|
log.debug(string.format("玩家 %d 宠物服饰商店 设置商店数据 最新猫的面饰品 ID %d 是否购买 %d" , player.basicInfo.userID , goodsId , buyCount ))
|
|
end
|
|
table.insert( curShop.catHistoryGoods[ clothesType ] , { id = goodsId } )
|
|
elseif dataType.PetType_Dog == petType then
|
|
if self.PetClothesType_Clothes == clothesType then
|
|
curShop.dogClothes.id = goodsId
|
|
curShop.dogClothes.buyCount = buyCount
|
|
log.debug(string.format("玩家 %d 宠物服饰商店 设置商店数据 最新狗的服装 ID %d 是否购买 %d" , player.basicInfo.userID , goodsId , buyCount ))
|
|
elseif self.PetClothesType_Hat == clothesType then
|
|
curShop.dogHat.id = goodsId
|
|
curShop.dogHat.buyCount = buyCount
|
|
log.debug(string.format("玩家 %d 宠物服饰商店 设置商店数据 最新狗的帽子 ID %d 是否购买 %d" , player.basicInfo.userID , goodsId , buyCount ))
|
|
elseif self.PetClothesType_Decoration == clothesType then
|
|
curShop.dogDecoration.id = goodsId
|
|
curShop.dogDecoration.buyCount = buyCount
|
|
log.debug(string.format("玩家 %d 宠物服饰商店 设置商店数据 最新狗的装饰品 ID %d 是否购买 %d" , player.basicInfo.userID , goodsId , buyCount ))
|
|
end
|
|
table.insert( curShop.dogHistoryGoods[ clothesType ] , { id = goodsId } )
|
|
end
|
|
end
|
|
|
|
--获取商店数据
|
|
function PetClothesShop:GetShopData( player , shopType )
|
|
local shopInfo = {}
|
|
local curShop = player.gameData.shop[ shopType ]
|
|
|
|
shopInfo.catClothes = curShop.catClothes
|
|
shopInfo.catHat = curShop.catHat
|
|
shopInfo.catDecoration = curShop.catDecoration
|
|
shopInfo.dogClothes = curShop.dogClothes
|
|
shopInfo.dogHat = curShop.dogHat
|
|
shopInfo.dogDecoration = curShop.dogDecoration
|
|
shopInfo.nextRefreshTime = curShop.nextRefreshTime
|
|
return shopInfo
|
|
end
|
|
|
|
skynet.server.petClothesShop = PetClothesShop
|
|
return PetClothesShop |