HomeServer/lualib-src/Server-main/AllServer/GameServer/Backpack.lua

271 lines
9.0 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 Backpack = oo.class()
Backpack.BackpackType_Prop = 1 --道具类型
Backpack.BackpackType_Goods = 2 --物资类型
Backpack.BackpackType_End = 3
--背包展示
function Backpack:Show( player , c2sData , s2cData )
c2sData.data = assert(pb.decode("C2SBackpackShow", c2sData.data ))
local data = {}
local type = c2sData.data.type
if not type or type < self.BackpackType_Prop or type >= dataType.BackpackType_End then
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
else
data.type = type
data.backpackInfos = {}
local allItem = {}
if self.BackpackType_Prop == type then
allItem = player.gameData.Backpack.prop
elseif self.BackpackType_Goods == type then
allItem = player.gameData.Backpack.goods
end
--根据商品类型来取数据
for k, v in pairs( allItem ) do
table.insert( data.backpackInfos , { id = v.id , count = v.count , getTime = v.getTime , isNew = v.isNew })
end
end
s2cData.cmd = pb.enum("MsgType","CMD_S2C_BackpackShow")
s2cData.data = assert(pb.encode("S2CBackpackShow", data))
end
--背包点击
function Backpack:Click( player , c2sData , s2cData )
c2sData.data = assert(pb.decode("C2SBackpackClick", c2sData.data ))
local data = {}
local id = c2sData.data.id
if not id then
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
else
data.id = id
local isExist = false --是否已经处理过了
for k, v in pairs( player.gameData.Backpack.prop ) do
if id == v.id then
v.isNew = false
isExist = true
end
end
if not isExist then
for k, v in pairs( player.gameData.Backpack.goods ) do
if id == v.id then
v.isNew = false
isExist = true
end
end
end
end
s2cData.cmd = pb.enum("MsgType","CMD_S2C_BackpackClick")
s2cData.data = assert(pb.encode("S2CBackpackClick", data))
end
--添加物品
function Backpack:AddGoods( player , backType , id , count )
--检查各参数
if not player or not backType or not backType or not count then
log.info("无法添加物品到背包,请求参数错误", player , backType , id, count)
return false
end
--检查物品的合法性
if not self:IsValidGoodsType( backType ) then
log.info("无法添加物品到背包,商品类型错误", backType)
return false
end
local data = {}
data.goodsInfo = {}
local userId = player.basicInfo.userID
local gainTime = skynet.GetTime()
local goodInfo = {}
local lastCount = 0 --最新数量
if self:IsExistGoods( backType , id ) then
--存在家具
for k, v in pairs( player.gameData.Backpack ) do
if goodsType == v.type and id == v.id then
v.count = v.count + count
v.gainTime = gainTime
goodInfo = { type = v.type , id = v.id , count = v.count , gainTime = v.gainTime }
break
end
end
else
--不存在家具
goodInfo = { type = goodsType , id = id , count = count , gainTime = gainTime }
table.insert( player.gameData.Backpack , goodInfo )
end
skynet.server.illustration:Add( player , goodsType , id )
skynet.server.playerRecord:Add( userId , dataType.RecordType_100 , goodInfo.type , goodInfo.id , goodInfo.count )
table.insert( data.goodsInfo , goodInfo )
log.info(string.format("玩家 %d 背包更新 商品类型 %d 商品ID %d 数量 %d" , userId , goodInfo.type , goodInfo.id , goodInfo.count))
skynet.server.gameServer:SendMsgToUser( player.userId , "CMD_S2C_BackpackUpdate" , data )
return true
end
--删除背包中的道具
function Backpack:RemoveGoods( player , goodsType , id , count )
--检查各参数
if not player or not goodsType or not id or not count then
log.info("无法删除物品从背包,请求参数错误", player,goodsType , id, count)
return false
end
--检查物品的合法性
if not self:IsValidGoodsType( goodsType ) then
log.info("无法删除物品从背包,商品类型错误", goodsType)
return false
end
local data = {}
data.goodsInfo = {}
local userId = player.basicInfo.userID
local goodInfo = {}
local lastCount = 0 --最新数量
local isSuc = false
for k, v in pairs( player.gameData.Backpack ) do
if goodsType == v.type and id == v.id then
--背包减去数量
v.count = v.count - count
goodInfo = { goodsType = goodsType , id = v.id , count = v.count, gainTime = v.gainTime }
if v.count <= 0 then
table.remove( player.gameData.Backpack , k)
end
log.info(string.format("玩家 %d 背包删除 商品类型 %d 商品ID %d 数量 %d 最新数量 %d" , userId , goodsType ,id , count, v.count))
isSuc = true
break
end
end
if isSuc then
skynet.server.playerRecord:Add( userId , dataType.RecordType_101 , goodInfo.goodsType , goodInfo.id , goodInfo.count )
table.insert( data.goodsInfo , goodInfo )
skynet.server.gameServer:SendMsgToUser( player.userId , "CMD_S2C_BackpackUpdate" , data )
end
return isSuc
end
--获取背包中的道具数量
function Backpack:GetGoodsCount( player , goodsType , id )
--检查各参数
if not player or not goodsType or not id then
log.info("获取背包中的道具数量,请求参数错误", player,goodsType , id)
return 0
end
--检查物品的合法性
if not self:IsValidGoodsType( goodsType ) then
log.info("获取背包中的道具数量,商品类型错误", goodsType)
return 0
end
for k, v in pairs( player.gameData.Backpack ) do
if goodsType == v.type and id == v.id then
return v.count
end
end
return 0
end
--计算经验
function Backpack:CalcExp( goodsType , id , count )
local cfgGoods = {}
local addExp = 0
--获取指定类型配置
if dataType.GoodsType_Furniture == goodsType then
cfgGoods = skynet.server.gameConfig:GetCurFurnitureCfg( id )
if cfgGoods then
addExp = count * cfgGoods.exp
end
elseif dataType.GoodsType_Decorate == goodsType then
cfgGoods = skynet.server.gameConfig:GetCurDecorationCfg( id )
if cfgGoods then
addExp = count * cfgGoods.exp
end
end
return addExp
end
--是否正常的商品类型
function Backpack:IsValidGoodsType( goodsType )
if goodsType >= self.BackpackType_Prop and goodsType < self.BackpackType_End then
return true
end
return false
end
--获取未使用的商品信息
function Backpack:GetUnusedGoodsInfo( player , goodsType )
local goodsInfo = {}
local t1 = skynet.GetTime()
for k, v in pairs( player.gameData.Backpack ) do
if goodsType == v.type then
local maxCount = skynet.server.house:GetCurFurnitureMaxCount( player , v.type , v.id )
maxCount = v.count - maxCount
table.insert( goodsInfo , { type = v.type , id = v.id , count = maxCount > 0 and maxCount or 0 , gainTime = v.gainTime })
end
end
t1 = skynet.GetTime() - t1
if t1 >= 1 then
local errorText = string.format("玩家 %d 背包 未使用的商品信息 用时太长 超过 %d 秒" , player.basicInfo.userID , t1 )
log.info(errorText)
skynet.server.gameServer:SendErrorInfoToCenter( errorInfo.ErrorCode.CalcTimeTooLong , errorText )
end
return goodsInfo
end
--是否能移除商品
function Backpack:IsRemoveGoods( player , goodsType , goodsId , takeOutCount )
for k, v in pairs( player.gameData.Backpack ) do
--找到对应的商品
if goodsType == v.type and goodsId == v.id then
local maxCount = skynet.server.house:GetCurFurnitureMaxCount( player , v.type , v.id )
--剩余数量 = 当前数量 - 房间最大数量
maxCount = v.count - maxCount
--剩余数量 - 扣除的数量 >=0 表示可以扣除
if maxCount - takeOutCount >= 0 then
return true
else
return false
end
end
end
return false
end
--是否存在商品
function Backpack:IsExistGoods( player , backType , id )
local allItem = {}
if self.BackpackType_Prop == backType then
allItem = player.gameData.Backpack.prop
elseif self.BackpackType_Goods == backType then
allItem = player.gameData.Backpack.goods
end
--根据商品类型来取数据
for k, v in pairs( allItem ) do
if id == v.id then
return true
end
end
return false
end
skynet.server.backpack = Backpack
return Backpack