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
MaxMembers int
}
type GuildLog struct {
GuildId int
AccountId string
LogType uint16
Content string
}
func (g *Guild) findMemberIndex(AccountId string) int {
for i, member := range g.Members {

View File

@ -4,20 +4,21 @@ import (
"cs"
"errors"
"fmt"
"math/rand"
)
type GuildMgr struct {
cs.MsgHandlerImpl
Guilds map[int]*Guild // 公会ID -> 公会列表
pendingAccountIds map[int][]string // 公会ID -> 申请者账户ID列表
Logs []string // 公会日志
Guilds map[int]*Guild // 公会ID -> 公会列表
PendingAccountIds map[int][]string // 公会ID -> 申请者账户ID列表
Logs map[int][]*GuildLog // 公会ID -> 公会日志
}
func NewGuildMgr() *GuildMgr {
return &GuildMgr{
Guilds: make(map[int]*Guild),
pendingAccountIds: make(map[int][]string),
Logs: []string{},
PendingAccountIds: make(map[int][]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")
}
gm.pendingAccountIds[guildID] = append(gm.pendingAccountIds[guildID], applicantAccountId)
gm.PendingAccountIds[guildID] = append(gm.PendingAccountIds[guildID], applicantAccountId)
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 {
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 {
return err
}
gm.pendingAccountIds[guildID] = append(pendingAccountIds[:i], pendingAccountIds[i+1:]...)
gm.PendingAccountIds[guildID] = append(pendingAccountIds[:i], pendingAccountIds[i+1:]...)
return nil
}
}
@ -99,14 +100,14 @@ func (gm *GuildMgr) ApproveApplication(guildID int, applicantAccountId string) e
// RejectApplication 拒绝申请加入公会
func (gm *GuildMgr) RejectApplication(guildID int, applicantAccountId string) error {
pendingAccountIds, exists := gm.pendingAccountIds[guildID]
pendingAccountIds, exists := gm.PendingAccountIds[guildID]
if !exists {
return errors.New("no pending applications for this guild")
}
for i, accountId := range pendingAccountIds {
if accountId == applicantAccountId {
gm.pendingAccountIds[guildID] = append(pendingAccountIds[:i], pendingAccountIds[i+1:]...)
gm.PendingAccountIds[guildID] = append(pendingAccountIds[:i], pendingAccountIds[i+1:]...)
return nil
}
}
@ -211,7 +212,18 @@ func (gm *GuildMgr) DemoteMember(guildID int, memberID string) error {
}
// 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 根据关键字搜索公会
@ -225,22 +237,23 @@ func (gm *GuildMgr) SearchGuilds(keyword string) []Guild {
return results
}
// RandomGuilds 随机显示公会
// RandomGuilds 随机显示10个公会
func (gm *GuildMgr) RandomGuilds() []Guild {
//const numRandomGuilds = 10
//count := len(gm.Guilds)
//if count <= numRandomGuilds {
// return gm.Guilds
//}
//randIndices := rand.Perm(count)[:numRandomGuilds]
//var results []Guild
//for _, idx := range randIndices {
// results = append(results, gm.Guilds[idx])
//}
//return results
return nil
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]
if !exists {
@ -253,11 +266,15 @@ func (gm *GuildMgr) Disband(guildID int, accountId string) error {
guildName := guild.Name
guild.Members = nil
delete(gm.pendingAccountIds, guildID)
delete(gm.Guilds, guildID)
// Write log about the guild disbandment
gm.WriteLog("Guild disbanded: " + guildName)
gm.Guilds[guildID] = nil
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
}