TimeManager.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /** @format */
  2. import {Data, Mgr} from '../GameControl'
  3. import {LOCAL, MOD} from '../enums/Enum'
  4. /** @format */
  5. export class TimeManager {
  6. intervalID: number[] = []
  7. timeoutID: number[] = []
  8. globalTimeIntervalID: any
  9. reset() {
  10. this.intervalID.forEach(value => clearInterval(value))
  11. this.timeoutID.forEach(value => clearTimeout(value))
  12. this.intervalID.length = 0
  13. this.timeoutID.length = 0
  14. }
  15. setInterval(callback: Function, time: number, ...args: any[]) {
  16. let id = setInterval(callback, time, ...args)
  17. this.intervalID.push(id)
  18. return id
  19. }
  20. setTimeout(callback: Function, time: number, ...args: any[]) {
  21. let id = setTimeout(callback, time, ...args)
  22. this.timeoutID.push(id)
  23. return id
  24. }
  25. globalTimer() {
  26. if (this.globalTimeIntervalID) {
  27. clearInterval(this.globalTimeIntervalID)
  28. }
  29. this.globalTimeIntervalID = setInterval(() => {
  30. Data.main.globalTime += 0.1
  31. let curSecond = Math.floor(Data.main.globalTime)
  32. if (curSecond != Data.main.secondTime) {
  33. Data.main.secondTime = curSecond
  34. Data.main.serverTime = Data.main.loginTime + Data.main.secondTime
  35. }
  36. let curMinutes = Math.floor(Data.main.globalTime / 60)
  37. if (curMinutes != Data.main.minutesTime) Data.main.minutesTime = curMinutes
  38. }, 100)
  39. }
  40. initLoginDate(time: number) {
  41. let lastTime = Mgr.storage.getNumber(LOCAL.loginDay, 1627508200)
  42. let lastZero = Date.getDayTime(lastTime, 0)
  43. let nowZero = Date.getDayTime(time, 0)
  44. Data.main.isSecondDay = nowZero > lastZero
  45. Mgr.storage.set(LOCAL.loginDay, nowZero)
  46. }
  47. }