actor.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package player
  2. import (
  3. nameActor "f1-game/internal/name/actor"
  4. nameRemote "f1-game/internal/name/remote"
  5. "f1-game/internal/sessions"
  6. "f1-game/nodes/game/internal/db"
  7. "time"
  8. cstring "github.com/cherry-game/cherry/extend/string"
  9. cfacade "github.com/cherry-game/cherry/facade"
  10. clog "github.com/cherry-game/cherry/logger"
  11. cactor "github.com/cherry-game/cherry/net/actor"
  12. )
  13. // actor 玩家管理
  14. type actor struct {
  15. cactor.Base
  16. cleanChildTime time.Duration
  17. }
  18. func NewActor() *actor {
  19. return &actor{
  20. cleanChildTime: 10 * time.Minute,
  21. }
  22. }
  23. func (p *actor) AliasID() string {
  24. return nameActor.GamePlayer
  25. }
  26. func (p *actor) OnInit() {
  27. p.Timer().Add(p.cleanChildTime, p.cleanChildTimer)
  28. }
  29. func (p *actor) OnLocalReceived(m *cfacade.Message) (bool, bool) {
  30. playerID := m.Session.GetString(sessions.PlayerID)
  31. if playerID == "" {
  32. clog.Warnf("[OnLocalReceived] player not found. m = %+v", m)
  33. return false, false
  34. }
  35. if child, found := p.Child().Get(playerID); found {
  36. child.PostLocal(m)
  37. }
  38. return false, false
  39. }
  40. func (p *actor) OnRemoteReceived(m *cfacade.Message) (next bool, invoke bool) {
  41. if m.TargetPath().IsParent() {
  42. // 不拦截父actor的remote消息
  43. return true, false
  44. }
  45. childID := m.TargetPath().ChildID
  46. playerID, ok := cstring.ToInt64(childID)
  47. if !ok {
  48. return false, false
  49. }
  50. if m.FuncName != nameRemote.Player_OnEnter {
  51. if _, found := db.GetPlayerTable(playerID); !found {
  52. clog.Warnf("player table not found. [playerID = %d, target = %s]", playerID, m.Target)
  53. return false, false
  54. }
  55. }
  56. return true, false
  57. }
  58. func (p *actor) OnFindChild(m *cfacade.Message) (cfacade.IActor, bool) {
  59. childID := m.TargetPath().ChildID
  60. playerID, ok := cstring.ToInt64(childID)
  61. if !ok {
  62. return nil, false
  63. }
  64. childPlayer := newActorPlayer(playerID)
  65. childActor, err := p.Child().Create(childID, &childPlayer)
  66. if err != nil {
  67. return nil, false
  68. }
  69. clog.Debugf("[actor] create child. actorID = %s, targetPath = %v",
  70. childActor.ActorID(),
  71. m.TargetPath(),
  72. )
  73. return childActor, true
  74. }
  75. // cleanChildTimer 定期清理actorPlayer
  76. func (p *actor) cleanChildTimer() {
  77. // 玩家已下线,并且超过childExitTime时间没有收发消息,则退出actor
  78. //deadline := ctime.Now().Add(-p.cleanChildTime).Unix()
  79. deadline := time.Now().Add(-p.cleanChildTime).Unix()
  80. p.Child().Each(func(iActor cfacade.IActor) {
  81. if iActor.LastAt() < deadline {
  82. p.Call(iActor.Path().String(), nameRemote.Player_OnCheckExit, nil)
  83. }
  84. })
  85. }