Skip to content

Commit 8ac4feb

Browse files
committed
Refactor rustplus time
1 parent 6323475 commit 8ac4feb

3 files changed

Lines changed: 30 additions & 27 deletions

File tree

src/handlers/rustPlusInfoHandler.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ export async function handler(rpInstance: RustPlusInstance, info: rp.AppInfo) {
7575
}
7676

7777
if (rpInfo.isPlayersChanged(info)) {
78-
log.info(`${fn} players changed, ` +
79-
`old: ${rpInfo?.appInfo.players}, ` +
80-
`new: ${info.players}`,
81-
logParam);
78+
//log.info(`${fn} players changed, ` +
79+
// `old: ${rpInfo?.appInfo.players}, ` +
80+
// `new: ${info.players}`,
81+
// logParam);
8282
}
8383

8484
if (rpInfo.isMaxPlayersChanged(info)) {
@@ -89,10 +89,10 @@ export async function handler(rpInstance: RustPlusInstance, info: rp.AppInfo) {
8989
}
9090

9191
if (rpInfo.isQueuedPlayersChanged(info)) {
92-
log.info(`${fn} queuedPlayers changed, ` +
93-
`old: ${rpInfo?.appInfo.queuedPlayers}, ` +
94-
`new: ${info.queuedPlayers}`,
95-
logParam);
92+
//log.info(`${fn} queuedPlayers changed, ` +
93+
// `old: ${rpInfo?.appInfo.queuedPlayers}, ` +
94+
// `new: ${info.queuedPlayers}`,
95+
// logParam);
9696
}
9797

9898
if (rpInfo.isSeedChanged(info)) {

src/handlers/rustPlusTimeHandler.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,41 +28,41 @@ export async function handler(rpInstance: RustPlusInstance, time: rp.AppTime) {
2828
const fn = '[rustPlusTimeHandler: handler]';
2929
const logParam = {
3030
guildId: rpInstance.guildId,
31-
serverId: rpInstance.serverId,
32-
serverName: rpInstance.serverName
31+
serverId: rpInstance.serverId
3332
};
33+
const rpTime = rpInstance.rpTime as RustPlusTime;
3434

35-
if ((rpInstance.rpTime as RustPlusTime).isDayLengthMinutesChanged(time)) {
35+
if (rpTime.isDayLengthMinutesChanged(time)) {
3636
log.info(`${fn} dayLengthMinutes changed, ` +
37-
`old: ${rpInstance.rpTime?.appTime.dayLengthMinutes}, ` +
37+
`old: ${rpTime?.appTime.dayLengthMinutes}, ` +
3838
`new: ${time.dayLengthMinutes}`,
3939
logParam);
4040
}
4141

42-
if ((rpInstance.rpTime as RustPlusTime).isTimeScaleChanged(time)) {
42+
if (rpTime.isTimeScaleChanged(time)) {
4343
log.info(`${fn} timeScale changed, ` +
44-
`old: ${rpInstance.rpTime?.appTime.timeScale}, ` +
44+
`old: ${rpTime?.appTime.timeScale}, ` +
4545
`new: ${time.timeScale}`,
4646
logParam);
4747
}
4848

49-
if ((rpInstance.rpTime as RustPlusTime).isSunriseChanged(time)) {
49+
if (rpTime.isSunriseChanged(time)) {
5050
log.info(`${fn} sunrise changed, ` +
51-
`old: ${rpInstance.rpTime?.appTime.sunrise}, ` +
51+
`old: ${rpTime?.appTime.sunrise}, ` +
5252
`new: ${time.sunrise}`,
5353
logParam);
5454
}
5555

56-
if ((rpInstance.rpTime as RustPlusTime).isSunsetChanged(time)) {
56+
if (rpTime.isSunsetChanged(time)) {
5757
log.info(`${fn} sunset changed, ` +
58-
`old: ${rpInstance.rpTime?.appTime.sunset}, ` +
58+
`old: ${rpTime?.appTime.sunset}, ` +
5959
`new: ${time.sunset}`,
6060
logParam);
6161
}
6262

63-
if ((rpInstance.rpTime as RustPlusTime).isTimeChanged(time)) {
63+
if (rpTime.isTimeChanged(time)) {
6464
//log.info(`${fn} time changed, ` +
65-
// `old: ${rpInstance.rpTime?.appTime.time}, ` +
65+
// `old: ${rpTime?.appTime.time}, ` +
6666
// `new: ${time.time}`,
6767
// logParam);
6868
}
@@ -71,11 +71,11 @@ export async function handler(rpInstance: RustPlusInstance, time: rp.AppTime) {
7171
* Custom handlers
7272
*/
7373

74-
if ((rpInstance.rpTime as RustPlusTime).isTurnedDay(time)) {
74+
if (rpTime.isTurnedDay(time)) {
7575
log.info(`${fn} Just turned day.`, logParam);
7676
}
7777

78-
if ((rpInstance.rpTime as RustPlusTime).isTurnedNight(time)) {
78+
if (rpTime.isTurnedNight(time)) {
7979
log.info(`${fn} Just turned night.`, logParam);
8080
}
8181
}

src/structures/rustPlusTime.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ export class RustPlusTime {
8787
return this.appTime.time !== appTime.time;
8888
}
8989

90+
/**
91+
* Other methods
92+
*/
93+
9094
public isTurnedDay(appTime: rp.AppTime): boolean {
9195
return this.isNight() && appTime.time >= appTime.sunrise && appTime.time < appTime.sunset;
9296
}
@@ -111,13 +115,13 @@ export class RustPlusTime {
111115
let isDay = this.isDay();
112116

113117
/* Can't calculate time till sunrise or sunset, so use the default time table */
114-
if ((this.isDay() && (this.latestSunrise === null || server.dayDurationSeconds === null)) ||
115-
this.isNight() && (this.latestSunset === null || server.nightDurationSeconds === null)) {
118+
if ((isDay && (this.latestSunrise === null || server.dayDurationSeconds === null)) ||
119+
!isDay && (this.latestSunset === null || server.nightDurationSeconds === null)) {
116120
const closestTimeKey = getClosestTimeKey(this.appTime.time);
117121
seconds = timeTable[closestTimeKey];
118122
isDay = (this.appTime.time >= sunrise) && (this.appTime.time < sunset)
119123
}
120-
else if (this.isDay()) {
124+
else if (isDay) {
121125
const currentTimeSeconds = (new Date()).getTime() / 1000;
122126
const latestSunriseSeconds = (this.latestSunrise as Date).getTime() / 1000;
123127
seconds = (server.dayDurationSeconds as number) - (currentTimeSeconds - latestSunriseSeconds);
@@ -135,8 +139,7 @@ export class RustPlusTime {
135139
const fn = '[RustPlusTime: updateVariables]';
136140
const logParam = {
137141
guildId: this.rpInstance.guildId,
138-
serverId: this.rpInstance.serverId,
139-
serverName: this.rpInstance.serverName
142+
serverId: this.rpInstance.serverId
140143
};
141144

142145
const gInstance = gim.getGuildInstance(this.rpInstance.guildId) as GuildInstance;

0 commit comments

Comments
 (0)