Skip to content

Commit

Permalink
Convert Time class
Browse files Browse the repository at this point in the history
  • Loading branch information
alexemanuelol committed Aug 6, 2024
1 parent 58c3c07 commit fc8a92c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 42 deletions.
5 changes: 3 additions & 2 deletions src/handlers/polling-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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);
}
Expand Down
10 changes: 5 additions & 5 deletions src/structures/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand Down
117 changes: 82 additions & 35 deletions src/structures/Time.js → src/structures/Time.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,66 +18,115 @@
*/

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;
this._sunset = time.sunset;
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;
Expand All @@ -91,15 +140,15 @@ 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;
this.sunset = time.sunset;
this.time = time.time;
}

getTimeTillDayOrNight(ignore = '') {
getTimeTillDayOrNight(ignore: string = ''): string | null {
if (!this.timeTillActive) {
return null;
}
Expand All @@ -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;
}

0 comments on commit fc8a92c

Please sign in to comment.