56 lines
1.6 KiB
Go
56 lines
1.6 KiB
Go
![]() |
package model
|
|||
|
|
|||
|
import (
|
|||
|
"context"
|
|||
|
"errors"
|
|||
|
"fmt"
|
|||
|
"github.com/go-redis/redis/v8"
|
|||
|
"samba/pkg/xtime"
|
|||
|
"samba/util/rdbkey"
|
|||
|
)
|
|||
|
|
|||
|
type UserStreakOp struct {
|
|||
|
rdb *redis.Client
|
|||
|
}
|
|||
|
|
|||
|
func NewUserStreakOp() *UserStreakOp {
|
|||
|
return &UserStreakOp{
|
|||
|
rdb: rdbGameLog,
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// SetLossStreak 设置用户连败次数
|
|||
|
func (op *UserStreakOp) SetLossStreak(uid int64, gameType string, count int) error {
|
|||
|
return op.rdb.HSet(context.TODO(), rdbkey.UserLossStreakKey(), fmt.Sprintf("%d-%s", uid, gameType), count).Err()
|
|||
|
}
|
|||
|
|
|||
|
// IncLossStreak 增加用户连败次数
|
|||
|
func (op *UserStreakOp) IncLossStreak(uid int64, gameType string, inc int) (updated int64, err error) {
|
|||
|
return op.rdb.HIncrBy(context.TODO(), rdbkey.UserLossStreakKey(), fmt.Sprintf("%d-%s", uid, gameType), int64(inc)).Result()
|
|||
|
}
|
|||
|
|
|||
|
// UpdateBankruptStreak 更新连续破产次数
|
|||
|
func (op *UserStreakOp) UpdateBankruptStreak(uid int64) (updated int64, err error) {
|
|||
|
// 获取上次更新的时间
|
|||
|
lastTime, err := op.rdb.HGet(context.TODO(), rdbkey.UserBankruptStreakKey(), fmt.Sprintf("%d-timestamp", uid)).Int64()
|
|||
|
if err != nil && !errors.Is(err, redis.Nil) {
|
|||
|
return 0, err
|
|||
|
}
|
|||
|
if xtime.IsToday(lastTime) {
|
|||
|
// 时间为今天,直接Inc
|
|||
|
updated, err = op.rdb.HIncrBy(context.TODO(), rdbkey.UserBankruptStreakKey(), fmt.Sprintf("%d", uid), 1).Result()
|
|||
|
if err != nil {
|
|||
|
return 0, err
|
|||
|
}
|
|||
|
|
|||
|
} else {
|
|||
|
err = op.rdb.HSet(context.TODO(), rdbkey.UserBankruptStreakKey(), fmt.Sprintf("%d", uid), 1).Err()
|
|||
|
updated = 1
|
|||
|
if err != nil {
|
|||
|
return 0, err
|
|||
|
}
|
|||
|
}
|
|||
|
err = op.rdb.HSet(context.TODO(), rdbkey.UserBankruptStreakKey(), fmt.Sprintf("%d-timestamp", uid), xtime.Now().Timestamp()).Err()
|
|||
|
return
|
|||
|
}
|