change logging api to accept variadic properties

This commit is contained in:
JOLIMAITRE Matthieu 2024-05-02 16:14:15 +02:00
parent 336efff5c1
commit aab2e60dab
7 changed files with 61 additions and 23 deletions

View file

@ -2,7 +2,7 @@ import { assertExists } from "https://deno.land/std@0.224.0/assert/assert_exists
import { Guessing } from "./guesser/guesser.ts";
import { Gaming, GuessResult } from "./game/game.ts";
import { last, wait } from "./utils.ts";
import { last, wait, zip } from "./utils.ts";
export class Runner<Ga extends Gaming, Gu extends Guessing> {
game;
@ -22,10 +22,10 @@ export class Runner<Ga extends Gaming, Gu extends Guessing> {
async play_all() {
this.logging.on_start(this.game.length());
while (true) {
const result = await this.guesser.guess(async (guess, known) => {
const result = await this.game.guess(guess, known);
const result = await this.guesser.guess(async (guess, ...properties) => {
const result = await this.game.guess(guess);
this.turns.push({ guess, result });
this.logging.on_guess(known, guess, result);
this.logging.on_guess(guess, result, ...properties);
return result;
});
if (result !== null) break;
@ -49,18 +49,29 @@ function format_result(result: GuessResult) {
}
interface LoggingStrategy {
on_start(length: number): void;
on_guess(known: string, guess: string, result: GuessResult): void;
on_start(length: number, ...properties: string[]): void;
on_guess(guess: string, result: GuessResult, ...properties: unknown[]): void;
on_finish(turns: Turn[]): void;
}
export class VerboseLogging implements LoggingStrategy {
on_start(_length: number) {}
properties;
length;
on_guess(known: string, guess: string, result: GuessResult): void {
console.log(" Knows:", known);
console.log("Guessing:", guess);
console.log(" Got:", format_result(result));
constructor() {
this.length = 0;
this.properties = [] as string[];
}
on_start(_length: number, ...properties: string[]) {
this.length = Math.max(8, ...properties.map((p) => p.length));
this.properties = properties;
}
on_guess(guess: string, result: GuessResult, ...properties: unknown[]): void {
for (const [name, value] of zip(this.properties, properties)) this.log_entry(name, value);
this.log_entry("Guessing", guess);
this.log_entry("Got", format_result(result));
console.log();
}
@ -71,17 +82,35 @@ export class VerboseLogging implements LoggingStrategy {
console.log("With", last_.result.kind, ".");
console.log("In", turns.length, "steps");
}
log_entry(name: string, value: unknown) {
console.log(name.padStart(this.length) + ": " + value);
}
}
export class TableLogging implements LoggingStrategy {
on_start(length: number): void {
const pad = (title: string) => title.padEnd(length);
console.log(`${pad("known")}\t${pad("guess")}\t${pad("result")}`);
columns;
constructor() {
this.columns = [] as (readonly [string, number])[];
}
on_start(length: number, ...properties: string[]): void {
this.columns = properties.map((p) => [p, p.length] as const);
this.columns.splice(0, 0, ["guess", length], ["result", length]);
let line = "";
for (const [name, width] of this.columns) line += name.padStart(width) + " ";
console.log(line);
console.log();
}
on_guess(known: string, guess: string, result: GuessResult): void {
console.log(`${known}\t${guess}\t${format_result(result)}`);
on_guess(guess: string, result: GuessResult, ...properties: unknown[]): void {
const properties_ = [guess, format_result(result), ...properties];
let line = "";
for (const [[_, width], value] of zip(this.columns, properties_)) line += `${value}`.padStart(width) + " ";
console.log(line);
}
on_finish(turns: Turn[]): void {
const last_ = last(turns);
assertExists(last_);