diff --git a/src/handlers/polling-handler.ts b/src/handlers/polling-handler.ts index b6c316ab1..0b173fdc9 100644 --- a/src/handlers/polling-handler.ts +++ b/src/handlers/polling-handler.ts @@ -26,11 +26,12 @@ import { vendingMachineHandler } from './vending-machine-handler'; import { storageMonitorHandler } from './storage-monitor-handler'; import { smartAlarmHandler } from './smart-alarm-handler'; import { smartSwitchHandler } from './smart-switch-handler'; +import { Time } from '../structures/Time'; const { RustPlus } = require('../structures/RustPlus'); const Info = require('../structures/Info'); const MapMarkers = require('../structures/MapMarkers.js'); const Team = require('../structures/Team'); -const Time = require('../structures/Time'); +//const Time = require('../structures/Time'); export async function pollingHandler(rustplus: typeof RustPlus) { /* Poll information such as info, mapMarkers, teamInfo and time */ @@ -45,7 +46,7 @@ export async function pollingHandler(rustplus: typeof RustPlus) { if (rustplus.isFirstPoll) { rustplus.sInfo = new Info(info.info); - rustplus.time = new Time(time.time, rustplus, client); + rustplus.time = new Time(rustplus, time.time); rustplus.team = new Team(teamInfo.teamInfo, rustplus); rustplus.mapMarkers = new MapMarkers(mapMarkers.mapMarkers, rustplus, client); } diff --git a/src/structures/Player.js b/src/structures/Player.js index 1278ffd92..6ae4c52b4 100644 --- a/src/structures/Player.js +++ b/src/structures/Player.js @@ -20,7 +20,7 @@ const Constants = require('../util/constants.ts'); const Map = require('../util/map.ts'); -const Time = require('../util/timer.ts'); +const Timer = require('../util/timer.ts'); class Player { constructor(player, rustplus) { @@ -148,23 +148,23 @@ class Player { } getAfkSeconds() { return (new Date() - this.lastMovement) / 1000; } - getAfkTime(ignore = '') { return Time.secondsToFullScale(this.getAfkSeconds(), ignore); } + getAfkTime(ignore = '') { return Timer.secondsToFullScale(this.getAfkSeconds(), ignore); } getAliveSeconds() { if (this.spawnTime === 0) return 0; return (new Date() - new Date(this.spawnTime * 1000)) / 1000; } - getAliveTime(ignore = '') { return Time.secondsToFullScale(this.getAliveSeconds(), ignore); } + getAliveTime(ignore = '') { return Timer.secondsToFullScale(this.getAliveSeconds(), ignore); } getDeathSeconds() { if (this.deathTime === 0) return 0; return (new Date() - new Date(this.deathTime * 1000)) / 1000; } - getDeathTime(ignore = '') { return (Time.secondsToFullScale(this.getDeathSeconds(), ignore)); } + getDeathTime(ignore = '') { return (Timer.secondsToFullScale(this.getDeathSeconds(), ignore)); } getOfflineTime(ignore = '') { if (this.wentOfflineTime === null) return null; const seconds = (new Date() - this.wentOfflineTime) / 1000; - return (Time.secondsToFullScale(seconds, ignore)); + return (Timer.secondsToFullScale(seconds, ignore)); } async assignLeader() { diff --git a/src/structures/Time.js b/src/structures/Time.ts similarity index 51% rename from src/structures/Time.js rename to src/structures/Time.ts index 64a6924ff..e0a840a25 100644 --- a/src/structures/Time.js +++ b/src/structures/Time.ts @@ -1,5 +1,5 @@ /* - Copyright (C) 2022 Alexander Emanuelsson (alexemanuelol) + Copyright (C) 2024 Alexander Emanuelsson (alexemanuelol) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -18,10 +18,37 @@ */ -const TimeLib = require('../util/timer.ts'); +import * as guildInstance from '../util/guild-instance'; +import { secondsToFullScale } from "../util/timer"; +const { RustPlus } = require('./RustPlus'); -class Time { - constructor(time, rustplus, client) { +export interface TimeConfig { + dayLengthMinutes: number; + timeScale: number; + sunrise: number; + sunset: number; + time: number; +} + +export interface TimeTillConfig { + [key: number]: number; +} + +export class Time { + private _dayLengthMinutes: number; + private _timeScale: number; + private _sunrise: number; + private _sunset: number; + private _time: number; + + private _rustplus: typeof RustPlus; + + private _startTime: number; + private _timeTillDay: TimeTillConfig; + private _timeTillNight: TimeTillConfig; + private _timeTillActive: boolean; + + constructor(rustplus: typeof RustPlus, time: TimeConfig) { this._dayLengthMinutes = time.dayLengthMinutes; this._timeScale = time.timeScale; this._sunrise = time.sunrise; @@ -29,55 +56,77 @@ class Time { this._time = time.time; this._rustplus = rustplus; - this._client = client; this._startTime = time.time; - this._timeTillDay = new Object(); - this._timeTillNight = new Object(); + this._timeTillDay = {}; + this._timeTillNight = {}; this._timeTillActive = false; this.loadTimeTillConfig(); } /* Getters and Setters */ - get dayLengthMinutes() { return this._dayLengthMinutes; } + get dayLengthMinutes(): number { return this._dayLengthMinutes; } set dayLengthMinutes(dayLengthMinutes) { this._dayLengthMinutes = dayLengthMinutes; } - get timeScale() { return this._timeScale; } + get timeScale(): number { return this._timeScale; } set timeScale(timeScale) { this._timeScale = timeScale; } - get sunrise() { return this._sunrise; } + get sunrise(): number { return this._sunrise; } set sunrise(sunrise) { this._sunrise = sunrise; } - get sunset() { return this._sunset; } + get sunset(): number { return this._sunset; } set sunset(sunset) { this._sunset = sunset; } - get time() { return this._time; } + get time(): number { return this._time; } set time(time) { this._time = time; } - get rustplus() { return this._rustplus; } + get rustplus(): typeof RustPlus { return this._rustplus; } set rustplus(rustplus) { this._rustplus = rustplus; } - get client() { return this._client; } - set client(client) { this._client = client; } - get startTime() { return this._startTime; } + get startTime(): number { return this._startTime; } set startTime(startTime) { this._startTime = startTime; } - get timeTillDay() { return this._timeTillDay; } + get timeTillDay(): TimeTillConfig { return this._timeTillDay; } set timeTillDay(timeTillDay) { this._timeTillDay = timeTillDay; } - get timeTillNight() { return this._timeTillNight; } + get timeTillNight(): TimeTillConfig { return this._timeTillNight; } set timeTillNight(timeTillNight) { this._timeTillNight = timeTillNight; } - get timeTillActive() { return this._timeTillActive; } + get timeTillActive(): boolean { return this._timeTillActive; } set timeTillActive(timeTillActive) { this._timeTillActive = timeTillActive; } /* Change checkers */ - isDayLengthMinutesChanged(time) { return ((this.dayLengthMinutes) !== (time.dayLengthMinutes)); } - isTimeScaleChanged(time) { return ((this.timeScale) !== (time.timeScale)); } - isSunriseChanged(time) { return ((this.sunrise) !== (time.sunrise)); } - isSunsetChanged(time) { return ((this.sunset) !== (time.sunset)); } - isTimeChanged(time) { return ((this.time) !== (time.time)); } + isDayLengthMinutesChanged(time: TimeConfig): boolean { + return ((this.dayLengthMinutes) !== (time.dayLengthMinutes)); + } + + isTimeScaleChanged(time: TimeConfig): boolean { + return ((this.timeScale) !== (time.timeScale)); + } + + isSunriseChanged(time: TimeConfig): boolean { + return ((this.sunrise) !== (time.sunrise)); + } + + isSunsetChanged(time: TimeConfig): boolean { + return ((this.sunset) !== (time.sunset)); + } + + isTimeChanged(time: TimeConfig): boolean { + return ((this.time) !== (time.time)); + } /* Other checkers */ - isDay() { return ((this.time >= this.sunrise) && (this.time < this.sunset)); } - isNight() { return !this.isDay(); } - isTurnedDay(time) { return (this.isNight() && time.time >= time.sunrise && time.time < time.sunset); } - isTurnedNight(time) { return (this.isDay() && !(time.time >= time.sunrise && time.time < time.sunset)); } + isDay(): boolean { + return ((this.time >= this.sunrise) && (this.time < this.sunset)); + } + + isNight(): boolean { + return !this.isDay(); + } + + isTurnedDay(time: TimeConfig): boolean { + return (this.isNight() && time.time >= time.sunrise && time.time < time.sunset); + } + + isTurnedNight(time: TimeConfig): boolean { + return (this.isDay() && !(time.time >= time.sunrise && time.time < time.sunset)); + } loadTimeTillConfig() { - let instance = this.client.getInstance(this.rustplus.guildId); + const instance = guildInstance.readGuildInstanceFile(this.rustplus.guildId); if (instance.serverList[this.rustplus.serverId].timeTillDay !== null) { this.timeTillDay = instance.serverList[this.rustplus.serverId].timeTillDay; @@ -91,7 +140,7 @@ class Time { Object.keys(this.timeTillNight).length !== 0; } - updateTime(time) { + updateTime(time: TimeConfig) { this.dayLengthMinutes = time.dayLengthMinutes; this.timeScale = time.timeScale; this.sunrise = time.sunrise; @@ -99,7 +148,7 @@ class Time { this.time = time.time; } - getTimeTillDayOrNight(ignore = '') { + getTimeTillDayOrNight(ignore: string = ''): string | null { if (!this.timeTillActive) { return null; } @@ -117,9 +166,7 @@ class Time { return (Math.abs(b - time) < Math.abs(a - time) ? b : a); }); - return TimeLib.secondsToFullScale(object[closest], ignore); + return secondsToFullScale(object[closest], ignore); } -} - -module.exports = Time; \ No newline at end of file +} \ No newline at end of file