From f279de7ba3397ccec26ffad27f130eb8bc4307da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AE=B7=E5=8B=87?= Date: Fri, 1 Sep 2023 11:58:53 +0800 Subject: [PATCH] save --- server/imserver/guild.go | 29 +++++++++++++++ server/imserver/guilddbmgr.go | 13 +++---- server/imserver/guildmgr.go | 67 +++++++++++++++++++++++------------ 3 files changed, 81 insertions(+), 28 deletions(-) diff --git a/server/imserver/guild.go b/server/imserver/guild.go index b8896e1b..823dc259 100644 --- a/server/imserver/guild.go +++ b/server/imserver/guild.go @@ -33,6 +33,11 @@ type GuildLog struct { Content string } +// PendingReq 待审核请求 +type PendingReq struct { + Users map[string]bool +} + func (g *Guild) GetGuildId() int64 { return g.GuildId } @@ -104,3 +109,27 @@ func (g *Guild) SetNotice(notice *string) { func (g *Guild) GetNotice() string { return g.Notice } + +func (p *PendingReq) GetUsers() map[string]bool { + return p.Users +} + +func (p *PendingReq) AddUser(accountId string) { + if _, exists := p.Users[accountId]; !exists { + p.Users[accountId] = true + return + } + p.Users[accountId] = true +} + +func (p *PendingReq) RemoveUser(accountId string) { + delete(p.Users, accountId) +} + +func (p *PendingReq) IsInReq(accountId string) bool { + user, exists := p.Users[accountId] + if exists { + return user == true + } + return false +} diff --git a/server/imserver/guilddbmgr.go b/server/imserver/guilddbmgr.go index 8e135f46..362246e1 100644 --- a/server/imserver/guilddbmgr.go +++ b/server/imserver/guilddbmgr.go @@ -56,7 +56,7 @@ func (gm *GuildMgr) loadGuildFromDBResult(err error, rows *f5.DataSet) { MaxMembers: q5.ToInt32(*rows.GetByIndex(11)), } // init pendingReqs - gm.pendingReqs[guildId] = make(map[string]bool) + gm.pendingReqs[guildId] = &PendingReq{} } q5.UnSetBitFlag(&gm.loadedFlags, LoadGuildFlag) } @@ -95,10 +95,7 @@ func (gm *GuildMgr) loadGuildMemberFromDBResult(err error, rows *f5.DataSet) { Level: level, } if guild, ok := gm.guilds[guildId]; ok { - err := guild.AddMember(guildMember) - if err != nil { - f5.GetSysLog().Info("Guild:%d member is full\n", guildId) - } + guild.AddMember(guildMember) } } q5.UnSetBitFlag(&gm.loadedFlags, LoadGuildMemberFlag) @@ -130,7 +127,11 @@ func (gm *GuildMgr) loadPendingReqsFromDBResult(err error, rows *f5.DataSet) { ) guildId = q5.ToInt64(*rows.GetByIndex(0)) accountId = q5.ToString(*rows.GetByIndex(1)) - gm.pendingReqs[guildId][accountId] = true + + pendingReq := &PendingReq{} + pendingReq.Users = make(map[string]bool) + pendingReq.AddUser(accountId) + gm.pendingReqs[guildId] = pendingReq } q5.UnSetBitFlag(&gm.loadedFlags, LoadGuildReqFlag) } diff --git a/server/imserver/guildmgr.go b/server/imserver/guildmgr.go index 5477d57c..e658f5b7 100644 --- a/server/imserver/guildmgr.go +++ b/server/imserver/guildmgr.go @@ -17,9 +17,9 @@ const ( type GuildMgr struct { cs.MsgHandlerImpl - guilds map[int64]*Guild // 公会ID -> 公会列表 - pendingReqs map[int64]map[string]bool // 公会ID -> 申请者账户ID -> bool - guildLogs map[int64][]*GuildLog // 公会ID -> 公会日志 + guilds map[int64]*Guild // 公会ID -> 公会列表 + pendingReqs map[int64]*PendingReq // 公会ID -> PendingReq + guildLogs map[int64][]*GuildLog // 公会ID -> 公会日志 loadedFlags int64 guildIds []int64 // 公会ids } @@ -27,7 +27,7 @@ type GuildMgr struct { func NewGuildMgr() *GuildMgr { return &GuildMgr{ guilds: make(map[int64]*Guild), - pendingReqs: make(map[int64]map[string]bool), + pendingReqs: make(map[int64]*PendingReq), guildLogs: make(map[int64][]*GuildLog), } } @@ -112,18 +112,19 @@ func (gm *GuildMgr) ApplyToGuild(guildId int64, applicantAccountId string) error if guild.JoinCond == JoinCondStar { var userStar int32 = 200 - if userStar < guild.JoinCondValue { - return fmt.Errorf("joincond error") + if userStar >= guild.JoinCondValue { + gm.JoinGuild(guild, applicantAccountId) + return nil } } // IF exists, then replace it fields `isJoinGuild` is 0 gm.upsertPendingReqs(guildId, applicantAccountId, PendingReqIsJoinGuildStatusDefault) - pendReq, exists := gm.pendingReqs[guildId] - if !exists { - pendReq = make(map[string]bool) + pendReq, err := gm.GetPendingReq(guildId) + if err != nil { + pendReq = &PendingReq{} } - pendReq[applicantAccountId] = true + pendReq.AddUser(applicantAccountId) return nil } @@ -135,6 +136,11 @@ func (gm *GuildMgr) Approve(guildId int64, operatorAccountId, accountId string) return err } + // 通常默认为审核加入 + if guild.JoinCond != JoinCondDefault { + return fmt.Errorf("join cond error") + } + operatorMember, err := guild.GetMember(operatorAccountId) if err != nil { return err @@ -158,8 +164,13 @@ func (gm *GuildMgr) Approve(guildId int64, operatorAccountId, accountId string) } // 是否在申请队列中 - _, exists := gm.pendingReqs[guildId][accountId] - if !exists { + pendingReq, err := gm.GetPendingReq(guildId) + if err == nil { + return err + } + + inReq := pendingReq.IsInReq(accountId) + if !inReq { return fmt.Errorf("no pending applications for this guild") } @@ -171,20 +182,20 @@ func (gm *GuildMgr) Approve(guildId int64, operatorAccountId, accountId string) logContent := fmt.Sprintf("Approve[operator:%s]", operatorAccountId) gm.WriteLog(guildId, accountId, LogTypeApprove, logContent) - delete(gm.pendingReqs[guildId], accountId) + pendingReq.RemoveUser(accountId) return nil } // Reject 拒绝申请者加入公会 -func (gm *GuildMgr) Reject(guildId int64, applicantAccountId string) error { - _, exists := gm.pendingReqs[guildId] - if !exists { - return fmt.Errorf("no pending applications for this guild") +func (gm *GuildMgr) Reject(guildId int64, accountId string) error { + pendingReq, err := gm.GetPendingReq(guildId) + if err == nil { + return err } - gm.updatePendingReqs(guildId, applicantAccountId, PendingReqIsJoinGuildStatusReject) - delete(gm.pendingReqs[guildId], applicantAccountId) + gm.updatePendingReqs(guildId, accountId, PendingReqIsJoinGuildStatusReject) + pendingReq.RemoveUser(accountId) return nil } @@ -404,7 +415,7 @@ func (gm *GuildMgr) SetNotice(guildId int64, accountId string, notice *string) e // 仅会长可操作 if accountId != guild.LeaderId { - return fmt.Errorf("cannot disband guild") + return fmt.Errorf("cannot set guild notices, only leader") } guild.SetNotice(notice) @@ -489,6 +500,13 @@ func (gm *GuildMgr) GetGuild(guildId int64) (*Guild, error) { return nil, fmt.Errorf("guild not found") } +func (gm *GuildMgr) GetPendingReq(guildId int64) (*PendingReq, error) { + if pendingReq, exists := gm.pendingReqs[guildId]; exists { + return pendingReq, nil + } + return nil, fmt.Errorf("pendingReq not found, guildId:%d", guildId) +} + // GetGuildByAccountId 查询我的工会 func (gm *GuildMgr) GetGuildByAccountId(accountId string) *Guild { for _, guild := range gm.guilds { @@ -508,7 +526,12 @@ func (gm *GuildMgr) Info(accountId string) (*Guild, map[string]bool, []*GuildLog guildLogs := make([]*GuildLog, DefaultLogs) if guild != nil { guildId := guild.GuildId - pendingReqs = gm.pendingReqs[guildId] + if p, exists := gm.pendingReqs[guildId]; exists { + pendingReqs = p.GetUsers() + } else { + pendingReqs = nil + } + guildLogs = gm.guildLogs[guildId] } @@ -563,7 +586,7 @@ func (gm *GuildMgr) RemoveGuildsId(guildId int64) { func (gm *GuildMgr) GetAllPendingReqs(guildId int64) map[string]bool { if pendingReq, exists := gm.pendingReqs[guildId]; exists { - return pendingReq + return pendingReq.GetUsers() } return nil }