2023-08-30 16:28:40 +08:00

496 lines
14 KiB
Go

package main
import (
"cs"
"f5"
proto "github.com/golang/protobuf/proto"
"q5"
)
type Player struct {
cs.MsgHandlerImpl
socket f5.WspCliConn
accountId string
sessionId string
chatChannel int
privateTargetAccountId string // 私聊对象 accountId
worldChannelLastId uint64 // 世界聊天 last id
guildChannelLastId uint64 // 公会聊天 last id
privateChatLastId uint64 // 私聊 last id
}
func (p *Player) GetAccountId() string {
return p.accountId
}
// CMSearchUser 搜索用户
func (p *Player) CMSearchUser(hdr *f5.MsgHdr, msg *cs.CMSearchUser) {
rspMsg := new(cs.SMSearchUser)
//rspMsg := &cs.SMSearchFriend{}
listUsers := friendMgr.searchUsers(msg.GetSearchKeyword())
for _, u := range listUsers {
friend := &cs.MFUser{
AccountId: &u.AccountId,
Username: &u.Username,
}
rspMsg.Users = append(rspMsg.Users, friend)
}
f5.GetSysLog().Info("CMSearchUser user count:%d\n", len(rspMsg.Users))
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMSearchUserByAccountId 搜索指定用户
func (p *Player) CMSearchUserByAccountId(hdr *f5.MsgHdr, msg *cs.CMSearchUserByAccountId) {
rspMsg := new(cs.SMSearchUserByAccountId)
user := friendMgr.searchByAccountId(msg.GetAccountId())
if user != nil {
rspMsg.AccountId = &user.AccountId
rspMsg.Username = &user.Username
f5.GetSysLog().Info("CMSearchUserByAccountId AccountId: %s\n", *rspMsg.AccountId)
}
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMAddFriendRequest 添加好友请求
func (p *Player) CMAddFriendRequest(hdr *f5.MsgHdr, msg *cs.CMAddFriendRequest) {
rspMsg := &cs.SMAddFriendRequest{}
user1Id := p.accountId
user2Id := msg.GetTargetAccountId()
err := friendMgr.addFriendRequest(user1Id, user2Id)
if err != nil {
reason := err.Error()
rspMsg.Reason = &reason
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
f5.GetSysLog().Info("CMAddFriendRequest err:%s, params: %s or %s\n", reason, user1Id, user2Id)
return
}
status := "pending"
rspMsg.Status = &status
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMAcceptFriendRequest 接受好友请求
func (p *Player) CMAcceptFriendRequest(hdr *f5.MsgHdr, msg *cs.CMAcceptFriendRequest) {
rspMsg := &cs.SMAcceptFriendRequest{}
user1Id := p.accountId
user2Id := msg.GetTargetAccountId()
err := friendMgr.acceptFriendRequest(user1Id, user2Id)
if err != nil {
reason := err.Error()
rspMsg.Reason = &reason
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
f5.GetSysLog().Info("CMAcceptFriendRequest err:%s, params: %s or %s\n", reason, user1Id, user2Id)
return
}
status := "approved"
rspMsg.Status = &status
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMRejectFriendRequest 拒绝好友请求
func (p *Player) CMRejectFriendRequest(hdr *f5.MsgHdr, msg *cs.CMRejectFriendRequest) {
rspMsg := &cs.SMRejectFriendRequest{}
user1Id := p.accountId
user2Id := msg.GetTargetAccountId()
err := friendMgr.rejectFriendRequest(user1Id, user2Id)
if err != nil {
reason := err.Error()
rspMsg.Reason = &reason
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
f5.GetSysLog().Info("CMRejectFriendRequest err:%s, params: %s or %s\n", reason, user1Id, user2Id)
return
}
status := "reject"
rspMsg.Status = &status
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMListPendingFriendRequest 等待验证的好友请求
func (p *Player) CMListPendingFriendRequest(hdr *f5.MsgHdr, msg *cs.CMListPendingFriendRequest) {
rspMsg := &cs.SMListPendingFriendRequest{}
accountId := p.accountId
resAccountIds := make([]string, MaxPendingFriendReqs)
for accountId, _ := range friendMgr.pendingReqs[accountId] {
resAccountIds = append(resAccountIds, accountId)
}
rspMsg.AccountIds = resAccountIds
f5.GetSysLog().Info("CMListPendingFriendRequest requests count:%d\n", len(rspMsg.GetAccountIds()))
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMListFriend 我的好友列表
func (p *Player) CMListFriend(hdr *f5.MsgHdr, msg *cs.CMListFriend) {
rspMsg := &cs.SMListFriend{}
accountId := p.accountId
for _, friendship := range friendMgr.friendships[accountId] {
if friendship.User1.AccountId != accountId {
friend := &cs.MFUser{
AccountId: &friendship.User1.AccountId,
Username: &friendship.User1.Username,
}
rspMsg.Users = append(rspMsg.Users, friend)
} else {
friend := &cs.MFUser{
AccountId: &friendship.User2.AccountId,
Username: &friendship.User2.Username,
}
rspMsg.Users = append(rspMsg.Users, friend)
}
}
f5.GetSysLog().Info("CMListFriend friends count:%d\n", len(rspMsg.Users))
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMDeleteFriendShip 删除好友
func (p *Player) CMDeleteFriendShip(hdr *f5.MsgHdr, msg *cs.CMDeleteFriendShip) {
rspMsg := &cs.SMDeleteFriendShip{}
user1Id := p.accountId
user2Id := msg.GetTargetAccountId()
err := friendMgr.deleteFriendShip(user1Id, user2Id)
if err != nil {
reason := err.Error()
rspMsg.Reason = &reason
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
f5.GetSysLog().Info("CMDeleteFriendShip err:%s, params: %s or %s\n", reason, user1Id, user2Id)
return
}
status := "deleted"
rspMsg.Status = &status
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMAddBlacklist 加入黑名单
func (p *Player) CMAddBlacklist(hdr *f5.MsgHdr, msg *cs.CMAddBlacklist) {
rspMsg := &cs.SMDeleteFriendShip{}
user1Id := p.accountId
user2Id := msg.GetTargetAccountId()
err := friendMgr.addBlacklist(user1Id, user2Id)
if err != nil {
reason := err.Error()
rspMsg.Reason = &reason
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
f5.GetSysLog().Info("CMAddBlacklist err:%s, params: %s or %s\n", reason, user1Id, user2Id)
return
}
status := "added"
rspMsg.Status = &status
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMRemoveBlacklist 移除黑名单
func (p *Player) CMRemoveBlacklist(hdr *f5.MsgHdr, msg *cs.CMRemoveBlacklist) {
rspMsg := &cs.SMRemoveBlacklist{}
user1Id := p.accountId
user2Id := msg.GetTargetAccountId()
err := friendMgr.removeBlacklist(user1Id, user2Id)
if err != nil {
reason := err.Error()
rspMsg.Reason = &reason
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
f5.GetSysLog().Info("CMRemoveBlacklist err:%s, params: %s or %s\n", reason, user1Id, user2Id)
return
}
status := "removed"
rspMsg.Status = &status
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMFriendInfo 我的好友信息
func (p *Player) CMFriendInfo(hdr *f5.MsgHdr, msg *cs.CMFriendInfo) {
rspMsg := new(cs.SMFriendInfo)
accountId := p.accountId
// friendships
users := friendMgr.listFriends(accountId)
for _, user := range users {
rspMsg.FriendshipsAccountIds = append(rspMsg.FriendshipsAccountIds, user.AccountId)
}
// pending reqs
reqs := friendMgr.getPendingReqs(accountId)
for reqAccountId, _ := range reqs {
rspMsg.PendingReqsAccountIds = append(rspMsg.PendingReqsAccountIds, reqAccountId)
}
// blacklist
blacklist := friendMgr.getBlacklist(accountId)
for blockedAccountId, isRemoved := range blacklist {
if isRemoved == false {
rspMsg.BlacklistAccountIds = append(rspMsg.BlacklistAccountIds, blockedAccountId)
}
}
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMSendChatMsg 发送聊天消息
func (p *Player) CMSendChatMsg(hdr *f5.MsgHdr, msg *cs.CMSendChatMsg) {
chatChannel := *msg.ChatChannel
if !IsValidChatChannel(chatChannel) {
return
}
switch chatChannel {
case kCCWorld:
chatMgr.ProcWorldChat(p, msg)
case kCCPrivate:
chatMgr.ProcPrivateChat(p, msg)
case kCCGuild:
chatMgr.ProcGuildChat(p, msg)
default:
return
}
}
func (p *Player) SyncPrivateChatRedPoint() {
//rspMsg := &cs.SMDeleteFriendShip{}
msg := &cs.SMUpdatePrivateChatRedPointNotify{}
chatMgr.FillSMUpdatePrivateChatRedPointNotify(p, msg)
}
// CMReadMsgAndOpenChatNotify 读取聊天消息列表并且开启聊天通知
func (p *Player) CMReadMsgAndOpenChatNotify(hdr *f5.MsgHdr, msg *cs.CMReadMsgAndOpenChatNotify) {
chatChannel := *msg.CurrChannel
if !IsValidChatChannel(chatChannel) {
return
}
if chatChannel == kCCPrivate {
p.SyncPrivateChatRedPoint()
}
}
// CMSetCurrPrivateChatTarget 设置当前私聊目标
func (p *Player) CMSetCurrPrivateChatTarget(hdr *f5.MsgHdr, msg *cs.CMSetCurrPrivateChatTarget) {
p.privateTargetAccountId = msg.GetTargetAccountId()
chatMgr.SyncPrivateChatMsg(p)
}
func (p *Player) MarkNewMsg() {
rspMsg := &cs.SMUpdateChatRedPointNotify{}
chatMgr.FillSMUpdateChatRedPointNotify(p, rspMsg)
p.SendMsg(rspMsg)
}
func (p *Player) SendMsg(rspMsg proto.Message) {
wspListener.sendProxyMsg(p.socket.Conn, p.socket.SocketHandle, rspMsg)
}
func IsValidChatChannel(chatChannel int32) bool {
return chatChannel > kCCBegin && chatChannel < kCCEnd
}
// CMGuildInfo 我的公会信息
func (p *Player) CMGuildInfo(hdr *f5.MsgHdr, msg *cs.CMGuildInfo) {
rspMsg := new(cs.SMGuildInfo)
g := guildMgr.GetGuildByAccountId(p.accountId)
if g == nil {
rspMsg.Guild = nil
rspMsg.RandomGuilds = p.FillGuild(guildMgr.RandomGuilds())
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
return
}
members := q5.ToInt32(len(g.Members))
maxMembers := q5.ToInt32(g.MaxMembers)
currGuild := &cs.MFGuild{
GuildId: &g.GuildId,
Name: &g.Name,
LeaderId: &g.LeaderId,
Members: &members,
MaxMembers: &maxMembers,
}
rspMsg.Guild = currGuild
rspMsg.RandomGuilds = nil
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMCreateGuild 创建公会
func (p *Player) CMCreateGuild(hdr *f5.MsgHdr, msg *cs.CMCreateGuild) {
rspMsg := new(cs.SMCreateGuild)
guildId, err := guildMgr.CreateGuild(*msg.Name, p.accountId)
if err != nil {
errMsg := err.Error()
rspMsg.ErrMsg = &errMsg
f5.GetSysLog().Info("CMCreateGuild err:%s\n", errMsg)
} else {
rspMsg.GuildId = &guildId
}
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMApplyToGuild 同意申请者加入公会
func (p *Player) CMApplyToGuild(hdr *f5.MsgHdr, msg *cs.CMApplyToGuild) {
rspMsg := new(cs.SMApplyToGuild)
err := guildMgr.ApplyToGuild(
msg.GetGuildId(),
p.accountId,
)
if err != nil {
errMsg := err.Error()
rspMsg.ErrMsg = &errMsg
f5.GetSysLog().Info("CMApplyToGuild err:%s\n", errMsg)
}
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMApprove 同意申请者加入公会
func (p *Player) CMApprove(hdr *f5.MsgHdr, msg *cs.CMApprove) {
rspMsg := new(cs.SMApprove)
applicantAccountId := msg.GetApplicantAccountId()
err := guildMgr.Approve(
msg.GetGuildId(),
p.accountId,
applicantAccountId,
)
if err != nil {
errMsg := err.Error()
rspMsg.ErrMsg = &errMsg
f5.GetSysLog().Info("CMApprove err:%s\n", errMsg)
}
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMReject 拒绝申请者加入公会
func (p *Player) CMReject(hdr *f5.MsgHdr, msg *cs.CMReject) {
rspMsg := new(cs.SMReject)
applicantAccountId := msg.GetApplicantAccountId()
err := guildMgr.Reject(
msg.GetGuildId(),
applicantAccountId,
)
if err != nil {
errMsg := err.Error()
rspMsg.ErrMsg = &errMsg
f5.GetSysLog().Info("CMReject err:%s\n", errMsg)
}
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMLeaveGuild 离开公会
func (p *Player) CMLeaveGuild(hdr *f5.MsgHdr, msg *cs.CMLeaveGuild) {
rspMsg := new(cs.SMLeaveGuild)
guildId := msg.GetGuildId()
err := guildMgr.LeaveGuild(
guildId,
p.accountId,
)
if err != nil {
errMsg := err.Error()
rspMsg.ErrMsg = &errMsg
f5.GetSysLog().Info("CMLeaveGuild err:%s\n", errMsg)
}
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMDismissMember 开除成员
func (p *Player) CMDismissMember(hdr *f5.MsgHdr, msg *cs.CMDismissMember) {
rspMsg := new(cs.SMDismissMember)
err := guildMgr.DismissMember(
msg.GetGuildId(),
p.accountId,
msg.GetDismissAccountId(),
)
if err != nil {
errMsg := err.Error()
rspMsg.ErrMsg = &errMsg
f5.GetSysLog().Info("CMDismissMember err:%s\n", errMsg)
}
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMPromoteMember 提升成员为干部
func (p *Player) CMPromoteMember(hdr *f5.MsgHdr, msg *cs.CMPromoteMember) {
rspMsg := new(cs.SMPromoteMember)
err := guildMgr.PromoteMember(
msg.GetGuildId(),
p.accountId,
msg.GetPromoteAccountId(),
)
if err != nil {
errMsg := err.Error()
rspMsg.ErrMsg = &errMsg
f5.GetSysLog().Info("CMPromoteMember err:%s\n", errMsg)
}
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMDemoteMember 解除成员干部身份
func (p *Player) CMDemoteMember(hdr *f5.MsgHdr, msg *cs.CMDemoteMember) {
rspMsg := new(cs.SMDemoteMember)
err := guildMgr.DemoteMember(
msg.GetGuildId(),
p.accountId,
msg.GetDemoteAccountId(),
)
if err != nil {
errMsg := err.Error()
rspMsg.ErrMsg = &errMsg
f5.GetSysLog().Info("CMDemoteMember err:%s\n", errMsg)
}
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMDisband 解散公会
func (p *Player) CMDisband(hdr *f5.MsgHdr, msg *cs.CMDisband) {
rspMsg := new(cs.SMDisband)
err := guildMgr.Disband(
msg.GetGuildId(),
p.accountId,
)
if err != nil {
errMsg := err.Error()
rspMsg.ErrMsg = &errMsg
f5.GetSysLog().Info("CMDisband err:%s\n", errMsg)
}
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// CMSearchGuilds 搜索公会
func (p *Player) CMSearchGuilds(hdr *f5.MsgHdr, msg *cs.CMSearchGuilds) {
rspMsg := new(cs.SMSearchGuilds)
rspMsg.Guilds = p.FillGuild(guildMgr.SearchGuilds(msg.GetKeyword()))
f5.GetSysLog().Info("CMSearchGuilds keyword[%s], result:%d \n", msg.GetKeyword(), len(rspMsg.Guilds))
wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
}
// FillGuild 填充公共列表
func (p *Player) FillGuild(guilds []*Guild) []*cs.MFGuild {
var resGuilds []*cs.MFGuild
for _, g := range guilds {
members := q5.ToInt32(len(g.Members))
maxMembers := q5.ToInt32(g.MaxMembers)
guild := &cs.MFGuild{
GuildId: &g.GuildId,
Name: &g.Name,
LeaderId: &g.LeaderId,
Members: &members,
MaxMembers: &maxMembers,
}
resGuilds = append(resGuilds, guild)
}
return resGuilds
}
func (p *Player) IncrPrivateChatLastId() uint64 {
p.privateChatLastId++
return p.privateChatLastId
}