player_timer.go 3.0 KB

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