205 lines
8.9 KiB
Lua
205 lines
8.9 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 ClothesShop = oo.class(shop)
|
||
|
|
|
||
|
|
function ClothesShop:Init()
|
||
|
|
|
||
|
|
end
|
||
|
|
|
||
|
|
--显示一种类型
|
||
|
|
function ClothesShop:Show( player , c2sData , s2cData )
|
||
|
|
c2sData.data = assert(pb.decode("C2SClothesShopShow", c2sData.data ))
|
||
|
|
local shopType = dataType.ShopType_Clothes
|
||
|
|
|
||
|
|
local data = {}
|
||
|
|
data.shopInfo = {}
|
||
|
|
|
||
|
|
--[[
|
||
|
|
if not player.gameData.shop[ shopType ].buyGoods then
|
||
|
|
player.gameData.shop[ shopType ].buyGoods = {}
|
||
|
|
end
|
||
|
|
]]
|
||
|
|
--时间超过了,刷新新的一批
|
||
|
|
if skynet.GetTime() >= player.gameData.shop[ shopType ].nextRefreshTime then
|
||
|
|
self:RefreshShop( player , shopType )
|
||
|
|
end
|
||
|
|
|
||
|
|
skynet.server.msgTips:Reduce( player , 9)
|
||
|
|
|
||
|
|
--获取最新的商店数据
|
||
|
|
data.shopInfo = self:GetShopData(player , shopType )
|
||
|
|
s2cData.cmd = pb.enum("MsgType","CMD_S2C_ClothesShopShow")
|
||
|
|
s2cData.data = assert(pb.encode("S2CClothesShopShow", data))
|
||
|
|
end
|
||
|
|
|
||
|
|
--刷新
|
||
|
|
function ClothesShop:Refresh( player , c2sData , s2cData )
|
||
|
|
c2sData.data = assert(pb.decode("C2SClothesShopRefresh", c2sData.data ))
|
||
|
|
local shopType = dataType.ShopType_Clothes
|
||
|
|
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 )
|
||
|
|
skynet.server.achieveTask:Modify( player , 34 , 1)
|
||
|
|
else
|
||
|
|
s2cData.code = errorInfo.ErrorCode.NoEnoughMoney
|
||
|
|
end
|
||
|
|
else
|
||
|
|
s2cData.code = errorInfo.ErrorCode.NoExistShopType
|
||
|
|
end
|
||
|
|
|
||
|
|
s2cData.cmd = pb.enum("MsgType","CMD_S2C_ClothesShopRefresh")
|
||
|
|
s2cData.data = assert(pb.encode("S2CClothesShopRefresh", data))
|
||
|
|
end
|
||
|
|
|
||
|
|
--商店购买
|
||
|
|
function ClothesShop:Buy( player , c2sData , s2cData )
|
||
|
|
c2sData.data = assert(pb.decode("C2SClothesShopBuy", c2sData.data ))
|
||
|
|
local goodsId = c2sData.data.goodsId
|
||
|
|
local data = {}
|
||
|
|
local shopType = dataType.ShopType_Clothes
|
||
|
|
local curShop = player.gameData.shop[ shopType ]
|
||
|
|
|
||
|
|
--特殊商品购买
|
||
|
|
if not goodsId then
|
||
|
|
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
|
||
|
|
else
|
||
|
|
--检查是商店中是否存在该商品
|
||
|
|
local isExistGoods = false
|
||
|
|
if goodsId == curShop.headWear.id then --头饰
|
||
|
|
isExistGoods = true
|
||
|
|
elseif goodsId == curShop.clothes.id then --衣服
|
||
|
|
isExistGoods = true
|
||
|
|
elseif goodsId == curShop.trousers.id then --裤子
|
||
|
|
isExistGoods = true
|
||
|
|
elseif goodsId == curShop.shoes.id then --鞋子
|
||
|
|
isExistGoods = true
|
||
|
|
end
|
||
|
|
|
||
|
|
--检查该商品是否存在背包
|
||
|
|
local isBagGoods = player:IsBuyGoods( dataType.GoodsType_Clothes , 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("Clothes" , goodsId ) --self:GetClothesPrice( goodsId )
|
||
|
|
if moneyType and player:MoneyChange( moneyType , -moneyCount ) then
|
||
|
|
if goodsId == curShop.headWear.id then --头饰
|
||
|
|
player.gameData.shop[ shopType ].headWear.buyCount = 1
|
||
|
|
elseif goodsId == curShop.clothes.id then --衣服
|
||
|
|
player.gameData.shop[ shopType ].clothes.buyCount = 1
|
||
|
|
elseif goodsId == curShop.trousers.id then --裤子
|
||
|
|
player.gameData.shop[ shopType ].trousers.buyCount = 1
|
||
|
|
elseif goodsId == curShop.shoes.id then --鞋子
|
||
|
|
player.gameData.shop[ shopType ].shoes.buyCount = 1
|
||
|
|
end
|
||
|
|
table.insert( player.gameData.shop[ shopType ].buyGoods , goodsId )
|
||
|
|
skynet.server.bag:AddGoods( player , dataType.GoodsType_Clothes , goodsId , 1 )
|
||
|
|
|
||
|
|
skynet.server.achieveTask:Modify( player , 14 , 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_ClothesShopBuy")
|
||
|
|
s2cData.data = assert(pb.encode("S2CClothesShopBuy", data))
|
||
|
|
end
|
||
|
|
|
||
|
|
--刷新商店类型
|
||
|
|
function ClothesShop:RefreshShop( player , shopType )
|
||
|
|
local goodsId= 0
|
||
|
|
local curGoods = {}
|
||
|
|
|
||
|
|
--头饰
|
||
|
|
curGoods = {}
|
||
|
|
table.insert( curGoods , { id = player.gameData.shop[ shopType ].headWear.id })
|
||
|
|
goodsId = self:GetRandClothes( player , curGoods , 1 , dataType.ClothesType_HeadWear )
|
||
|
|
self:SetShopData( player , shopType , dataType.ClothesType_HeadWear , goodsId )
|
||
|
|
|
||
|
|
--衣服
|
||
|
|
curGoods = {}
|
||
|
|
table.insert( curGoods , { id = player.gameData.shop[ shopType ].clothes.id })
|
||
|
|
goodsId = self:GetRandClothes( player , curGoods , 1 , dataType.ClothesType_Clothes )
|
||
|
|
self:SetShopData( player , shopType , dataType.ClothesType_Clothes , goodsId )
|
||
|
|
|
||
|
|
--裤子
|
||
|
|
curGoods = {}
|
||
|
|
table.insert( curGoods , { id = player.gameData.shop[ shopType ].trousers.id })
|
||
|
|
goodsId = self:GetRandClothes( player , curGoods , 1 , dataType.ClothesType_Trousers )
|
||
|
|
self:SetShopData( player , shopType , dataType.ClothesType_Trousers , goodsId )
|
||
|
|
|
||
|
|
--鞋子
|
||
|
|
curGoods = {}
|
||
|
|
table.insert( curGoods , { id = player.gameData.shop[ shopType ].shoes.id })
|
||
|
|
goodsId = self:GetRandClothes( player , curGoods , 1 , dataType.ClothesType_Shoes )
|
||
|
|
self:SetShopData( player , shopType , dataType.ClothesType_Shoes , goodsId )
|
||
|
|
|
||
|
|
--本次刷新已经购买的商品
|
||
|
|
player.gameData.shop[ shopType ].buyGoods = {}
|
||
|
|
--刷新时间
|
||
|
|
player.gameData.shop[ shopType ].nextRefreshTime = self:GetRefreshTime(shopType) + skynet.GetTime()
|
||
|
|
end
|
||
|
|
|
||
|
|
--设置商店数据
|
||
|
|
function ClothesShop:SetShopData( player , shopType , clothesType , goodsId )
|
||
|
|
if nil == goodsId then
|
||
|
|
return
|
||
|
|
end
|
||
|
|
|
||
|
|
local buCount = 0 --是否购买
|
||
|
|
if player:IsBuyGoods( dataType.GoodsType_Clothes , goodsId ) then
|
||
|
|
buCount = 1
|
||
|
|
end
|
||
|
|
|
||
|
|
if dataType.ClothesType_HeadWear == clothesType then
|
||
|
|
player.gameData.shop[ shopType ].headWear.id = goodsId
|
||
|
|
player.gameData.shop[ shopType ].headWear.buyCount = buCount
|
||
|
|
log.info(string.format("玩家 %d 服饰商店 设置商店数据 最新头饰 ID %d 是否购买 %d" , player.basicInfo.userID , goodsId , buCount ))
|
||
|
|
elseif dataType.ClothesType_Clothes == clothesType then
|
||
|
|
player.gameData.shop[ shopType ].clothes.id = goodsId
|
||
|
|
player.gameData.shop[ shopType ].clothes.buyCount = buCount
|
||
|
|
log.info(string.format("玩家 %d 服饰商店 设置商店数据 最新衣服 ID %d 是否购买 %d" , player.basicInfo.userID , goodsId , buCount ))
|
||
|
|
elseif dataType.ClothesType_Trousers == clothesType then
|
||
|
|
player.gameData.shop[ shopType ].trousers.id = goodsId
|
||
|
|
player.gameData.shop[ shopType ].trousers.buyCount = buCount
|
||
|
|
log.info(string.format("玩家 %d 服饰商店 设置商店数据 最新裤子 ID %d 是否购买 %d" , player.basicInfo.userID , goodsId , buCount ))
|
||
|
|
elseif dataType.ClothesType_Shoes == clothesType then
|
||
|
|
player.gameData.shop[ shopType ].shoes.id = goodsId
|
||
|
|
player.gameData.shop[ shopType ].shoes.buyCount = buCount
|
||
|
|
log.info(string.format("玩家 %d 服饰商店 设置商店数据 最新鞋子 ID %d 是否购买 %d" , player.basicInfo.userID , goodsId , buCount ))
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
--获取商店数据
|
||
|
|
function ClothesShop:GetShopData( player , shopType )
|
||
|
|
local shopInfo = {}
|
||
|
|
shopInfo.headWear = player.gameData.shop[ shopType ].headWear
|
||
|
|
shopInfo.clothes = player.gameData.shop[ shopType ].clothes
|
||
|
|
shopInfo.trousers = player.gameData.shop[ shopType ].trousers
|
||
|
|
shopInfo.shoes = player.gameData.shop[ shopType ].shoes
|
||
|
|
shopInfo.buyGoods = player.gameData.shop[ shopType ].buyGoods
|
||
|
|
shopInfo.nextRefreshTime = player.gameData.shop[ shopType ].nextRefreshTime
|
||
|
|
return shopInfo
|
||
|
|
end
|
||
|
|
|
||
|
|
skynet.server.clothesShop = ClothesShop
|
||
|
|
return ClothesShop
|