actor.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package scout
  2. import (
  3. capp "f1-game/internal/cherry_app"
  4. "f1-game/internal/code"
  5. "f1-game/internal/enum"
  6. nameActor "f1-game/internal/name/actor"
  7. nameRemote "f1-game/internal/name/remote"
  8. "f1-game/internal/pb"
  9. "f1-game/nodes/map/internal/db"
  10. dbMapScout "f1-game/nodes/map/internal/db/data/scout"
  11. "time"
  12. ctime "github.com/cherry-game/cherry/extend/time"
  13. cfacade "github.com/cherry-game/cherry/facade"
  14. clog "github.com/cherry-game/cherry/logger"
  15. cactor "github.com/cherry-game/cherry/net/actor"
  16. )
  17. // 侦察
  18. type actor struct {
  19. cactor.Base
  20. table *db.MapScoutTable
  21. }
  22. func Init(app cfacade.IApplication, table *db.MapScoutTable) {
  23. actor := &actor{
  24. table: table,
  25. }
  26. app.ActorSystem().CreateActor(actor.AliasID(), actor)
  27. }
  28. func (p *actor) AliasID() string {
  29. return nameActor.Map_Scout
  30. }
  31. func (p *actor) OnInit() {
  32. // 添加定时器处理
  33. p.Timer().Add(1*time.Second, p.every1STimer)
  34. p.Timer().Add(5*time.Minute, p.checkAndSaveTimer)
  35. p.Remote().Register(nameRemote.MapScout_UpdateStatus, p.updateScoutStatus)
  36. }
  37. // 更新侦察状态
  38. func (p *actor) updateScoutStatus(req *pb.ScoutRequest) int32 {
  39. var (
  40. queueID = req.QueueID
  41. scoutType = req.ScoutType
  42. playerID = req.PlayerID
  43. endTime = req.EndTime
  44. )
  45. if enum.CheckNotScoutType(scoutType) {
  46. return code.ScoutTypeError
  47. }
  48. playerScout, found := p.table.GetPlayerScout(playerID)
  49. if !found {
  50. playerScout = dbMapScout.NewPlayerScout()
  51. p.table.AddPlayerScount(playerID, playerScout)
  52. }
  53. if exist := playerScout.CheckQueueScouting(queueID); exist {
  54. return code.ScoutQueueBusy
  55. }
  56. if scoutType == enum.ScoutType_March {
  57. // 添加行军记录
  58. playerScout.AddScoutMarch(queueID, endTime)
  59. } else {
  60. // 添加开始侦察记录
  61. playerScout.AddScouting(queueID, endTime)
  62. }
  63. p.table.SetChangeData()
  64. return code.OK
  65. }
  66. func (p *actor) every1STimer() {
  67. now := ctime.Now().ToMillisecond()
  68. // 判断侦察是否结束,通知map处理
  69. for playerID, playerScout := range p.table.PlayerScoutMap {
  70. // 获取侦察行军结束的列表
  71. startIDs := playerScout.RemoveAndGetScoutMarchs(now)
  72. // 获取已结束的侦察列表
  73. endIDs := playerScout.RemoveAndGetScouting(now)
  74. if len(startIDs) <= 0 && len(endIDs) <= 0 {
  75. continue
  76. }
  77. req := &pb.ScoutUpdateNotice{
  78. Starts: startIDs,
  79. Ends: endIDs,
  80. }
  81. targetPath := cfacade.NewChildPath("", nameActor.Map_Player, playerID)
  82. errCode := capp.Call(targetPath, nameRemote.MapScout_UpdateNotice, req)
  83. if code.IsFail(errCode) {
  84. clog.Warnf("scout send update notice fail. playerID:%d, starts:%+v, ends:%d", playerID, startIDs, endIDs)
  85. }
  86. p.table.SetChangeData()
  87. }
  88. }
  89. func (p *actor) checkAndSaveTimer() {
  90. // 删除无用的玩家侦察数据
  91. p.table.RemoveEmptyPlayerScout()
  92. // 检测数据保存
  93. if p.table.IsChangeData() {
  94. p.table.Save2Queue()
  95. }
  96. }
  97. func (p *actor) OnStop() {
  98. p.checkAndSaveTimer()
  99. }