package model import ( "context" "errors" "fmt" "github.com/go-redis/redis/v8" "samba/pkg/log" "samba/pkg/xtime" "samba/util/rdbkey" ) type RobotScorePool struct { Score int64 `redis:"score"` Time int64 `redis:"time"` } type RobotScorePoolOp struct { rdb *redis.Client } func NewRobotScorePoolOp() *RobotScorePoolOp { return &RobotScorePoolOp{rdb: rdbMoney} } func (op *RobotScorePoolOp) Load() *RobotScorePool { res := &RobotScorePool{} err := op.rdb.HGetAll(context.TODO(), rdbkey.RobotClubScorePoolKey()).Scan(res) if err != nil { log.Error(fmt.Sprintf("failed to Load robotScorePool %s", err)) } return res } func (op *RobotScorePoolOp) Reset() { err := op.rdb.HMSet(context.TODO(), rdbkey.RobotClubScorePoolKey(), "score", 0, "time", xtime.Now().Timestamp()).Err() if err != nil { log.Error(fmt.Sprintf("failed to reset robotScorePool %s", err)) } } func AddRobotResourcePool(roomType int, add int64, resType string) { var err error switch resType { case ResCoins: err = rdbMoney.IncrBy(context.Background(), rdbkey.RobotCoinPoolKey(roomType), add).Err() case ResClubUserScore: err = rdbMoney.HIncrBy(context.TODO(), rdbkey.RobotClubScorePoolKey(), "score", add).Err() default: err = errors.New(fmt.Sprintf("unsupported resourceType %s", resType)) } if err != nil { log.Error(err.Error()) } }