player_timer.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package mapPlayer
  2. import (
  3. "f1-game/internal/data"
  4. "f1-game/internal/event"
  5. "f1-game/nodes/map/internal/db"
  6. mapFacade "f1-game/nodes/map/internal/facade"
  7. "f1-game/nodes/map/player/conscript"
  8. "f1-game/nodes/map/player/facility"
  9. "f1-game/nodes/map/player/hero"
  10. sandMap "f1-game/nodes/map/player/map"
  11. show "f1-game/nodes/map/player/show"
  12. "f1-game/nodes/map/player/storage"
  13. "f1-game/nodes/map/player/team"
  14. "time"
  15. )
  16. func (p *actorPlayer) startTimer() {
  17. //移除所有定时器
  18. p.Timer().RemoveAll()
  19. // 添加跨天定时器
  20. p.Timer().AddFixedHour(int(data.Const.ResetTime), 0, 0, p.onCrossDayTimer)
  21. // 添加每秒定时器
  22. p.Timer().Add(1*time.Second, p.every1STimer)
  23. // 资源定时器
  24. p.Timer().Add(3*time.Second, p.every3STimer)
  25. // 每隔5秒检查自动防守
  26. p.Timer().Add(5*time.Second, p.every5Stimer)
  27. }
  28. // onCrossDayTimer 跨天执行定时器
  29. func (p *actorPlayer) onCrossDayTimer() {
  30. // 不在线不处理跨天,等下次登录的时候处理
  31. if !p.isOnline {
  32. return
  33. }
  34. // 更新时间
  35. p.updateNowTime()
  36. playerTable := db.GetMapPlayerTable(p.playerID)
  37. // 跨天重置处理
  38. p.onReset(playerTable, false)
  39. p.Debug("[crossDay] isOnline = %v", p.isOnline)
  40. }
  41. // oneSecondTimer 每1秒定时器
  42. func (p *actorPlayer) every1STimer() {
  43. p.updateNowTime()
  44. nowSecond := p.now.ToSecond()
  45. facility.Service().CheckQueue(p.playerID, nowSecond)
  46. conscript.Service().ConscriptSettle(p.playerID, nowSecond)
  47. // 检查守军状态
  48. playerTable := db.GetMapPlayerTable(p.playerID)
  49. if playerTable == nil {
  50. return
  51. }
  52. playerTable.CheckDefendStatus(nowSecond)
  53. // 城外征兵
  54. team.Service().TeamOutsideAutoTroopsTimer(p.playerID, nowSecond)
  55. // 定时检测是否可以城外征兵
  56. team.Service().CheckCanOutsideAutoTroopsTimer(p.playerID, p.now.ToSecond())
  57. }
  58. // threeSecondTimer 每3秒定时器
  59. func (p *actorPlayer) every3STimer() {
  60. playerTable := db.GetMapPlayerTable(p.playerID)
  61. if playerTable == nil {
  62. return
  63. }
  64. hero.Service().CheckAddEnergyTimer(playerTable, p.now.ToSecond())
  65. // 检查资源自动产出
  66. storage.Service().CheckAutoAdd(playerTable, p.now.ToSecond(), p.isOnline)
  67. // 检查主城显示过期
  68. show.Service().CheckMainCityShowExpire(playerTable, p.now.ToSecond())
  69. // 检查VIP是否过期
  70. if !playerTable.Vip.IsExpired && playerTable.Vip.ExpireTime < p.now.ToMillisecond() {
  71. // 设置过期状态
  72. playerTable.Vip.IsExpired = true
  73. playerTable.Save2Queue()
  74. }
  75. }
  76. func (p *actorPlayer) every5Stimer() {
  77. playerTable := db.GetMapPlayerTable(p.playerID)
  78. if playerTable == nil {
  79. return
  80. }
  81. // 检查自动防守
  82. sandMap.Service().CheckAutoDefend(playerTable, p.now.ToMillisecond())
  83. }
  84. // onReset 重置处理(每次登录、跨天触发)
  85. func (p *actorPlayer) onReset(playerTable *db.MapPlayerTable, byLogin bool) {
  86. playerTable.Stats.CheckReset(p.now)
  87. // 无需重置,则跳过
  88. if !playerTable.Stats.IsCanReset() {
  89. return
  90. }
  91. // 各系统模块执行重置
  92. mapFacade.ServiceOnReset(playerTable, p.now, byLogin)
  93. // 发送跨天重置事件
  94. p.PostEvent(event.NewPlayerReset(p.playerID))
  95. p.Warn("daily reset finish. playerID=%d", p.playerID)
  96. }