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

151 lines
5.1 KiB
Lua
Raw Permalink 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 Pet = oo.class()
function Pet:Init()
end
--新增宠物
function Pet:CheckNew( player )
local cfgPetSkin = skynet.server.gameConfig.PetSkin
local level = player.gameData.level
for k, v in pairs( cfgPetSkin ) do
if level >= v.unlockLvl then
local petType = v.petType
if not player.gameData.pet [ petType ] then
--到了30级解锁宠物
player.gameData.pet [ petType ] = {}
player.gameData.pet [ petType ].nickName = ""
player.gameData.pet [ petType ].status = dataType.PetStatus_Lock
player.gameData.pet [ petType ].type = v.petType
player.gameData.pet [ petType ].skinId = v.id
player.gameData.pet [ petType ].hatId = 0
player.gameData.pet [ petType ].clothesId = 0
player.gameData.pet [ petType ].ornamentId = 0
end
end
end
end
--宠物特征展示
function Pet:Show( player , c2sData , s2cData )
c2sData.data = assert(pb.decode("C2SPetShow", c2sData.data ))
local data ={}
local petType = c2sData.data.petType
if not petType then
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
else
data.petInfo = self:GetData( player , petType )
end
s2cData.cmd = pb.enum("MsgType","CMD_S2C_PetShow")
s2cData.data = assert(pb.encode("S2CPetShow", data))
end
--宠物特征改变
function Pet:Change( player , c2sData , s2cData )
c2sData.data = assert(pb.decode("C2SPetChange", c2sData.data ))
local data ={}
local petInfo = c2sData.data.petInfo
if not petInfo or not petInfo.type or not petInfo.nickName or not petInfo.skinId or not petInfo.petClothesId then
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
else
for k1, v1 in pairs( player.gameData.pet ) do
--找到对就的宠物类型
if petInfo.type == v1.type then
v1.nickName = petInfo.nickName
v1.skinId = petInfo.skinId
--根据协议来的顺序分别填入 服装 帽子 装饰品
for k2, v2 in pairs( petInfo.petClothesId ) do
if player:IsBuyGoods( dataType.GoodsType_PetClothes , v2 ) or 0 == v2 then
if 1 == k2 then
player.gameData.pet[ k1 ].clothesId = v2
elseif 2 == k2 then
player.gameData.pet[ k1 ].hatId = v2
elseif 3 == k2 then
player.gameData.pet[ k1 ].ornamentId = v2
end
end
end
data.petInfo = self:GetData( player , petInfo.type )
break
end
end
end
s2cData.cmd = pb.enum("MsgType","CMD_S2C_PetChange")
s2cData.data = assert(pb.encode("S2CPetChange", data))
end
--获取数据
function Pet:GetData( player , petType )
local data = {}
for k, v in pairs( player.gameData.pet ) do
if petType == v.type then
data.type = v.type
data.nickName = v.nickName
data.skinId = v.skinId
data.petClothesId = {}
table.insert(data.petClothesId , v.clothesId )
table.insert(data.petClothesId , v.hatId )
table.insert(data.petClothesId , v.ornamentId )
break
end
end
return data
end
--宠物特征展示
function Pet:Unlock( player , c2sData , s2cData )
c2sData.data = assert(pb.decode("C2SPetUnlock", c2sData.data ))
local data ={}
local petType = c2sData.data.petType
local nickName = c2sData.data.nickName
if not petType or not nickName then
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
else
if dataType.PetStatus_Lock == player.gameData.pet[ petType ].status then
player.gameData.pet[ petType ].status = dataType.PetStatus_Unlock
player.gameData.pet[ petType ].nickName = nickName
end
end
data.petType = petType
s2cData.cmd = pb.enum("MsgType","CMD_S2C_PetUnlock")
s2cData.data = assert(pb.encode("S2CPetUnlock", data))
end
--宠物改名
function Pet:Rename( player , c2sData , s2cData )
c2sData.data = assert(pb.decode("C2SPetRename", c2sData.data ))
local petType = c2sData.data.petType
local nickName = c2sData.data.nickName
local data ={}
if not petType or not nickName then
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
else
for k, v in pairs( player.gameData.pet ) do
if petType == v.type then
v.nickName = nickName
data.petType = petType
data.nickName = nickName
break
end
end
end
s2cData.cmd = pb.enum("MsgType","CMD_S2C_PetRename")
s2cData.data = assert(pb.encode("S2CPetRename", data))
end
skynet.server.pet = Pet
return Pet