```
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful

feat(database): 添加多个玩家相关表的联合唯一约束

- 为player_talk表添加玩家+挖矿联合唯一索引
- 为player_task表添加玩家+任务联合唯一索引
- 为player_title表添加玩家+称号联合唯一索引
- 为player_pet表添加玩家+精灵联合唯一索引
- 为player_cdk_log表添加玩家+CDK联合唯一索引
- 为player_egg表添加玩家孵蛋联合唯一索引
- 为player_pvp表添加PVP索引
- 为player_sign_in_log表添加签到联合唯一索引
- 为player_room_house表添加房间索引

fix(user-talk): 修复获取聊天配置
This commit is contained in:
昔念
2026-03-28 02:22:15 +08:00
parent 06091ff42c
commit d55c96e383
4 changed files with 57 additions and 18 deletions

View File

@@ -78,7 +78,6 @@ func (s *TalkService) Cheak(mapid uint32, flag int) (int, bool) {
}
// Update 原子更新返回是否更新成功true=成功 false=上限/失败/VIP
func (s *TalkService) Update(flag int, count int) bool {
// VIP 直接返回失败
if cool.Config.ServerInfo.IsVip != 0 {
@@ -96,28 +95,27 @@ func (s *TalkService) Update(flag int, count int) bool {
}
maxCount := int(cfg.DailyCollectCount)
// 2. 原子插入(不存在则插入,存在忽略,高并发安全)
talk := model.NewTalk()
talk.PlayerID = userID
talk.TalkID = talkID
_, _ = db.Data(talk).FieldsEx("id").InsertIgnore()
// 2. 原子操作:不存在则插入count=0存在则不操作
talk := model.Talk{
PlayerID: userID,
TalkID: talkID,
Count: 0, // 关键:必须初始化 count 为 0
}
// 唯一索引 + InsertIgnore 保证只会插入1次
_, _ = db.Data(talk).InsertIgnore()
// 3. 原子条件自增只有没超上限才会更新
// 核心:数据库原子判断,绝对不溢出
// 3. 原子条件自增只有没超上限才会增加)
affected, err := db.
Where("talk_id = ?", talkID).
Where("player_id = ?", userID). // 关键:必须加 player_id 条件
Where("count + ? <= ?", count, maxCount).
Increment("count", count)
if err != nil {
return false
}
row, _ := affected.RowsAffected()
// 有错误 或 没有影响行数 = 更新失败(已达上限
if row == 0 {
return false
}
return true
// 0 行受影响 = 已达上限
return row > 0
}