| 1 | """t1203: opponent minis scale to fit above the field on narrow portrait screens. |
| 2 | Usage: python3 test/t1203-mini-scale-playwright.py ROOT [tag]""" |
| 3 | import sys, json |
| 4 | from playwright.sync_api import sync_playwright |
| 5 | URL = sys.argv[1].rstrip("/") + "/" |
| 6 | TAG = sys.argv[2] if len(sys.argv) > 2 else "live" |
| 7 | fails = [] |
| 8 | def check(ok, msg): |
| 9 | print(("OK " if ok else "FAIL ") + msg) |
| 10 | if not ok: fails.append(msg) |
| 11 | |
| 12 | ROUND_FOR = {6: 12, 8: 23, 12: 57, 16: 107, 31: 437} |
| 13 | |
| 14 | MEASURE = """() => { |
| 15 | const minis = [...document.querySelectorAll('#minis .mini')]; |
| 16 | const fieldTop = app.fieldY, vw = innerWidth; |
| 17 | const out = minis.map(el => { const r = el.getBoundingClientRect(); const c = el.querySelector('canvas').getBoundingClientRect(); |
| 18 | return { id: el.dataset.player, slot: +el.dataset.slot, elim: el.classList.contains('eliminated'), |
| 19 | x: r.left, y: r.top, w: r.width, bottom: r.bottom, right: r.right, cw: c.width, ch: c.height, font: parseFloat(getComputedStyle(el).fontSize) }; }); |
| 20 | return { fieldTop, vw, hud: app.hudH, mini: app.miniLayout.mini, minis: out }; |
| 21 | }""" |
| 22 | |
| 23 | def start(page, n, elim_idx): |
| 24 | page.evaluate(f"""() => {{ |
| 25 | teardownPlay && teardownPlay(); |
| 26 | hideEl('menu'); |
| 27 | app.offline = true; |
| 28 | startPlay({{ planet: Planet.defaultPlanet(), offline: true, round: {ROUND_FOR[n]} }}); |
| 29 | }}""") |
| 30 | page.wait_for_function(f"() => app.mode === 'playing' && app.battle && app.battle.bots.length === {n}") |
| 31 | page.evaluate("() => { app.core.noSpawn = true; for (const b of app.battle.bots) { b.core.noSpawn = true; } }") |
| 32 | if elim_idx: |
| 33 | page.evaluate(f"() => {{ for (const i of {json.dumps(elim_idx)}) app.battle.bots[i].core.state = 'gameover'; }}") |
| 34 | page.wait_for_timeout(600) |
| 35 | return page.evaluate(MEASURE) |
| 36 | |
| 37 | with sync_playwright() as p: |
| 38 | br = p.chromium.launch() |
| 39 | for vw, vh in ((412, 915), (360, 800), (460, 900)): |
| 40 | ctx = br.new_context(viewport={"width": vw, "height": vh}, is_mobile=True, has_touch=True, device_scale_factor=2) |
| 41 | page = ctx.new_page(); errs = [] |
| 42 | page.on("pageerror", lambda e: errs.append(str(e))) |
| 43 | page.goto(URL + "index.html", wait_until="networkidle") |
| 44 | for n, elim in ((6, []), (6, [1, 4]), (8, [0, 2, 5]), (12, [3]), (16, [0, 1, 2, 3])): |
| 45 | m = start(page, n, elim) |
| 46 | tag = f"{vw}x{vh}-{n}bots" + (f"-{len(elim)}out" if elim else "") |
| 47 | ms = m["minis"] |
| 48 | above = all(q["bottom"] <= m["fieldTop"] + 0.5 for q in ms) |
| 49 | inside = all(q["x"] >= -0.5 and q["right"] <= m["vw"] + 0.5 and q["y"] >= m["hud"] - 0.5 for q in ms) |
| 50 | check(len(ms) == n and above and inside, f"{tag}: all {len(ms)} minis above the field (field top {m['fieldTop']}, lowest {max(q['bottom'] for q in ms):.0f}) and on screen; canvas {m['mini']['w']}x{m['mini']['h']}, font {ms[0]['font']}px") |
| 51 | check(m["mini"]["fits"] and [q["slot"] for q in ms] == list(range(n)), f"{tag}: order unchanged while they fit") |
| 52 | check(all(abs(q["cw"] - m["mini"]["w"]) < 1 for q in ms) and ms[0]["font"] >= 9, f"{tag}: canvas and label scaled ({ms[0]['cw']:.0f}px, {ms[0]['font']}px)") |
| 53 | if vw != 460 or n in (6,): |
| 54 | page.screenshot(path=f"/workspace/meteors-shots/t1203-{TAG}-{tag}.png") |
| 55 | # overflow: 31 bots, 11 defeated |
| 56 | elim = [i for i in range(31) if i % 3 == 0] |
| 57 | m = start(page, 31, elim) |
| 58 | ms = m["minis"] |
| 59 | hidden = [q for q in ms if q["bottom"] > m["fieldTop"] + 0.5] |
| 60 | alive_ok = all(q["bottom"] <= m["fieldTop"] + 0.5 for q in ms if not q["elim"]) |
| 61 | check(not m["mini"]["fits"] and m["mini"]["w"] == 32, f"{vw}x{vh}-31bots: minimum size used (canvas {m['mini']['w']}x{m['mini']['h']}), capacity {m['mini']['capacity']}") |
| 62 | check(alive_ok and hidden and all(q["elim"] for q in hidden), f"{vw}x{vh}-31bots: living all visible, only defeated behind the field ({len(hidden)} hidden, all defeated)") |
| 63 | alive_slots = [q["slot"] for q in ms if not q["elim"]] |
| 64 | check(alive_slots == sorted(alive_slots), f"{vw}x{vh}-31bots: stable order of the living") |
| 65 | page.screenshot(path=f"/workspace/meteors-shots/t1203-{TAG}-{vw}x{vh}-31bots-11out.png") |
| 66 | # Online uses the same layout: fake an 8-opponent lobby (2 out) and render its minis. |
| 67 | page.evaluate("() => { teardownPlay && teardownPlay(); }") |
| 68 | m = page.evaluate("""() => { |
| 69 | const players = [{ id: 'me', name: 'Ik', is_me: true }]; |
| 70 | for (let i = 1; i <= 8; i++) players.push({ id: 'p' + i, name: 'Speler ' + i, score: i, eliminated: i === 2 || i === 5, snapshot: { blocks: [] }, planet: Planet.defaultPlanet() }); |
| 71 | Online.lobby = { players }; |
| 72 | app.online = true; app.offline = false; app.battle = null; |
| 73 | hideEl('menu'); document.getElementById('minis').hidden = false; |
| 74 | app._layoutN = -1; layout(); |
| 75 | Online.renderMinis(document.getElementById('minis')); |
| 76 | return true; }""") |
| 77 | page.wait_for_timeout(200) |
| 78 | m = page.evaluate(MEASURE) |
| 79 | ms = m["minis"] |
| 80 | check(len(ms) == 8 and all(q["bottom"] <= m["fieldTop"] + 0.5 and q["x"] >= -0.5 and q["right"] <= m["vw"] + 0.5 for q in ms) and abs(ms[0]["cw"] - m["mini"]["w"]) < 1, f"{vw}x{vh}-online-8: online minis use the same scaled top layout (canvas {m['mini']['w']}px, lowest {max(q['bottom'] for q in ms):.0f} <= field {m['fieldTop']})") |
| 81 | page.screenshot(path=f"/workspace/meteors-shots/t1203-{TAG}-{vw}x{vh}-online-8.png") |
| 82 | page.evaluate("() => { app.online = false; Online.lobby = null; }") |
| 83 | check(not errs, f"{vw}x{vh}: no page errors {errs[:2]}") |
| 84 | ctx.close() |
| 85 | br.close() |
| 86 | print("ALL OK" if not fails else f"{len(fails)} FAILED") |
| 87 | sys.exit(1 if fails else 0) |
projects-timfalken-com meteors