Files
bl/modules/player/service/talk.go

122 lines
2.6 KiB
Go
Raw Normal View History

package service
import (
"blazing/common/utils"
2025-11-16 20:30:17 +00:00
"blazing/cool"
config "blazing/modules/config/service"
"blazing/modules/player/model"
"github.com/gogf/gf/v2/os/gtime"
)
2025-11-16 20:30:17 +00:00
type TalkService struct {
BaseService
}
func NewTalkService(id uint32) *TalkService {
return &TalkService{
BaseService: BaseService{userid: id,
Service: &cool.Service{Model: model.NewTalk()},
},
}
}
func (s *TalkService) reset(flag int) {
s.dbm(s.Model).Where("talk_id", flag).Data("count", 0, "last_reset_time", gtime.Now()).Update()
}
2025-11-16 20:30:17 +00:00
//实现挖矿次数确认
func (s *TalkService) Cheak(mapid uint32, flag int) (int, bool) {
2026-02-13 22:57:05 +08:00
m1 := s.dbm(s.Model)
var talks *model.Talk
m1.Where("talk_id", flag).Scan(&talks)
if talks == nil {
return 0, true //如果表里没有记载数据,那么就可以直接挖矿
}
c := config.NewTalkConfigService().GetCache(flag)
//因为这个是挖一次更新一次,而且是实时更新的,如果更新日期是今天,那么就可以确认不用再重置,否则就需要重置挖矿记录
if c == nil {
return 0, false //没在地图
}
switch c.Limit {
case 0:
if !utils.IsToday(talks.LastResetTime) {
s.reset(flag)
return int(0), true
}
case 1:
if !utils.IsWEEK(talks.LastResetTime) {
s.reset(flag)
return int(0), true
}
case 2:
if !utils.IsMon(talks.LastResetTime) {
s.reset(flag)
return int(0), true
}
}
2026-02-14 04:27:57 +08:00
if uint32(mapid) != c.MapID {
return 0, false //没在地图
}
2026-02-14 04:27:57 +08:00
if talks.Count >= c.DailyCollectCount {
return 0, false
2025-10-07 08:14:11 +00:00
}
return int(talks.Count), true //int(config.MaxDailyCnt - talks.Count)
2025-10-07 08:14:11 +00:00
}
func (s *TalkService) Update(flag int, count int) bool {
// VIP 直接返回失败
if cool.Config.ServerInfo.IsVip != 0 {
return false
}
2026-02-13 22:57:05 +08:00
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. 原子操作不存在则插入count=0存在则不操作
talk := model.Talk{
PlayerID: userID,
TalkID: talkID,
Count: 0, // 关键:必须初始化 count 为 0
}
// 唯一索引 + InsertIgnore 保证只会插入1次
_, _ = db.Data(talk).InsertIgnore()
// 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()
// 0 行受影响 = 已达上限
return row > 0
}