Skip to content

Commit fc8a92c

Browse files
committed
Convert Time class
1 parent 58c3c07 commit fc8a92c

File tree

3 files changed

+90
-42
lines changed

3 files changed

+90
-42
lines changed

src/handlers/polling-handler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ import { vendingMachineHandler } from './vending-machine-handler';
2626
import { storageMonitorHandler } from './storage-monitor-handler';
2727
import { smartAlarmHandler } from './smart-alarm-handler';
2828
import { smartSwitchHandler } from './smart-switch-handler';
29+
import { Time } from '../structures/Time';
2930
const { RustPlus } = require('../structures/RustPlus');
3031
const Info = require('../structures/Info');
3132
const MapMarkers = require('../structures/MapMarkers.js');
3233
const Team = require('../structures/Team');
33-
const Time = require('../structures/Time');
34+
//const Time = require('../structures/Time');
3435

3536
export async function pollingHandler(rustplus: typeof RustPlus) {
3637
/* Poll information such as info, mapMarkers, teamInfo and time */
@@ -45,7 +46,7 @@ export async function pollingHandler(rustplus: typeof RustPlus) {
4546

4647
if (rustplus.isFirstPoll) {
4748
rustplus.sInfo = new Info(info.info);
48-
rustplus.time = new Time(time.time, rustplus, client);
49+
rustplus.time = new Time(rustplus, time.time);
4950
rustplus.team = new Team(teamInfo.teamInfo, rustplus);
5051
rustplus.mapMarkers = new MapMarkers(mapMarkers.mapMarkers, rustplus, client);
5152
}

src/structures/Player.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
const Constants = require('../util/constants.ts');
2222
const Map = require('../util/map.ts');
23-
const Time = require('../util/timer.ts');
23+
const Timer = require('../util/timer.ts');
2424

2525
class Player {
2626
constructor(player, rustplus) {
@@ -148,23 +148,23 @@ class Player {
148148
}
149149

150150
getAfkSeconds() { return (new Date() - this.lastMovement) / 1000; }
151-
getAfkTime(ignore = '') { return Time.secondsToFullScale(this.getAfkSeconds(), ignore); }
151+
getAfkTime(ignore = '') { return Timer.secondsToFullScale(this.getAfkSeconds(), ignore); }
152152

153153
getAliveSeconds() {
154154
if (this.spawnTime === 0) return 0;
155155
return (new Date() - new Date(this.spawnTime * 1000)) / 1000;
156156
}
157-
getAliveTime(ignore = '') { return Time.secondsToFullScale(this.getAliveSeconds(), ignore); }
157+
getAliveTime(ignore = '') { return Timer.secondsToFullScale(this.getAliveSeconds(), ignore); }
158158

159159
getDeathSeconds() {
160160
if (this.deathTime === 0) return 0;
161161
return (new Date() - new Date(this.deathTime * 1000)) / 1000;
162162
}
163-
getDeathTime(ignore = '') { return (Time.secondsToFullScale(this.getDeathSeconds(), ignore)); }
163+
getDeathTime(ignore = '') { return (Timer.secondsToFullScale(this.getDeathSeconds(), ignore)); }
164164
getOfflineTime(ignore = '') {
165165
if (this.wentOfflineTime === null) return null;
166166
const seconds = (new Date() - this.wentOfflineTime) / 1000;
167-
return (Time.secondsToFullScale(seconds, ignore));
167+
return (Timer.secondsToFullScale(seconds, ignore));
168168
}
169169

170170
async assignLeader() {
Lines changed: 82 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2022 Alexander Emanuelsson (alexemanuelol)
2+
Copyright (C) 2024 Alexander Emanuelsson (alexemanuelol)
33
44
This program is free software: you can redistribute it and/or modify
55
it under the terms of the GNU General Public License as published by
@@ -18,66 +18,115 @@
1818
1919
*/
2020

21-
const TimeLib = require('../util/timer.ts');
21+
import * as guildInstance from '../util/guild-instance';
22+
import { secondsToFullScale } from "../util/timer";
23+
const { RustPlus } = require('./RustPlus');
2224

23-
class Time {
24-
constructor(time, rustplus, client) {
25+
export interface TimeConfig {
26+
dayLengthMinutes: number;
27+
timeScale: number;
28+
sunrise: number;
29+
sunset: number;
30+
time: number;
31+
}
32+
33+
export interface TimeTillConfig {
34+
[key: number]: number;
35+
}
36+
37+
export class Time {
38+
private _dayLengthMinutes: number;
39+
private _timeScale: number;
40+
private _sunrise: number;
41+
private _sunset: number;
42+
private _time: number;
43+
44+
private _rustplus: typeof RustPlus;
45+
46+
private _startTime: number;
47+
private _timeTillDay: TimeTillConfig;
48+
private _timeTillNight: TimeTillConfig;
49+
private _timeTillActive: boolean;
50+
51+
constructor(rustplus: typeof RustPlus, time: TimeConfig) {
2552
this._dayLengthMinutes = time.dayLengthMinutes;
2653
this._timeScale = time.timeScale;
2754
this._sunrise = time.sunrise;
2855
this._sunset = time.sunset;
2956
this._time = time.time;
3057

3158
this._rustplus = rustplus;
32-
this._client = client;
3359

3460
this._startTime = time.time;
35-
this._timeTillDay = new Object();
36-
this._timeTillNight = new Object();
61+
this._timeTillDay = {};
62+
this._timeTillNight = {};
3763
this._timeTillActive = false;
3864

3965
this.loadTimeTillConfig();
4066
}
4167

4268
/* Getters and Setters */
43-
get dayLengthMinutes() { return this._dayLengthMinutes; }
69+
get dayLengthMinutes(): number { return this._dayLengthMinutes; }
4470
set dayLengthMinutes(dayLengthMinutes) { this._dayLengthMinutes = dayLengthMinutes; }
45-
get timeScale() { return this._timeScale; }
71+
get timeScale(): number { return this._timeScale; }
4672
set timeScale(timeScale) { this._timeScale = timeScale; }
47-
get sunrise() { return this._sunrise; }
73+
get sunrise(): number { return this._sunrise; }
4874
set sunrise(sunrise) { this._sunrise = sunrise; }
49-
get sunset() { return this._sunset; }
75+
get sunset(): number { return this._sunset; }
5076
set sunset(sunset) { this._sunset = sunset; }
51-
get time() { return this._time; }
77+
get time(): number { return this._time; }
5278
set time(time) { this._time = time; }
53-
get rustplus() { return this._rustplus; }
79+
get rustplus(): typeof RustPlus { return this._rustplus; }
5480
set rustplus(rustplus) { this._rustplus = rustplus; }
55-
get client() { return this._client; }
56-
set client(client) { this._client = client; }
57-
get startTime() { return this._startTime; }
81+
get startTime(): number { return this._startTime; }
5882
set startTime(startTime) { this._startTime = startTime; }
59-
get timeTillDay() { return this._timeTillDay; }
83+
get timeTillDay(): TimeTillConfig { return this._timeTillDay; }
6084
set timeTillDay(timeTillDay) { this._timeTillDay = timeTillDay; }
61-
get timeTillNight() { return this._timeTillNight; }
85+
get timeTillNight(): TimeTillConfig { return this._timeTillNight; }
6286
set timeTillNight(timeTillNight) { this._timeTillNight = timeTillNight; }
63-
get timeTillActive() { return this._timeTillActive; }
87+
get timeTillActive(): boolean { return this._timeTillActive; }
6488
set timeTillActive(timeTillActive) { this._timeTillActive = timeTillActive; }
6589

6690
/* Change checkers */
67-
isDayLengthMinutesChanged(time) { return ((this.dayLengthMinutes) !== (time.dayLengthMinutes)); }
68-
isTimeScaleChanged(time) { return ((this.timeScale) !== (time.timeScale)); }
69-
isSunriseChanged(time) { return ((this.sunrise) !== (time.sunrise)); }
70-
isSunsetChanged(time) { return ((this.sunset) !== (time.sunset)); }
71-
isTimeChanged(time) { return ((this.time) !== (time.time)); }
91+
isDayLengthMinutesChanged(time: TimeConfig): boolean {
92+
return ((this.dayLengthMinutes) !== (time.dayLengthMinutes));
93+
}
94+
95+
isTimeScaleChanged(time: TimeConfig): boolean {
96+
return ((this.timeScale) !== (time.timeScale));
97+
}
98+
99+
isSunriseChanged(time: TimeConfig): boolean {
100+
return ((this.sunrise) !== (time.sunrise));
101+
}
102+
103+
isSunsetChanged(time: TimeConfig): boolean {
104+
return ((this.sunset) !== (time.sunset));
105+
}
106+
107+
isTimeChanged(time: TimeConfig): boolean {
108+
return ((this.time) !== (time.time));
109+
}
72110

73111
/* Other checkers */
74-
isDay() { return ((this.time >= this.sunrise) && (this.time < this.sunset)); }
75-
isNight() { return !this.isDay(); }
76-
isTurnedDay(time) { return (this.isNight() && time.time >= time.sunrise && time.time < time.sunset); }
77-
isTurnedNight(time) { return (this.isDay() && !(time.time >= time.sunrise && time.time < time.sunset)); }
112+
isDay(): boolean {
113+
return ((this.time >= this.sunrise) && (this.time < this.sunset));
114+
}
115+
116+
isNight(): boolean {
117+
return !this.isDay();
118+
}
119+
120+
isTurnedDay(time: TimeConfig): boolean {
121+
return (this.isNight() && time.time >= time.sunrise && time.time < time.sunset);
122+
}
123+
124+
isTurnedNight(time: TimeConfig): boolean {
125+
return (this.isDay() && !(time.time >= time.sunrise && time.time < time.sunset));
126+
}
78127

79128
loadTimeTillConfig() {
80-
let instance = this.client.getInstance(this.rustplus.guildId);
129+
const instance = guildInstance.readGuildInstanceFile(this.rustplus.guildId);
81130

82131
if (instance.serverList[this.rustplus.serverId].timeTillDay !== null) {
83132
this.timeTillDay = instance.serverList[this.rustplus.serverId].timeTillDay;
@@ -91,15 +140,15 @@ class Time {
91140
Object.keys(this.timeTillNight).length !== 0;
92141
}
93142

94-
updateTime(time) {
143+
updateTime(time: TimeConfig) {
95144
this.dayLengthMinutes = time.dayLengthMinutes;
96145
this.timeScale = time.timeScale;
97146
this.sunrise = time.sunrise;
98147
this.sunset = time.sunset;
99148
this.time = time.time;
100149
}
101150

102-
getTimeTillDayOrNight(ignore = '') {
151+
getTimeTillDayOrNight(ignore: string = ''): string | null {
103152
if (!this.timeTillActive) {
104153
return null;
105154
}
@@ -117,9 +166,7 @@ class Time {
117166
return (Math.abs(b - time) < Math.abs(a - time) ? b : a);
118167
});
119168

120-
return TimeLib.secondsToFullScale(object[closest], ignore);
169+
return secondsToFullScale(object[closest], ignore);
121170
}
122171

123-
}
124-
125-
module.exports = Time;
172+
}

0 commit comments

Comments
 (0)