diff --git a/server/imserver/guildmgr.go b/server/imserver/guildmgr.go index ce129899..2d2e168a 100644 --- a/server/imserver/guildmgr.go +++ b/server/imserver/guildmgr.go @@ -239,33 +239,6 @@ func (gm *GuildMgr) WriteLog(guildID int, accountId string, logType uint16, cont gm.Logs[guildID] = append(gm.Logs[guildID], log) } -// SearchGuilds 根据关键字搜索公会 -func (gm *GuildMgr) SearchGuilds(keyword string) []Guild { - var results []Guild - for _, guild := range gm.Guilds { - if containsSubstring(guild.Name, keyword) { - results = append(results, *guild) - } - } - return results -} - -// RandomGuilds 随机显示10个公会 -func (gm *GuildMgr) RandomGuilds() []Guild { - randomGuildNum := 10 - count := len(gm.Guilds) - if count <= randomGuildNum { - randomGuildNum = count - } - // shuffle 从count50,返回一个slice []int, 包含10个元素 - randIndices := rand.Perm(count)[:randomGuildNum] - var results []Guild - for _, idx := range randIndices { - results = append(results, *gm.Guilds[idx]) - } - return results -} - // Disband 解散公会 func (gm *GuildMgr) Disband(guildID int, accountId string) error { guild, exists := gm.Guilds[guildID] @@ -283,6 +256,8 @@ func (gm *GuildMgr) Disband(guildID int, accountId string) error { gm.Guilds[guildID] = nil gm.PendingAccountIds[guildID] = nil gm.Logs[guildID] = nil + + // 确保在删除之前没有其他地方引用了公会对象的指针, 以避免空指针异常 delete(gm.Guilds, guildID) delete(gm.PendingAccountIds, guildID) delete(gm.Logs, guildID) @@ -292,6 +267,59 @@ func (gm *GuildMgr) Disband(guildID int, accountId string) error { return nil } +// SearchGuilds 根据关键字搜索公会 +func (gm *GuildMgr) SearchGuilds(keyword string) []Guild { + var results []Guild + for _, guild := range gm.Guilds { + if containsSubstring(guild.Name, keyword) { + results = append(results, *guild) + } + } + return results +} + +// RandomGuilds 随机10个公会 +func (gm *GuildMgr) RandomGuilds() []Guild { + randomGuildNum := 10 + count := len(gm.Guilds) + if count <= randomGuildNum { + randomGuildNum = count + } + // shuffle 从count50,返回一个slice []int, 包含10个元素 + randIndices := rand.Perm(count)[:randomGuildNum] + var results []Guild + for _, idx := range randIndices { + results = append(results, *gm.Guilds[idx]) + } + return results +} + +// GetGuildByAccountId 查询我的工会 +func (gm *GuildMgr) GetGuildByAccountId(accountId string) *Guild { + for _, guild := range gm.Guilds { + for _, member := range guild.Members { + if accountId == member.AccountId { + return guild + } + } + } + return nil +} + +// Info 工会信息 +func (gm *GuildMgr) Info(accountId string) (*Guild, []string, []*GuildLog) { + guild := gm.GetGuildByAccountId(accountId) + pendingAccountIds := make([]string, 10) + guildLogs := make([]*GuildLog, 10) + if guild != nil { + guildId := guild.ID + pendingAccountIds = gm.PendingAccountIds[guildId] + guildLogs = gm.Logs[guildId] + } + + return guild, pendingAccountIds, guildLogs +} + func containsSubstring(s, substr string) bool { return len(s) >= len(substr) && s[len(s)-len(substr):] == substr }