Player skins !

This commit is contained in:
2026-09-27 12:01:52 -03:00
parent edf4b72dc4
commit 60188ff735
14 changed files with 325 additions and 8 deletions
+7 -1
View File
@@ -41,6 +41,8 @@ export async function test_game(mods_dir: string, save?: string, seed = "test-se
EverythingRegistry.clear();
const outbox = new Map<number, unknown[]>();
const closed = new Set<number>();
// what host.download gives back, by url. other urls fail
const downloads = new Map<string, Uint8Array>();
const host = {
send: (conn: number, data: string) => {
if (!outbox.has(conn)) outbox.set(conn, []);
@@ -49,6 +51,10 @@ export async function test_game(mods_dir: string, save?: string, seed = "test-se
close(conn: number) {
closed.add(conn);
},
download(url: string) {
const bytes = downloads.get(url);
return bytes ? Promise.resolve(bytes) : Promise.reject(new Error("Couldn't download it"));
},
};
const game: GameServer = await start_game(host, save, seed, mod_sources(mods_dir));
// deno-lint-ignore no-explicit-any
@@ -65,7 +71,7 @@ export async function test_game(mods_dir: string, save?: string, seed = "test-se
send(conn, { type: "ready" });
return take(conn).find((m) => m.type === "join");
};
return { game, take, send, join, closed };
return { game, take, send, join, closed, downloads };
}
export function copy_dir(from: string, to: string) {
+69
View File
@@ -0,0 +1,69 @@
import { assertEquals } from "@std/assert";
import { encode_skin } from "$/server/game/skins.ts";
import { test_game } from "./helpers.ts";
const SKIN = Deno.readFileSync("assets/sprites/skin_template.png");
// the downloads resolve on a later turn of the event loop
const settle = () => new Promise((resolve) => setTimeout(resolve, 0));
// deno-lint-ignore no-explicit-any
const chat = (messages: any[]) => messages.filter((m) => m.type === "chat").map((m) => m.text);
Deno.test("/skin downloads a png, tells everyone and is saved with the player", async () => {
const t = await test_game("mods");
t.downloads.set("https://example.com/skin.png", SKIN);
t.join(1, "alice");
t.join(2, "bob");
t.take(1);
t.take(2);
t.send(1, { type: "chat", text: "/skin https://example.com/skin.png" });
await settle();
const skin = encode_skin(SKIN);
const alice_id = t.game.players().find((p) => p.name === "alice")!.id;
const to_alice = t.take(1);
assertEquals(chat(to_alice), ["Downloading your skin...", "Skin changed"]);
assertEquals(to_alice.find((m) => m.type === "player_skin"), { type: "player_skin", id: alice_id, skin });
assertEquals(t.take(2).find((m) => m.type === "player_skin"), { type: "player_skin", id: alice_id, skin });
// players joining later get it with the rest of alice's info, and it's still there after a restart
const loaded = await test_game("mods", t.game.save());
loaded.join(1, "alice");
const join = loaded.join(2, "bob");
assertEquals(join.players.find((p: { name: string }) => p.name === "alice").skin, skin);
});
Deno.test("/skin says why a download can't be a skin, and /skin alone goes back to the default", async () => {
const t = await test_game("mods");
const wide = SKIN.slice();
new DataView(wide.buffer).setUint32(20, 32);
t.downloads.set("https://example.com/old.png", wide);
t.downloads.set("https://example.com/page.html", new TextEncoder().encode("<html></html>"));
t.join(1, "alice");
t.take(1);
const skin_messages = async (text: string) => {
t.send(1, { type: "chat", text });
await settle();
const messages = t.take(1);
return { chat: chat(messages), skins: messages.filter((m) => m.type === "player_skin") };
};
assertEquals((await skin_messages("/skin https://example.com/old.png")).chat, [
"Downloading your skin...",
"Couldn't change your skin: Skins have to be 64x64 (it's 64x32)",
]);
assertEquals(
(await skin_messages("/skin https://example.com/page.html")).chat.at(-1),
"Couldn't change your skin: That isn't a png",
);
const missing = await skin_messages("/skin https://example.com/nothing.png");
assertEquals(missing.chat.at(-1), "Couldn't change your skin: Couldn't download it");
assertEquals(missing.skins, []);
const reset = await skin_messages("/skin");
assertEquals(reset.chat, ["Your skin is back to the default"]);
assertEquals(reset.skins.length, 1);
assertEquals(reset.skins[0].skin, undefined);
});