HomeServer/lualib-src/Server-main/AllServer/GameServer/Shop/PlantShop.lua
2024-11-20 15:41:37 +08:00

160 lines
6.8 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 PlantShop = oo.class(shop)
PlantShop.OpType_StartSell = 1 --上架植物
PlantShop.OpType_StopSell = 2 --下架植物
PlantShop.OpType_GainClovers = 3 --获取四叶草
PlantShop.MaxShelfCount = 9 --最大厨窗数量
function PlantShop:Init()
end
--显示一种类型
function PlantShop:Show( player , c2sData , s2cData )
c2sData.data = assert(pb.decode("C2SPlantShopShow", c2sData.data ))
local shopType = dataType.ShopType_Plant
local data = {}
data.shopInfo = {}
--检查厨窗正在售卖的东西是否能卖出
local curShelf = nil
for i = 1, self.MaxShelfCount , 1 do
curShelf = player.gameData.shop[ shopType ].shopShelf[ i ]
if 0 ~= curShelf.goodsId and skynet.GetTime() >= curShelf.sellTime and not curShelf.isSell then
player.gameData.shop[ shopType ].shopShelf[ i ].isSell = true
player.gameData.shop[ shopType ].shopShelf[ i ].sellTime = 0
end
end
skynet.server.msgTips:Reduce( player , 12)
--获取最新的商店数据
data.shopInfo = self:GetShopData(player , shopType )
s2cData.cmd = pb.enum("MsgType","CMD_S2C_PlantShopShow")
s2cData.data = assert(pb.encode("S2CPlantShopShow", data))
end
--商店购买
function PlantShop:Manage( player , c2sData , s2cData )
c2sData.data = assert(pb.decode("C2SPlantShopManage", c2sData.data ))
local opType = c2sData.data.opType
local shelfId = c2sData.data.shelfId
local goodsId = c2sData.data.goodsId
local data = {}
local shopType = dataType.ShopType_Plant
local curShop = player.gameData.shop[ shopType ]
if self.OpType_StartSell == opType and shelfId and goodsId then --上架植物
if not self:IsValidShelfID( shelfId ) then
s2cData.code = errorInfo.ErrorCode.NoExistShelfID
elseif not self:IsValidPlantID( goodsId ) then
s2cData.code = errorInfo.ErrorCode.NoGoodsID
elseif not self:IsSellPlant( goodsId ) then
s2cData.code = errorInfo.ErrorCode.ForbidSell
else
--商品从背包移除
local cfgGoods = skynet.server.gameConfig:GetPlantCfg( goodsId )
if 0 == player.gameData.shop[ shopType ].shopShelf[ shelfId ].goodsId then
if cfgGoods and skynet.server.bag:RemoveGoods( player , dataType.GoodsType_Plant , goodsId , 1) then
player.gameData.shop[ shopType ].shopShelf[ shelfId ].goodsId = goodsId
player.gameData.shop[ shopType ].shopShelf[ shelfId ].sellTime = skynet.GetTime() + math.ceil(cfgGoods.coin * 0.3) * 60
log.info(string.format("玩家 %d 植物商店 上架植物成功 货架编号 %d 商品ID %d 售卖时间 %d" , player.basicInfo.userID , shelfId , goodsId , player.gameData.shop[ shopType ].shopShelf[ shelfId ].sellTime ))
end
end
end
elseif self.OpType_StopSell == opType and shelfId then --下架植物
if not self:IsValidShelfID( shelfId ) then
s2cData.code = errorInfo.ErrorCode.NoExistShelfID
elseif not player.gameData.shop[ shopType ].shopShelf[ shelfId ].isSell then
--商品放回背包
if 0 ~= player.gameData.shop[ shopType ].shopShelf[ shelfId ].goodsId then
skynet.server.bag:AddGoods( player , dataType.GoodsType_Plant , goodsId , 1)
player.gameData.shop[ shopType ].shopShelf[ shelfId ].goodsId = 0
player.gameData.shop[ shopType ].shopShelf[ shelfId ].isSell = false
player.gameData.shop[ shopType ].shopShelf[ shelfId ].sellTime = 0
log.info(string.format("玩家 %d 植物商店 下架植物成功 货架编号 %d 商品ID %d" , player.basicInfo.userID , shelfId , goodsId , 1))
end
end
elseif self.OpType_GainClovers == opType then --获取金币
if not self:IsValidShelfID( shelfId ) then
s2cData.code = errorInfo.ErrorCode.NoExistShelfID
elseif player.gameData.shop[ shopType ].shopShelf[ shelfId ].isSell then
local goodsId = player.gameData.shop[ shopType ].shopShelf[ shelfId ].goodsId
local cfgPlant = skynet.server.gameConfig.Plant
for k, v in pairs( cfgPlant ) do
if goodsId == v.id then
player.gameData.shop[ shopType ].sellCount = player.gameData.shop[ shopType ].sellCount + 1
--获取四叶草
player:MoneyChange( dataType.MoneyType_Coin , v.coin )
--还原当前货架的初始信息
player.gameData.shop[ shopType ].shopShelf[ shelfId ].goodsId = 0
player.gameData.shop[ shopType ].shopShelf[ shelfId ].isSell = false
player.gameData.shop[ shopType ].shopShelf[ shelfId ].sellTime = 0
skynet.server.levelTask:Modify( player , 47 , 1)
log.info(string.format("玩家 %d 植物商店 获取四叶草成功 货架编号 %d 商品ID %d 四叶草数量 %f" , player.basicInfo.userID , shelfId , goodsId , v.coin))
break
end
end
end
else
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
end
data.shopInfo = self:GetShopData(player , shopType )
s2cData.cmd = pb.enum("MsgType","CMD_S2C_PlantShopManage")
s2cData.data = assert(pb.encode("S2CPlantShopManage", data))
end
--是否为有效的货架ID
function PlantShop:IsValidShelfID( shelfId )
if shelfId >= 1 and shelfId <= 9 then
return true
end
return false
end
--是否为有效的植物ID
function PlantShop:IsValidPlantID( plantId )
local cfgPlant = skynet.server.gameConfig.Plant
for k, v in pairs( cfgPlant ) do
if plantId == v.id then
return true
end
end
return false
end
--是否能出售该植物
function PlantShop:IsSellPlant( plantId )
local cfgPlant = skynet.server.gameConfig.Plant
for k, v in pairs(cfgPlant) do
if plantId == v.id and 0 == #v.currencySell then
return false
end
end
return true
end
--获取商店数据
function PlantShop:GetShopData( player , shopType )
local shopInfo = {}
shopInfo.shopShelf = {}
local curShop = player.gameData.shop[ shopType ]
for k, v in pairs( curShop.shopShelf ) do
table.insert( shopInfo.shopShelf , { shelfId = v.shelfId , goodsId = v.goodsId , isSell = v.isSell } )
end
return shopInfo
end
skynet.server.plantShop = PlantShop
return PlantShop