samba/util/model/robot.go

156 lines
4.1 KiB
Go
Raw Permalink Normal View History

2025-06-04 09:51:39 +08:00
package model
import (
"context"
"encoding/json"
"fmt"
"github.com/go-redis/redis/v8"
"gorm.io/gorm"
"samba/pkg/log"
"samba/util/rdbkey"
"strconv"
"strings"
)
//type RobotInfoOp struct {
// rdb *redis.Client
// db *gorm.DB
//}
//
//func NewRobotInfoOp() *RobotInfoOp {
// return &RobotInfoOp{rdb: rdbBaseInfo, db: userDB}
//}
//
//func (op *RobotInfoOp) loadDb(minUid int64, count int) ([]*UserInfo, error) {
// var users []*UserInfo
// //result := op.db.Where("uid > ? AND utype = ?", minUid, eternal.RobotType).Order("uid ASC").Limit(count).Find(&users)
//
// result := op.db.Raw("SELECT u.*, r.type FROM t_user_info u JOIN t_robot r ON u.uid = r.uid AND u.uid>? ORDER BY u.uid ASC LIMIT ?", minUid, count).Scan(&users)
// if result.Error != nil {
// return nil, result.Error
// }
//
// return users, result.Error
//}
type RobotInfo struct {
UID int64 `gorm:"column:uid" json:"uid" redis:"uid"` // UID
Plays int `gorm:"column:plays" json:"plays" redis:"plays"` // 机器人已进行的游戏局数
ATime int64 `gorm:"column:atime" json:"atime" redis:"atime"` // 机器人的创建时间
}
type RobotInitOp struct {
rdb *redis.Client
db *gorm.DB
}
type userInfoWithClub struct {
UserInfo
ClubId int64 `gorm:"column:club_id" json:"club_id"`
}
// 初始化并加载所有机器人
func NewRobotInitOp() *RobotInitOp {
return &RobotInitOp{rdb: rdbBaseInfo, db: userDB}
}
func (op *RobotInitOp) LoadIds() []int64 {
ids := make([]int64, 0)
sids, err := op.rdb.SMembers(context.Background(), rdbkey.RobotIdSetKey()).Result()
if err == nil && len(sids) > 0 {
//log.Debug(fmt.Sprintf("robot:%v", sids))
for _, sid := range sids {
id, _ := strconv.ParseInt(sid, 10, 64)
if id > 0 {
ids = append(ids, id)
}
}
return ids
}
var users []*UserInfo
result := op.db.Raw("SELECT u.*, r.type FROM t_user_info u JOIN t_robot r ON u.uid = r.uid").Scan(&users)
if result.Error != nil {
log.Error(result.Error.Error())
return nil
}
for _, robot := range users {
var bytesUser []byte
bytesUser, err = json.Marshal(robot)
if err != nil {
log.Error(err.Error())
continue
}
//log.Debug(string(bytesUser))
err = op.rdb.Set(context.Background(), rdbkey.BaseUserKey(robot.UID), bytesUser, 0).Err()
if err != nil {
log.Error(err.Error())
continue
}
op.rdb.SAdd(context.Background(), rdbkey.RobotIdSetKey(), robot.UID)
ids = append(ids, robot.UID)
}
return ids
}
func (op *RobotInitOp) LoadBy(uid int64) *RobotInfo {
var info RobotInfo
err := op.rdb.HMGet(context.TODO(), rdbkey.RobotInfoKey(uid), "atime", "plays").Scan(&info)
if err != nil {
log.Error(fmt.Sprintf("Load robot %d info error:%s", uid, err))
return &info
}
return &info
}
func (op *RobotInitOp) LoadClubIds() map[int64][]int64 {
vals, err := op.rdb.SMembers(context.Background(), rdbkey.ClubRobotIdSetKey()).Result()
if err == nil && len(vals) > 0 {
return op.groupUidsByClubId(vals)
}
res := make(map[int64][]int64)
var users []*userInfoWithClub
result := op.db.Raw("SELECT u.*, r.type,c.club_id FROM t_user_info u JOIN t_robot r ON u.uid = r.uid JOIN t_club_member_info c On u.uid = c.uid").Scan(&users)
if result.Error != nil {
log.Error(result.Error.Error())
return nil
}
for _, robot := range users {
var bytesUser []byte
bytesUser, err = json.Marshal(robot.UserInfo)
if err != nil {
log.Error(err.Error())
continue
}
//log.Debug(string(bytesUser))
err = op.rdb.Set(context.Background(), rdbkey.BaseUserKey(robot.UID), bytesUser, 0).Err()
if err != nil {
log.Error(err.Error())
continue
}
op.rdb.SAdd(context.Background(), rdbkey.ClubRobotIdSetKey(), fmt.Sprintf("%d-%d", robot.ClubId, robot.UID))
res[robot.ClubId] = append(res[robot.ClubId], robot.UID)
}
return res
}
// 将 []clubId-uid 转换为 map[clubId][]uid
func (op *RobotInitOp) groupUidsByClubId(vals []string) map[int64][]int64 {
res := make(map[int64][]int64)
for _, val := range vals {
splits := strings.Split(val, "-")
clubId, _ := strconv.ParseInt(splits[0], 10, 64)
uid, _ := strconv.ParseInt(splits[1], 10, 64)
res[clubId] = append(res[clubId], uid)
}
return res
}