428 lines
10 KiB
Go
428 lines
10 KiB
Go
package main
|
|
|
|
import (
|
|
"f5"
|
|
"fmt"
|
|
"q5"
|
|
)
|
|
|
|
// loadGuildFromDB 加载公会
|
|
func (gm *GuildMgr) loadGuildFromDB() {
|
|
fields := []string{
|
|
"idx",
|
|
"guild_id",
|
|
"name",
|
|
"leader_account_id",
|
|
"avatar",
|
|
"notice",
|
|
"join_cond",
|
|
"join_cond_value",
|
|
"total_stars",
|
|
"total_kills",
|
|
"chicken_dinners",
|
|
"max_members",
|
|
}
|
|
where := [][]string{
|
|
{"is_deleted", "0"},
|
|
}
|
|
f5.GetJsStyleDb().Select(
|
|
FRIEND_DB,
|
|
"t_guild",
|
|
fields,
|
|
where,
|
|
gm.loadGuildFromDBResult,
|
|
)
|
|
}
|
|
|
|
func (gm *GuildMgr) loadGuildFromDBResult(err error, rows *f5.DataSet) {
|
|
if err != nil {
|
|
f5.GetSysLog().Info("loadGuildFromDBResult err:%v \n", err)
|
|
panic(err)
|
|
}
|
|
for rows.Next() {
|
|
guildId := q5.ToInt64(rows.GetByIndex(1))
|
|
guild := &Guild{
|
|
AutoId: q5.ToInt64(rows.GetByIndex(0)),
|
|
GuildId: guildId,
|
|
Name: q5.ToString(rows.GetByIndex(2)),
|
|
LeaderId: q5.ToString(rows.GetByIndex(3)),
|
|
Avatar: q5.ToInt32(rows.GetByIndex(4)),
|
|
Notice: q5.ToString(rows.GetByIndex(5)),
|
|
JoinCond: q5.ToInt32(rows.GetByIndex(6)),
|
|
JoinCondValue: q5.ToInt32(rows.GetByIndex(7)),
|
|
TotalStars: q5.ToInt32(rows.GetByIndex(8)),
|
|
TotalKills: q5.ToInt32(rows.GetByIndex(9)),
|
|
ChickenDinners: q5.ToInt32(rows.GetByIndex(10)),
|
|
MaxMembers: q5.ToInt32(rows.GetByIndex(11)),
|
|
Members: make(map[string]*GuildMember, MaxMembers),
|
|
PendingReqs: make(map[string]int32, MaxPendingReqs),
|
|
}
|
|
gm.guilds[guildId] = guild
|
|
}
|
|
q5.UnSetBitFlag(&gm.loadedFlags, LoadGuildFlag)
|
|
}
|
|
|
|
// loadGuildMemberFromDB 加载公会成员
|
|
func (gm *GuildMgr) loadGuildMemberFromDB() {
|
|
fields := []string{"guild_id", "account_id", "level"}
|
|
where := [][]string{
|
|
{"is_leave_guild", "0"},
|
|
}
|
|
f5.GetJsStyleDb().Select(
|
|
FRIEND_DB,
|
|
"t_guild_members",
|
|
fields,
|
|
where,
|
|
gm.loadGuildMemberFromDBResult,
|
|
)
|
|
}
|
|
|
|
func (gm *GuildMgr) loadGuildMemberFromDBResult(err error, rows *f5.DataSet) {
|
|
if err != nil {
|
|
f5.GetSysLog().Info("loadGuildMemberFromDBResult err:%v \n", err)
|
|
panic(err)
|
|
}
|
|
for rows.Next() {
|
|
var (
|
|
guildId int64
|
|
accountId string
|
|
level int32
|
|
)
|
|
guildId = q5.ToInt64(rows.GetByIndex(0))
|
|
accountId = q5.ToString(rows.GetByIndex(1))
|
|
level = q5.ToInt32(rows.GetByIndex(2))
|
|
|
|
guildMember := &GuildMember{
|
|
AccountId: accountId,
|
|
Level: level,
|
|
}
|
|
if guild, ok := gm.guilds[guildId]; ok {
|
|
guild.AddMember(guildMember)
|
|
gm.userGuilds[accountId] = guildId
|
|
}
|
|
}
|
|
q5.UnSetBitFlag(&gm.loadedFlags, LoadGuildMemberFlag)
|
|
}
|
|
|
|
// loadPendingReqsFromDB 加载公会申请者列表
|
|
func (gm *GuildMgr) loadPendingReqsFromDB() {
|
|
fields := []string{"guild_id", "account_id"}
|
|
where := [][]string{
|
|
{"is_join_guild", q5.ToString(PendingReqIsJoinGuildStatusDefault)},
|
|
}
|
|
f5.GetJsStyleDb().Select(
|
|
FRIEND_DB,
|
|
"t_guild_pending_request",
|
|
fields,
|
|
where,
|
|
gm.loadPendingReqsFromDBResult,
|
|
)
|
|
}
|
|
func (gm *GuildMgr) loadPendingReqsFromDBResult(err error, rows *f5.DataSet) {
|
|
if err != nil {
|
|
f5.GetSysLog().Info("loadPendingReqsFromDBResult err:%v \n", err)
|
|
panic(err)
|
|
}
|
|
for rows.Next() {
|
|
var (
|
|
guildId int64
|
|
accountId string
|
|
)
|
|
guildId = q5.ToInt64(rows.GetByIndex(0))
|
|
accountId = q5.ToString(rows.GetByIndex(1))
|
|
|
|
pendingReq := &PendingReq{
|
|
AccountId: accountId,
|
|
Status: PendingReqIsJoinGuildStatusDefault,
|
|
}
|
|
if guild, ok := gm.guilds[guildId]; ok {
|
|
guild.AddPendingReq(pendingReq)
|
|
}
|
|
}
|
|
q5.UnSetBitFlag(&gm.loadedFlags, LoadGuildReqFlag)
|
|
}
|
|
|
|
// loadGuildLogsFromDB 加载公会日志
|
|
func (gm *GuildMgr) loadGuildLogsFromDB() {
|
|
var perPage int32 = 10
|
|
var page int32 = 1
|
|
sql := "SELECT `guild_id`, `account_id`, `log_type`, `content` FROM `t_guild_logs`"
|
|
var params []string
|
|
var filter f5.DbQueryFilter
|
|
orderBy := "ORDER BY `createtime` DESC"
|
|
f5.GetJsStyleDb().PageQuery(
|
|
FRIEND_DB,
|
|
perPage,
|
|
page,
|
|
sql,
|
|
params,
|
|
filter,
|
|
orderBy,
|
|
gm.loadGuildLogsFromDBResult,
|
|
)
|
|
}
|
|
func (gm *GuildMgr) loadGuildLogsFromDBResult(err error, pagination *f5.Pagination) {
|
|
if err != nil {
|
|
//panic(err)
|
|
return
|
|
}
|
|
//total := pagination.Total
|
|
var (
|
|
guildId int64
|
|
accountId string
|
|
logType int32
|
|
content string
|
|
)
|
|
gm.guildLogs[guildId] = make([]*GuildLog, DefaultLogs)
|
|
|
|
for pagination.Rows.Next() {
|
|
guildId = q5.ToInt64(pagination.Rows.GetByIndex(0))
|
|
accountId = q5.ToString(pagination.Rows.GetByIndex(1))
|
|
logType = q5.ToInt32(pagination.Rows.GetByIndex(2))
|
|
content = q5.ToString(pagination.Rows.GetByIndex(3))
|
|
|
|
guildLog := &GuildLog{
|
|
GuildId: guildId,
|
|
AccountId: accountId,
|
|
LogType: logType,
|
|
Content: content,
|
|
}
|
|
gm.guildLogs[guildId] = append(gm.guildLogs[guildId], guildLog)
|
|
}
|
|
q5.UnSetBitFlag(&gm.loadedFlags, LoadGuildLogFlag)
|
|
}
|
|
|
|
// createGuildDB 创建公会
|
|
func (gm *GuildMgr) createGuildDB(g *Guild) {
|
|
fields := [][]string{
|
|
{"guild_id", q5.ToString(g.GuildId)},
|
|
{"name", g.Name},
|
|
{"leader_account_id", g.LeaderId},
|
|
{"max_members", q5.ToString(g.MaxMembers)},
|
|
}
|
|
var insertError error
|
|
var lastInsertId int64
|
|
var rowsAffected int64
|
|
|
|
insertCallback := func(err error, id int64, affectedRows int64) {
|
|
insertError = err
|
|
lastInsertId = id
|
|
rowsAffected = affectedRows
|
|
}
|
|
|
|
f5.GetJsStyleDb().Insert(
|
|
FRIEND_DB,
|
|
"t_guild",
|
|
fields,
|
|
insertCallback,
|
|
)
|
|
|
|
if insertError != nil || rowsAffected != 1 {
|
|
// 插入失败,处理错误逻辑
|
|
fmt.Printf("Failed to insert guild: %v", insertError)
|
|
}
|
|
|
|
f5.GetSysLog().Info("Guild inserted successfully. LastInsertId: %d, RowsAffected: %d", lastInsertId, rowsAffected)
|
|
}
|
|
|
|
// updateGuild 更新公会信息
|
|
func (gm *GuildMgr) updateGuild(g *Guild, fields [][]string, cb func(error)) {
|
|
where := [][]string{
|
|
{"guild_id", q5.ToString(g.GuildId)},
|
|
}
|
|
f5.GetJsStyleDb().Update(
|
|
FRIEND_DB,
|
|
"t_guild",
|
|
fields,
|
|
where,
|
|
func(err error, lastInsertId int64, rowsAffected int64) {
|
|
if cb != nil {
|
|
cb(err)
|
|
}
|
|
},
|
|
)
|
|
}
|
|
|
|
// insertPendingReqs 添加申请加入公会记录, 存在则更新记录
|
|
func (gm *GuildMgr) upsertPendingReqs(guildId int64, accountId string, isJoinGuild int) {
|
|
where := [][]string{
|
|
{"guild_id", q5.ToString(guildId)},
|
|
{"account_id", accountId},
|
|
}
|
|
insertKv := [][]string{
|
|
{"guild_id", q5.ToString(guildId)},
|
|
{"account_id", accountId},
|
|
{"is_join_guild", q5.ToString(isJoinGuild)},
|
|
}
|
|
updateKv := [][]string{
|
|
{"is_join_guild", q5.ToString(isJoinGuild)},
|
|
}
|
|
f5.GetJsStyleDb().Upsert(
|
|
FRIEND_DB,
|
|
"t_guild_pending_request",
|
|
where,
|
|
updateKv,
|
|
insertKv,
|
|
func(err error, lastInsertId int64, rowsAffected int64) {
|
|
if err != nil {
|
|
f5.GetSysLog().Info("error:%v\n", err)
|
|
}
|
|
f5.GetSysLog().Info("lastInsertId:%d\n", lastInsertId)
|
|
f5.GetSysLog().Info("rowsAffected:%d\n", rowsAffected)
|
|
})
|
|
}
|
|
|
|
// updateAllPendingReqs 更新所有申请加入公会记录
|
|
func (gm *GuildMgr) updateAllPendingReqs(guildId int64, isJoinGuild int, cb func(error)) {
|
|
fields := [][]string{
|
|
{"is_join_guild", q5.ToString(isJoinGuild)},
|
|
}
|
|
where := [][]string{
|
|
{"guild_id", q5.ToString(guildId)},
|
|
}
|
|
f5.GetJsStyleDb().Update(
|
|
FRIEND_DB,
|
|
"t_guild_pending_request",
|
|
fields,
|
|
where,
|
|
func(err error, lastInsertId int64, rowsAffected int64) {
|
|
cb(err)
|
|
},
|
|
)
|
|
}
|
|
|
|
// updatePendingReqs 更新申请加入公会记录
|
|
func (gm *GuildMgr) updatePendingReqs(guildId int64, accountId string, isJoinGuild int, cb func(error)) {
|
|
fields := [][]string{
|
|
{"is_join_guild", q5.ToString(isJoinGuild)},
|
|
}
|
|
where := [][]string{
|
|
{"guild_id", q5.ToString(guildId)},
|
|
{"account_id", accountId},
|
|
}
|
|
f5.GetJsStyleDb().Update(
|
|
FRIEND_DB,
|
|
"t_guild_pending_request",
|
|
fields,
|
|
where,
|
|
func(err error, lastInsertId int64, rowsAffected int64) {
|
|
cb(err)
|
|
},
|
|
)
|
|
}
|
|
|
|
// insertPendingReqs 添加公会成员
|
|
func (gm *GuildMgr) upsertGuildMember(guildId int64, member *GuildMember, cb func(error)) {
|
|
where := [][]string{
|
|
{"guild_id", q5.ToString(guildId)},
|
|
{"account_id", member.AccountId},
|
|
}
|
|
insertKv := [][]string{
|
|
{"guild_id", q5.ToString(guildId)},
|
|
{"account_id", member.AccountId},
|
|
{"level", q5.ToString(member.Level)},
|
|
{"is_leave_guild", "0"},
|
|
}
|
|
updateKv := insertKv
|
|
f5.GetJsStyleDb().Upsert(
|
|
FRIEND_DB,
|
|
"t_guild_members",
|
|
where,
|
|
updateKv,
|
|
insertKv,
|
|
func(err error, lastInsertId int64, rowsAffected int64) {
|
|
cb(err)
|
|
},
|
|
)
|
|
}
|
|
|
|
func (gm *GuildMgr) updateGuildMembers(guildId int64, fields [][]string, cb func(error)) {
|
|
where := [][]string{
|
|
{"guild_id", q5.ToString(guildId)},
|
|
}
|
|
f5.GetJsStyleDb().Update(
|
|
FRIEND_DB,
|
|
"t_guild_members",
|
|
fields,
|
|
where,
|
|
func(err error, lastInsertId int64, rowsAffected int64) {
|
|
cb(err)
|
|
},
|
|
)
|
|
}
|
|
|
|
// updateGuildMember 更新成员信息
|
|
func (gm *GuildMgr) updateGuildMember(g *Guild, accountId string, fields [][]string, cb func(error)) {
|
|
where := [][]string{
|
|
{"guild_id", q5.ToString(g.GuildId)},
|
|
{"account_id", accountId},
|
|
}
|
|
|
|
f5.GetJsStyleDb().Update(
|
|
FRIEND_DB,
|
|
"t_guild_members",
|
|
fields,
|
|
where,
|
|
func(err error, lastInsertId int64, rowsAffected int64) {
|
|
if cb != nil {
|
|
cb(err)
|
|
}
|
|
},
|
|
)
|
|
}
|
|
|
|
// insertGuildLog 添加公会日志
|
|
func (gm *GuildMgr) insertGuildLog(log *GuildLog) {
|
|
fields := [][]string{
|
|
{"guild_id", q5.ToString(log.GuildId)},
|
|
{"account_id", log.AccountId},
|
|
{"log_type", q5.ToString(int(log.LogType))},
|
|
{"content", log.Content}}
|
|
f5.GetJsStyleDb().Insert(
|
|
FRIEND_DB,
|
|
"t_guild_logs",
|
|
fields,
|
|
func(err error, lastInsertId int64, rowsAffected int64) {
|
|
if err != nil {
|
|
f5.GetSysLog().Info("error:%v\n", err)
|
|
}
|
|
f5.GetSysLog().Info("lastInsertId:%d\n", lastInsertId)
|
|
f5.GetSysLog().Info("rowsAffected:%d\n", rowsAffected)
|
|
})
|
|
}
|
|
|
|
func (gm *GuildMgr) findGuildIdsByName(sinceId int64, name string, cb func(err error, lastId int64, guildIds []int64)) {
|
|
fields := []string{"idx", "guild_id"}
|
|
var where [][]string
|
|
nameLike := fmt.Sprintf("%%%s%%", name)
|
|
likeWhere := [][]string{
|
|
{"name", nameLike},
|
|
}
|
|
f5.GetJsStyleDb().SelectLike(
|
|
FRIEND_DB,
|
|
"t_guild",
|
|
fields,
|
|
where,
|
|
likeWhere,
|
|
sinceId,
|
|
MaxSearchResults,
|
|
func(err error, rows *f5.DataSet) {
|
|
if err != nil {
|
|
cb(err, 0, nil)
|
|
return
|
|
}
|
|
lastId := sinceId
|
|
ids := make([]int64, 0, MaxSearchResults)
|
|
for rows.Next() {
|
|
autoId := q5.ToInt64(rows.GetByIndex(0))
|
|
if lastId < autoId {
|
|
lastId = autoId
|
|
}
|
|
guildId := q5.ToInt64(rows.GetByIndex(1))
|
|
ids = append(ids, guildId)
|
|
}
|
|
cb(nil, lastId, ids)
|
|
},
|
|
)
|
|
}
|