63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
|
|
import { randomBytes, randomUUID } from 'crypto';
|
|
import * as readline from 'readline';
|
|
import { Tile } from '../game/entities/Tile';
|
|
import chalk from 'chalk';
|
|
import { Board } from '../game/entities/Board';
|
|
|
|
const rl = readline.createInterface({
|
|
input: process.stdin,
|
|
output: process.stdout,
|
|
});
|
|
|
|
export async function wait(ms: number) {
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
}
|
|
|
|
export function askQuestion(question: string): Promise<string> {
|
|
return new Promise((resolve) => {
|
|
// console.log(chalk.yellow(question));
|
|
rl.question(`${chalk.yellow(question + ' > ')}`, (answer) => {
|
|
resolve(answer);
|
|
});
|
|
});
|
|
}
|
|
|
|
export function getRandomSeed(): string {
|
|
const timestamp = Date.now();
|
|
const randomPart = Math.random().toString(36).substring(2);
|
|
const securePart = randomBytes(4).toString('hex');
|
|
return `${timestamp}-${randomPart}-${securePart}`;
|
|
}
|
|
|
|
export function printTiles(prefix:string, tiles: Tile[]): void {
|
|
console.log(`${prefix}${tiles.join(' ')}`);
|
|
}
|
|
|
|
export function printSelection(prefix:string, tiles: Tile[]): void {
|
|
const line: string = tiles.map((t,i) => {
|
|
const index = i + 1;
|
|
return `(${index > 9 ? `${index})`: `${index}) `} `
|
|
}).join(' ');
|
|
printTiles(prefix, tiles);
|
|
console.log(`${Array(prefix.length).join((' '))} ${line}`);
|
|
}
|
|
|
|
export function printBoard(board: Board, highlighted: boolean = false): void {
|
|
if (highlighted)
|
|
console.log(chalk.cyan(`Board: ${board.tiles.length > 0 ? board.tiles.join(' ') : '--empty--'}`));
|
|
else
|
|
console.log(chalk.gray(`Board: ${board.tiles.length > 0 ? board.tiles.join(' ') : '--empty--'}`));
|
|
}
|
|
|
|
export function printLine(msg: string): void {
|
|
console.log(chalk.grey(msg));
|
|
}
|
|
|
|
export function printError(msg: string): void {
|
|
console.log(chalk.red(msg));
|
|
}
|
|
|
|
export function uuid() {
|
|
return randomUUID();
|
|
} |