player_timer.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. 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. if !p.isOnline {
  17. return
  18. }
  19. //移除所有定时器
  20. p.Timer().RemoveAll()
  21. // 添加跨天定时器
  22. p.Timer().AddFixedHour(int(data.Const.ResetTime), 0, 0, p.onCrossDayTimer)
  23. // 添加每秒定时器
  24. p.Timer().Add(1*time.Second, p.every1STimer)
  25. // 资源定时器
  26. p.Timer().Add(3*time.Second, p.every3STimer)
  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. if !p.isOnline {
  44. return
  45. }
  46. p.updateNowTime()
  47. nowSecond := p.now.ToSecond()
  48. facility.Service().CheckQueue(p.playerID, nowSecond)
  49. conscript.Service().ConscriptSettle(p.playerID, nowSecond)
  50. // 检查守军状态
  51. playerTable := db.GetMapPlayerTable(p.playerID)
  52. if playerTable == nil {
  53. return
  54. }
  55. playerTable.CheckDefendStatus(nowSecond)
  56. // 城外征兵
  57. team.Service().TeamOutsideAutoTroopsTimer(p.playerID, nowSecond)
  58. // 定时检测是否可以城外征兵
  59. team.Service().CheckCanOutsideAutoTroopsTimer(p.playerID, p.now.ToSecond())
  60. }
  61. // threeSecondTimer 每3秒定时器
  62. func (p *actorPlayer) every3STimer() {
  63. playerTable := db.GetMapPlayerTable(p.playerID)
  64. if playerTable == nil {
  65. return
  66. }
  67. hero.Service().CheckAddEnergyTimer(playerTable, p.now.ToSecond())
  68. // 检查资源自动产出
  69. storage.Service().CheckAutoAdd(playerTable, p.now.ToSecond(), p.isOnline)
  70. // 检查主城显示过期
  71. show.Service().CheckMainCityShowExpire(playerTable, p.now.ToSecond())
  72. // 检查VIP是否过期
  73. if !playerTable.Vip.IsExpired && playerTable.Vip.ExpireTime < p.now.ToMillisecond() {
  74. // 设置过期状态
  75. playerTable.Vip.IsExpired = true
  76. playerTable.Save2Queue()
  77. }
  78. }
  79. // onReset 重置处理(每次登录、跨天触发)
  80. func (p *actorPlayer) onReset(playerTable *db.MapPlayerTable, byLogin bool) {
  81. playerTable.Stats.CheckReset(p.now)
  82. // 无需重置,则跳过
  83. if !playerTable.Stats.IsCanReset() {
  84. return
  85. }
  86. // 各系统模块执行重置
  87. mapFacade.ServiceOnReset(playerTable, p.now, byLogin)
  88. // 发送跨天重置事件
  89. p.PostEvent(event.NewPlayerReset(p.playerID))
  90. p.Warn("daily reset finish. playerID=%d", p.playerID)
  91. }