179 lines
6.3 KiB
Lua
179 lines
6.3 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 Pet = oo.class()
|
||
|
|
|
||
|
|
Pet.PetType_Cat = 1 --猫
|
||
|
|
Pet.PetType_Dog = 2 --狗
|
||
|
|
|
||
|
|
function Pet:Init()
|
||
|
|
|
||
|
|
end
|
||
|
|
|
||
|
|
--登陆初始化数据
|
||
|
|
function Pet:LoginInitData( player )
|
||
|
|
self:CheckNew( player )
|
||
|
|
end
|
||
|
|
|
||
|
|
--新增宠物
|
||
|
|
function Pet:CheckNew( player )
|
||
|
|
|
||
|
|
local petType = self.PetType_Cat
|
||
|
|
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 = petType
|
||
|
|
player.gameData.pet [ petType ].skinId = 1
|
||
|
|
player.gameData.pet [ petType ].hatId = 0
|
||
|
|
player.gameData.pet [ petType ].clothesId = 0
|
||
|
|
player.gameData.pet [ petType ].ornamentId = 0 --尾饰
|
||
|
|
player.gameData.pet [ petType ].facingId = 0 --面饰
|
||
|
|
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
|
||
|
|
elseif skynet.server.common:IsExistSpecialChar( petInfo.nickName ) then
|
||
|
|
s2cData.code = errorInfo.ErrorCode.ExistSpecialChar
|
||
|
|
elseif skynet.server.common:IsMaskWords( petInfo.nickName ) then
|
||
|
|
s2cData.code = errorInfo.ErrorCode.ExistMaskWords
|
||
|
|
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
|
||
|
|
elseif 4 == k2 then
|
||
|
|
player.gameData.pet[ k1 ].facingId = 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 )
|
||
|
|
table.insert(data.petClothesId , v.facingId )
|
||
|
|
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
|
||
|
|
elseif skynet.server.common:IsExistSpecialChar( nickName ) then
|
||
|
|
s2cData.code = errorInfo.ErrorCode.ExistSpecialChar
|
||
|
|
elseif skynet.server.common:IsMaskWords( nickName ) then
|
||
|
|
s2cData.code = errorInfo.ErrorCode.ExistMaskWords
|
||
|
|
else
|
||
|
|
if dataType.PetType_UnlockNoOpen == player.gameData.pet[ petType ].status then
|
||
|
|
player.gameData.pet[ petType ].status = dataType.PetType_UnlockOpened
|
||
|
|
player.gameData.pet[ petType ].nickName = nickName
|
||
|
|
else
|
||
|
|
s2cData.code = errorInfo.ErrorCode.NoUnlock
|
||
|
|
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
|
||
|
|
elseif skynet.server.common:IsExistSpecialChar( nickName ) then
|
||
|
|
s2cData.code = errorInfo.ErrorCode.ExistSpecialChar
|
||
|
|
elseif skynet.server.common:IsMaskWords( nickName ) then
|
||
|
|
s2cData.code = errorInfo.ErrorCode.ExistMaskWords
|
||
|
|
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
|
||
|
|
|
||
|
|
--打包宠物
|
||
|
|
function Pet:Package( player , petType )
|
||
|
|
if player.gameData.pet[ petType ] and dataType.PetStatus_Lock == player.gameData.pet[ petType ].status then
|
||
|
|
player.gameData.pet[ petType ].status = dataType.PetType_UnlockNoOpen
|
||
|
|
log.debug(string.format("玩家 %d 宠物 %d 打包 ", player.userId , petType))
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
skynet.server.pet = Pet
|
||
|
|
return Pet
|