Remove duplicate code
This commit is contained in:
@@ -5,16 +5,13 @@
|
||||
// mods/bworld/scripts/curios.ts
|
||||
import { assert, assertEquals } from "@std/assert";
|
||||
import { PLAYER_MENU_EXTRA, PROTOCOL_VERSION } from "$/common/protocol.ts";
|
||||
import { copy_dir, test_game } from "./helpers.ts";
|
||||
import { container_items, copy_dir, test_game } from "./helpers.ts";
|
||||
import type { WorldSave } from "$/server/game/saves.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;
|
||||
|
||||
// the base game, and a mod with a ring and an amulet that goes in any slot
|
||||
async function setup(save?: WorldSave) {
|
||||
const dir = Deno.makeTempDirSync({ prefix: "bworld_curios_" });
|
||||
@@ -58,7 +55,7 @@ async function setup(save?: WorldSave) {
|
||||
}
|
||||
|
||||
// what's in the curios slots, the ones the base game added to the player's own menu, as last synced
|
||||
const curios_items = (messages: Msg[]) => last_container(messages, "extra");
|
||||
const curios_items = (messages: Msg[]) => container_items(messages, "extra");
|
||||
|
||||
// which of those is a slot type, found by its icon. which types there are depends on what items every mod has
|
||||
function curio_slot(t: Game, type: string) {
|
||||
@@ -86,8 +83,8 @@ Deno.test("items only go in the slots they fit, and fall out when the player die
|
||||
// #bworld:curios/curio items in any slot, the curio slot first
|
||||
for (const slot of [2, 0, 1]) t.send(1, { type: "click", menu: 0, slot, button: 0, shift: true });
|
||||
const messages = t.take(1);
|
||||
assertEquals(last_container(messages, "inventory").slice(0, 3), [null, null, null]);
|
||||
assertEquals(last_container(messages, "inventory")[9], { id: "bworld:coal", count: 1 });
|
||||
assertEquals(container_items(messages, "inventory").slice(0, 3), [null, null, null]);
|
||||
assertEquals(container_items(messages, "inventory")[9], { id: "bworld:coal", count: 1 });
|
||||
const [curio, ring] = [curio_slot(t, "curio"), curio_slot(t, "ring")];
|
||||
assertEquals(curios_items(messages)[curio], { id: "jewelry:amulet", count: 1 });
|
||||
assertEquals(curios_items(messages)[ring], { id: "jewelry:ring", count: 1 });
|
||||
@@ -125,7 +122,7 @@ Deno.test("curios worn when curios was a mod of its own are still worn", async (
|
||||
Deno.test("the giant chestplate goes in the body slot and gives 20 more health while worn", async () => {
|
||||
const t = await setup();
|
||||
t.send(1, { type: "chat", text: "/give bworld:giant_chestplate" });
|
||||
const slot = last_container(t.take(1), "inventory").findIndex((i: Msg) => i?.id === "bworld:giant_chestplate");
|
||||
const slot = container_items(t.take(1), "inventory").findIndex((i: Msg) => i?.id === "bworld:giant_chestplate");
|
||||
const alice = t.game.players()[0];
|
||||
t.send(1, { type: "click", menu: 0, slot, button: 0, shift: true });
|
||||
// what curios give is worked out each tick, from what changed
|
||||
@@ -161,7 +158,7 @@ Deno.test("the giant chestplate goes in the body slot and gives 20 more health w
|
||||
Deno.test("the giant chestplate gives regeneration III after 10 seconds without being hurt", async () => {
|
||||
const t = await setup();
|
||||
t.send(1, { type: "chat", text: "/give bworld:giant_chestplate" });
|
||||
const slot = last_container(t.take(1), "inventory").findIndex((i: Msg) => i?.id === "bworld:giant_chestplate");
|
||||
const slot = container_items(t.take(1), "inventory").findIndex((i: Msg) => i?.id === "bworld:giant_chestplate");
|
||||
const alice = t.game.players()[0];
|
||||
t.send(1, { type: "click", menu: 0, slot, button: 0, shift: true });
|
||||
t.game.damage_player(alice, 2, "test");
|
||||
|
||||
Reference in New Issue
Block a user