96 lines
2.4 KiB
Go
96 lines
2.4 KiB
Go
![]() |
package player
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"samba/pkg/log"
|
||
|
"samba/pkg/xtime"
|
||
|
"samba/stub"
|
||
|
"samba/util/model"
|
||
|
"samba/util/util"
|
||
|
)
|
||
|
|
||
|
var ClubRobotMgr = &clubRobotMgr{busyRobots: make(map[int64]map[int64]*Player), idleRobots: make(map[int64]map[int64]*Player)}
|
||
|
|
||
|
type clubRobotMgr struct {
|
||
|
idleRobots map[int64]map[int64]*Player // map[clubId]map[Uid]*Player
|
||
|
busyRobots map[int64]map[int64]*Player
|
||
|
}
|
||
|
|
||
|
func (r *clubRobotMgr) Load() {
|
||
|
clubs := model.NewRobotInitOp().LoadClubIds()
|
||
|
op := model.NewUserInfoOp()
|
||
|
for clubId, ids := range clubs {
|
||
|
for _, id := range ids {
|
||
|
if user, err := op.Load(id); err == nil {
|
||
|
robot := &Player{UserInfo: user}
|
||
|
mp, ok := r.idleRobots[clubId]
|
||
|
if !ok {
|
||
|
mp = make(map[int64]*Player)
|
||
|
r.idleRobots[clubId] = mp
|
||
|
}
|
||
|
mp[robot.UID] = robot
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func (r *clubRobotMgr) Pop(robotCnf *stub.ClubRobotConfig, clubId int) *Player {
|
||
|
if len(r.idleRobots) > 0 {
|
||
|
ids := r.idleRobots[int64(clubId)]
|
||
|
for _, robot := range ids {
|
||
|
delete(ids, robot.UID)
|
||
|
if r.isExpiration(robot) {
|
||
|
log.Debug(fmt.Sprintf("club robot is expiration, UID: %d", robot.UID))
|
||
|
continue
|
||
|
}
|
||
|
r.resetRobotClubScore(robot, robotCnf, clubId)
|
||
|
mp, ok := r.busyRobots[int64(clubId)]
|
||
|
if !ok {
|
||
|
mp = make(map[int64]*Player)
|
||
|
r.busyRobots[int64(clubId)] = mp
|
||
|
}
|
||
|
mp[robot.UID] = robot
|
||
|
return robot
|
||
|
}
|
||
|
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (r *clubRobotMgr) resetRobotClubScore(robot *Player, robotConf *stub.ClubRobotConfig, clubId int) {
|
||
|
|
||
|
op := model.NewUserResourceOp()
|
||
|
score, err := op.Get(robot.UID, model.ResClubUserScore)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
if score < robotConf.RobotMinScore || score > robotConf.RobotMaxScore {
|
||
|
resetScore := util.RandRange(robotConf.RobotMinScore, robotConf.RobotMaxScore)
|
||
|
add := resetScore - score
|
||
|
_, _ = op.Add(clubId, robot.UID, add, model.ResClubUserScore)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (r *clubRobotMgr) Push(player *Player, clubId int) {
|
||
|
if player.IsRobot() {
|
||
|
player.Robot = nil
|
||
|
delete(r.busyRobots[int64(clubId)], player.UID)
|
||
|
r.idleRobots[int64(clubId)][player.UID] = player
|
||
|
} else {
|
||
|
log.Error(fmt.Sprintf("player is not robot %d", player.UID))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 判断是否过期
|
||
|
func (r *clubRobotMgr) isExpiration(robot *Player) bool {
|
||
|
if stub.GGlobalAI.TrucoLifeDays == 0 && stub.GGlobalAI.TrucoMaxPlays == 0 {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
info := model.NewRobotInitOp().LoadBy(robot.UID)
|
||
|
ddl := info.ATime + int64(stub.GGlobalAI.TrucoLifeDays*24*3600)
|
||
|
return xtime.IsBeforeNow(ddl) || info.Plays >= stub.GGlobalAI.TrucoMaxPlays
|
||
|
|
||
|
}
|