This commit is contained in:
殷勇 2023-08-17 15:31:47 +08:00
parent 65d12328be
commit 80383c5bb8
2 changed files with 51 additions and 28 deletions

View File

@ -15,6 +15,12 @@ type Guild struct {
Members []GuildMember Members []GuildMember
MaxMembers int MaxMembers int
} }
type GuildLog struct {
GuildId int
AccountId string
LogType uint16
Content string
}
func (g *Guild) findMemberIndex(AccountId string) int { func (g *Guild) findMemberIndex(AccountId string) int {
for i, member := range g.Members { for i, member := range g.Members {

View File

@ -4,20 +4,21 @@ import (
"cs" "cs"
"errors" "errors"
"fmt" "fmt"
"math/rand"
) )
type GuildMgr struct { type GuildMgr struct {
cs.MsgHandlerImpl cs.MsgHandlerImpl
Guilds map[int]*Guild // 公会ID -> 公会列表 Guilds map[int]*Guild // 公会ID -> 公会列表
pendingAccountIds map[int][]string // 公会ID -> 申请者账户ID列表 PendingAccountIds map[int][]string // 公会ID -> 申请者账户ID列表
Logs []string // 公会日志 Logs map[int][]*GuildLog // 公会ID -> 公会日志
} }
func NewGuildMgr() *GuildMgr { func NewGuildMgr() *GuildMgr {
return &GuildMgr{ return &GuildMgr{
Guilds: make(map[int]*Guild), Guilds: make(map[int]*Guild),
pendingAccountIds: make(map[int][]string), PendingAccountIds: make(map[int][]string),
Logs: []string{}, Logs: make(map[int][]*GuildLog),
} }
} }
@ -57,7 +58,7 @@ func (gm *GuildMgr) ApplyToGuild(guildID int, applicantAccountId string) error {
return errors.New("guild is full") return errors.New("guild is full")
} }
gm.pendingAccountIds[guildID] = append(gm.pendingAccountIds[guildID], applicantAccountId) gm.PendingAccountIds[guildID] = append(gm.PendingAccountIds[guildID], applicantAccountId)
return nil return nil
} }
@ -77,7 +78,7 @@ func (gm *GuildMgr) ApproveApplication(guildID int, applicantAccountId string) e
} }
} }
pendingAccountIds, pendingExists := gm.pendingAccountIds[guildID] pendingAccountIds, pendingExists := gm.PendingAccountIds[guildID]
if !pendingExists { if !pendingExists {
return errors.New("no pending applications for this guild") return errors.New("no pending applications for this guild")
} }
@ -89,7 +90,7 @@ func (gm *GuildMgr) ApproveApplication(guildID int, applicantAccountId string) e
if err != nil { if err != nil {
return err return err
} }
gm.pendingAccountIds[guildID] = append(pendingAccountIds[:i], pendingAccountIds[i+1:]...) gm.PendingAccountIds[guildID] = append(pendingAccountIds[:i], pendingAccountIds[i+1:]...)
return nil return nil
} }
} }
@ -99,14 +100,14 @@ func (gm *GuildMgr) ApproveApplication(guildID int, applicantAccountId string) e
// RejectApplication 拒绝申请加入公会 // RejectApplication 拒绝申请加入公会
func (gm *GuildMgr) RejectApplication(guildID int, applicantAccountId string) error { func (gm *GuildMgr) RejectApplication(guildID int, applicantAccountId string) error {
pendingAccountIds, exists := gm.pendingAccountIds[guildID] pendingAccountIds, exists := gm.PendingAccountIds[guildID]
if !exists { if !exists {
return errors.New("no pending applications for this guild") return errors.New("no pending applications for this guild")
} }
for i, accountId := range pendingAccountIds { for i, accountId := range pendingAccountIds {
if accountId == applicantAccountId { if accountId == applicantAccountId {
gm.pendingAccountIds[guildID] = append(pendingAccountIds[:i], pendingAccountIds[i+1:]...) gm.PendingAccountIds[guildID] = append(pendingAccountIds[:i], pendingAccountIds[i+1:]...)
return nil return nil
} }
} }
@ -211,7 +212,18 @@ func (gm *GuildMgr) DemoteMember(guildID int, memberID string) error {
} }
// WriteLog 记录公会日志 // WriteLog 记录公会日志
func (gm *GuildMgr) WriteLog(logEntry string) { func (gm *GuildMgr) WriteLog(guildID int, accountId string, logType uint16, content string) {
_, exists := gm.Logs[guildID]
if !exists {
gm.Logs[guildID] = make([]*GuildLog, 0)
}
log := &GuildLog{
GuildId: guildID,
AccountId: accountId,
LogType: logType,
Content: content,
}
gm.Logs[guildID] = append(gm.Logs[guildID], log)
} }
// SearchGuilds 根据关键字搜索公会 // SearchGuilds 根据关键字搜索公会
@ -225,22 +237,23 @@ func (gm *GuildMgr) SearchGuilds(keyword string) []Guild {
return results return results
} }
// RandomGuilds 随机显示公会 // RandomGuilds 随机显示10个公会
func (gm *GuildMgr) RandomGuilds() []Guild { func (gm *GuildMgr) RandomGuilds() []Guild {
//const numRandomGuilds = 10 randomGuildNum := 10
//count := len(gm.Guilds) count := len(gm.Guilds)
//if count <= numRandomGuilds { if count <= randomGuildNum {
// return gm.Guilds randomGuildNum = count
//} }
//randIndices := rand.Perm(count)[:numRandomGuilds] // shuffle 从count50返回一个slice []int, 包含10个元素
//var results []Guild randIndices := rand.Perm(count)[:randomGuildNum]
//for _, idx := range randIndices { var results []Guild
// results = append(results, gm.Guilds[idx]) for _, idx := range randIndices {
//} results = append(results, *gm.Guilds[idx])
//return results }
return nil return results
} }
// Disband 解散公会
func (gm *GuildMgr) Disband(guildID int, accountId string) error { func (gm *GuildMgr) Disband(guildID int, accountId string) error {
guild, exists := gm.Guilds[guildID] guild, exists := gm.Guilds[guildID]
if !exists { if !exists {
@ -253,11 +266,15 @@ func (gm *GuildMgr) Disband(guildID int, accountId string) error {
guildName := guild.Name guildName := guild.Name
guild.Members = nil guild.Members = nil
delete(gm.pendingAccountIds, guildID)
delete(gm.Guilds, guildID)
// Write log about the guild disbandment gm.Guilds[guildID] = nil
gm.WriteLog("Guild disbanded: " + guildName) gm.PendingAccountIds[guildID] = nil
gm.Logs[guildID] = nil
delete(gm.Guilds, guildID)
delete(gm.PendingAccountIds, guildID)
delete(gm.Logs, guildID)
gm.WriteLog(guildID, accountId, 1, "Guild disbanded: "+guildName)
return nil return nil
} }