add GodOfTimeService.tick() to easily increment time
This commit is contained in:
parent
a21a53b9f4
commit
2dfc878445
2 changed files with 25 additions and 2 deletions
|
|
@ -17,12 +17,12 @@ export abstract class TimeService<TTimer extends Timer = Timer> implements OnApp
|
|||
protected constructor() {}
|
||||
|
||||
/**
|
||||
* Returns Date.now()
|
||||
* Returns the current time, in milliseconds since the Unix epoch.
|
||||
*/
|
||||
public abstract get now(): number;
|
||||
|
||||
/**
|
||||
* Returns a new Date instance.
|
||||
* Returns a new Date instance representing the current time.
|
||||
*/
|
||||
public get date(): Date {
|
||||
return new Date(this.now);
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { TimeService, Timer } from '@/core/TimeService.js';
|
||||
import { addPatch, type DatePatch } from '@/misc/patch-date.js';
|
||||
|
||||
/**
|
||||
* Fake implementation of TimeService that allows manual control of time.
|
||||
|
|
@ -52,6 +53,28 @@ export class GodOfTimeService extends TimeService<GodsOwnTimer> {
|
|||
this._now = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or set the current time, as a JavaScript Date object.
|
||||
*/
|
||||
get date(): Date {
|
||||
return super.date;
|
||||
}
|
||||
set date(value: Date) {
|
||||
this.now = value.getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves time by a relative "tick" amount.
|
||||
* Ticks can be a raw number of milliseconds, or an inline object containing time and/or date increments.
|
||||
*/
|
||||
public tick(tick: number | DatePatch) {
|
||||
if (typeof(tick) === 'number') {
|
||||
this.now += tick;
|
||||
} else {
|
||||
this.date = addPatch(this.date, tick);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all timers and resets to time=0.
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue