package mapLeague import ( "f1-game/internal/code" "f1-game/internal/component/redis" "f1-game/internal/data" "f1-game/internal/enum" "f1-game/internal/extend/utils" nameLocal "f1-game/internal/name/local" nameRedis "f1-game/internal/name/redis" nameRoute "f1-game/internal/name/route" "f1-game/internal/pb" "f1-game/internal/sessions" "f1-game/internal/types" "f1-game/nodes/map/internal/db" dbLeague "f1-game/nodes/map/internal/db/data/league" leagueMail "f1-game/nodes/map/league/mail" ctime "github.com/cherry-game/cherry/extend/time" cproto "github.com/cherry-game/cherry/net/proto" ) func (p *actorLeague) initLeagueMail() { p.Local().Register(nameLocal.MapLeagueMail_Send, p.leagueMailSend) p.Local().Register(nameLocal.MapLeagueMail_Del, p.leagueMailDel) p.Local().Register(nameLocal.MapLeagueMail_MemberDel, p.leagueMailMemberDel) p.Local().Register(nameLocal.MapLeagueMail_Top, p.leagueMailTop) p.Local().Register(nameLocal.MapLeagueMail_Read, p.leagueMailRead) p.Local().Register(nameLocal.MapLeagueMail_FastRead, p.leagueMailFastRead) } // 发送/更新联盟邮件 func (p *actorLeague) leagueMailSend(session *cproto.Session, req *pb.LeagueMailSend) { opPlayerID := sessions.GetPlayerID(session) _, found := db.GetMapLeagueTable(p.leagueID) if !found { p.ResponseCode(session, code.LeagueNotExist) return } leagueMemberTable, found := db.GetMapLeagueMemberTable(p.leagueID) if !found { p.ResponseCode(session, code.LeagueMembersEmpty) return } // 获取发送联盟邮件所需权限 permID := enum.GetSendLeagueMailPermission(req.MailType) // 判断玩家是否有权处理 if !leagueMemberTable.LeagueMember.CheckPermission(opPlayerID, permID) { p.ResponseCode(session, code.LeaguePermissionDenied) return } // 发送联盟邮件 errCode := p.SendLeagueMail(leagueMemberTable, opPlayerID, req.MailID, req.MailType, req.Title, req.Content, req.IsTop) p.ResponseCode(session, errCode) } // 删除联盟邮件 func (p *actorLeague) leagueMailDel(session *cproto.Session, req *pb.I64) { var ( opPlayerID = sessions.GetPlayerID(session) mailID = req.Value mailType = enum.MailType_Decree ) _, found := db.GetMapLeagueTable(p.leagueID) if !found { p.ResponseCode(session, code.LeagueNotExist) return } leagueMemberTable, found := db.GetMapLeagueMemberTable(p.leagueID) if !found { p.ResponseCode(session, code.LeagueMembersEmpty) return } // 判断玩家是否有权处理 if !leagueMemberTable.LeagueMember.CheckPermission(opPlayerID, enum.League_Permission_Decree) { p.ResponseCode(session, code.LeaguePermissionDenied) return } leagueMailTable, found := db.GetMapLeagueMailTable(p.leagueID) if !found { p.ResponseCode(session, code.LeagueMailDataNotExist) return } removeMailIDs := []int64{mailID} leagueMailList := leagueMailTable.GetLeagueMails(mailType) if len(leagueMailList.List) <= 0 { p.ResponseCode(session, code.LeagueMailDataNotExist) return } if mailID <= 0 { // 删除所有邮件 leagueMailList := leagueMailTable.DeleteLeagueMailList(mailType) for _, mail := range leagueMailList.List { removeMailIDs = append(removeMailIDs, mail.MailID) } } else { _, index := leagueMailList.GetLeagueMail(mailID) if index < 0 { p.ResponseCode(session, code.MailDataNotFound) return } _, ok := leagueMailList.DeleteLeagueMail(mailID) if !ok { p.ResponseCode(session, code.MailDelFail) return } removeMailIDs = append(removeMailIDs, mailID) } leagueMailTable.Save2Queue() // 通知联盟所有在线玩家 leagueMail.Service().ChangePush(leagueMemberTable.GetLeagueMemberIDs(), mailType, leagueMailList.TopMailID, removeMailIDs) p.ResponseCode(session, code.OK) } func (p *actorLeague) leagueMailMemberDel(session *cproto.Session, req *pb.I64I32) { var ( opPlayerID = sessions.GetPlayerID(session) mailID = req.Key mailType = req.Value ) if mailType == enum.MailType_Decree { p.ResponseCode(session, code.MailDecreeNoCanDel) return } _, found := db.GetMapLeagueTable(p.leagueID) if !found { p.ResponseCode(session, code.LeagueNotExist) return } leagueMailTable, found := db.GetMapLeagueMailTable(p.leagueID) if !found { p.ResponseCode(session, code.LeagueMailDataNotExist) return } leagueMailList := leagueMailTable.GetLeagueMails(mailType) if len(leagueMailList.List) <= 0 { p.ResponseCode(session, code.LeagueMailDataNotExist) return } removeMailIDs := []int64{} // 删除特定同盟/指挥邮件 if mailID > 0 { leagueMail, index := leagueMailList.GetLeagueMail(mailID) if index < 0 { p.ResponseCode(session, code.MailDataNotFound) return } if leagueMail.DelPlayerIDs.Contains(opPlayerID) { p.ResponseCode(session, code.MailAlreadyDel) return } leagueMail.DelPlayerIDs.Add(opPlayerID) removeMailIDs = append(removeMailIDs, mailID) } else { for _, leagueMail := range leagueMailList.List { if leagueMail.DelPlayerIDs.Contains(opPlayerID) || !leagueMail.ReadPlayerIDs.Contains(opPlayerID) { continue } leagueMail.DelPlayerIDs.Add(opPlayerID) removeMailIDs = append(removeMailIDs, leagueMail.MailID) } } // 暂无可删除邮件 if len(removeMailIDs) <= 0 { p.ResponseCode(session, code.MailDelNotExist) return } // 通知玩家删除邮件 member := []int64{opPlayerID} leagueMail.Service().ChangePush(member, mailType, leagueMailList.TopMailID, removeMailIDs) p.ResponseCode(session, code.OK) } // 置顶联盟邮件 func (p *actorLeague) leagueMailTop(session *cproto.Session, req *pb.I64) { var ( opPlayerID = sessions.GetPlayerID(session) mailID = req.Value mailType = enum.MailType_Decree ) _, found := db.GetMapLeagueTable(p.leagueID) if !found { p.ResponseCode(session, code.LeagueNotExist) return } leagueMemberTable, found := db.GetMapLeagueMemberTable(p.leagueID) if !found { p.ResponseCode(session, code.LeagueMembersEmpty) return } // 判断玩家是否有权处理 if !leagueMemberTable.LeagueMember.CheckPermission(opPlayerID, enum.League_Permission_Decree) { p.ResponseCode(session, code.LeaguePermissionDenied) return } leagueMailTable, found := db.GetMapLeagueMailTable(p.leagueID) if !found { p.ResponseCode(session, code.LeagueMailDataNotExist) return } leagueMailList := leagueMailTable.GetLeagueMails(mailType) _, index := leagueMailList.GetLeagueMail(mailID) if index < 0 { p.ResponseCode(session, code.MailDataNotFound) return } // 当前置顶邮件唯一ID相同则取消置顶 否则置顶 if leagueMailList.TopMailID == mailID { leagueMailList.TopMailID = 0 } else { leagueMailList.TopMailID = mailID } leagueMailTable.Save2Queue() resp := &pb.I64I32{ Key: leagueMailList.TopMailID, Value: mailType, } // 通知联盟所有在线玩家 sessions.PushPlayers(leagueMemberTable.GetLeagueMemberIDs(), nameRoute.PushLeagueMail_Top, resp) p.ResponseCode(session, code.OK) } // 阅读联盟邮件 func (p *actorLeague) leagueMailRead(session *cproto.Session, req *pb.I64I32) { playerID := sessions.GetPlayerID(session) if playerID < 1 { p.ResponseCode(session, code.MailRequestDataError) return } var ( mailID = req.Key mailType = req.Value ) if enum.VaildateMailType(mailType) { p.ResponseCode(session, code.MailTypeError) return } leagueMemberTable, found := db.GetMapLeagueMemberTable(p.leagueID) if !found { p.ResponseCode(session, code.LeagueMembersEmpty) return } if !leagueMemberTable.CheckMemberExist(playerID) { p.ResponseCode(session, code.LeagueMemberNotExist) return } leagueMailTable, found := db.GetMapLeagueMailTable(p.leagueID) if !found { p.ResponseCode(session, code.LeagueMailDataNotExist) return } leagueMailList := leagueMailTable.GetLeagueMails(mailType) leagueMail, index := leagueMailList.GetLeagueMail(mailID) if index < 0 { p.ResponseCode(session, code.MailDataNotFound) return } if !leagueMail.ReadPlayerIDs.Contains(playerID) { leagueMail.ReadPlayerIDs.Add(playerID) leagueMailTable.Save2Queue() } resp := &pb.I64I32{ Key: mailID, Value: mailType, } p.Response(session, resp) } // 一键阅读联盟邮件 func (p *actorLeague) leagueMailFastRead(session *cproto.Session, req *pb.I32) { playerID := sessions.GetPlayerID(session) if playerID < 1 { p.ResponseCode(session, code.MailRequestDataError) return } mailType := req.Value if enum.VaildateMailType(mailType) { p.ResponseCode(session, code.MailTypeError) return } leagueMemberTable, found := db.GetMapLeagueMemberTable(p.leagueID) if !found { p.ResponseCode(session, code.LeagueMembersEmpty) return } if !leagueMemberTable.CheckMemberExist(playerID) { p.ResponseCode(session, code.LeagueMemberNotExist) return } mailIDs := &pb.I64List{} leagueMailTable, found := db.GetMapLeagueMailTable(p.leagueID) if found { leagueMails := leagueMailTable.GetLeagueMails(mailType) for _, leagueMail := range leagueMails.List { if !leagueMail.ReadPlayerIDs.Contains(playerID) { mailIDs.List = append(mailIDs.List, leagueMail.MailID) leagueMail.ReadPlayerIDs.Add(playerID) } } } if len(mailIDs.List) > 0 { leagueMailTable.Save2Queue() } p.Response(session, mailIDs) } // 发送联盟邮件 func (p *actorLeague) SendLeagueMail(leagueMemberTable *db.MapLeagueMemberTable, opPlayerID, mailID int64, mailType int32, title, content string, top bool) int32 { leagueMailTable, found := db.GetMapLeagueMailTable(p.leagueID) if !found { return code.LeagueMailDataNotExist } if enum.VaildateLeagueMailType(mailType) { return code.MailTypeError } var ( leagueMailLimit = data.Const.GetLeagueMailLimit(mailType) leagueMailList = leagueMailTable.GetLeagueMails(mailType) ) // 当前存在类型邮件列表数据 且 当前为法令邮件 且 当前非更新邮件内容 判断是否达到上限 if mailType == enum.MailType_Decree && mailID <= 0 { // 检测邮件数量是否超过限制 法令数量超出上限后不能添加 其他是删除旧的数据 if leagueMailList.Size() >= leagueMailLimit { return code.MailNumLimit } } // 检测邮件标题&内容是否合法 if !utils.CheckStrLen(title, data.Const.LeagueMailTitleLimit) { return code.MailTitleError } if !utils.CheckStrLen(content, data.Const.LeagueMailContentLimit) { return code.MailContentError } newMailID := int64(0) // 如果是法令邮件更新 删除旧法令邮件数据 if mailType == enum.MailType_Decree && mailID > 0 { _, ok := leagueMailList.DeleteLeagueMail(mailID) if !ok { return code.MailDataNotFound } // 更新法令邮件 沿用旧的邮件ID newMailID = mailID } // 新增邮件 if newMailID <= 0 { newMailID = leagueMailTable.NewLeagueMailID() } // 创建一封邮件 联盟邮件更新出了发送人&发送人原来的职位不变,其他都变,且需要新的红点 newLeagueMail := &dbLeague.LeagueMail{ MailID: newMailID, Title: title, Content: content, FromPlayerID: opPlayerID, Job: leagueMemberTable.LeagueMember.GetLeagueMemberJob(opPlayerID), SendTime: ctime.Now().ToSecond(), ReadPlayerIDs: types.List[int64]{}, DelPlayerIDs: types.List[int64]{}, } // 获取玩家名称 playerData := redis.Game.GetPlayerData(opPlayerID, nameRedis.PlayerNameFields...) if playerData != nil { newLeagueMail.FromPlayerName = playerData.PlayerName } leagueMailList.AddLeagueMail(newLeagueMail) removeMailIDs := []int64{} // 检测邮件是否已满 for leagueMailLimit > 0 && leagueMailList.Size() > leagueMailLimit { leagueMail, ok := leagueMailList.DeleteLeagueMailIndex(0) if !ok { break } removeMailIDs = append(removeMailIDs, leagueMail.MailID) } // 当前法令邮件才能设置置顶 if mailType == enum.MailType_Decree && top { leagueMailList.TopMailID = newLeagueMail.MailID } // 更新保存邮件 leagueMailTable.Save2Queue() // 通知联盟所有在线玩家 leagueMail.Service().ChangePush(leagueMemberTable.GetLeagueMemberIDs(), mailType, leagueMailList.TopMailID, removeMailIDs, newLeagueMail) return code.OK }