player_timer.go 2.7 KB

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