好友及聊天功能

好友及聊天功能

Scroll Down

开头
用户交流是很多软件必备的功能,最近接到策划的新需求开发好友系统,下面分享我实现好友功能的具体方式
好友数据
数据库结构

{ "friend_data"	,"mediumblob"		,{
	myfriend = {},
	blacklist = {},
	applylist = {},
	delfriendlist = {},
	isRefuseApply = false,
}	,"玩家好友数据" },

分为:好友列表,黑名单列表,申请列表,被删除好友列表(客户端需要),允许陌生人申请好友开关

功能开发
玩家登陆加载好友数据,发送好友列表,黑名单列表,申请列表,删除列表信息
1、申请好友
1、判定是否已在好友列表,黑名单列表中
2、判定双方好友人数是否达到上线
3、通过即给对方推送一条消息,返回成功

sc_friend_apply_update 30006 {
	request {
		friendInfo 			0 : friend_data
	}
}

2、处理好友申请
传参人物dbid编号和applytype处理类型
1、applytype为1拒绝申请,applylist表元素删除
2、applytype为0同意申请,判定是黑名单列表、好友列表、好友人数上限
3、同意通过申请,双方好友列表添加元素,申请列表删除元素,推送一条id为0的系统信息
local chatdata = {id = 0,str = "对方已通过你的好友申请",time = lua_app.now()}
3、删除好友
1、从双方好友列表删除
2、对方在线推送删除消息,不在线保存在对方的delfriendlist列表(客户端要求)
4、加入黑名单
1、调用删除好友方法
2、加入黑名单,推送黑名单消息
5、更新好友消息
1、有新的聊天消息时推送数据
2、玩家升级、下线、上线的情况,对玩家在线好友推送消息

--玩家升级事件
function Friend:onLevelUp()
	self:NotifyStatusAlter()
end

--通知玩家状态改变
function Friend:NotifyStatusAlter()
	--更新好友点列表
	for dbid,_ in pairs(self.myfriend) do
		local target = server.playerCenter:GetPlayerByDBID(dbid)
		if target and target.isLogin then
			target.friend:UpdateFriendData(self.player.dbid)
		end
	end
end

玩家数据信息

local function _PackPlayerData(dbid)
	local player = server.playerCenter:DoGetPlayerByDBID(dbid)
	local data = {	
		name = player.cache.name,
		job = player.cache.job,
		sex = player.cache.sex,
		vip = player.cache.vip,
		level = player.cache.level,
		dbid = player.cache.dbid,
		character = player.cache.character,
		shows = player.role:GetShows(),
		power = player.cache.totalpower,
		guildId = player.prop.guildid,
		guildName = player.guild:GetGuildName(),
		offlineTime = player.isLogin and 0 or player.cache.lastonlinetime,
		
	}
	return data
end

local data = {friendInfo = _PackPlayerData(dbid),chatData = chatFriendData}
	server.sendReq(self.player, "sc_friend_follow_update", {
		friendInfo = data
		})

6、好友聊天功能
1、判断玩家的等级,字符串,黑名单列表是否异常
2、根据需求分为临时消息和好友消息,临时消息不保存数据,聊天数据客户端保存
3、离线玩家无法发送临时消息,重新登陆临时消息清空
4、非好友聊天推送临时好友数据
客户端传参接受者id :recvId,聊天信息 :str。聊天数据记录发言人id,内容,时间
local chatdata = {id = sender.cache.dbid,str = str,time = lua_app.now()}
推送消息

--临时好友聊天数据
local data = {friendInfo = _PackPlayerData(dbid),chatData = chatFriendData}
	server.sendReq(self.player, "sc_friend_short_update", {
		friendInfo = data
    })

5、好友聊天对方在线推送信息
6、好友不在线,保存离线聊天数据

--离线保存数据
local receiverrecords = receiverdata.myfriend[sender.cache.dbid].chatRecord	
table.insert(receiverrecords, chatdata)	
玩家登陆发送好友列表示例

--发送好友列表
function Friend:SendFriends()
	local datas = {}
	for dbid, data in pairs(self.myfriend) do
		table.insert(datas, {
			friendInfo = _PackPlayerData(dbid),
			chatData = data.chatRecord,
			})
		--离线消息发送完清空
		data.chatRecord = {}
	end
	server.sendReq(self.player, "sc_friend_follow_data", {
			friendlist = datas,
		})
end