Use Upsert
This commit is contained in:
parent
73339156e4
commit
ba703c871b
@ -16,7 +16,9 @@ func (fm *FriendsMgr) upsertFriendRequest(account1Id string, account2Id string,
|
|||||||
{"receiver_account_id", account2Id},
|
{"receiver_account_id", account2Id},
|
||||||
{"is_friendship", isFriendship},
|
{"is_friendship", isFriendship},
|
||||||
}
|
}
|
||||||
updateKv := insertKv
|
updateKv := [][]string{
|
||||||
|
{"is_friendship", isFriendship},
|
||||||
|
}
|
||||||
f5.GetJsStyleDb().Upsert(
|
f5.GetJsStyleDb().Upsert(
|
||||||
FRIEND_DB,
|
FRIEND_DB,
|
||||||
"t_friend_pending_request",
|
"t_friend_pending_request",
|
||||||
@ -38,13 +40,14 @@ func (fm *FriendsMgr) upsertFriendShip(account1Id string, account2Id string, isD
|
|||||||
{"account1_id", account1Id},
|
{"account1_id", account1Id},
|
||||||
{"account2_id", account2Id},
|
{"account2_id", account2Id},
|
||||||
}
|
}
|
||||||
fields := [][]string{
|
insertKv := [][]string{
|
||||||
{"account1_id", account1Id},
|
{"account1_id", account1Id},
|
||||||
{"account2_id", account2Id},
|
{"account2_id", account2Id},
|
||||||
{"is_delete_friendship", q5.ToString(isDeleteFriendship)},
|
{"is_delete_friendship", q5.ToString(isDeleteFriendship)},
|
||||||
}
|
}
|
||||||
insertKv := fields
|
updateKv := [][]string{
|
||||||
updateKv := fields
|
{"is_delete_friendship", q5.ToString(isDeleteFriendship)},
|
||||||
|
}
|
||||||
f5.GetJsStyleDb().Upsert(
|
f5.GetJsStyleDb().Upsert(
|
||||||
FRIEND_DB,
|
FRIEND_DB,
|
||||||
"t_friend_ships",
|
"t_friend_ships",
|
||||||
@ -90,7 +93,9 @@ func (fm *FriendsMgr) upsertBlacklist(account1Id string, account2Id string, isRe
|
|||||||
{"blocked_account_id", account2Id},
|
{"blocked_account_id", account2Id},
|
||||||
{"is_removed", q5.ToString(isRemoved)},
|
{"is_removed", q5.ToString(isRemoved)},
|
||||||
}
|
}
|
||||||
updateKv := insertKv
|
updateKv := [][]string{
|
||||||
|
{"is_removed", q5.ToString(isRemoved)},
|
||||||
|
}
|
||||||
f5.GetJsStyleDb().Upsert(
|
f5.GetJsStyleDb().Upsert(
|
||||||
FRIEND_DB,
|
FRIEND_DB,
|
||||||
"t_friend_blacklist",
|
"t_friend_blacklist",
|
||||||
|
@ -159,19 +159,33 @@ func (gm *GuildMgr) updateGuild(g *Guild, fields [][]string) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// insertPendingReqs 添加申请加入公会记录
|
// insertPendingReqs 添加申请加入公会记录, 存在则更新记录
|
||||||
func (gm *GuildMgr) insertPendingReqs(guildId int64, applicantAccountId string) {
|
func (gm *GuildMgr) upsertPendingReqs(guildId int64, accountId string, isJoinGuild string) {
|
||||||
fields := [][]string{
|
where := [][]string{
|
||||||
{"guild_id", q5.ToString(guildId)},
|
{"guild_id", q5.ToString(guildId)},
|
||||||
{"account_id", applicantAccountId},
|
{"account_id", accountId},
|
||||||
|
}
|
||||||
|
insertKv := [][]string{
|
||||||
|
{"guild_id", q5.ToString(guildId)},
|
||||||
|
{"account_id", accountId},
|
||||||
{"is_join_guild", "0"},
|
{"is_join_guild", "0"},
|
||||||
}
|
}
|
||||||
f5.GetJsStyleDb().Replace(
|
updateKv := [][]string{
|
||||||
|
{"is_join_guild", isJoinGuild},
|
||||||
|
}
|
||||||
|
f5.GetJsStyleDb().Upsert(
|
||||||
FRIEND_DB,
|
FRIEND_DB,
|
||||||
"t_guild_pending_request",
|
"t_guild_pending_request",
|
||||||
fields,
|
where,
|
||||||
func(error, int64, int64) {},
|
updateKv,
|
||||||
)
|
insertKv,
|
||||||
|
func(err error, lastInsertId int64, rowsAffected int64) {
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("error:%v\n", err)
|
||||||
|
}
|
||||||
|
fmt.Printf("lastInsertId:%d\n", lastInsertId)
|
||||||
|
fmt.Printf("rowsAffected:%d\n", rowsAffected)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// updatePendingReqs 更新所有申请加入公会记录
|
// updatePendingReqs 更新所有申请加入公会记录
|
||||||
@ -198,13 +212,13 @@ func (gm *GuildMgr) updateAllPendingReqs(guildId int64, isJoinGuild int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// updatePendingReqs 更新申请加入公会记录
|
// updatePendingReqs 更新申请加入公会记录
|
||||||
func (gm *GuildMgr) updatePendingReqs(guildId int64, applicantAccountId string, isJoinGuild int) {
|
func (gm *GuildMgr) updatePendingReqs(guildId int64, accountId string, isJoinGuild string) {
|
||||||
fields := [][]string{
|
fields := [][]string{
|
||||||
{"is_join_guild", q5.ToString(isJoinGuild)},
|
{"is_join_guild", isJoinGuild},
|
||||||
}
|
}
|
||||||
where := [][]string{
|
where := [][]string{
|
||||||
{"guild_id", q5.ToString(guildId)},
|
{"guild_id", q5.ToString(guildId)},
|
||||||
{"account_id", applicantAccountId},
|
{"account_id", accountId},
|
||||||
}
|
}
|
||||||
f5.GetJsStyleDb().Update(
|
f5.GetJsStyleDb().Update(
|
||||||
FRIEND_DB,
|
FRIEND_DB,
|
||||||
@ -222,17 +236,24 @@ func (gm *GuildMgr) updatePendingReqs(guildId int64, applicantAccountId string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// insertPendingReqs 添加公会成员
|
// insertPendingReqs 添加公会成员
|
||||||
func (gm *GuildMgr) insertGuildMember(guildId int64, member *GuildMember) {
|
func (gm *GuildMgr) upsertGuildMember(guildId int64, member *GuildMember) {
|
||||||
fields := [][]string{
|
where := [][]string{
|
||||||
|
{"guild_id", q5.ToString(guildId)},
|
||||||
|
{"account_id", member.AccountId},
|
||||||
|
}
|
||||||
|
insertKv := [][]string{
|
||||||
{"guild_id", q5.ToString(guildId)},
|
{"guild_id", q5.ToString(guildId)},
|
||||||
{"account_id", member.AccountId},
|
{"account_id", member.AccountId},
|
||||||
{"level", q5.ToString(member.Level)},
|
{"level", q5.ToString(member.Level)},
|
||||||
{"is_leave_guild", "0"},
|
{"is_leave_guild", "0"},
|
||||||
}
|
}
|
||||||
f5.GetJsStyleDb().Replace(
|
updateKv := insertKv
|
||||||
|
f5.GetJsStyleDb().Upsert(
|
||||||
FRIEND_DB,
|
FRIEND_DB,
|
||||||
"t_guild_members",
|
"t_guild_members",
|
||||||
fields,
|
where,
|
||||||
|
updateKv,
|
||||||
|
insertKv,
|
||||||
func(err error, lastInsertId int64, rowsAffected int64) {
|
func(err error, lastInsertId int64, rowsAffected int64) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("error:%v\n", err)
|
fmt.Printf("error:%v\n", err)
|
||||||
@ -291,7 +312,6 @@ func (gm *GuildMgr) insertGuildLog(log *GuildLog) {
|
|||||||
{"account_id", log.AccountId},
|
{"account_id", log.AccountId},
|
||||||
{"log_type", q5.ToString(int(log.LogType))},
|
{"log_type", q5.ToString(int(log.LogType))},
|
||||||
{"content", log.Content}}
|
{"content", log.Content}}
|
||||||
|
|
||||||
f5.GetJsStyleDb().Insert(
|
f5.GetJsStyleDb().Insert(
|
||||||
FRIEND_DB,
|
FRIEND_DB,
|
||||||
"t_guild_logs",
|
"t_guild_logs",
|
||||||
|
@ -35,9 +35,9 @@ func (gm *GuildMgr) init() {
|
|||||||
|
|
||||||
// CreateGuild 创建公会
|
// CreateGuild 创建公会
|
||||||
func (gm *GuildMgr) CreateGuild(name string, leaderID string) (int64, error) {
|
func (gm *GuildMgr) CreateGuild(name string, leaderID string) (int64, error) {
|
||||||
// check join guild
|
// Check joined guild
|
||||||
otherGuildId, isJoin := gm.checkJoinGuild(leaderID)
|
otherGuildId, isJoined := gm.checkJoinGuild(leaderID)
|
||||||
if isJoin {
|
if isJoined {
|
||||||
errMsg := fmt.Sprintf("Player:%s has joined other guild:%d", leaderID, otherGuildId)
|
errMsg := fmt.Sprintf("Player:%s has joined other guild:%d", leaderID, otherGuildId)
|
||||||
return 0, fmt.Errorf(errMsg)
|
return 0, fmt.Errorf(errMsg)
|
||||||
}
|
}
|
||||||
@ -58,9 +58,9 @@ func (gm *GuildMgr) CreateGuild(name string, leaderID string) (int64, error) {
|
|||||||
newLevel := 1 // 会长
|
newLevel := 1 // 会长
|
||||||
newMember := GuildMember{AccountId: leaderID, Level: newLevel}
|
newMember := GuildMember{AccountId: leaderID, Level: newLevel}
|
||||||
_ = guild.AddMember(&newMember)
|
_ = guild.AddMember(&newMember)
|
||||||
gm.insertGuildMember(guildId, &newMember)
|
gm.upsertGuildMember(guildId, &newMember)
|
||||||
|
|
||||||
gm.Guilds[guildId] = guild
|
gm.Guilds[guildId] = guild
|
||||||
|
|
||||||
return guildId, nil
|
return guildId, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,7 +81,7 @@ func (gm *GuildMgr) ApplyToGuild(guildId int64, applicantAccountId string) error
|
|||||||
return fmt.Errorf("guild is full")
|
return fmt.Errorf("guild is full")
|
||||||
}
|
}
|
||||||
// IF exists, then replace it
|
// IF exists, then replace it
|
||||||
gm.insertPendingReqs(guildId, applicantAccountId)
|
gm.upsertPendingReqs(guildId, applicantAccountId, "0")
|
||||||
gm.pendingReqs[guildId][applicantAccountId] = true
|
gm.pendingReqs[guildId][applicantAccountId] = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -116,10 +116,9 @@ func (gm *GuildMgr) Approve(guildId int64, operatorAccountId, accountId string)
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
gm.insertGuildMember(guildId, &newMember)
|
gm.upsertGuildMember(guildId, &newMember)
|
||||||
|
|
||||||
delete(gm.pendingReqs[guildId], accountId)
|
delete(gm.pendingReqs[guildId], accountId)
|
||||||
gm.updatePendingReqs(guildId, accountId, 1)
|
gm.updatePendingReqs(guildId, accountId, "1")
|
||||||
|
|
||||||
logContent := fmt.Sprintf("Approve[%d-%s-%s]", guildId, operatorAccountId, accountId)
|
logContent := fmt.Sprintf("Approve[%d-%s-%s]", guildId, operatorAccountId, accountId)
|
||||||
gm.WriteLog(guildId, accountId, LogTypeApprove, logContent)
|
gm.WriteLog(guildId, accountId, LogTypeApprove, logContent)
|
||||||
@ -136,26 +135,34 @@ func (gm *GuildMgr) Reject(guildId int64, applicantAccountId string) error {
|
|||||||
|
|
||||||
//delete(gm.pendingReqs[guildId], applicantAccountId)
|
//delete(gm.pendingReqs[guildId], applicantAccountId)
|
||||||
gm.pendingReqs[guildId][applicantAccountId] = false
|
gm.pendingReqs[guildId][applicantAccountId] = false
|
||||||
gm.updatePendingReqs(guildId, applicantAccountId, 2)
|
gm.updatePendingReqs(guildId, applicantAccountId, "2")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// JoinGuild 可直接加入公会
|
// JoinGuild 直接加入公会
|
||||||
//func (gm *GuildMgr) JoinGuild(guildId int64, accountId string) error {
|
func (gm *GuildMgr) JoinGuild(guildId int64, accountId string) error {
|
||||||
// guild, exists := gm.Guilds[guildId]
|
guild, exists := gm.Guilds[guildId]
|
||||||
// if !exists {
|
if !exists {
|
||||||
// return fmt.Errorf("guild not found")
|
return fmt.Errorf("guild not found")
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// if len(guild.Members) >= guild.MaxMembers {
|
if len(guild.Members) >= guild.MaxMembers {
|
||||||
// return fmt.Errorf("guild is full")
|
return fmt.Errorf("guild is full")
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// newMember := GuildMember{AccountId: accountId, Level: 3}
|
newMember := GuildMember{AccountId: accountId, Level: 3}
|
||||||
// guild.Members = append(guild.Members, newMember)
|
err := guild.AddMember(&newMember)
|
||||||
// return nil
|
if err != nil {
|
||||||
//}
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
gm.upsertGuildMember(guildId, &newMember)
|
||||||
|
delete(gm.pendingReqs[guildId], accountId)
|
||||||
|
gm.updatePendingReqs(guildId, accountId, "1")
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// LeaveGuild 离开公会
|
// LeaveGuild 离开公会
|
||||||
func (gm *GuildMgr) LeaveGuild(guildId int64, accountId string) error {
|
func (gm *GuildMgr) LeaveGuild(guildId int64, accountId string) error {
|
||||||
@ -320,8 +327,8 @@ func (gm *GuildMgr) Disband(guildId int64, accountId string) error {
|
|||||||
fields := [][]string{{"is_leave_guild", q5.ToString(1)}}
|
fields := [][]string{{"is_leave_guild", q5.ToString(1)}}
|
||||||
gm.updateGuildMembers(guild, fields)
|
gm.updateGuildMembers(guild, fields)
|
||||||
|
|
||||||
is_join_guild := 3
|
isJoinGuild := 3
|
||||||
gm.updateAllPendingReqs(guildId, is_join_guild)
|
gm.updateAllPendingReqs(guildId, isJoinGuild)
|
||||||
|
|
||||||
// 确保在删除之前没有其他地方引用了公会对象的指针, 以避免空指针异常
|
// 确保在删除之前没有其他地方引用了公会对象的指针, 以避免空指针异常
|
||||||
gm.Guilds[guildId] = nil
|
gm.Guilds[guildId] = nil
|
||||||
|
Loading…
x
Reference in New Issue
Block a user