This commit is contained in:
@@ -147,9 +147,12 @@ func (h Controller) BuyGoldItem(data *item.C2S_GOLD_BUY_PRODUCT, player *player.
|
|||||||
player.Info.Coins -= int64(usegold)
|
player.Info.Coins -= int64(usegold)
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
|
r := player.Service.Talk.Update(int(pro.ProductID), int(data.Count))
|
||||||
|
if !r {
|
||||||
|
return nil, errorcode.ErrorCodes.ErrGoldBeanSingleLimit
|
||||||
|
}
|
||||||
player.User.UpdateGold(player.Info.UserID, -int64(usegold))
|
player.User.UpdateGold(player.Info.UserID, -int64(usegold))
|
||||||
//购买成功,执行记录
|
//购买成功,执行记录
|
||||||
player.Service.Talk.Update(int(pro.ProductID), int(data.Count))
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -77,19 +77,47 @@ func (s *TalkService) Cheak(mapid uint32, flag int) (int, bool) {
|
|||||||
return int(talks.Count), true //int(config.MaxDailyCnt - talks.Count)
|
return int(talks.Count), true //int(config.MaxDailyCnt - talks.Count)
|
||||||
|
|
||||||
}
|
}
|
||||||
func (s *TalkService) Update(flag int, count int) {
|
|
||||||
|
// Update 原子更新,返回是否更新成功(true=成功 false=上限/失败/VIP)
|
||||||
|
func (s *TalkService) Update(flag int, count int) bool {
|
||||||
|
// VIP 直接返回失败
|
||||||
if cool.Config.ServerInfo.IsVip != 0 {
|
if cool.Config.ServerInfo.IsVip != 0 {
|
||||||
|
return false
|
||||||
return
|
|
||||||
}
|
|
||||||
m := s.dbm(s.Model).Where("talk_id", flag)
|
|
||||||
if condition, _ := m.Exist(); !condition {
|
|
||||||
talks := model.NewTalk()
|
|
||||||
talks.PlayerID = uint64(s.userid)
|
|
||||||
talks.TalkID = uint32(flag)
|
|
||||||
s.dbm(s.Model).Data(talks).FieldsEx("id").Insert()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s.dbm(s.Model).Where("talk_id", flag).Increment("count", count)
|
talkID := uint32(flag)
|
||||||
|
userID := uint64(s.userid)
|
||||||
|
db := s.dbm(s.Model)
|
||||||
|
|
||||||
|
// 1. 获取配置上限
|
||||||
|
cfg := config.NewTalkConfigService().GetCache(flag)
|
||||||
|
if cfg == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
maxCount := int(cfg.DailyCollectCount)
|
||||||
|
|
||||||
|
// 2. 原子插入(不存在则插入,存在忽略,高并发安全)
|
||||||
|
talk := model.NewTalk()
|
||||||
|
talk.PlayerID = userID
|
||||||
|
talk.TalkID = talkID
|
||||||
|
_, _ = db.Data(talk).FieldsEx("id").InsertIgnore()
|
||||||
|
|
||||||
|
// 3. 原子条件自增:只有没超过上限才会更新
|
||||||
|
// 核心:数据库原子判断,绝对不溢出
|
||||||
|
affected, err := db.
|
||||||
|
Where("talk_id = ?", talkID).
|
||||||
|
Where("count + ? <= ?", count, maxCount).
|
||||||
|
Increment("count", count)
|
||||||
|
if err != nil {
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
row, _ := affected.RowsAffected()
|
||||||
|
// 有错误 或 没有影响行数 = 更新失败(已达上限)
|
||||||
|
if row == 0 {
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user