Remove duplicate code

This commit is contained in:
2026-10-13 11:57:43 -03:00
parent dfc6a6d77b
commit 5165698fe3
50 changed files with 307 additions and 812 deletions
+9 -14
View File
@@ -6,22 +6,17 @@ import { assert, assertEquals } from "@std/assert";
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import { CYCLE_TICKS } from "$/common/time.ts";
import { display_name, get_state_value } from "$/common/utils.ts";
import { menu_data, menu_items, opened_menu, test_game } from "./helpers.ts";
import { container_items, menu_data, menu_items, opened_menu, stone_floor, test_game } from "./helpers.ts";
// deno-lint-ignore no-explicit-any
type Msg = any;
type Game = Awaited<ReturnType<typeof test_game>>;
const last_container = (messages: Msg[], container: string) =>
messages.filter((m) => m.type === "container" && m.container === container).at(-1)?.items;
// a player standing on a stone floor at y 99, high above the terrain, with the items given in their inventory
async function setup(items: [string, number][] = []) {
const t = await test_game("mods");
t.join(1, "alice");
for (let x = -3; x <= 3; x++) {
for (let z = -3; z <= 3; z++) t.game.set_block(x, 99, z, "bworld:stone");
}
stone_floor(t.game);
t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 });
for (const [id, count] of items) t.send(1, { type: "chat", text: `/give ${id} ${count}` });
return t;
@@ -29,7 +24,7 @@ async function setup(items: [string, number][] = []) {
// the inventory slot holding an item, from what the server sent last
function slot_of(messages: Msg[], id: string) {
const slot = (last_container(messages, "inventory") ?? []).findIndex((i: Msg) => i?.id === id);
const slot = (container_items(messages, "inventory") ?? []).findIndex((i: Msg) => i?.id === id);
assert(slot >= 0, `no ${id} in the inventory`);
return slot;
}
@@ -109,7 +104,7 @@ Deno.test("a shipping bin sells what's in it every morning for as few coins as i
assertEquals(EverythingRegistry.get<ItemRegistry>("items", "bworld:oak_planks")?.base_sell_value, 2);
t.game.set_block(1, 100, 0, "bworld:shipping_bin");
const messages = t.take(1);
const inventory = last_container(messages, "inventory");
const inventory = container_items(messages, "inventory");
const planks = inventory.flatMap((item: Msg, slot: number) => item?.id === "bworld:oak_planks" ? [slot] : []);
const copper = slot_of(messages, "bworld:copper_coin");
@@ -190,7 +185,7 @@ Deno.test("bone meal grows wheat until it's fully grown", async () => {
assertEquals(age(), 1);
for (let i = 0; i < 7; i++) t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
assertEquals(age(), 7);
assertEquals(last_container(t.take(1), "inventory")[slot]?.count, 1);
assertEquals(container_items(t.take(1), "inventory")[slot]?.count, 1);
assertEquals(t.game.world.get_block_id(1, 101, 0), "bworld:air");
});
@@ -200,12 +195,12 @@ Deno.test("the last bone meal is used up", async () => {
const slot = slot_of(t.take(1), "bworld:bone_meal");
t.send(1, { type: "select_slot", slot });
t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
assertEquals(last_container(t.take(1), "inventory")[slot], null);
assertEquals(container_items(t.take(1), "inventory")[slot], null);
});
Deno.test("a new watering can starts empty, and players are told so in its lore", async () => {
const t = await setup([["bworld:copper_watering_can", 1]]);
const can = last_container(t.take(1), "inventory").find((i: Msg) => i?.id === "bworld:copper_watering_can");
const can = container_items(t.take(1), "inventory").find((i: Msg) => i?.id === "bworld:copper_watering_can");
assertEquals(can.data, { water: 0, max_water: 32 });
assertEquals(can.lore, "Water: 0/32");
@@ -259,7 +254,7 @@ Deno.test("a smithing table upgrades a wooden pickaxe with 5 stone, using up exa
// closing gives back the leftover stone, and the cursor's pickaxe goes into the inventory
t.send(1, { type: "close_menu", menu: opened.id });
const inventory = last_container(t.take(1), "inventory").filter((i: Msg) => i);
const inventory = container_items(t.take(1), "inventory").filter((i: Msg) => i);
assertEquals(
inventory.sort((a: Msg, b: Msg) => a.id.localeCompare(b.id)),
[{ id: "bworld:stone", count: 2 }, { id: "bworld:stone_pickaxe", count: 1 }],
@@ -339,7 +334,7 @@ Deno.test("a stonecutter cuts its input into the recipe picked with a button, on
assertEquals(menu_data(latest).selected, -1);
assertEquals(menu_items(latest)[1], null);
t.send(1, { type: "close_menu", menu: opened.id });
const inventory = last_container(t.take(1), "inventory").filter((i: Msg) => i);
const inventory = container_items(t.take(1), "inventory").filter((i: Msg) => i);
assertEquals(
inventory.map((i: Msg) => i.id).sort(),
["bworld:cobblestone", "bworld:dirt", result.id].sort(),