Something went wrong. Try again.
(WIP) Modern Minecraft server-side HUD framework.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102import { test as base, type Page } from '@playwright/test';import { generateHuds } from './generate-huds';
export const HUD_COUNT = 500;export const huds = generateHuds(HUD_COUNT);
const ALL_IMAGE_SRCS = [ 'heart.png', 'shield.png', 'sword.png', 'potion.png', 'star.png', 'coin.png', 'skull.png', 'arrow.png', 'idle.png', 'combat.png', 'dead.png', 'stunned.png', 'full.png', 'half.png', 'low.png', 'critical.png', 'lobby.png', 'playing.png', 'ending.png', 'spectating.png', 'standing.png', 'crouching.png', 'swimming.png', 'flying.png', 'clear.png', 'rain.png', 'snow.png', 'storm.png', 'pickaxe.png', 'axe.png', 'shovel.png', 'bow.png',];
async function waitForHarness(page: Page): Promise<void> { await page.waitForFunction( () => (window as any).harness?.ready === true, null, { timeout: 15_000 } );}
async function loadDummyImages(page: Page): Promise<void> { await page.evaluate((srcs) => { const sizes = [8, 16, 24, 32]; const h = (window as any).harness; for (let i = 0; i < srcs.length; i++) { const size = sizes[i % sizes.length]; const canvas = document.createElement('canvas'); canvas.width = size; canvas.height = size; const ctx = canvas.getContext('2d')!;
if (i % 3 === 0) { ctx.fillStyle = `hsl(${(i * 37) % 360}, 80%, 50%)`; ctx.fillRect(0, 0, size, size); } else if (i % 3 === 1) { ctx.fillStyle = `rgba(${(i * 73) % 256}, ${(i * 43) % 256}, ${(i * 97) % 256}, 0.6)`; ctx.fillRect(0, 0, size, size); ctx.clearRect(size / 4, size / 4, size / 2, size / 2); } else { const grad = ctx.createLinearGradient(0, 0, size, size); grad.addColorStop(0, `hsl(${(i * 53) % 360}, 70%, 40%)`); grad.addColorStop(1, `hsl(${(i * 53 + 180) % 360}, 70%, 60%)`); ctx.fillStyle = grad; ctx.fillRect(0, 0, size, size); }
const dataUrl = canvas.toDataURL('image/png'); const binary = atob(dataUrl.split(',')[1]); const bytes = new Uint8Array(binary.length); for (let j = 0; j < binary.length; j++) bytes[j] = binary.charCodeAt(j); h.loadMedia(srcs[i], bytes); } }, ALL_IMAGE_SRCS);}
export type RenderResult = { error: string | null };
export const test = base.extend<{}, { sharedPage: Page; renderCache: Map<number, RenderResult> }>({ sharedPage: [async ({ browser }, use) => { const context = await browser.newContext(); const page = await context.newPage(); await page.goto('http://localhost:4173/'); await waitForHarness(page); await loadDummyImages(page); await use(page); await context.close(); }, { scope: 'worker' }],
renderCache: [async ({}, use) => { await use(new Map()); }, { scope: 'worker' }],
page: async ({ sharedPage }, use) => { await use(sharedPage); },});
export async function renderHud(page: Page, cache: Map<number, RenderResult>, index: number): Promise<RenderResult> { const cached = cache.get(index); if (cached) return cached;
const error = await page.evaluate(async (xml) => { const h = (window as any).harness; h.resize(640, 480); try { await h.render(xml, { scale: 2 }); return null; } catch (e: any) { return e.message ?? String(e); } }, huds[index]);
const result: RenderResult = { error }; cache.set(index, result); return result;}