953 lines
25 KiB
Go
953 lines
25 KiB
Go
package guild
|
|
|
|
import (
|
|
"q5"
|
|
"f5"
|
|
"main/common"
|
|
"main/constant"
|
|
"main/model"
|
|
"fmt"
|
|
"main/mt"
|
|
"strings"
|
|
. "main/global"
|
|
"github.com/golang/protobuf/proto"
|
|
)
|
|
|
|
type guildMgr struct {
|
|
idHash map[string]*guild
|
|
nameHash map[string]*guild
|
|
guildRankList []*guild
|
|
guildRankHash map[string]*guild
|
|
memberIdHash map[string]*member
|
|
usingNameHash map[string]int64
|
|
}
|
|
|
|
func (this *guildMgr) Init() {
|
|
this.idHash = make(map[string]*guild)
|
|
this.nameHash = make(map[string]*guild)
|
|
this.guildRankList = []*guild{}
|
|
this.guildRankHash = make(map[string]*guild)
|
|
this.memberIdHash = make(map[string]*member)
|
|
this.usingNameHash = make(map[string]int64)
|
|
this.loadFromDB()
|
|
}
|
|
|
|
func (this *guildMgr) UnInit() {
|
|
}
|
|
|
|
func (this *guildMgr) loadFromDB() {
|
|
this.loadGuild()
|
|
this.loadGuildMember()
|
|
this.clearEmptyGuild()
|
|
this.rearrangement()
|
|
}
|
|
|
|
func (this *guildMgr) loadGuild() {
|
|
f5.GetSysLog().Info("loadGuild begin")
|
|
lastIdx := f5.GetJsStyleDb().SyncBatchLoadFullTable(
|
|
constant.FRIEND_DB,
|
|
"SELECT * FROM t_guild WHERE idx > %d AND deleted = 0",
|
|
func (ds *f5.DataSet) {
|
|
p := newGuild()
|
|
p.loadFromDb(ds)
|
|
this.addGuild(p)
|
|
},
|
|
func (err error) {
|
|
panic(fmt.Sprintf("loadGuild dberror:%s", err))
|
|
})
|
|
f5.GetSysLog().Info("loadGuild end lastIdx:%d idNum:%d nameNum:%d",
|
|
lastIdx,
|
|
len(this.idHash),
|
|
len(this.nameHash))
|
|
}
|
|
|
|
func (this *guildMgr) loadGuildMember() {
|
|
f5.GetSysLog().Info("loadGuildMember begin")
|
|
lastIdx := f5.GetJsStyleDb().SyncBatchLoadFullTable(
|
|
constant.FRIEND_DB,
|
|
"SELECT * FROM t_guild_member WHERE idx > %d AND deleted = 0",
|
|
func (ds *f5.DataSet) {
|
|
guildId := ds.GetByName("guild_id")
|
|
guildJob := q5.ToInt32(ds.GetByName("guild_job"))
|
|
memberId := ds.GetByName("member_id")
|
|
joinTime := q5.ToInt32(ds.GetByName("join_time"))
|
|
g := this.internalGetGuildByGuildId(guildId)
|
|
if g != nil {
|
|
p := newMember()
|
|
p.init(g, guildJob, memberId, joinTime)
|
|
g.addMember(p)
|
|
this.addGuildMember(p)
|
|
}
|
|
},
|
|
func (err error) {
|
|
panic(fmt.Sprintf("loadGuildMember dberror:%s", err))
|
|
})
|
|
f5.GetSysLog().Info("loadGuildMember end memberNum:%d",
|
|
lastIdx,
|
|
len(this.memberIdHash))
|
|
}
|
|
|
|
func (this *guildMgr) isNameTooLong(name string) bool {
|
|
return len(name) > 15
|
|
}
|
|
|
|
func (this *guildMgr) isValidName(name string) bool {
|
|
return !strings.Contains(name, "!")
|
|
}
|
|
|
|
func (this *guildMgr) GetGuildByGuildId(guildId string) common.Guild {
|
|
if p := this.internalGetGuildByGuildId(guildId); p != nil {
|
|
return p
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (this *guildMgr) GetGuildJob(accountId string) int32 {
|
|
if m, ok := this.memberIdHash[accountId]; ok {
|
|
return m.guildJob
|
|
} else {
|
|
return constant.GuildMemberLevelDefault
|
|
}
|
|
}
|
|
|
|
func (this *guildMgr) GetGuildByAccountId(accountId string) common.Guild {
|
|
if p := this.internalGetGuildByAccountId(accountId); p != nil {
|
|
return p
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (this *guildMgr) GetGuildByGuildName(guildName string) common.Guild {
|
|
if p := this.internalGetGuildByGuildName(guildName); p != nil {
|
|
return p
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (this *guildMgr) internalGetGuildByAccountId(accountId string) *guild {
|
|
m := this.internalGetMemberByAccountId(accountId)
|
|
if m != nil {
|
|
return m.guild
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (this *guildMgr) internalGetMemberByAccountId(accountId string) *member {
|
|
if m, ok := this.memberIdHash[accountId]; ok {
|
|
return m
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (this *guildMgr) internalGetGuildByGuildId(guildId string) *guild {
|
|
if guild, ok := this.idHash[guildId]; ok {
|
|
return guild
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (this *guildMgr) internalGetGuildByGuildName(guildName string) *guild {
|
|
if guild, ok := this.nameHash[guildName]; ok {
|
|
return guild
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (this *guildMgr) addUsingName(name string) {
|
|
this.usingNameHash[name] = f5.GetApp().GetNowSeconds();
|
|
}
|
|
|
|
func (this *guildMgr) isUsingName(name string) bool {
|
|
if _, ok := this.usingNameHash[name]; ok {
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
|
|
func (this* guildMgr) removeUsingName(name string) {
|
|
delete(this.usingNameHash, name)
|
|
}
|
|
|
|
func (this *guildMgr) isValidGuildJob(guildJob int32) bool {
|
|
return guildJob == constant.GuildMemberLevelLeader ||
|
|
guildJob == constant.GuildMemberLevelViceLeader ||
|
|
guildJob == constant.GuildMemberLevelElite ||
|
|
guildJob == constant.GuildMemberLevelDefault
|
|
}
|
|
|
|
func (this *guildMgr) asyncCreateGuildTask(task *f5.LockAsyncTask,
|
|
guildId string, creatorId string, badge int32, guildName string,
|
|
cb func(int32, string, string)) {
|
|
this.addUsingName(guildName)
|
|
nowTime := f5.GetApp().GetNowSeconds()
|
|
model.Guild.New(guildId, guildName, creatorId, badge, nowTime,
|
|
func (err error, lastInsertId int64, rowsAffected int64) {
|
|
if err != nil {
|
|
this.removeUsingName(guildName)
|
|
task.SetFail()
|
|
cb(500, "server internal error", "")
|
|
return
|
|
}
|
|
model.GuildMember.Force(creatorId, guildId, constant.GuildMemberLevelLeader, nowTime,
|
|
func (err error, lastInsertId int64, rowsAffected int64) {
|
|
defer this.removeUsingName(guildName)
|
|
if err != nil {
|
|
task.SetFail()
|
|
cb(500, "server internal error", "")
|
|
return
|
|
}
|
|
if this.internalGetGuildByAccountId(creatorId) != nil {
|
|
task.SetFail()
|
|
panic(fmt.Sprintf("asyncCreateGuildTask:%s", ""))
|
|
} else {
|
|
this.createGuildAndAddOwner(guildId, guildName, creatorId, badge, nowTime)
|
|
task.SetSucc()
|
|
cb(0, "", guildId)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
func (this *guildMgr) AsyncCreateGuild(accountId string, sessionId string, avatar int32, name string,
|
|
cb func(int32, string, string)) {
|
|
guildId := q5.ToString(f5.GetApp().NewNodeUuid())
|
|
verifyFunc := func (task *f5.LockAsyncTask, cb func(int32, string, string)) bool {
|
|
if len(name) <= 0 {
|
|
task.SetFail()
|
|
cb(1, "Name cantnot be empty", "")
|
|
return false
|
|
}
|
|
if this.isNameTooLong(name) {
|
|
task.SetFail()
|
|
cb(2, "Name is to long", "")
|
|
return false
|
|
}
|
|
if !this.isValidName(name) {
|
|
task.SetFail()
|
|
cb(2, "Name is invalid", "")
|
|
return false
|
|
}
|
|
if this.GetGuildByAccountId(accountId) != nil {
|
|
task.SetFail()
|
|
cb(3, "You already have a cube", "")
|
|
return false
|
|
}
|
|
if this.GetGuildByGuildName(name) != nil {
|
|
task.SetFail()
|
|
cb(4, "Cube name already exists", "")
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
f5.NewLockAsyncTask([][]string{
|
|
{constant.GUILD_ID_LOCK_KEY, guildId},
|
|
{constant.GUILD_NAME_LOCK_KEY, name},
|
|
{constant.GUILD_MEMBER_LOCK_KEY, accountId},
|
|
},
|
|
func (task *f5.LockAsyncTask) {
|
|
if !verifyFunc(task, cb) {
|
|
return
|
|
}
|
|
params := map[string]string{
|
|
"c": "Bag",
|
|
"a": "createGuildConsume",
|
|
"account_id": accountId,
|
|
"session_id": sessionId,
|
|
}
|
|
url := fmt.Sprintf("%s/webapp/index.php", mt.Table.Config.GetById(0).GetGameapiUrl())
|
|
rspObj := new(common.HeadRsp)
|
|
GetDbLogMgr().GuildCreateConsumeBegin(accountId, guildId, name)
|
|
f5.GetHttpCliMgr().SendJsStyleJsonRspRequest(
|
|
url,
|
|
params,
|
|
&rspObj,
|
|
func(rsp f5.HttpCliResponse) {
|
|
GetDbLogMgr().GuildCreateConsumeEnd(accountId, guildId, name, rspObj.Errcode)
|
|
if rsp.GetErr() == nil && rsp.JsonParseOk() {
|
|
if rspObj.Errcode != 0 {
|
|
task.SetFail()
|
|
cb(4, "item not enough", "")
|
|
return
|
|
}
|
|
if !verifyFunc(task, cb) {
|
|
return
|
|
}
|
|
this.asyncCreateGuildTask(task, guildId, accountId, avatar, name, cb)
|
|
} else {
|
|
task.SetFail()
|
|
cb(4, "server internal error", "")
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
func (this *guildMgr) AsyncGetApplyList(lastIdx int64, accountId string,
|
|
cb func(int32, string, int64, []string)) {
|
|
guild := this.GetGuildByAccountId(accountId)
|
|
if guild == nil {
|
|
cb(0, "", 0, nil)
|
|
return
|
|
}
|
|
f5.GetJsStyleDb().PageQuery(
|
|
constant.FRIEND_DB,
|
|
50,
|
|
0,
|
|
"SELECT * FROM t_guild_apply WHERE 1=1",
|
|
[]string{},
|
|
f5.GetDbFilter().Comp(
|
|
f5.GetDbFilter().GT("idx", q5.ToString(lastIdx)).And(),
|
|
f5.GetDbFilter().EQ("guild_id", guild.GetGuildId()).And(),
|
|
f5.GetDbFilter().EQ("status", q5.ToString(constant.GUILD_APPLY_STATUS_NONE)),
|
|
),
|
|
"",
|
|
func (err error, pg *f5.Pagination) {
|
|
var lastSinceId int64
|
|
accountIds := []string{}
|
|
if err != nil {
|
|
cb(500, "", lastSinceId, accountIds)
|
|
return
|
|
}
|
|
if pg.Rows.Next() {
|
|
idx := q5.ToInt64(pg.Rows.GetByName("idx"))
|
|
accountId := pg.Rows.GetByName("account_id")
|
|
if idx > lastSinceId {
|
|
lastSinceId = idx
|
|
} else {
|
|
panic(fmt.Sprintf("AsyncGetApply idx error:%s %s", idx, lastSinceId))
|
|
}
|
|
q5.AppendSlice(&accountIds, accountId)
|
|
}
|
|
cb(0, "", lastSinceId, accountIds)
|
|
return
|
|
})
|
|
}
|
|
|
|
func (this *guildMgr) AsyncApplyJoin(accountId string, guildId string, cb func(int32, string, common.Guild)) {
|
|
if this.internalGetGuildByAccountId(accountId) != nil {
|
|
cb(0, "", nil)
|
|
return
|
|
}
|
|
guild := this.internalGetGuildByGuildId(guildId)
|
|
if guild == nil {
|
|
cb(0, "", nil)
|
|
return
|
|
}
|
|
nowTime := f5.GetApp().GetNowSeconds()
|
|
if guild.joinCondType == constant.JoinCondFree &&
|
|
!guild.isFull() {
|
|
m := newMember()
|
|
m.init(guild, constant.GuildMemberLevelDefault, accountId, int32(nowTime))
|
|
guild.addMember(m)
|
|
this.addGuildMember(m)
|
|
model.GuildApply.SetStatus(guild.guildId, accountId, constant.GUILD_APPLY_STATUS_ACCEPT,
|
|
func (err error, lastInsertId int64, rowsAffected int64) {
|
|
GetDbLogMgr().GuildAccpetApply(guild.guildId, accountId, accountId)
|
|
return
|
|
})
|
|
cb(0, "", guild)
|
|
} else {
|
|
model.GuildApply.Force(
|
|
guildId,
|
|
accountId,
|
|
func (err error, lastInsertId int64, rowsAffected int64) {
|
|
if err != nil {
|
|
cb(500, "server internal error", nil)
|
|
return
|
|
}
|
|
cb(0, "", nil)
|
|
})
|
|
}
|
|
}
|
|
|
|
func (this *guildMgr) asyncAcceptApplyTask(task *f5.LockAsyncTask, guild *guild,
|
|
accountId string, targetId string,
|
|
cb func(int32, string, string, string)) {
|
|
if !guild.isOwner(accountId) {
|
|
task.SetFail()
|
|
cb(1, "", "", "")
|
|
return
|
|
}
|
|
if accountId == targetId {
|
|
task.SetFail()
|
|
cb(1, "", "", "")
|
|
return
|
|
}
|
|
if guild.isFull() {
|
|
task.SetFail()
|
|
cb(1, "Exceeding cube member number limit", "", "")
|
|
return
|
|
}
|
|
if this.internalGetMemberByAccountId(targetId) != nil {
|
|
task.SetFail()
|
|
cb(1, "", "", "")
|
|
return
|
|
}
|
|
nowTime := f5.GetApp().GetNowSeconds()
|
|
model.GuildMember.Force(targetId, guild.guildId, constant.GuildMemberLevelDefault, nowTime,
|
|
func (err error, lastInsertId int64, rowsAffected int64) {
|
|
if err != nil {
|
|
task.SetFail()
|
|
cb(500, "server internal error", "", "")
|
|
return
|
|
}
|
|
m := newMember()
|
|
m.init(guild, constant.GuildMemberLevelDefault, targetId, int32(nowTime))
|
|
guild.addMember(m)
|
|
this.addGuildMember(m)
|
|
model.GuildApply.SetStatus(guild.guildId, targetId, constant.GUILD_APPLY_STATUS_ACCEPT,
|
|
func (err error, lastInsertId int64, rowsAffected int64) {
|
|
task.SetSucc()
|
|
GetDbLogMgr().GuildAccpetApply(guild.guildId, accountId, targetId)
|
|
cb(0, "", guild.guildId, guild.guildName)
|
|
return
|
|
})
|
|
})
|
|
}
|
|
|
|
func (this *guildMgr) AsyncAcceptApply(accountId string, targetId string,
|
|
cb func(int32, string, string, string)) {
|
|
guild := this.internalGetGuildByAccountId(accountId)
|
|
if guild == nil {
|
|
cb(0, "", guild.guildId, guild.guildName)
|
|
return;
|
|
}
|
|
f5.NewLockAsyncTask([][]string{
|
|
{constant.MEMBER_LOCK_KEY, accountId},
|
|
{constant.MEMBER_LOCK_KEY, targetId},
|
|
{constant.GUILD_ID_LOCK_KEY, guild.guildId},
|
|
},
|
|
func (task *f5.LockAsyncTask) {
|
|
this.asyncAcceptApplyTask(task, guild, accountId, targetId, cb)
|
|
})
|
|
}
|
|
|
|
func (this *guildMgr) AsyncRejectApply(accountId string, targetId string,
|
|
cb func(int32, string)) {
|
|
guild := this.internalGetGuildByAccountId(accountId)
|
|
if guild == nil {
|
|
cb(0, "")
|
|
return;
|
|
}
|
|
model.GuildApply.SetStatus(guild.guildId, targetId, constant.GUILD_APPLY_STATUS_REJECT,
|
|
func (err error, lastInsertId int64, rowsAffected int64) {
|
|
cb(0, "")
|
|
})
|
|
}
|
|
|
|
func (this *guildMgr) AsyncLeave(accountId string,
|
|
cb func(int32, string, string, string, []string)) {
|
|
members := []string{}
|
|
keys := [][]string{}
|
|
{
|
|
guild := this.internalGetGuildByAccountId(accountId)
|
|
if guild == nil {
|
|
cb(0, "", "", "", members)
|
|
return
|
|
}
|
|
keys = [][]string{
|
|
{constant.GUILD_ID_LOCK_KEY, guild.guildId},
|
|
{constant.GUILD_NAME_LOCK_KEY, guild.guildName},
|
|
};
|
|
guild.traverseMembers(
|
|
func (m *member) bool {
|
|
q5.AppendSlice(&keys, []string{constant.GUILD_MEMBER_LOCK_KEY, m.memberId})
|
|
q5.AppendSlice(&members, m.memberId)
|
|
return true
|
|
})
|
|
}
|
|
f5.NewLockAsyncTask(
|
|
keys,
|
|
func (task *f5.LockAsyncTask) {
|
|
guild := this.internalGetGuildByAccountId(accountId)
|
|
members := []string{}
|
|
if guild == nil {
|
|
task.SetSucc()
|
|
cb(0, "", guild.guildId, guild.guildName, members)
|
|
return
|
|
}
|
|
guild.asyncLeave(accountId,
|
|
func (errCode int32, errMsg string) {
|
|
if errCode != 0 {
|
|
task.SetFail()
|
|
cb(errCode, errMsg, guild.guildId, guild.guildName, members)
|
|
return
|
|
}
|
|
task.SetSucc()
|
|
cb(0, "", guild.guildId, guild.guildName, members)
|
|
return
|
|
})
|
|
})
|
|
}
|
|
|
|
func (this *guildMgr) AsyncSetGuildJob(accountId string, targetId string, guildJob int32,
|
|
cb func(int32, string, string, string)) {
|
|
if accountId == targetId {
|
|
cb(0, "", "", "")
|
|
return
|
|
}
|
|
guild := this.internalGetGuildByAccountId(accountId)
|
|
if guild == nil {
|
|
cb(0, "", "", "")
|
|
return
|
|
}
|
|
if !this.isValidGuildJob(guildJob) {
|
|
cb(1, "params error", "", "")
|
|
return
|
|
}
|
|
if !guild.isOwner(accountId) {
|
|
cb(0, "", "", "")
|
|
return;
|
|
}
|
|
owner := guild.getMember(accountId)
|
|
if owner == nil {
|
|
cb(0, "", "", "")
|
|
return;
|
|
}
|
|
member := guild.getMember(targetId)
|
|
if member == nil {
|
|
cb(0, "", "", "")
|
|
return;
|
|
}
|
|
f5.NewLockAsyncTask(
|
|
[][]string{
|
|
{constant.GUILD_ID_LOCK_KEY, guild.guildId},
|
|
{constant.GUILD_NAME_LOCK_KEY, guild.guildName},
|
|
{constant.GUILD_MEMBER_LOCK_KEY, accountId},
|
|
{constant.GUILD_MEMBER_LOCK_KEY, targetId},
|
|
},
|
|
func (task *f5.LockAsyncTask) {
|
|
model.GuildMember.SetJob(guild.guildId, member.memberId, guildJob,
|
|
func (err error, lastInsertId int64, rowsAffected int64) {
|
|
if err != nil {
|
|
task.SetFail()
|
|
cb(500, "", "", "")
|
|
return
|
|
}
|
|
member.guildJob = guildJob
|
|
GetDbLogMgr().GuildSetJob(guild.guildId, accountId, targetId, guildJob)
|
|
if member.guildJob == constant.GuildMemberLevelLeader {
|
|
model.GuildMember.SetJob(guild.guildId, owner.memberId,
|
|
constant.GuildMemberLevelDefault,
|
|
func (err error, lastInsertId int64, rowsAffected int64) {
|
|
if err != nil {
|
|
task.SetFail()
|
|
cb(500, "", "", "")
|
|
return
|
|
}
|
|
owner.guildJob = constant.GuildMemberLevelDefault
|
|
GetDbLogMgr().GuildSetJob(guild.guildId, accountId, owner.memberId, owner.guildJob)
|
|
task.SetSucc()
|
|
cb(0, "", guild.guildId, guild.guildName)
|
|
})
|
|
} else {
|
|
task.SetSucc()
|
|
cb(0, "", guild.guildId, guild.guildName)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
func (this *guildMgr) AsyncKickout(accountId string, targetId string,
|
|
cb func(int32, string, string, string)) {
|
|
guild := this.internalGetGuildByAccountId(accountId)
|
|
if guild == nil {
|
|
cb(0, "", guild.guildId, guild.guildName)
|
|
return;
|
|
}
|
|
if accountId == targetId {
|
|
cb(0, "", guild.guildId, guild.guildName)
|
|
return;
|
|
}
|
|
if !guild.isOwner(accountId) {
|
|
cb(0, "", guild.guildId, guild.guildName)
|
|
return;
|
|
}
|
|
if guild.getMember(targetId) == nil {
|
|
cb(0, "", guild.guildId, guild.guildName)
|
|
return;
|
|
}
|
|
f5.NewLockAsyncTask(
|
|
[][]string{
|
|
{constant.GUILD_ID_LOCK_KEY, guild.guildId},
|
|
{constant.GUILD_NAME_LOCK_KEY, guild.guildName},
|
|
{constant.GUILD_MEMBER_LOCK_KEY, accountId},
|
|
{constant.GUILD_MEMBER_LOCK_KEY, targetId},
|
|
},
|
|
func (task *f5.LockAsyncTask) {
|
|
model.GuildMember.DeleteSoft(guild.guildId, targetId,
|
|
func (err error, lastInsertId int64, rowsAffected int64) {
|
|
if err != nil {
|
|
task.SetFail()
|
|
cb(500, "server internal error", "", "")
|
|
return
|
|
}
|
|
guild.delMember(targetId)
|
|
this.delGuildMember(targetId)
|
|
GetDbLogMgr().GuildKickout(guild.guildId, accountId, targetId)
|
|
task.SetSucc()
|
|
cb(0, "", guild.guildId, guild.guildName)
|
|
})
|
|
})
|
|
}
|
|
|
|
func (this *guildMgr) AsyncDisband(accountId string, cb func(int32, string, []string)) {
|
|
members := []string{}
|
|
keys := [][]string{}
|
|
{
|
|
guild := this.internalGetGuildByAccountId(accountId)
|
|
if guild == nil {
|
|
cb(0, "", members)
|
|
return
|
|
}
|
|
if !guild.isOwner(accountId) {
|
|
cb(1, "Disband only leader perm", members)
|
|
return
|
|
}
|
|
keys = [][]string{
|
|
{constant.GUILD_ID_LOCK_KEY, guild.guildId},
|
|
{constant.GUILD_NAME_LOCK_KEY, guild.guildName},
|
|
};
|
|
guild.traverseMembers(
|
|
func (m *member) bool {
|
|
q5.AppendSlice(&keys, []string{constant.GUILD_MEMBER_LOCK_KEY, m.memberId})
|
|
q5.AppendSlice(&members, m.memberId)
|
|
return true
|
|
})
|
|
}
|
|
f5.NewLockAsyncTask(
|
|
keys,
|
|
func (task *f5.LockAsyncTask) {
|
|
guild := this.internalGetGuildByAccountId(accountId)
|
|
members := []string{}
|
|
if guild == nil {
|
|
task.SetSucc()
|
|
cb(0, "", members)
|
|
return
|
|
}
|
|
if !guild.isOwner(accountId) {
|
|
task.SetFail()
|
|
cb(1, "Disband only leader perm", members)
|
|
return
|
|
}
|
|
guild.disband(accountId, 1)
|
|
task.SetSucc()
|
|
cb(0, "", members)
|
|
})
|
|
}
|
|
|
|
func (this *guildMgr) AsyncSearch(sinceId int64, q string, cb func(int32, string, int64, []string)) {
|
|
f5.GetJsStyleDb().PageQuery(
|
|
constant.FRIEND_DB,
|
|
50,
|
|
0,
|
|
"SELECT idx, guild_id FROM t_guild WHERE 1=1",
|
|
[]string{},
|
|
f5.GetDbFilter().Comp(
|
|
f5.GetDbFilter().GT("idx", q5.ToString(sinceId)).And(),
|
|
f5.GetDbFilter().EQ("deleted", q5.ToString(0)).And(),
|
|
f5.GetDbFilter().Like("guild_name", q5.ToString(q)),
|
|
),
|
|
"",
|
|
func (err error, pg *f5.Pagination) {
|
|
var lastSinceId int64 = sinceId
|
|
if err != nil {
|
|
cb(500, "", lastSinceId, []string{})
|
|
return
|
|
}
|
|
guildIds := []string{}
|
|
for pg.Rows.Next() {
|
|
idx := q5.ToInt64(pg.Rows.GetByName("idx"))
|
|
guildId := pg.Rows.GetByName("guild_id")
|
|
if idx > lastSinceId {
|
|
lastSinceId = idx
|
|
} else {
|
|
panic(fmt.Sprintf("guild.AsyncSearch idx error:%s %s", idx, lastSinceId))
|
|
}
|
|
if this.GetGuildByGuildId(guildId) != nil {
|
|
q5.AppendSlice(&guildIds, guildId)
|
|
}
|
|
}
|
|
cb(0, "", lastSinceId, guildIds)
|
|
})
|
|
}
|
|
|
|
func (this *guildMgr) AsyncGetGuildLogs(string, string, func(int32, string)) {
|
|
|
|
}
|
|
|
|
func (this *guildMgr) AsyncUpdateGuild(accountId string, sessionId string, kv map[int32]string,
|
|
cb func(int32, string)) {
|
|
guild := this.internalGetGuildByAccountId(accountId)
|
|
if guild == nil {
|
|
cb(1, "You don't have a cube yet")
|
|
return
|
|
}
|
|
if !guild.isOwner(accountId) {
|
|
cb(2, "Insufficient permissions")
|
|
return
|
|
}
|
|
guildName := f5.GetApp().NewGlobalUuid()
|
|
if name, ok := kv[constant.GUILD_UPDATE_FIELD_NAME]; ok {
|
|
if this.internalGetGuildByGuildName(name) != nil {
|
|
cb(4, "Cube name already exists")
|
|
return
|
|
}
|
|
if this.isUsingName(name) {
|
|
cb(4, "Cube name already exists")
|
|
return
|
|
}
|
|
this.addUsingName(name)
|
|
guildName = name
|
|
}
|
|
doFunc := func () {
|
|
f5.NewLockAsyncTask([][]string{
|
|
{constant.GUILD_ID_LOCK_KEY, guild.guildId},
|
|
{constant.GUILD_NAME_LOCK_KEY, guild.guildName},
|
|
{constant.GUILD_NAME_LOCK_KEY, guildName},
|
|
{constant.GUILD_MEMBER_LOCK_KEY, accountId},
|
|
},
|
|
func (task *f5.LockAsyncTask) {
|
|
fields := [][]string{}
|
|
for k, v := range(kv) {
|
|
if k == constant.GUILD_UPDATE_FIELD_NOTICE {
|
|
q5.AppendSlice(&fields, []string{"notice", v})
|
|
} else if k == constant.GUILD_UPDATE_FIELD_AVATAR {
|
|
q5.AppendSlice(&fields, []string{"badge", v})
|
|
} else if k == constant.GUILD_UPDATE_FIELD_NAME {
|
|
q5.AppendSlice(&fields, []string{"guild_name", v})
|
|
} else if k == constant.GUILD_UPDATE_FIELD_COND_TYPE {
|
|
q5.AppendSlice(&fields, []string{"join_cond_type", v})
|
|
} else if k == constant.GUILD_UPDATE_FIELD_COND_VALUE {
|
|
q5.AppendSlice(&fields, []string{"join_cond_value", v})
|
|
}
|
|
}
|
|
if len(fields) <= 0 {
|
|
task.SetSucc()
|
|
cb(0, "")
|
|
return
|
|
}
|
|
model.Guild.UpdateKv(guild.guildId, fields,
|
|
func (err error, lastInsertId int64, rowsAffected int64) {
|
|
if err != nil {
|
|
task.SetFail()
|
|
cb(500, "server internal error")
|
|
return
|
|
}
|
|
guild.updateByKv(kv)
|
|
task.SetSucc()
|
|
cb(0, "")
|
|
return
|
|
})
|
|
}).OnExit(
|
|
func (task *f5.LockAsyncTask) {
|
|
if name, ok := kv[constant.GUILD_UPDATE_FIELD_NAME]; ok {
|
|
this.removeUsingName(name)
|
|
}
|
|
})
|
|
}
|
|
if _, ok := kv[constant.GUILD_UPDATE_FIELD_NAME]; ok {
|
|
GetDbLogMgr().GuildRenameConsumeBegin(accountId, guild.guildId, guildName)
|
|
params := map[string]string{
|
|
"c": "Bag",
|
|
"a": "useItem",
|
|
"account_id": accountId,
|
|
"session_id": sessionId,
|
|
"item_id": q5.ToString(constant.GUILD_RENAME_CARD_ITEM_ID),
|
|
"item_num": q5.ToString(1),
|
|
}
|
|
url := fmt.Sprintf("%s/webapp/index.php", mt.Table.Config.GetById(0).GetGameapiUrl())
|
|
rspObj := new(common.LoginRsp)
|
|
f5.GetHttpCliMgr().SendJsStyleJsonRspRequest(
|
|
url,
|
|
params,
|
|
&rspObj,
|
|
func(rsp f5.HttpCliResponse) {
|
|
if rsp.GetErr() == nil && rsp.JsonParseOk() {
|
|
doFunc()
|
|
GetDbLogMgr().GuildRenameConsumeEnd(accountId, guild.guildId, guildName, 0)
|
|
} else {
|
|
GetDbLogMgr().GuildRenameConsumeEnd(accountId, guild.guildId, guildName, 1)
|
|
cb(4, "cube rename card not enough")
|
|
return
|
|
}
|
|
})
|
|
} else {
|
|
doFunc()
|
|
}
|
|
}
|
|
|
|
func (this *guildMgr) NotifyGuildMsg(guildId string, msg proto.Message) {
|
|
g := this.internalGetGuildByGuildId(guildId)
|
|
if g != nil {
|
|
g.traverseMembers(
|
|
func (m *member) bool {
|
|
hum := GetPlayerMgr().GetPlayerByAccountId(m.memberId)
|
|
if hum != nil {
|
|
hum.SendMsg(msg)
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
}
|
|
|
|
func (this *guildMgr) AsyncGetGuildRank(num int32, cb func(int32, string, []string)) {
|
|
guildIds := []string{}
|
|
for _, m := range(this.guildRankList) {
|
|
q5.AppendSlice(&guildIds, m.guildId)
|
|
}
|
|
cb(0, "", guildIds)
|
|
}
|
|
|
|
func (this *guildMgr) AsyncGetRecommendGuild(num int32, cb func(int32, string, []string)) {
|
|
guildIds := []string{}
|
|
for _, m := range(this.idHash) {
|
|
q5.AppendSlice(&guildIds, m.guildId)
|
|
if len(guildIds) >= constant.MAX_GUILD_RECOMMEND {
|
|
break
|
|
}
|
|
}
|
|
cb(0, "", guildIds)
|
|
}
|
|
|
|
func (this *guildMgr) AsyncHasApply(accountId string, cb func(int32, string, int32)) {
|
|
guild := this.GetGuildByAccountId(accountId)
|
|
if guild == nil {
|
|
cb(0, "", 0)
|
|
return
|
|
}
|
|
f5.GetJsStyleDb().OrmSelectOne(
|
|
constant.FRIEND_DB,
|
|
"t_guild_apply",
|
|
[][]string{
|
|
{"guild_id", guild.GetGuildId()},
|
|
{"status", q5.ToString(constant.GUILD_APPLY_STATUS_NONE)},
|
|
},
|
|
func (err error, ds *f5.DataSet) {
|
|
if err != nil {
|
|
cb(500, "", 0)
|
|
return
|
|
}
|
|
if ds.Next() {
|
|
cb(0, "", 1)
|
|
} else {
|
|
cb(0, "", 0)
|
|
}
|
|
})
|
|
}
|
|
|
|
func (this *guildMgr) rearrangement() {
|
|
this.guildRankList = []*guild{}
|
|
this.guildRankHash = make(map[string]*guild)
|
|
this.traverseGuild(
|
|
func (g *guild) bool {
|
|
this.updateGuildRank(g)
|
|
return true
|
|
})
|
|
}
|
|
|
|
func (this *guildMgr) clearEmptyGuild() {
|
|
emptyGuilds := make(map[string]*guild)
|
|
this.traverseGuild(
|
|
func (g *guild) bool {
|
|
if g.GetMemberNum() <= 0 {
|
|
emptyGuilds[g.guildId] = g
|
|
}
|
|
return true
|
|
})
|
|
for _, g := range(emptyGuilds) {
|
|
this.delGuildNoRearrangement(g)
|
|
GetDbLogMgr().GuildClearEmptyStart(g.guildId, g.guildName)
|
|
model.Guild.DeleteSoft(g.guildId,
|
|
func (err error, lastInsertId int64, rowsAffected int64) {
|
|
GetDbLogMgr().GuildClearEmptyEnd(g.guildId, g.guildName)
|
|
})
|
|
}
|
|
}
|
|
|
|
func (this* guildMgr) updateGuildRank(guild *guild) {
|
|
if _, ok := this.guildRankHash[guild.guildId]; ok {
|
|
this.rankSort()
|
|
return
|
|
}
|
|
if len(this.guildRankList) < constant.MAX_GUILD_TOP {
|
|
q5.AppendSlice(&this.guildRankList, guild)
|
|
this.guildRankHash[guild.guildId] = guild
|
|
this.rankSort()
|
|
} else {
|
|
lastGuild := this.guildRankList[len(this.guildRankList) - 1]
|
|
if guild.totalStars > lastGuild.totalStars ||
|
|
(guild.totalStars == lastGuild.totalStars && guild.guildId < lastGuild.guildId) {
|
|
delete(this.guildRankHash, lastGuild.guildId)
|
|
this.guildRankList[len(this.guildRankList) - 1] = guild
|
|
this.rankSort()
|
|
}
|
|
}
|
|
}
|
|
|
|
func (this* guildMgr) rankSort() {
|
|
q5.Sort(
|
|
this.guildRankList,
|
|
func (a *guild, b *guild) bool {
|
|
if a.totalStars == b.totalStars {
|
|
return a.guildId < b.guildId
|
|
}
|
|
return a.totalStars < b.totalStars
|
|
})
|
|
}
|
|
|
|
func (this* guildMgr) traverseGuild(cb func(*guild) bool) {
|
|
for _, g := range(this.idHash) {
|
|
if !cb(g) {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
func (this *guildMgr) createGuildAndAddOwner(guildId string, guildName string, creatorId string,
|
|
badge int32, nowTime int64) {
|
|
g := newGuild()
|
|
g.init(guildId, guildName, creatorId, badge, constant.GuildMaxMembers, int32(nowTime))
|
|
this.addGuild(g)
|
|
{
|
|
m := newMember()
|
|
m.init(g, constant.GuildMemberLevelLeader, creatorId, q5.ToInt32(nowTime))
|
|
g.addMember(m)
|
|
this.addGuildMember(m)
|
|
}
|
|
this.updateGuildRank(g)
|
|
}
|
|
|
|
func (this *guildMgr) addGuild(g *guild) {
|
|
this.idHash[g.guildId] = g
|
|
this.nameHash[g.guildName] = g
|
|
}
|
|
|
|
func (this *guildMgr) addGuildMember(m *member) {
|
|
this.memberIdHash[m.memberId] = m
|
|
}
|
|
|
|
func (this *guildMgr) delGuildMember(memberId string) {
|
|
delete(this.memberIdHash, memberId)
|
|
}
|
|
|
|
func (this *guildMgr) delGuild(g *guild) {
|
|
this.delGuildNoRearrangement(g)
|
|
if _, ok := this.guildRankHash[g.guildId]; ok {
|
|
this.rearrangement()
|
|
}
|
|
}
|
|
|
|
func (this *guildMgr) delGuildNoRearrangement(g *guild) {
|
|
g.traverseMembers(
|
|
func (m *member) bool {
|
|
this.delGuildMember(m.memberId)
|
|
return true
|
|
})
|
|
delete(this.idHash, g.guildId)
|
|
delete(this.nameHash, g.guildName)
|
|
}
|