102 lines
2.4 KiB
Go
102 lines
2.4 KiB
Go
![]() |
package player
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"math/rand"
|
||
|
"samba/pkg/log"
|
||
|
"samba/pkg/xtime"
|
||
|
"samba/stub"
|
||
|
"samba/util/model"
|
||
|
)
|
||
|
|
||
|
var RobotMgr = &robotMgr{busyRobots: make(map[int64]*Player), idleRobots: make(map[int64]*Player)}
|
||
|
|
||
|
type robotMgr struct {
|
||
|
idleRobots map[int64]*Player
|
||
|
busyRobots map[int64]*Player
|
||
|
}
|
||
|
|
||
|
func (r *robotMgr) Load() bool {
|
||
|
ids := model.NewRobotInitOp().LoadIds()
|
||
|
op := model.NewUserInfoOp()
|
||
|
for _, id := range ids {
|
||
|
if user, err := op.Load(id); err == nil {
|
||
|
robot := &Player{UserInfo: user}
|
||
|
r.idleRobots[robot.UID] = robot
|
||
|
}
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
func (r *robotMgr) resetRobotCoin(robot *Player, roomCnf *stub.Room) {
|
||
|
op := model.NewUserResourceOp()
|
||
|
coins, err := op.Get(robot.UID, model.ResCoins)
|
||
|
takeCoins, err1 := op.GetTakeCoin(robot.UID)
|
||
|
if err1 != nil || err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if coins-takeCoins < roomCnf.MinCoin || coins-takeCoins > roomCnf.MaxCoin {
|
||
|
resetCoin := int64(0)
|
||
|
if roomCnf.MaxCoin > roomCnf.MinCoin {
|
||
|
resetCoin = rand.Int63n(roomCnf.MaxCoin-roomCnf.MinCoin) + roomCnf.MinCoin
|
||
|
}
|
||
|
add := resetCoin - (coins - takeCoins)
|
||
|
_, _ = op.Add(0, robot.UID, add, model.ResCoins)
|
||
|
//model.AddRobotResourcePool(roomCnf.Id, add)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (r *robotMgr) Pop(roomCnf *stub.Room) *Player {
|
||
|
r.tryReload()
|
||
|
if len(r.idleRobots) > 0 {
|
||
|
for _, robot := range r.idleRobots {
|
||
|
//if robot.UID != 15389236 && robot.UID != 14275496 {
|
||
|
// continue
|
||
|
//}
|
||
|
delete(r.idleRobots, robot.UID)
|
||
|
if r.isExpiration(robot) {
|
||
|
log.Debug(fmt.Sprintf("robot is expiration, UID: %d", robot.UID))
|
||
|
continue
|
||
|
}
|
||
|
r.resetRobotCoin(robot, roomCnf)
|
||
|
r.busyRobots[robot.UID] = robot
|
||
|
return robot
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (r *robotMgr) Push(player *Player) {
|
||
|
if player.IsRobot() {
|
||
|
player.Robot = nil
|
||
|
delete(r.busyRobots, player.UID)
|
||
|
r.idleRobots[player.UID] = player
|
||
|
} else {
|
||
|
log.Error(fmt.Sprintf("db.robot not enough %d", player.UID))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 判断是否过期
|
||
|
func (r *robotMgr) 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
|
||
|
|
||
|
}
|
||
|
|
||
|
func (r *robotMgr) tryReload() {
|
||
|
if len(r.busyRobots)+len(r.idleRobots) < 100 {
|
||
|
// 内存中的机器人小于一定数量后重新加载
|
||
|
// FIXME
|
||
|
// 待PHP完成相关功能后修改阈值
|
||
|
r.Load()
|
||
|
}
|
||
|
}
|