feat: 0.2.0, all new full board format

This commit is contained in:
Shibo Lyu 2024-12-28 20:41:19 +08:00
parent 4dd6c01cc1
commit 92768c4ff5
5 changed files with 153 additions and 274 deletions

View file

@ -1,8 +1,8 @@
import { getCharacterWidth } from "../logic/character.ts";
import type { FullBoard } from "../types/board.ts";
function isCorrectWidth(cWdRaw: string, cCh: string): boolean {
return getCharacterWidth(cCh).toString() === cWdRaw;
function isCorrectWidth(cWd: number, cCh: string): boolean {
return getCharacterWidth(cCh) === cWd;
}
function isValidColor(color: string): boolean {
@ -24,8 +24,7 @@ export function checkFullBoard(board: FullBoard) {
const cCh = ch[i];
const cCo = board.color[i];
const cBg = board.bg_color[i];
const cWdRaw = board.width[i];
const cWd = parseInt(cWdRaw);
const cWd = board.width[i];
const printSituation = () => {
console.error(
@ -40,7 +39,7 @@ export function checkFullBoard(board: FullBoard) {
"cBg:",
JSON.stringify(cBg),
"cWd:",
JSON.stringify(cWdRaw),
JSON.stringify(cWd),
);
console.error("ch: ", chLine);
console.error("color: ", colorLine);
@ -48,41 +47,12 @@ export function checkFullBoard(board: FullBoard) {
console.error("width: ", widthLine);
};
if (cCh === "\n") {
if (cCo !== "\n" || cBg !== "\n" || cWdRaw !== "\n") {
printSituation();
throw new Error("cCh is newline while at least one other field aren't");
}
if (colorLine.length !== board.w) {
printSituation();
throw new Error("color line length error");
}
if (bgColorLine.length !== board.w) {
printSituation();
throw new Error("bg color line length error");
}
if (widthLine.length !== board.w) {
printSituation();
throw new Error("width line length error");
}
chLine = "";
colorLine = "";
bgColorLine = "";
widthLine = "";
lines++;
unsafeCurrentOffset += cCh.length;
continue;
}
if (!isValidColor(cCo) || !isValidColor(cBg)) {
printSituation();
throw new Error("cCo or cBg is not valid");
}
if (!isCorrectWidth(cWdRaw, cCh)) {
if (!isCorrectWidth(cWd, cCh)) {
printSituation();
throw new Error("cWd is wrong");
}
@ -90,9 +60,17 @@ export function checkFullBoard(board: FullBoard) {
chLine += cCh;
colorLine += cCo.padEnd(cWd);
bgColorLine += cBg.padEnd(cWd);
widthLine += cWdRaw.padEnd(cWd);
widthLine += String(cWd).padEnd(cWd);
unsafeCurrentOffset += cCh.length;
if (colorLine.length === board.w) {
lines++;
chLine = "";
colorLine = "";
bgColorLine = "";
widthLine = "";
}
}
if (lines + 1 !== board.h) throw new Error("board height error");
if (lines !== board.h) throw new Error("board height error");
}