Files
bl/modules/player/service/talk.go
xinian 7a7fae05ea
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
feat: 新增战斗技能效果648-677
2026-03-30 12:11:44 +08:00

121 lines
2.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package service
import (
"blazing/common/utils"
"blazing/cool"
config "blazing/modules/config/service"
"blazing/modules/player/model"
"github.com/gogf/gf/v2/os/gtime"
)
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()
}
//实现挖矿次数确认
func (s *TalkService) Cheak(mapid uint32, flag int) (int, bool) {
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
}
}
if uint32(mapid) != c.MapID {
return 0, false //没在地图
}
if talks.Count >= c.DailyCollectCount {
return 0, false
}
return int(talks.Count), true //int(config.MaxDailyCnt - talks.Count)
}
func (s *TalkService) Update(flag int, count int) bool {
// VIP 直接返回失败
if cool.Config.ServerInfo.IsVip != 0 {
return true
}
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,
}
// 唯一索引 + 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
}