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

363 lines
18 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 FlowerShop = oo.class(shop)
FlowerShop.FlowerpotStatus_NoBuy = 1 --未购买
FlowerShop.FlowerpotStatus_AlreadyBuy = 2 --已购买
FlowerShop.FlowerpotStatus_Lock = 3 --未解锁
FlowerShop.OrderStatus_Require = 1 --需要货物
FlowerShop.OrderStatus_Purchase = 2 --进货中
FlowerShop.shopType = dataType.ShopType_Flower
--初始化数据
function FlowerShop:RefreshShop( player , shopType )
if not player:IsUnlockSystem( dataType.UnlockSystem_Map ) then
return
end
local curShop = player.gameData.shop[ shopType ]
if not skynet.server.common:IsSameDay( curShop.nextRefreshTime , skynet.GetTime()) then
curShop.nextRefreshTime = skynet.GetTime()
local level = player.gameData.level
--获取福带种子
local randGoodsList = self:GetRandSeed( skynet.server.shop.FlowerpotType_Flower , {} , level , 1 )
curShop.bagSeedId = randGoodsList[1]
log.info(string.format("玩家 %d 花店 刷新福袋种子 商品ID %d " , player.userId , curShop.bagSeedId))
--获取家具
randGoodsList = self:GetRandFurniture( {} , level , 1 , skynet.server.generalShop.FurnitureBuyType_Flower )
curShop.furnitureId = randGoodsList[1]
log.info(string.format("玩家 %d 花店 刷新家具 商品ID %d " , player.userId , curShop.furnitureId))
--获取回收植物
local randQuality = math.random( skynet.server.shop.FlowerpotLevel_Common , skynet.server.shop.FlowerpotLevel_Rare)
randGoodsList = self:GetRandPlant( skynet.server.shop.FlowerpotType_Flower , randQuality , 1 )
curShop.recycleId = randGoodsList[1]
log.info(string.format("玩家 %d 花店 刷新回收植物 商品ID %d " , player.userId , curShop.recycleId))
--货架种子
local existGoods = {}
table.insert( existGoods , { id = curShop.bagSeedId , buyCount =0 })
randGoodsList = self:GetRandSeed( skynet.server.shop.FlowerpotType_Flower , existGoods , level , 3 )
curShop.shelfSeeds = {}
for k, v in pairs( randGoodsList ) do
table.insert( curShop.shelfSeeds , { id = v} )
log.info(string.format("玩家 %d 花店 刷新货架种子 商品ID %d " , player.userId , v))
end
--刷新货架花盆
self:RefreshFlowerpot(player)
--货架家具
existGoods = {}
table.insert( existGoods , { id = curShop.furnitureId , buyCount =0 })
curShop.shelfFurniture = {}
--先获取摆件
randGoodsList = self:GetRandCinnabar( existGoods , level , 1 , skynet.server.generalShop.FurnitureBuyType_Flower )
for k, v in pairs( randGoodsList ) do
table.insert( curShop.shelfFurniture , { id = v } )
log.info(string.format("玩家 %d 花店 刷新货架摆件 商品ID %d " , player.userId , v))
end
--再获取家具
randGoodsList =self:GetRandFurniture( existGoods , level , 1 , skynet.server.generalShop.FurnitureBuyType_Flower )
for k, v in pairs( randGoodsList ) do
table.insert( curShop.shelfFurniture , { id = v } )
log.info(string.format("玩家 %d 花店 刷新货架家具 商品ID %d " , player.userId , v))
end
end
end
--花店展示
function FlowerShop:Show( player , c2sData , s2cData )
c2sData.data = assert(pb.decode("C2SFlowerShopShow", c2sData.data ))
local data = {}
local shopType = dataType.ShopType_Flower
local curShop = player.gameData.shop[ shopType ]
data.bagSeed = curShop.bagSeedId
data.furId = curShop.furnitureId
data.recycleId = curShop.recycleId
data.todayBagSeedBuyTimes = player.gameData.todayGain.luckyBagBuyCount
data.currentHave = skynet.server.bag:GetGoodsCount( player , dataType.GoodsType_Plant , curShop.recycleId )
skynet.server.levelTask:Modify( player , 27 , 1 )
s2cData.cmd = pb.enum("MsgType","CMD_S2C_FlowerShopShow")
s2cData.data = assert(pb.encode("S2CFlowerShopShow", data))
end
--花店购买
function FlowerShop:Buy( player , c2sData , s2cData )
c2sData.data = assert(pb.decode("C2SFlowerShopBuy", c2sData.data ))
local data = {}
local goodsType = c2sData.data.goodsType
local buyGoods = c2sData.data.buyGoods
if not goodsType or not buyGoods then
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
else
local shopType = dataType.ShopType_Flower
local curShop = player.gameData.shop[ shopType ]
local goodsId = buyGoods.id or 0
local goodsCount = buyGoods.count or 0
if dataType.GoodsType_Furniture == goodsType then --家具
if goodsId ~= curShop.furnitureId then
s2cData.code = errorInfo.ErrorCode.NoGoodsID
else
local moneyType , moneyCount = skynet.server.gameConfig:GetBuyMoney("Furniture" , goodsId )
moneyCount = moneyCount * goodsCount
if moneyType and player:MoneyChange( moneyType , -moneyCount ) then
skynet.server.bag:AddGoods( player , dataType.GoodsType_Furniture , goodsId , goodsCount )
data.buyGoods = { id = goodsId , count = goodsCount }
skynet.server.passCheck:Modify( player , 18 , moneyCount )
curShop.consumeCoin = curShop.consumeCoin + moneyCount
log.info(string.format("玩家 %d 花店 展示购买家具 商品ID %d 数量 %d 货币类型 %d 扣除金额 %d" , player.userId , goodsId , goodsCount , moneyType , moneyCount ))
else
s2cData.code = errorInfo.ErrorCode.NoEnoughMoney
end
end
elseif dataType.GoodsType_Seed == goodsType then --福袋种子
local cfgSValue = skynet.server.gameConfig.SValue
if goodsId ~= curShop.bagSeedId then
s2cData.code = errorInfo.ErrorCode.NoGoodsID
elseif player.gameData.todayGain.luckyBagBuyCount >= cfgSValue.FlowerShopParas[ 3 ] then
s2cData.code = errorInfo.ErrorCode.TodayMaxLimit
else
goodsCount = cfgSValue.FlowerShopParas[1] --福袋种子默认购买3个
local moneyType , moneyCount = skynet.server.gameConfig:GetBuyMoney("Seed" , goodsId )
moneyCount = moneyCount * goodsCount * cfgSValue.FlowerShopParas[2] --种子单价*福袋种子数量*福袋种子折扣
if moneyType and player:MoneyChange( moneyType , - moneyCount ) then
player.gameData.todayGain.luckyBagBuyCount = player.gameData.todayGain.luckyBagBuyCount + 1
skynet.server.bag:AddGoods( player , dataType.GoodsType_Seed , goodsId , goodsCount )
data.buyGoods = { id = goodsId , count = goodsCount }
skynet.server.passCheck:Modify( player , 18 , moneyCount )
curShop.consumeCoin = curShop.consumeCoin + moneyCount
log.info(string.format("玩家 %d 花店 展示购买种子 商品ID %d 数量 %d 货币类型 %d 扣除金额 %d" , player.userId , goodsId , goodsCount , moneyType , moneyCount * goodsCount ))
else
s2cData.code = errorInfo.ErrorCode.NoEnoughMoney
end
end
end
skynet.server.levelTask:Modify( player , 52 , 1 )
skynet.server.passCheck:Modify( player , 17 , 1 )
data.goodsType = goodsType
data.todayBagSeedBuyTimes = player.gameData.todayGain.luckyBagBuyCount
end
s2cData.cmd = pb.enum("MsgType","CMD_S2C_FlowerShopBuy")
s2cData.data = assert(pb.encode("S2CFlowerShopBuy", data))
end
--高价回收区售卖植物
function FlowerShop:Recycle( player , c2sData , s2cData )
c2sData.data = assert(pb.decode("C2SFlowerShopRecycle", c2sData.data ))
local shopType = dataType.ShopType_Flower
local curShop = player.gameData.shop[ shopType ]
local soldPlant = c2sData.data.soldPlant
local goodsId = soldPlant.id
local buyCount = math.abs(soldPlant.buyCount)
local data = {}
if not soldPlant or not goodsId or not buyCount or goodsId <= 0 or buyCount < 0 then
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
else
local isExist = false
local bagCount = skynet.server.bag:GetGoodsCount( player , dataType.GoodsType_Plant , goodsId )
if goodsId == curShop.recycleId then
isExist = true
end
if not isExist then
s2cData.code = errorInfo.ErrorCode.NoGoodsID
elseif bagCount < buyCount then
s2cData.code = errorInfo.ErrorCode.NoEnoughGoods
else
local isSuc = skynet.server.bag:RemoveGoods( player , dataType.GoodsType_Plant , goodsId , buyCount )
if isSuc then
--成功删除
local moneyType , moneyCount = skynet.server.gameConfig:GetBuyMoney("Plant" , goodsId ) --self:GetPlantPrice( goodsId )
moneyCount = moneyCount * buyCount
if moneyType and player:MoneyChange( moneyType , moneyCount ) then
data.soldPlant = { id = goodsId , buyCount = buyCount }
data.currentHave = skynet.server.bag:GetGoodsCount( player , dataType.GoodsType_Plant , curShop.recycleId )
skynet.server.levelTask:Modify( player , dataType.GeneralTaskType_FlowerShopRecyclePlant , 1 )
skynet.server.achieveTask:Modify( player , 19 , 1)
log.info(string.format("玩家 %d 花店 展示回收植物 商品ID %d 数量 %d 货币类型 %d 增加金额 %d" , player.userId , goodsId , buyCount , moneyType , moneyCount * buyCount ))
end
else
s2cData.code = errorInfo.ErrorCode.NoEnoughGoods
log.info(string.format("玩家 %d 花店 展示回收植物 商品ID %d 数量 %d 删除失败" , player.userId , goodsId , buyCount))
end
end
end
s2cData.cmd = pb.enum("MsgType","CMD_S2C_FlowerShopRecycle")
s2cData.data = assert(pb.encode("S2CFlowerShopRecycle", data))
end
--花店货架展示
function FlowerShop:ShelfShow( player , c2sData , s2cData )
c2sData.data = assert(pb.decode("C2SFlowerShelfShow", c2sData.data ))
local data = {}
local shelf = player.gameData.shop[ self.shopType ]
data.shelfSeeds = {}
for k, v in pairs( shelf.shelfSeeds ) do
table.insert( data.shelfSeeds , v.id )
end
--刷新一次花盆能不能买
self:RefreshFlowerpot( player )
data.shelfPots = shelf.shelfFlowerpot
data.shelfFurs = {}
for k, v in pairs( shelf.shelfFurniture ) do
table.insert( data.shelfFurs , v.id )
end
skynet.server.levelTask:Modify( player , 27 , 1 )
data.plantCount = player.gameData.aquaticPlantCount
data.nextRefreshTime = 0
s2cData.cmd = pb.enum("MsgType","CMD_S2C_FlowerShelfShow")
s2cData.data = assert(pb.encode("S2CFlowerShelfShow", data))
end
--花店货架购买
function FlowerShop:ShelfBuy( player , c2sData , s2cData )
c2sData.data = assert(pb.decode("C2SFlowerShelfBuy", c2sData.data ))
local data = {}
local goodsType = c2sData.data.goodsType
local buyGoods = c2sData.data.buyGoods
if not goodsType or not buyGoods then
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
else
local shopType = dataType.ShopType_Flower
local curShop = player.gameData.shop[ shopType ]
local goodsId = buyGoods.id or 0
buyGoods.buyCount = buyGoods.buyCount or 1
if buyGoods.buyCount <= 0 then
buyGoods.buyCount = 1
end
local goodsCount = buyGoods.buyCount
goodsCount = math.abs(goodsCount)
data.goodsType = goodsType
if dataType.GoodsType_Furniture == goodsType then --家具
if not self:IsExistGoodsId( goodsId , curShop.shelfFurniture ) then
s2cData.code = errorInfo.ErrorCode.NoGoodsID
else
local moneyType , moneyCount = skynet.server.gameConfig:GetBuyMoney("Furniture" , goodsId )
moneyCount = moneyCount * goodsCount
if moneyType and player:MoneyChange( moneyType , -moneyCount) then
skynet.server.bag:AddGoods( player , dataType.GoodsType_Furniture , goodsId , goodsCount )
data.buyGoods = { id = goodsId , count = goodsCount }
skynet.server.passCheck:Modify( player , 18 , moneyCount )
curShop.consumeCoin = curShop.consumeCoin + moneyCount
local exp = skynet.server.bag:CalcExp( dataType.GoodsType_Furniture , goodsId , goodsCount )
player:AddExp( exp )
log.info(string.format("玩家 %d 花店 货架购买家具 商品ID %d 数量 %d 货币类型 %d 扣除金额 %d" , player.userId , goodsId , goodsCount , moneyType , moneyCount ))
else
s2cData.code = errorInfo.ErrorCode.NoEnoughMoney
end
end
elseif dataType.GoodsType_Flowerpot == goodsType then --花盆
local goodsIndex= 0 --花盆数据索引
local isBuy = false
for k, v in pairs(curShop.shelfFlowerpot ) do
if goodsId == v.id and v.buyCount > 0 then
isBuy = true
end
end
if not self:IsExistGoodsId( goodsId , curShop.shelfFlowerpot ) then
s2cData.code = errorInfo.ErrorCode.NoGoodsID
elseif isBuy then
s2cData.code = errorInfo.ErrorCode.AlreadyGet
else
local moneyType , moneyCount = skynet.server.gameConfig:GetBuyMoney("Flowerpot" , goodsId ) --self:GetFlowerpotPrice( goodsId )
moneyCount = moneyCount * goodsCount
if moneyType and player:MoneyChange( moneyType , -moneyCount ) then
skynet.server.bag:AddGoods( player , dataType.GoodsType_Flowerpot , goodsId , goodsCount )
for k, v in pairs(curShop.shelfFlowerpot ) do
if goodsId == v.id then
v.buyCount = v.buyCount + 1
v.status = self.FlowerpotStatus_AlreadyBuy
end
end
data.buyGoods = { id = goodsId , count = goodsCount }
skynet.server.passCheck:Modify( player , 18 , moneyCount )
curShop.consumeCoin = curShop.consumeCoin + moneyCount
log.info(string.format("玩家 %d 花店 货架购买花盆 商品ID %d 数量 %d 货币类型 %d 扣除金额 %d" , player.userId , goodsId , goodsCount , moneyType , moneyCount ))
else
s2cData.code = errorInfo.ErrorCode.NoEnoughMoney
end
end
elseif dataType.GoodsType_Seed == goodsType then --种子
local cfgSValue = skynet.server.gameConfig.SValue
if not self:IsExistGoodsId( goodsId , curShop.shelfSeeds ) then
s2cData.code = errorInfo.ErrorCode.NoGoodsID
else
local moneyType , moneyCount = skynet.server.gameConfig:GetBuyMoney("Seed" , goodsId ) --self:GetSeedPrice( goodsId )
moneyCount = moneyCount * goodsCount
if moneyType and player:MoneyChange( moneyType , -moneyCount ) then
skynet.server.bag:AddGoods( player , dataType.GoodsType_Seed , goodsId , goodsCount )
data.buyGoods = { id = goodsId , count = goodsCount }
skynet.server.passCheck:Modify( player , 18 , moneyCount )
curShop.consumeCoin = curShop.consumeCoin + moneyCount
log.info(string.format("玩家 %d 花店 货架购买种子 商品ID %d 数量 %d 货币类型 %d 扣除金额 %d" , player.userId , goodsId , goodsCount , moneyType , moneyCount ))
else
s2cData.code = errorInfo.ErrorCode.NoEnoughMoney
end
end
end
skynet.server.levelTask:Modify( player , 52 , 1 )
skynet.server.passCheck:Modify( player , 17 , 1 )
end
s2cData.cmd = pb.enum("MsgType","CMD_S2C_FlowerShelfBuy")
s2cData.data = assert(pb.encode("S2CFlowerShelfBuy", data))
end
--刷新货架花盆
function FlowerShop:RefreshFlowerpot( player )
local curShop = player.gameData.shop[ self.shopType ]
--货架花盆
local unlockCount = player.gameData.aquaticPlantCount --水生植物数量
local flowerpotList = self:GetUnLockFlowerpot( skynet.server.shop.FlowerpotType_Flower , unlockCount )
curShop.shelfFlowerpot = {}
for k1, v1 in pairs( flowerpotList ) do
local isExistFlowerpot = false --是否存在花盆
for k2, v2 in pairs( curShop.shelfFlowerpot ) do
if v1 == v2.id then
isExistFlowerpot = true
break
end
end
if not isExistFlowerpot then
--玩家可以购买该花盆
local buyCount = 0
if player:IsBuyGoods( dataType.GoodsType_Flowerpot , v1 ) then
buyCount = 1
end
table.insert( curShop.shelfFlowerpot , { id = v1 , buyCount = buyCount } )
log.info(string.format("玩家 %d 花店 刷新货架花盆 商品ID %d " , player.userId , v1))
end
end
end
skynet.server.flowerShop = FlowerShop
return FlowerShop