aozhiwei eaf14d6b2d 1
2024-04-09 18:03:58 +08:00

278 lines
6.4 KiB
Go

package guild
import (
"q5"
"f5"
"cs"
"github.com/golang/protobuf/proto"
"main/constant"
. "main/global"
)
type guild struct {
guildId string
guildName string
ownerId string
creatorId string
badge int32
notice string
joinCondType int32
joinCondVal int32
maxMemberNum int32
createTime int32
modifyTime int32
totalStars int32
idHash map[string]*member
}
func (this *guild) GetGuildId() string {
return this.guildId
}
func (this *guild) GetGuildName() string {
return this.guildName
}
func (this *guild) GetMemberNum() int32 {
return int32(len(this.idHash))
}
func (this *guild) loadFromDb(ds *f5.DataSet) {
this.guildId = ds.GetByName("guild_id")
this.guildName = ds.GetByName("guild_name")
this.ownerId = ds.GetByName("owner_id")
this.creatorId = ds.GetByName("creator_id")
this.badge = q5.ToInt32(ds.GetByName("badge"))
this.notice = ds.GetByName("notice")
this.joinCondType = q5.ToInt32(ds.GetByName("join_cond_type"))
this.joinCondVal = q5.ToInt32(ds.GetByName("join_cond_val"))
this.maxMemberNum = q5.ToInt32(ds.GetByName("max_members"))
this.createTime = q5.ToInt32(ds.GetByName("createtime"))
this.modifyTime = q5.ToInt32(ds.GetByName("modifytime"))
}
func (this *guild) addMember(m *member) {
this.idHash[m.memberId] = m
}
func (this *guild) getMember(accountId string) *member {
if m, ok := this.idHash[accountId]; ok {
return m
}
return nil
}
func (this *guild) traverseMembers(cb func(*member) bool) {
for _, m := range(this.idHash) {
if !cb(m) {
break
}
}
}
func (this *guild) isOwner(accountId string) bool {
if m := this.getMember(accountId); m != nil {
return m.guildJob == constant.GuildMemberLevelLeader
}
return false
}
func (this *guild) updateByKv(kv map[int32]string) {
for k, v := range(kv) {
if k == constant.GUILD_UPDATE_FIELD_NOTICE {
this.notice = v
} else if k == constant.GUILD_UPDATE_FIELD_AVATAR {
this.badge = q5.ToInt32(v)
} else if k == constant.GUILD_UPDATE_FIELD_NAME {
this.guildName = v
} else if k == constant.GUILD_UPDATE_FIELD_COND_TYPE {
this.joinCondType = q5.ToInt32(v)
} else if k == constant.GUILD_UPDATE_FIELD_COND_VALUE {
this.joinCondVal = q5.ToInt32(v)
}
}
}
func (this *guild) disband() {
{
f5.GetJsStyleDb().Update(
constant.FRIEND_DB,
"t_guild_member",
[][]string{
{"deleted", "1"},
},
[][]string{
{"guild_id", this.guildId},
},
func (err error, lastInsertId int64, rowsAffected int64) {
})
}
{
f5.GetJsStyleDb().Update(
constant.FRIEND_DB,
"t_guild_apply",
[][]string{
{"deleted", "1"},
},
[][]string{
{"guild_id", this.guildId},
},
func (err error, lastInsertId int64, rowsAffected int64) {
})
}
{
delete(_guildMgr.idHash, this.guildId)
delete(_guildMgr.nameHash, this.guildName)
this.traverseMembers(
func (m *member) bool {
delete(_guildMgr.memberIdHash, m.memberId)
return true
})
}
}
func (this *guild) AsyncFillMFGuild(pbGuild *cs.MFGuild, cb func(int32, string)) {
pbGuild.AutoId = proto.Int64(0)
pbGuild.GuildId = proto.Int64(q5.ToInt64(this.guildId))
pbGuild.Name = proto.String(this.guildName)
pbGuild.LeaderId = proto.String(this.ownerId)
pbGuild.Avatar = proto.Int32(this.badge)
pbGuild.Notice = proto.String(this.notice)
pbGuild.JoinCond = proto.Int32(this.joinCondType)
pbGuild.JoinCondValue = proto.Int32(this.joinCondVal)
pbGuild.TotalStars = proto.Int32(0)
pbGuild.TotalKills = proto.Int32(0)
pbGuild.ChickenDinners = proto.Int32(0)
pbGuild.MaxMembers = proto.Int32(this.maxMemberNum)
accountIds := []string{}
for _, m := range(this.idHash) {
q5.AppendSlice(&accountIds, m.memberId)
}
GetCacheMgr().AsyncGetUsers(
accountIds,
func (errCode int32, errMsg string) {
if errCode != 0 {
cb(500, "server internal error")
return
}
q5.NewSlice(&pbGuild.Members, 0, int32(len(accountIds)))
for _, accountId := range(accountIds) {
u := GetCacheMgr().GetUserProfile(accountId)
if u != nil {
pbMember := new(cs.MFGuildMember)
u.FillMFGuildMember(pbMember)
q5.AppendSlice(&pbGuild.Members, pbMember)
}
}
cb(0, "")
})
}
func (this *guild) asyncLeave(accountId string, cb func(int32, string)) {
m := this.getMember(accountId)
if m == nil {
cb(0, "")
return
}
f5.GetJsStyleDb().Update(
constant.FRIEND_DB,
"t_guild_member",
[][]string{
{"deleted", "1"},
},
[][]string{
{"guild_id", this.guildId},
{"member_id", m.memberId},
},
func (err error, lastInsertId int64, rowsAffected int64) {
if err != nil {
cb(500, "server internal error")
return
}
delete(this.idHash, m.memberId)
delete(_guildMgr.memberIdHash, m.memberId)
if m.guildJob == constant.GuildMemberLevelLeader {
var nextLeader *member
this.traverseMembers(
func (ele *member) bool {
if ele != m {
if nextLeader == nil {
nextLeader = m
} else if ele.guildJob < nextLeader.guildJob {
nextLeader = m
}
}
return true
})
if nextLeader != nil {
f5.GetJsStyleDb().Update(
constant.FRIEND_DB,
"t_guild_member",
[][]string{
{"guild_job", q5.ToString(constant.GuildMemberLevelLeader)},
},
[][]string{
{"guild_id", this.guildId},
{"member_id", m.memberId},
},
func (err error, lastInsertId int64, rowsAffected int64) {
if err != nil {
cb(500, "server internal error")
return
}
nextLeader.guildJob = constant.GuildMemberLevelLeader
cb(0, "")
return
})
}
}
if this.GetMemberNum() <= 0 {
f5.GetJsStyleDb().Update(
constant.FRIEND_DB,
"t_guild",
[][]string{
{"deleted", "1"},
},
[][]string{
{"guild_id", this.guildId},
},
func (err error, lastInsertId int64, rowsAffected int64) {
if err != nil {
cb(500, "server internal error")
return
}
this.disband()
cb(0, "")
return
})
}
})
}
func (this *guild) asyncUpdateOwner(ownerId string, cb func(int32, string)) {
f5.GetJsStyleDb().Update(
constant.FRIEND_DB,
"t_guild",
[][]string{
{"owner_id", ownerId},
},
[][]string{
{"guild_id", this.guildId},
},
func (err error, lastInsertId int64, rowsAffected int64) {
if err != nil {
cb(500, "server internal error")
return
}
this.ownerId = ownerId
cb(0, "")
return
})
}
func newGuild() *guild {
p := new(guild)
p.idHash = make(map[string]*member)
return p
}