HomeServer/lualib-src/Server-main/AllServer/GameServer/Shop/PetClothesShop.lua

234 lines
12 KiB
Lua
Raw Normal View History

2024-11-20 15:41:09 +08:00
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 PetClothesShop = oo.class(shop)
function PetClothesShop:Init()
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 ] = {}
player.gameData.shop[ shopType ].type = shopType
player.gameData.shop[ shopType ].catClothes = {} --猫的服装
player.gameData.shop[ shopType ].catHat = {} --猫的帽子
player.gameData.shop[ shopType ].catDecoration = {} --猫的装饰品
player.gameData.shop[ shopType ].dogClothes = {} --狗的服装
player.gameData.shop[ shopType ].dogHat = {} --狗的帽子
player.gameData.shop[ shopType ].dogDecoration = {} --狗的装饰品
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
--获取最新的商店数据
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 data = {}
data.shopInfo = {}
local refreshPrice = self:GetRefreshShopPrice( shopType )
if refreshPrice then
--扣除
if player:MoneyChange( dataType.MoneyType_Volute , -refreshPrice ) then
self:RefreshShop( player , shopType )
data.shopInfo = self:GetShopData( player , shopType )
else
s2cData.code = errorInfo.ErrorCode.NoEnoughMoney
end
else
s2cData.code = errorInfo.ErrorCode.NoExistShopType
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("PetClothes" , goodsId ) --self:GetPetClothesPrice( goodsId )
if moneyType and player:MoneyChange( moneyType , -moneyCount ) 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.passCheck:Modify( player , 5 , moneyCount )
skynet.server.passCheck:Modify( player , 6 , 1 )
log.info(string.format("玩家 %d 宠物服装商店 购买成功 商品 %d 扣除四叶草 %d" , player.basicInfo.userID , goodsId , moneyCount))
else
s2cData.code = errorInfo.ErrorCode.NoEnoughMoney
log.info(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 curGoods = {}
--猫的服装
table.insert( curGoods , { id = player.gameData.shop[ shopType ].catClothes.id })
goodsId = self:GetRandPetClothes( player , curGoods , 1 , dataType.PetType_Cat , self.PetClothesType_Clothes )
self:SetShopData( player , shopType , dataType.PetType_Cat , self.PetClothesType_Clothes , goodsId )
--猫的帽子
table.insert( curGoods , { id = player.gameData.shop[ shopType ].catHat.id })
goodsId = self:GetRandPetClothes( player , curGoods , 1 , dataType.PetType_Cat , self.PetClothesType_Hat )
self:SetShopData( player , shopType , dataType.PetType_Cat , self.PetClothesType_Hat , goodsId )
--猫的装饰品
table.insert( curGoods , { id = player.gameData.shop[ shopType ].catDecoration.id })
goodsId = self:GetRandPetClothes( player , curGoods , 1 , dataType.PetType_Cat , self.PetClothesType_Decoration )
self:SetShopData( player , shopType , dataType.PetType_Cat , self.PetClothesType_Decoration , goodsId )
--狗的服装
table.insert( curGoods , { id = player.gameData.shop[ shopType ].dogClothes.id })
goodsId = self:GetRandPetClothes( player , curGoods , 1 , dataType.PetType_Dog , self.PetClothesType_Clothes )
self:SetShopData( player , shopType , dataType.PetType_Dog , self.PetClothesType_Clothes , goodsId )
--狗的帽子
table.insert( curGoods , { id = player.gameData.shop[ shopType ].dogHat.id })
goodsId = self:GetRandPetClothes( player , curGoods , 1 , dataType.PetType_Dog , self.PetClothesType_Hat )
self:SetShopData( player , shopType , dataType.PetType_Dog , self.PetClothesType_Hat , goodsId )
--狗的装饰品
table.insert( curGoods , { id = player.gameData.shop[ shopType ].dogDecoration.id })
goodsId = self:GetRandPetClothes( player , curGoods , 1 , dataType.PetType_Dog , self.PetClothesType_Decoration )
self:SetShopData( player , shopType , dataType.PetType_Dog , self.PetClothesType_Decoration , goodsId )
--刷新时间
player.gameData.shop[ shopType ].nextRefreshTime = self:GetRefreshTime(shopType) + 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
if dataType.PetType_Cat == petType then
if self.PetClothesType_Clothes == clothesType then
player.gameData.shop[ shopType ].catClothes.id = goodsId
player.gameData.shop[ shopType ].catClothes.buyCount = buyCount
log.info(string.format("玩家 %d 宠物服饰商店 设置商店数据 最新猫的服装 ID %d 是否购买 %d" , player.basicInfo.userID , goodsId , buyCount ))
elseif self.PetClothesType_Hat == clothesType then
player.gameData.shop[ shopType ].catHat.id = goodsId
player.gameData.shop[ shopType ].catHat.buyCount = buyCount
log.info(string.format("玩家 %d 宠物服饰商店 设置商店数据 最新猫的帽子 ID %d 是否购买 %d" , player.basicInfo.userID , goodsId , buyCount ))
elseif self.PetClothesType_Decoration == clothesType then
player.gameData.shop[ shopType ].catDecoration.id = goodsId
player.gameData.shop[ shopType ].catDecoration.buyCount = buyCount
log.info(string.format("玩家 %d 宠物服饰商店 设置商店数据 最新猫的装饰品 ID %d 是否购买 %d" , player.basicInfo.userID , goodsId , buyCount ))
end
elseif dataType.PetType_Dog == petType then
if self.PetClothesType_Clothes == clothesType then
player.gameData.shop[ shopType ].dogClothes.id = goodsId
player.gameData.shop[ shopType ].dogClothes.buyCount = buyCount
log.info(string.format("玩家 %d 宠物服饰商店 设置商店数据 最新狗的服装 ID %d 是否购买 %d" , player.basicInfo.userID , goodsId , buyCount ))
elseif self.PetClothesType_Hat == clothesType then
player.gameData.shop[ shopType ].dogHat.id = goodsId
player.gameData.shop[ shopType ].dogHat.buyCount = buyCount
log.info(string.format("玩家 %d 宠物服饰商店 设置商店数据 最新狗的帽子 ID %d 是否购买 %d" , player.basicInfo.userID , goodsId , buyCount ))
elseif self.PetClothesType_Decoration == clothesType then
player.gameData.shop[ shopType ].dogDecoration.id = goodsId
player.gameData.shop[ shopType ].dogDecoration.buyCount = buyCount
log.info(string.format("玩家 %d 宠物服饰商店 设置商店数据 最新狗的装饰品 ID %d 是否购买 %d" , player.basicInfo.userID , goodsId , buyCount ))
end
end
end
--获取商店数据
function PetClothesShop:GetShopData( player , shopType )
local shopInfo = {}
shopInfo.catClothes = player.gameData.shop[ shopType ].catClothes
shopInfo.catHat = player.gameData.shop[ shopType ].catHat
shopInfo.catDecoration = player.gameData.shop[ shopType ].catDecoration
shopInfo.dogClothes = player.gameData.shop[ shopType ].dogClothes
shopInfo.dogHat = player.gameData.shop[ shopType ].dogHat
shopInfo.dogDecoration = player.gameData.shop[ shopType ].dogDecoration
shopInfo.nextRefreshTime = player.gameData.shop[ shopType ].nextRefreshTime
return shopInfo
end
skynet.server.petClothesShop = PetClothesShop
return PetClothesShop