| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package ops
- import (
- capp "f1-game/internal/cherry_app"
- "f1-game/internal/code"
- "f1-game/internal/data"
- "f1-game/internal/enum"
- nameActor "f1-game/internal/name/actor"
- nameRemote "f1-game/internal/name/remote"
- "f1-game/internal/pb"
- "f1-game/internal/sessions"
- "f1-game/nodes/game/internal/db"
- "sync/atomic"
- clog "github.com/cherry-game/cherry/logger"
- "github.com/cherry-game/cherry/net/parser/pomelo"
- )
- type (
- actor struct {
- pomelo.ActorBase
- nodeState int32
- }
- )
- func NewActor() *actor {
- return &actor{}
- }
- func (p *actor) AliasID() string {
- return nameActor.GameOps
- }
- // OnInit Actor初始化前触发该函数
- func (p *actor) OnInit() {
- p.nodeState = data.NodeState.State
- clog.Infof("node default state [%d]", p.nodeState)
- p.Remote().Register(nameRemote.Ops_CheckNodeState, p.checkNodeState)
- p.Remote().Register(nameRemote.Ops_ChangeNodeState, p.changeNodeState)
- p.Remote().Register(nameRemote.Ops_Shutdown, p.shutdown)
- p.Remote().Register(nameRemote.Ops_DbReport, p.dbReport)
- p.Remote().Register(nameRemote.Ops_Online, p.online)
- p.Remote().Register(nameRemote.Ops_SetBattleValidation, p.setBattleValidation)
- }
- // OnStop Actor停止前触发该函数
- func (*actor) OnStop() {
- }
- // checkGameNode 检查当前游戏服状态
- func (p *actor) checkNodeState(req *pb.User) int32 {
- if data.NodeState.IsMaxOfOnline(sessions.UIDCount()) {
- return code.NodeIsMaxOfOnlineUser
- }
- // 开放状态,跳过检测
- if p.nodeState == enum.NodeOpen {
- return code.OK
- }
- // 维护状态,不在白名单中,则禁止进入
- if p.nodeState == enum.NodeMaintain {
- if data.NodeState.InWhiteList(req.Ip, req.Uid) {
- return code.OK
- }
- return code.NodeIsMaintain
- }
- // 否则禁止任何用户进入
- return code.NodeIsClosed
- }
- // changeNodeState 变更节点状态
- func (p *actor) changeNodeState(req *pb.I32) int32 {
- if req.Value < enum.NodeOpen || req.Value > enum.NodeClosed {
- return code.NodeChangeStateError
- }
- clog.Infof("node state from [%d] to [%d]", p.nodeState, req.Value)
- atomic.StoreInt32(&p.nodeState, req.Value)
- // 如果修改为关闭状态,则踢除本服所有在线用户
- if p.nodeState == enum.NodeClosed {
- clog.Infof("node state change to close. kick all online players! [count = %d]", sessions.UIDCount())
- rsp := &pb.I32{
- Value: code.NodeIsClosed,
- }
- sessions.KickGame(p.App().NodeID(), rsp, true)
- // for _, playerId := range sessions.OnlinePlayerIDS() {
- // sessions.Kick(playerId, rsp, true)
- // }
- }
- return code.OK
- }
- // shutdown 停机
- func (p *actor) shutdown() int32 {
- p.App().Shutdown()
- return code.OK
- }
- // dbReport 数据库报表
- func (p *actor) dbReport() (*pb.DBQueueReportList, int32) {
- return db.Queue().Report(), code.OK
- }
- // online 当前在线人数
- func (p *actor) online() (*pb.I64, int32) {
- rsp := &pb.I64{
- Value: sessions.UIDCount(),
- }
- return rsp, code.OK
- }
- func (p *actor) setBattleValidation(req *pb.Bool) int32 {
- oldState := capp.IsBattleValidation()
- capp.SetBattleValidation(req.Value)
- clog.Infof("set battle validation [%t -> %t]", oldState, req.Value)
- return code.OK
- }
|