前言
放一起讲是觉得这两个功能比较类似,区别是公会数据保存在数据库里
组队数据结构
服务器队伍类,组队数据仅在内存中使用,如果做成支付宝种树,叠猫猫那类持久组队,可以新建张数据表
function TeamMgr:Init()
self.teamlist = {
}
self.singlePersion = {
}
end
玩家队伍数据
local Team = oo.class()
local _TEAM_MEM_NUM = 5 --一个队伍最多有5个成员,包括仙君和玩家--
function Team:ctor(teamid,player,targetid)
self.teamid = teamid
self.leader = player
self.targetid = targetid
self.cfgid = math.floor(targetid / 1000)
self.index = targetid % 1000
self.isAuto = 1--是否进行自动匹配
if self.targetid == 0 then
self.isAuto = 0--是否进行自动匹配
end
self.memlist = {
player}
self.xianjunlist = {
}
self.levelRange = {
0,1000}
--self.xianjunStar = 0
end
组队功能
组队事件
注册onJoinTeam入队和onLeaveTeam离队方法
function TeamPerson:ctor(player,targetid)
self.player = player
self.targetid = targetid
self.cfgid = math.floor(targetid / 1000)
end
function TeamPerson:init()
self.config = server.configCenter.TeamTargetConfig[self.cfgid]
if not self.config then
return false
end
self.m_joinTeamE = self.player:SubEvent(server.enEvent.PlayerEvnt.EVENTID_ENTITY_JION_TEAM,self.onJoinTeam,self)
self.m_leaveTeamE = self.player:SubEvent(server.enEvent.PlayerEvnt.EVENTID_ENTITY_LEAVE_TEAM,self.onLeaveTeam,self)
return true
end
--示例
function TeamPerson:onLeaveTeam(args)
if args.entityID ~= self.player.entityID then
return
end
--删除定时器
if self.del_timer then
lua_app.del_timer(self.del_timer)
self.del_timer = nil
end
end
创建队伍
function TeamMgr:createTeam(player,targetid)
if player.prop.teamid ~= 0 then --已经有队伍不能再次创建
return false
end
local teamid = _Cur_TeamID + 1
local team = Team.new(teamid,player,targetid)
if team and team:init(true) then
_Cur_TeamID = teamid
self.teamlist[teamid] = team
else
return false
end
--触发入队事件
local args = {
}
args.entityID = player.entityID --创建队伍
args.teamid = teamid
args.EventID = server.enEvent.PlayerEvnt.EVENTID_ENTITY_JION_TEAM
player:FireEvent(args.EventID,args)
local function _matchTeam()
if self.matchTime then
lua_app.del_timer(self.matchTime)
end
if next(self.singlePersion) then
self:doMatch()
end
self.matchTime = lua_app.add_timer(5000, _matchTeam)
end
if not self.matchTime then
self.matchTime = lua_app.add_timer(5000, _matchTeam)
end
--存在目标的的可以进行发送超链接
if targetid and targetid > 0 then
local info = team:getTargetInfo()
local players = {
}
for _, actor in pairs(server.playerCenter:GetOnlinePlayers()) do
if actor.prop.level >= team.levelRange[1] and
actor.prop.level <= team.levelRange[2] then
table.insert(players,actor)
end
end
server.chatCenter:SendLink2SomePalyers(1320,player,players,info.activeName,info.rangeName,
{
server.ChatLinkType.team,teamid,player.entityID,"申请组队"})
end
return true
end
申请加入队伍
与客户端通信协议
--[[
#请求加入队伍
cs_team_jointeam 17002 {
request {
teamid 0 : integer
}
}
]]
function server.cs_team_jointeam(socketid, msg)
local player = server.playerCenter:GetPlayerBySocket(socketid)
server.teamMgr:joinTeam(msg.teamid,player.entityID)
end
服务器验证队伍信息
function TeamMgr:joinTeam(teamid,entityID)
local player = server.gameEntityCenter:GetEntityByID(entityID)
if not player then
return false
end
local team = self.teamlist[teamid]
if not team then
--链接已经失效
server.chatCenter:sendTipMsg(1321,player.dbid)
return false
end
if player.prop.teamid ~= 0 then
server.sendErr(player, "你已经有队伍了")