actor.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package ops
  2. import (
  3. capp "f1-game/internal/cherry_app"
  4. "f1-game/internal/code"
  5. "f1-game/internal/data"
  6. "f1-game/internal/enum"
  7. nameActor "f1-game/internal/name/actor"
  8. nameRemote "f1-game/internal/name/remote"
  9. "f1-game/internal/pb"
  10. "f1-game/internal/sessions"
  11. "f1-game/nodes/game/internal/db"
  12. "sync/atomic"
  13. clog "github.com/cherry-game/cherry/logger"
  14. "github.com/cherry-game/cherry/net/parser/pomelo"
  15. )
  16. type (
  17. actor struct {
  18. pomelo.ActorBase
  19. nodeState int32
  20. }
  21. )
  22. func NewActor() *actor {
  23. return &actor{}
  24. }
  25. func (p *actor) AliasID() string {
  26. return nameActor.GameOps
  27. }
  28. // OnInit Actor初始化前触发该函数
  29. func (p *actor) OnInit() {
  30. p.nodeState = data.NodeState.State
  31. clog.Infof("node default state [%d]", p.nodeState)
  32. p.Remote().Register(nameRemote.Ops_CheckNodeState, p.checkNodeState)
  33. p.Remote().Register(nameRemote.Ops_ChangeNodeState, p.changeNodeState)
  34. p.Remote().Register(nameRemote.Ops_Shutdown, p.shutdown)
  35. p.Remote().Register(nameRemote.Ops_DbReport, p.dbReport)
  36. p.Remote().Register(nameRemote.Ops_Online, p.online)
  37. p.Remote().Register(nameRemote.Ops_SetBattleValidation, p.setBattleValidation)
  38. }
  39. // OnStop Actor停止前触发该函数
  40. func (*actor) OnStop() {
  41. }
  42. // checkGameNode 检查当前游戏服状态
  43. func (p *actor) checkNodeState(req *pb.User) int32 {
  44. if data.NodeState.IsMaxOfOnline(sessions.UIDCount()) {
  45. return code.NodeIsMaxOfOnlineUser
  46. }
  47. // 开放状态,跳过检测
  48. if p.nodeState == enum.NodeOpen {
  49. return code.OK
  50. }
  51. // 维护状态,不在白名单中,则禁止进入
  52. if p.nodeState == enum.NodeMaintain {
  53. if data.NodeState.InWhiteList(req.Ip, req.Uid) {
  54. return code.OK
  55. }
  56. return code.NodeIsMaintain
  57. }
  58. // 否则禁止任何用户进入
  59. return code.NodeIsClosed
  60. }
  61. // changeNodeState 变更节点状态
  62. func (p *actor) changeNodeState(req *pb.I32) int32 {
  63. if req.Value < enum.NodeOpen || req.Value > enum.NodeClosed {
  64. return code.NodeChangeStateError
  65. }
  66. clog.Infof("node state from [%d] to [%d]", p.nodeState, req.Value)
  67. atomic.StoreInt32(&p.nodeState, req.Value)
  68. // 如果修改为关闭状态,则踢除本服所有在线用户
  69. if p.nodeState == enum.NodeClosed {
  70. clog.Infof("node state change to close. kick all online players! [count = %d]", sessions.UIDCount())
  71. rsp := &pb.I32{
  72. Value: code.NodeIsClosed,
  73. }
  74. sessions.KickGame(p.App().NodeID(), rsp, true)
  75. // for _, playerId := range sessions.OnlinePlayerIDS() {
  76. // sessions.Kick(playerId, rsp, true)
  77. // }
  78. }
  79. return code.OK
  80. }
  81. // shutdown 停机
  82. func (p *actor) shutdown() int32 {
  83. p.App().Shutdown()
  84. return code.OK
  85. }
  86. // dbReport 数据库报表
  87. func (p *actor) dbReport() (*pb.DBQueueReportList, int32) {
  88. return db.Queue().Report(), code.OK
  89. }
  90. // online 当前在线人数
  91. func (p *actor) online() (*pb.I64, int32) {
  92. rsp := &pb.I64{
  93. Value: sessions.UIDCount(),
  94. }
  95. return rsp, code.OK
  96. }
  97. func (p *actor) setBattleValidation(req *pb.Bool) int32 {
  98. oldState := capp.IsBattleValidation()
  99. capp.SetBattleValidation(req.Value)
  100. clog.Infof("set battle validation [%t -> %t]", oldState, req.Value)
  101. return code.OK
  102. }