we need 'this'
This commit is contained in:
4
constants/time.ts
Normal file
4
constants/time.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
export const SECOND = 1000;
|
||||||
|
export const MINUTE = 60 * SECOND;
|
||||||
|
export const HOUR = 60 * MINUTE;
|
||||||
|
export const DAY = 24 * HOUR;
|
||||||
@@ -1,15 +1,11 @@
|
|||||||
import Deck from '@/lib/TarokkaDeck';
|
import Deck from '@/lib/TarokkaDeck';
|
||||||
import generateID from '@/tools/simpleID';
|
import generateID from '@/tools/simpleID';
|
||||||
import parseMilliseconds from '@/tools/parseMilliseconds';
|
import parseMilliseconds from '@/tools/parseMilliseconds';
|
||||||
|
import { MINUTE, HOUR, DAY } from '@/constants/time';
|
||||||
import { GameState, GameUpdate, Settings } from '@/types';
|
import { GameState, GameUpdate, Settings } from '@/types';
|
||||||
|
|
||||||
const deck = new Deck();
|
const deck = new Deck();
|
||||||
|
|
||||||
const SECOND = 1000;
|
|
||||||
const MINUTE = 60 * SECOND;
|
|
||||||
const HOUR = 60 * MINUTE;
|
|
||||||
const DAY = 24 * HOUR;
|
|
||||||
|
|
||||||
const tilMidnight = () => {
|
const tilMidnight = () => {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const midnight = new Date(now);
|
const midnight = new Date(now);
|
||||||
@@ -18,6 +14,20 @@ const tilMidnight = () => {
|
|||||||
return midnight.getTime() - now.getTime();
|
return midnight.getTime() - now.getTime();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const uptime = (startTime: number) => {
|
||||||
|
const now = Date.now();
|
||||||
|
const uptime = now - startTime;
|
||||||
|
|
||||||
|
const { days, hours, minutes, seconds } = parseMilliseconds(uptime);
|
||||||
|
|
||||||
|
const dayLog = days ? ` ${days} ${days > 1 ? 'days' : 'day'}` : '';
|
||||||
|
const hourLog = hours ? ` ${hours} ${hours > 1 ? 'hours' : 'hour'}` : '';
|
||||||
|
const minuteLog = minutes ? ` ${minutes} ${minutes > 1 ? 'minutes' : 'minute'}` : '';
|
||||||
|
const secondLog = seconds ? ` ${seconds} ${seconds > 1 ? 'seconds' : 'second'}` : '';
|
||||||
|
|
||||||
|
return `Up${dayLog}${hourLog}${minuteLog}${secondLog}`;
|
||||||
|
};
|
||||||
|
|
||||||
export default class GameStore {
|
export default class GameStore {
|
||||||
private startTime: number;
|
private startTime: number;
|
||||||
private totalCreated: number;
|
private totalCreated: number;
|
||||||
@@ -151,16 +161,7 @@ export default class GameStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log() {
|
log() {
|
||||||
const now = Date.now();
|
const uptimeLog = uptime(this.startTime);
|
||||||
const uptime = now - this.startTime;
|
|
||||||
|
|
||||||
const { days, hours, minutes, seconds } = parseMilliseconds(uptime);
|
|
||||||
|
|
||||||
const dayLog = days ? ` ${days} ${days > 1 ? 'days' : 'day'}` : '';
|
|
||||||
const hourLog = hours ? ` ${hours} ${hours > 1 ? 'hours' : 'hour'}` : '';
|
|
||||||
const minuteLog = minutes ? ` ${minutes} ${minutes > 1 ? 'minutes' : 'minute'}` : '';
|
|
||||||
|
|
||||||
const uptimeLog = `Up${dayLog}${hourLog}${minuteLog} ${seconds} seconds`;
|
|
||||||
|
|
||||||
console.log('-'.repeat(uptimeLog.length));
|
console.log('-'.repeat(uptimeLog.length));
|
||||||
console.log(uptimeLog);
|
console.log(uptimeLog);
|
||||||
@@ -170,16 +171,7 @@ export default class GameStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
wrapUp() {
|
wrapUp() {
|
||||||
const now = Date.now();
|
const uptimeLog = uptime(this.startTime);
|
||||||
const uptime = now - this.startTime;
|
|
||||||
|
|
||||||
const { days, hours, minutes, seconds } = parseMilliseconds(uptime);
|
|
||||||
|
|
||||||
const dayLog = days ? ` ${days} ${days > 1 ? 'days' : 'day'}` : '';
|
|
||||||
const hourLog = hours ? ` ${hours} ${hours > 1 ? 'hours' : 'hour'}` : '';
|
|
||||||
const minuteLog = minutes ? ` ${minutes} ${minutes > 1 ? 'minutes' : 'minute'}` : '';
|
|
||||||
|
|
||||||
const uptimeLog = `Up${dayLog}${hourLog}${minuteLog} ${seconds} seconds`;
|
|
||||||
|
|
||||||
console.log('='.repeat(uptimeLog.length));
|
console.log('='.repeat(uptimeLog.length));
|
||||||
console.log(uptimeLog);
|
console.log(uptimeLog);
|
||||||
@@ -207,8 +199,8 @@ export default class GameStore {
|
|||||||
this.totalExpired += expired.length;
|
this.totalExpired += expired.length;
|
||||||
this.totalUnused += unused.length;
|
this.totalUnused += unused.length;
|
||||||
|
|
||||||
expired.forEach(this.deleteGame);
|
expired.forEach((game) => this.deleteGame(game));
|
||||||
unused.forEach(this.deleteGame);
|
unused.forEach((game) => this.deleteGame(game));
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteGame(game: GameState): void {
|
deleteGame(game: GameState): void {
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { SECOND, MINUTE, HOUR, DAY } from '@/constants/time';
|
||||||
|
|
||||||
export interface ParsedMilliseconds {
|
export interface ParsedMilliseconds {
|
||||||
days: number;
|
days: number;
|
||||||
hours: number;
|
hours: number;
|
||||||
@@ -6,11 +8,6 @@ export interface ParsedMilliseconds {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function parseMilliseconds(timestamp: number): ParsedMilliseconds {
|
export default function parseMilliseconds(timestamp: number): ParsedMilliseconds {
|
||||||
const SECOND = 1000;
|
|
||||||
const MINUTE = 60 * SECOND;
|
|
||||||
const HOUR = 60 * MINUTE;
|
|
||||||
const DAY = 24 * HOUR;
|
|
||||||
|
|
||||||
const days = Math.floor(timestamp / DAY);
|
const days = Math.floor(timestamp / DAY);
|
||||||
timestamp %= DAY;
|
timestamp %= DAY;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user