2021-06-15 19:15:49 -04:00
|
|
|
import chalk from "chalk";
|
2021-06-14 22:03:55 -04:00
|
|
|
import { Serializable } from "frigid";
|
2021-06-15 19:15:49 -04:00
|
|
|
import { isThisTypeNode } from "typescript";
|
2021-06-14 22:03:55 -04:00
|
|
|
import log from "./log.js";
|
|
|
|
|
|
|
|
|
|
const daysInMonth = [
|
|
|
|
|
31, 28, 31,
|
|
|
|
|
30, 31, 30,
|
|
|
|
|
31, 31, 30,
|
|
|
|
|
31, 30, 31
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const months = [
|
|
|
|
|
'Jan', 'Feb', 'Mar',
|
|
|
|
|
'Apr', 'May', 'Jun',
|
|
|
|
|
'Jul', 'Aug', 'Sep',
|
|
|
|
|
'Oct', 'Nov', 'Dec'
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
export default class Time extends Serializable{
|
|
|
|
|
rate: number;
|
2021-06-15 19:15:49 -04:00
|
|
|
paused = true;
|
2021-06-14 22:03:55 -04:00
|
|
|
|
|
|
|
|
thing: Tickable;
|
|
|
|
|
|
|
|
|
|
year: number;
|
|
|
|
|
month: number;
|
|
|
|
|
day: number;
|
|
|
|
|
hour: number;
|
|
|
|
|
minute: number;
|
|
|
|
|
|
|
|
|
|
toString() {
|
2021-06-15 19:15:49 -04:00
|
|
|
const sym = (this.hour >= 6 && this.hour < 20) ?
|
|
|
|
|
chalk.yellowBright('☼') :
|
|
|
|
|
chalk.blue('☾')
|
|
|
|
|
|
|
|
|
|
return `${sym} ${this.hour.toString().padStart(2, ' ')}:${this.minute.toString().padStart(2, '0')} ${months[this.month]} ${this.day + 1}, ${(this.year + 1).toString().padStart(4, '0')}`
|
|
|
|
|
|
2021-06-14 22:03:55 -04:00
|
|
|
// return '☾' || '☼';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctor() {
|
2021-06-15 21:53:00 -04:00
|
|
|
this.rate = 60;
|
2021-06-14 22:03:55 -04:00
|
|
|
this.minute ??= 0;
|
|
|
|
|
this.hour ??= 0;
|
|
|
|
|
this.day ??= 0;
|
|
|
|
|
this.month ??= 0;
|
|
|
|
|
this.year ??= 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-15 19:15:49 -04:00
|
|
|
pause() {
|
|
|
|
|
this.paused = true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-14 22:03:55 -04:00
|
|
|
start() {
|
2021-06-15 19:15:49 -04:00
|
|
|
this.paused = false;
|
2021-06-14 22:03:55 -04:00
|
|
|
setTimeout(this.doTick.bind(this), 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
advanceTime(minutes) {
|
|
|
|
|
this.minute ++;
|
|
|
|
|
while(this.minute >= 60) {
|
|
|
|
|
this.minute -= 60;
|
|
|
|
|
this.hour ++;
|
|
|
|
|
}
|
|
|
|
|
while(this.hour >= 24) {
|
|
|
|
|
this.hour -= 24;
|
|
|
|
|
this.day ++;
|
|
|
|
|
}
|
|
|
|
|
while(this.day >= daysInMonth[this.month]) {
|
|
|
|
|
this.day -= daysInMonth[this.month];
|
|
|
|
|
this.month ++;
|
|
|
|
|
}
|
|
|
|
|
while(this.month >= 12) {
|
|
|
|
|
this.month -= 12;
|
|
|
|
|
this.year ++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async doTick() {
|
|
|
|
|
this.advanceTime(1);
|
|
|
|
|
const timeout = 1000 / this.rate;
|
|
|
|
|
const start = new Date().getTime();
|
|
|
|
|
if(this.thing) {
|
|
|
|
|
await this.thing.tick();
|
|
|
|
|
}
|
|
|
|
|
const elapsed = new Date().getTime() - start;
|
|
|
|
|
const wait = Math.max(timeout - elapsed, 0);
|
2021-06-15 19:15:49 -04:00
|
|
|
if(this.paused) return;
|
2021-06-14 22:03:55 -04:00
|
|
|
setTimeout(this.doTick.bind(this), wait)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export interface Tickable {
|
|
|
|
|
tick: () => Promise<void>
|
|
|
|
|
}
|