1
1
/*
2
- Copyright (C) 2022 Alexander Emanuelsson (alexemanuelol)
2
+ Copyright (C) 2024 Alexander Emanuelsson (alexemanuelol)
3
3
4
4
This program is free software: you can redistribute it and/or modify
5
5
it under the terms of the GNU General Public License as published by
18
18
19
19
*/
20
20
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' ) ;
22
24
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 ) {
25
52
this . _dayLengthMinutes = time . dayLengthMinutes ;
26
53
this . _timeScale = time . timeScale ;
27
54
this . _sunrise = time . sunrise ;
28
55
this . _sunset = time . sunset ;
29
56
this . _time = time . time ;
30
57
31
58
this . _rustplus = rustplus ;
32
- this . _client = client ;
33
59
34
60
this . _startTime = time . time ;
35
- this . _timeTillDay = new Object ( ) ;
36
- this . _timeTillNight = new Object ( ) ;
61
+ this . _timeTillDay = { } ;
62
+ this . _timeTillNight = { } ;
37
63
this . _timeTillActive = false ;
38
64
39
65
this . loadTimeTillConfig ( ) ;
40
66
}
41
67
42
68
/* Getters and Setters */
43
- get dayLengthMinutes ( ) { return this . _dayLengthMinutes ; }
69
+ get dayLengthMinutes ( ) : number { return this . _dayLengthMinutes ; }
44
70
set dayLengthMinutes ( dayLengthMinutes ) { this . _dayLengthMinutes = dayLengthMinutes ; }
45
- get timeScale ( ) { return this . _timeScale ; }
71
+ get timeScale ( ) : number { return this . _timeScale ; }
46
72
set timeScale ( timeScale ) { this . _timeScale = timeScale ; }
47
- get sunrise ( ) { return this . _sunrise ; }
73
+ get sunrise ( ) : number { return this . _sunrise ; }
48
74
set sunrise ( sunrise ) { this . _sunrise = sunrise ; }
49
- get sunset ( ) { return this . _sunset ; }
75
+ get sunset ( ) : number { return this . _sunset ; }
50
76
set sunset ( sunset ) { this . _sunset = sunset ; }
51
- get time ( ) { return this . _time ; }
77
+ get time ( ) : number { return this . _time ; }
52
78
set time ( time ) { this . _time = time ; }
53
- get rustplus ( ) { return this . _rustplus ; }
79
+ get rustplus ( ) : typeof RustPlus { return this . _rustplus ; }
54
80
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 ; }
58
82
set startTime ( startTime ) { this . _startTime = startTime ; }
59
- get timeTillDay ( ) { return this . _timeTillDay ; }
83
+ get timeTillDay ( ) : TimeTillConfig { return this . _timeTillDay ; }
60
84
set timeTillDay ( timeTillDay ) { this . _timeTillDay = timeTillDay ; }
61
- get timeTillNight ( ) { return this . _timeTillNight ; }
85
+ get timeTillNight ( ) : TimeTillConfig { return this . _timeTillNight ; }
62
86
set timeTillNight ( timeTillNight ) { this . _timeTillNight = timeTillNight ; }
63
- get timeTillActive ( ) { return this . _timeTillActive ; }
87
+ get timeTillActive ( ) : boolean { return this . _timeTillActive ; }
64
88
set timeTillActive ( timeTillActive ) { this . _timeTillActive = timeTillActive ; }
65
89
66
90
/* 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
+ }
72
110
73
111
/* 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
+ }
78
127
79
128
loadTimeTillConfig ( ) {
80
- let instance = this . client . getInstance ( this . rustplus . guildId ) ;
129
+ const instance = guildInstance . readGuildInstanceFile ( this . rustplus . guildId ) ;
81
130
82
131
if ( instance . serverList [ this . rustplus . serverId ] . timeTillDay !== null ) {
83
132
this . timeTillDay = instance . serverList [ this . rustplus . serverId ] . timeTillDay ;
@@ -91,15 +140,15 @@ class Time {
91
140
Object . keys ( this . timeTillNight ) . length !== 0 ;
92
141
}
93
142
94
- updateTime ( time ) {
143
+ updateTime ( time : TimeConfig ) {
95
144
this . dayLengthMinutes = time . dayLengthMinutes ;
96
145
this . timeScale = time . timeScale ;
97
146
this . sunrise = time . sunrise ;
98
147
this . sunset = time . sunset ;
99
148
this . time = time . time ;
100
149
}
101
150
102
- getTimeTillDayOrNight ( ignore = '' ) {
151
+ getTimeTillDayOrNight ( ignore : string = '' ) : string | null {
103
152
if ( ! this . timeTillActive ) {
104
153
return null ;
105
154
}
@@ -117,9 +166,7 @@ class Time {
117
166
return ( Math . abs ( b - time ) < Math . abs ( a - time ) ? b : a ) ;
118
167
} ) ;
119
168
120
- return TimeLib . secondsToFullScale ( object [ closest ] , ignore ) ;
169
+ return secondsToFullScale ( object [ closest ] , ignore ) ;
121
170
}
122
171
123
- }
124
-
125
- module . exports = Time ;
172
+ }
0 commit comments