公会
This commit is contained in:
parent
6a0709efd4
commit
cf7a3f542f
@ -26,6 +26,10 @@ type GuildLog struct {
|
|||||||
Content string
|
Content string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *Guild) GetGuildId() int64 {
|
||||||
|
return g.GuildId
|
||||||
|
}
|
||||||
|
|
||||||
// findMemberIndex 根据 AccountId 查找成员在 Members 切片中的索引
|
// findMemberIndex 根据 AccountId 查找成员在 Members 切片中的索引
|
||||||
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 {
|
||||||
@ -46,15 +50,20 @@ func (g *Guild) GetMember(accountId string) (*GuildMember, error) {
|
|||||||
return g.Members[index], nil
|
return g.Members[index], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// isMember 是否是公会成员
|
// IsMember 是否是公会成员
|
||||||
func (g *Guild) isMember(accountId string) bool {
|
func (g *Guild) IsMember(accountId string) bool {
|
||||||
index := g.findMemberIndex(accountId)
|
index := g.findMemberIndex(accountId)
|
||||||
return index >= 0
|
return index >= 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsFull 成员是否已满
|
||||||
|
func (g *Guild) IsFull() bool {
|
||||||
|
return len(g.Members) >= g.MaxMembers
|
||||||
|
}
|
||||||
|
|
||||||
// AddMember 添加成员
|
// AddMember 添加成员
|
||||||
func (g *Guild) AddMember(member *GuildMember) error {
|
func (g *Guild) AddMember(member *GuildMember) error {
|
||||||
if len(g.Members) >= g.MaxMembers {
|
if g.IsFull() {
|
||||||
return fmt.Errorf("guild is full")
|
return fmt.Errorf("guild is full")
|
||||||
}
|
}
|
||||||
g.Members = append(g.Members, member)
|
g.Members = append(g.Members, member)
|
||||||
@ -71,7 +80,6 @@ func (g *Guild) RemoveMember(accountId string) error {
|
|||||||
if index == -1 {
|
if index == -1 {
|
||||||
return fmt.Errorf("member not found")
|
return fmt.Errorf("member not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
//g.Members = append(g.Members[:index], g.Members[index+1:]...)
|
//g.Members = append(g.Members[:index], g.Members[index+1:]...)
|
||||||
// 使用 copy 方法来删除切片中的元素,保证数据一致性
|
// 使用 copy 方法来删除切片中的元素,保证数据一致性
|
||||||
copy(g.Members[index:], g.Members[index+1:])
|
copy(g.Members[index:], g.Members[index+1:])
|
||||||
@ -80,7 +88,3 @@ func (g *Guild) RemoveMember(accountId string) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Guild) GetGuildId() int64 {
|
|
||||||
return g.GuildId
|
|
||||||
}
|
|
||||||
|
@ -90,7 +90,8 @@ func (gm *GuildMgr) CreateGuild(name string, leaderId string) (int64, error) {
|
|||||||
|
|
||||||
// ApplyToGuild 申请加入公会
|
// ApplyToGuild 申请加入公会
|
||||||
func (gm *GuildMgr) ApplyToGuild(guildId int64, applicantAccountId string) error {
|
func (gm *GuildMgr) ApplyToGuild(guildId int64, applicantAccountId string) error {
|
||||||
if _, exists := gm.Guilds[guildId]; !exists {
|
guild := gm.GetGuild(guildId)
|
||||||
|
if guild == nil {
|
||||||
return fmt.Errorf("guild not found")
|
return fmt.Errorf("guild not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,8 +101,7 @@ func (gm *GuildMgr) ApplyToGuild(guildId int64, applicantAccountId string) error
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
guild := gm.Guilds[guildId]
|
if guild.IsFull() {
|
||||||
if len(guild.Members) >= guild.MaxMembers {
|
|
||||||
return fmt.Errorf("guild is full")
|
return fmt.Errorf("guild is full")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,8 +118,8 @@ func (gm *GuildMgr) ApplyToGuild(guildId int64, applicantAccountId string) error
|
|||||||
|
|
||||||
// Approve 同意申请者加入公会
|
// Approve 同意申请者加入公会
|
||||||
func (gm *GuildMgr) Approve(guildId int64, operatorAccountId, accountId string) error {
|
func (gm *GuildMgr) Approve(guildId int64, operatorAccountId, accountId string) error {
|
||||||
guild, exists := gm.Guilds[guildId]
|
guild := gm.GetGuild(guildId)
|
||||||
if !exists {
|
if guild == nil {
|
||||||
return fmt.Errorf("guild not found")
|
return fmt.Errorf("guild not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,12 +134,14 @@ func (gm *GuildMgr) Approve(guildId int64, operatorAccountId, accountId string)
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 是否加入其他公会
|
||||||
_, err = gm.checkJoinGuild(accountId)
|
_, err = gm.checkJoinGuild(accountId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, exists = gm.pendingReqs[guildId][accountId]
|
// 是否在申请队列中
|
||||||
|
_, exists := gm.pendingReqs[guildId][accountId]
|
||||||
if !exists {
|
if !exists {
|
||||||
return fmt.Errorf("no pending applications for this guild")
|
return fmt.Errorf("no pending applications for this guild")
|
||||||
}
|
}
|
||||||
@ -199,8 +201,8 @@ func (gm *GuildMgr) JoinGuild(guildId int64, accountId string) error {
|
|||||||
|
|
||||||
// LeaveGuild 离开公会
|
// LeaveGuild 离开公会
|
||||||
func (gm *GuildMgr) LeaveGuild(guildId int64, accountId string) error {
|
func (gm *GuildMgr) LeaveGuild(guildId int64, accountId string) error {
|
||||||
guild, exists := gm.Guilds[guildId]
|
guild := gm.GetGuild(guildId)
|
||||||
if !exists {
|
if guild == nil {
|
||||||
return fmt.Errorf("guild not found")
|
return fmt.Errorf("guild not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -225,8 +227,8 @@ func (gm *GuildMgr) LeaveGuild(guildId int64, accountId string) error {
|
|||||||
|
|
||||||
// DismissMember 开除成员 踢出
|
// DismissMember 开除成员 踢出
|
||||||
func (gm *GuildMgr) DismissMember(guildId int64, operatorAccountId, accountId string) error {
|
func (gm *GuildMgr) DismissMember(guildId int64, operatorAccountId, accountId string) error {
|
||||||
guild, exists := gm.Guilds[guildId]
|
guild := gm.GetGuild(guildId)
|
||||||
if !exists {
|
if guild == nil {
|
||||||
return fmt.Errorf("guild not found")
|
return fmt.Errorf("guild not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -270,8 +272,8 @@ func (gm *GuildMgr) DismissMember(guildId int64, operatorAccountId, accountId st
|
|||||||
|
|
||||||
// PromoteMember 提升成员为干部
|
// PromoteMember 提升成员为干部
|
||||||
func (gm *GuildMgr) PromoteMember(guildId int64, operatorAccountId, accountId string) error {
|
func (gm *GuildMgr) PromoteMember(guildId int64, operatorAccountId, accountId string) error {
|
||||||
guild, exists := gm.Guilds[guildId]
|
guild := gm.GetGuild(guildId)
|
||||||
if !exists {
|
if guild == nil {
|
||||||
return fmt.Errorf("guild not found")
|
return fmt.Errorf("guild not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -314,8 +316,8 @@ func (gm *GuildMgr) PromoteMember(guildId int64, operatorAccountId, accountId st
|
|||||||
|
|
||||||
// DemoteMember 解除成员干部身份
|
// DemoteMember 解除成员干部身份
|
||||||
func (gm *GuildMgr) DemoteMember(guildId int64, operatorAccountId, accountId string) error {
|
func (gm *GuildMgr) DemoteMember(guildId int64, operatorAccountId, accountId string) error {
|
||||||
guild, exists := gm.Guilds[guildId]
|
guild := gm.GetGuild(guildId)
|
||||||
if !exists {
|
if guild == nil {
|
||||||
return fmt.Errorf("guild not found")
|
return fmt.Errorf("guild not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -358,8 +360,8 @@ func (gm *GuildMgr) DemoteMember(guildId int64, operatorAccountId, accountId str
|
|||||||
|
|
||||||
// Disband 解散公会
|
// Disband 解散公会
|
||||||
func (gm *GuildMgr) Disband(guildId int64, accountId string) error {
|
func (gm *GuildMgr) Disband(guildId int64, accountId string) error {
|
||||||
guild, exists := gm.Guilds[guildId]
|
guild := gm.GetGuild(guildId)
|
||||||
if !exists {
|
if guild == nil {
|
||||||
return fmt.Errorf("guild not found")
|
return fmt.Errorf("guild not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -396,6 +398,11 @@ func (gm *GuildMgr) Disband(guildId int64, accountId string) error {
|
|||||||
|
|
||||||
// WriteLog 记录公会日志
|
// WriteLog 记录公会日志
|
||||||
func (gm *GuildMgr) WriteLog(guildId int64, accountId string, logType uint16, content string) {
|
func (gm *GuildMgr) WriteLog(guildId int64, accountId string, logType uint16, content string) {
|
||||||
|
guild := gm.GetGuild(guildId)
|
||||||
|
if guild == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
_, exists := gm.Logs[guildId]
|
_, exists := gm.Logs[guildId]
|
||||||
if !exists {
|
if !exists {
|
||||||
gm.Logs[guildId] = make([]*GuildLog, DefaultLogs)
|
gm.Logs[guildId] = make([]*GuildLog, DefaultLogs)
|
||||||
@ -454,6 +461,13 @@ func (gm *GuildMgr) RandomGuilds() []Guild {
|
|||||||
return results
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (gm *GuildMgr) GetGuild(guildId int64) *Guild {
|
||||||
|
if guild, exists := gm.Guilds[guildId]; exists {
|
||||||
|
return guild
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetGuildByAccountId 查询我的工会
|
// GetGuildByAccountId 查询我的工会
|
||||||
func (gm *GuildMgr) GetGuildByAccountId(accountId string) *Guild {
|
func (gm *GuildMgr) GetGuildByAccountId(accountId string) *Guild {
|
||||||
for _, guild := range gm.Guilds {
|
for _, guild := range gm.Guilds {
|
||||||
@ -466,13 +480,6 @@ func (gm *GuildMgr) GetGuildByAccountId(accountId string) *Guild {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gm *GuildMgr) getGuild(guildId int64) *Guild {
|
|
||||||
if guild, ok := gm.Guilds[guildId]; ok {
|
|
||||||
return guild
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Info 工会信息
|
// Info 工会信息
|
||||||
func (gm *GuildMgr) Info(accountId string) (*Guild, map[string]bool, []*GuildLog) {
|
func (gm *GuildMgr) Info(accountId string) (*Guild, map[string]bool, []*GuildLog) {
|
||||||
guild := gm.GetGuildByAccountId(accountId)
|
guild := gm.GetGuildByAccountId(accountId)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user