237 lines
9.7 KiB
TypeScript
237 lines
9.7 KiB
TypeScript
|
|
import { describe, expect, it } from "vitest";
|
|||
|
|
import { BILLBOARD_MS, billboardScore, makeBillboard, walkway } from "../src/game/minigames/billboard-logic.ts";
|
|||
|
|
import { HANDSHAKE_MS, handshakeScore, makeHandshake, portraitSpots } from "../src/game/minigames/handshake-logic.ts";
|
|||
|
|
import { GOOD_MS, PERFECT_MS, judgeOffset, makeChart, rallyScore } from "../src/game/minigames/rally-logic.ts";
|
|||
|
|
import {
|
|||
|
|
MAX_STARS,
|
|||
|
|
RECORDS_KEY,
|
|||
|
|
defaultTier,
|
|||
|
|
emptyRecords,
|
|||
|
|
isUnlocked,
|
|||
|
|
loadRecords,
|
|||
|
|
lockReason,
|
|||
|
|
parseTier,
|
|||
|
|
recordResult,
|
|||
|
|
sanitizeRecords,
|
|||
|
|
saveRecords,
|
|||
|
|
totalStars,
|
|||
|
|
} from "../src/game/minigames/records.ts";
|
|||
|
|
import { clampScore, starsFor } from "../src/game/minigames/types.ts";
|
|||
|
|
|
|||
|
|
/** 測試用的 localStorage(vitest 跑在 Node,沒有瀏覽器的儲存空間) */
|
|||
|
|
function fakeStorage(): Storage {
|
|||
|
|
const m = new Map<string, string>();
|
|||
|
|
return {
|
|||
|
|
get length() {
|
|||
|
|
return m.size;
|
|||
|
|
},
|
|||
|
|
clear: () => m.clear(),
|
|||
|
|
getItem: (k) => m.get(k) ?? null,
|
|||
|
|
key: (i) => [...m.keys()][i] ?? null,
|
|||
|
|
removeItem: (k) => void m.delete(k),
|
|||
|
|
setItem: (k, v) => void m.set(k, String(v)),
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
describe("星等換算", () => {
|
|||
|
|
it("40/70/90 分是 1/2/3 星的門檻", () => {
|
|||
|
|
expect(starsFor(0)).toBe(0);
|
|||
|
|
expect(starsFor(39)).toBe(0);
|
|||
|
|
expect(starsFor(40)).toBe(1);
|
|||
|
|
expect(starsFor(69)).toBe(1);
|
|||
|
|
expect(starsFor(70)).toBe(2);
|
|||
|
|
expect(starsFor(89)).toBe(2);
|
|||
|
|
expect(starsFor(90)).toBe(3);
|
|||
|
|
expect(starsFor(100)).toBe(3);
|
|||
|
|
});
|
|||
|
|
it("超出範圍、小數、NaN 先夾到 0–100 的整數", () => {
|
|||
|
|
expect(starsFor(150)).toBe(3);
|
|||
|
|
expect(starsFor(-5)).toBe(0);
|
|||
|
|
expect(starsFor(39.6)).toBe(1);
|
|||
|
|
expect(starsFor(Number.NaN)).toBe(0);
|
|||
|
|
expect(clampScore(Number.POSITIVE_INFINITY)).toBe(0);
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
describe("最佳紀錄清洗", () => {
|
|||
|
|
it("壞掉、不認得的東西一律清成空紀錄", () => {
|
|||
|
|
for (const raw of [null, undefined, 42, "x", [], { handshake: "nope" }]) expect(sanitizeRecords(raw)).toEqual(emptyRecords());
|
|||
|
|
});
|
|||
|
|
it("分數夾到 0–100、次數至少 1、型別不對的那一級丟掉、不認得的遊戲丟掉", () => {
|
|||
|
|
const r = sanitizeRecords({
|
|||
|
|
handshake: [{ best: 250, plays: 3 }, { best: "90" }, { best: 55.4, plays: -2 }],
|
|||
|
|
rally: [{ best: -10, plays: 1e12 }],
|
|||
|
|
hacked: [{ best: 100 }],
|
|||
|
|
});
|
|||
|
|
expect(r.handshake).toEqual([{ best: 100, plays: 3 }, null, { best: 55, plays: 1 }]);
|
|||
|
|
expect(r.rally).toEqual([{ best: 0, plays: 1_000_000 }, null, null]);
|
|||
|
|
expect(r.billboard).toEqual([null, null, null]);
|
|||
|
|
expect("hacked" in r).toBe(false);
|
|||
|
|
});
|
|||
|
|
it("存進去再讀出來一樣;讀到壞 JSON 不會出錯", () => {
|
|||
|
|
const s = fakeStorage();
|
|||
|
|
const { records } = recordResult(emptyRecords(), "rally", 0, 82);
|
|||
|
|
saveRecords(records, s);
|
|||
|
|
expect(loadRecords(s)).toEqual(records);
|
|||
|
|
s.setItem(RECORDS_KEY, "{not json");
|
|||
|
|
expect(loadRecords(s)).toEqual(emptyRecords());
|
|||
|
|
expect(loadRecords(null)).toEqual(emptyRecords());
|
|||
|
|
});
|
|||
|
|
it("儲存空間丟錯(私密模式)時不會讓遊戲壞掉", () => {
|
|||
|
|
const broken = {
|
|||
|
|
getItem: () => {
|
|||
|
|
throw new Error("blocked");
|
|||
|
|
},
|
|||
|
|
setItem: () => {
|
|||
|
|
throw new Error("blocked");
|
|||
|
|
},
|
|||
|
|
};
|
|||
|
|
expect(loadRecords(broken)).toEqual(emptyRecords());
|
|||
|
|
expect(() => saveRecords(emptyRecords(), broken)).not.toThrow();
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
describe("記分與新紀錄", () => {
|
|||
|
|
it("第一次玩、分數大於 0 算新紀錄;比較低不會蓋掉最高分,但次數會加", () => {
|
|||
|
|
let r = emptyRecords();
|
|||
|
|
let res = recordResult(r, "handshake", 0, 61);
|
|||
|
|
expect(res.newBest).toBe(true);
|
|||
|
|
r = res.records;
|
|||
|
|
res = recordResult(r, "handshake", 0, 48);
|
|||
|
|
expect(res.newBest).toBe(false);
|
|||
|
|
expect(res.records.handshake[0]).toEqual({ best: 61, plays: 2 });
|
|||
|
|
res = recordResult(res.records, "handshake", 0, 61);
|
|||
|
|
expect(res.newBest).toBe(false);
|
|||
|
|
res = recordResult(res.records, "handshake", 0, 77);
|
|||
|
|
expect(res.newBest).toBe(true);
|
|||
|
|
expect(res.records.handshake[0]).toEqual({ best: 77, plays: 4 });
|
|||
|
|
expect(recordResult(emptyRecords(), "rally", 1, 0).newBest).toBe(false);
|
|||
|
|
});
|
|||
|
|
it("不改原本的物件", () => {
|
|||
|
|
const r = emptyRecords();
|
|||
|
|
recordResult(r, "billboard", 2, 90);
|
|||
|
|
expect(r).toEqual(emptyRecords());
|
|||
|
|
});
|
|||
|
|
it("總星數:每個遊戲每級最多 3 顆,總共 27", () => {
|
|||
|
|
let r = emptyRecords();
|
|||
|
|
expect(totalStars(r)).toBe(0);
|
|||
|
|
r = recordResult(r, "handshake", 0, 95).records;
|
|||
|
|
r = recordResult(r, "rally", 0, 72).records;
|
|||
|
|
r = recordResult(r, "rally", 1, 41).records;
|
|||
|
|
expect(totalStars(r)).toBe(6);
|
|||
|
|
expect(MAX_STARS).toBe(27);
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
describe("難度解鎖", () => {
|
|||
|
|
it("新手一開始就能玩,老手、選戰老將要前一級拿到 2 星", () => {
|
|||
|
|
let r = emptyRecords();
|
|||
|
|
expect(isUnlocked(r, "rally", 0)).toBe(true);
|
|||
|
|
expect(isUnlocked(r, "rally", 1)).toBe(false);
|
|||
|
|
expect(lockReason(r, "rally", 1)).toBe("新手拿到 2 星解鎖");
|
|||
|
|
r = recordResult(r, "rally", 0, 69).records;
|
|||
|
|
expect(isUnlocked(r, "rally", 1)).toBe(false);
|
|||
|
|
r = recordResult(r, "rally", 0, 70).records;
|
|||
|
|
expect(isUnlocked(r, "rally", 1)).toBe(true);
|
|||
|
|
expect(isUnlocked(r, "rally", 2)).toBe(false);
|
|||
|
|
expect(lockReason(r, "rally", 2)).toBe("老手拿到 2 星解鎖");
|
|||
|
|
// 各遊戲分開算
|
|||
|
|
expect(isUnlocked(r, "handshake", 1)).toBe(false);
|
|||
|
|
});
|
|||
|
|
it("預設難度是解鎖的最高一級;網址指定鎖住的難度會退回預設", () => {
|
|||
|
|
let r = emptyRecords();
|
|||
|
|
expect(defaultTier(r, "billboard")).toBe(0);
|
|||
|
|
r = recordResult(r, "billboard", 0, 88).records;
|
|||
|
|
expect(defaultTier(r, "billboard")).toBe(1);
|
|||
|
|
expect(parseTier("0", r, "billboard")).toBe(0);
|
|||
|
|
expect(parseTier("2", r, "billboard")).toBe(1);
|
|||
|
|
expect(parseTier("abc", r, "billboard")).toBe(1);
|
|||
|
|
expect(parseTier(null, r, "billboard")).toBe(1);
|
|||
|
|
});
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
describe("三個遊戲的關卡與分數", () => {
|
|||
|
|
it("同一個種子、難度產生一樣的關卡", () => {
|
|||
|
|
expect(makeHandshake(7, 0.5)).toEqual(makeHandshake(7, 0.5));
|
|||
|
|
expect(makeChart(7, 0.5)).toEqual(makeChart(7, 0.5));
|
|||
|
|
expect(makeBillboard(7, 0.5)).toEqual(makeBillboard(7, 0.5));
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
it("掃街握手:同一個人不會同時舉兩次手、時間內結束、難度越高舉手越多", () => {
|
|||
|
|
for (const seed of [1, 2, 3, 99, 2026]) {
|
|||
|
|
const easy = makeHandshake(seed, 0.2);
|
|||
|
|
const hard = makeHandshake(seed, 0.9);
|
|||
|
|
for (const lv of [easy, hard]) {
|
|||
|
|
expect(lv.spawns[0]!.kind).toBe("fan");
|
|||
|
|
const busy = new Map<number, number>();
|
|||
|
|
for (const s of lv.spawns) {
|
|||
|
|
expect(s.t).toBeLessThan(HANDSHAKE_MS);
|
|||
|
|
expect(s.t).toBeGreaterThanOrEqual(busy.get(s.person) ?? 0);
|
|||
|
|
busy.set(s.person, s.t + s.dur);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
expect(hard.spawns.length).toBeGreaterThan(easy.spawns.length);
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
it("掃街握手:直式手機四排 5-4-5-4,18 個位置都在畫面裡、不重疊", () => {
|
|||
|
|
const spots = portraitSpots(18);
|
|||
|
|
expect(spots.map((s) => s.row)).toEqual([0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3]);
|
|||
|
|
for (const s of spots) {
|
|||
|
|
expect(s.x).toBeGreaterThan(5);
|
|||
|
|
expect(s.x).toBeLessThan(95);
|
|||
|
|
expect(s.y).toBeLessThanOrEqual(98);
|
|||
|
|
}
|
|||
|
|
for (let i = 1; i < spots.length; i++) if (spots[i]!.row === spots[i - 1]!.row) expect(spots[i]!.x - spots[i - 1]!.x).toBeGreaterThan(15);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
it("掃街握手的分數:全部握到 100、握一半約 50、被拍與揮空扣分", () => {
|
|||
|
|
expect(handshakeScore({ points: 30, totalPoints: 30, reporters: 0, misses: 0 })).toBe(100);
|
|||
|
|
expect(handshakeScore({ points: 15, totalPoints: 30, reporters: 0, misses: 0 })).toBe(53);
|
|||
|
|
expect(handshakeScore({ points: 25, totalPoints: 30, reporters: 1, misses: 2 })).toBe(77);
|
|||
|
|
expect(handshakeScore({ points: 0, totalPoints: 30, reporters: 3, misses: 9 })).toBe(0);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
it("造勢喊凍蒜:判定窗、譜面在曲子裡、分數一致", () => {
|
|||
|
|
expect(judgeOffset(0)).toBe("perfect");
|
|||
|
|
expect(judgeOffset(-PERFECT_MS)).toBe("perfect");
|
|||
|
|
expect(judgeOffset(PERFECT_MS + 1)).toBe("good");
|
|||
|
|
expect(judgeOffset(-GOOD_MS)).toBe("good");
|
|||
|
|
expect(judgeOffset(GOOD_MS + 1)).toBeNull();
|
|||
|
|
for (const lv of [0.2, 0.55, 0.9]) {
|
|||
|
|
const c = makeChart(5, lv);
|
|||
|
|
expect(c.notes.length).toBeGreaterThan(18);
|
|||
|
|
expect(Math.max(...c.notes)).toBeLessThan(c.endMs);
|
|||
|
|
expect([...c.notes].sort((a, b) => a - b)).toEqual(c.notes);
|
|||
|
|
}
|
|||
|
|
expect(rallyScore({ perfect: 30, good: 0, early: 0, maxCombo: 30, total: 30 })).toBe(100);
|
|||
|
|
// 隨手:一半 Perfect、三分之一 Good、幾次亂按 → 40–60
|
|||
|
|
const casual = rallyScore({ perfect: 15, good: 10, early: 3, maxCombo: 6, total: 30 });
|
|||
|
|
expect(casual).toBeGreaterThanOrEqual(40);
|
|||
|
|
expect(casual).toBeLessThanOrEqual(60);
|
|||
|
|
// 玩得好:大多 Perfect → 80–95
|
|||
|
|
const good = rallyScore({ perfect: 25, good: 4, early: 1, maxCombo: 18, total: 30 });
|
|||
|
|
expect(good).toBeGreaterThanOrEqual(80);
|
|||
|
|
expect(good).toBeLessThanOrEqual(95);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
it("看板卡位:一開始路是通的、最佳擺法夠大、分數曲線", () => {
|
|||
|
|
for (const seed of [1, 42, 777, 20261128]) {
|
|||
|
|
for (const level of [0.2, 0.9]) {
|
|||
|
|
const lv = makeBillboard(seed, level);
|
|||
|
|
expect(walkway((i) => lv.tiles[i] !== "free")).not.toBeNull();
|
|||
|
|
expect(lv.best).toBeGreaterThanOrEqual(18);
|
|||
|
|
expect(lv.pieces.length).toBe(level > 0.5 ? 4 : 3);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
expect(BILLBOARD_MS).toBe(40_000);
|
|||
|
|
expect(billboardScore(40, 40)).toBe(100);
|
|||
|
|
expect(billboardScore(50, 40)).toBe(100);
|
|||
|
|
expect(billboardScore(0, 40)).toBe(0);
|
|||
|
|
const casual = billboardScore(30, 40);
|
|||
|
|
expect(casual).toBeGreaterThanOrEqual(40);
|
|||
|
|
expect(casual).toBeLessThanOrEqual(60);
|
|||
|
|
expect(billboardScore(37, 40)).toBeGreaterThanOrEqual(80);
|
|||
|
|
});
|
|||
|
|
});
|