champions-of-luneraria/Assets/Luneraria/UI/Game/LuneSmoke.cs
Martin Stickelbroeck d590fdebeb U4-Nachtrag: Startbildschirm in der Szene, Rückkehr aus der Partie
Befund aus dem Spieltest des U3-Nachtrags: Nichts legte LuneNetPlay in die
Szene. Das Spiel begann direkt in einer Partie, der Startbildschirm aus U4
war unerreichbar, und der Verlassen-Dialog beendete das Spiel statt zum
Hauptmenü zu führen.

- LuneWorldBuilder legt LuneNetPlay auf das Match-Objekt; Szene neu gebaut.
- Das Menü bekommt ein eigenes UIDocument über dem HUD. Vorher hing es an
  keinem Panel, und das HUD hätte es beim ersten Partiestart geleert.
- Rückkehr über „Menü“ → „Zum Menü“: HUD und Aufstellungsflächen weg,
  Sitzung beendet, leere Karte. Die Kamera ruht hinter dem Menü und beginnt
  jede Partie ungedreht. Die Bund-Wahl ist markiert, die Knopfreihe passt
  in die Tafel.
- Tests und Benchmark ohne Startbildschirm (LuneMatchDriver.DirectStart,
  -luneBenchmark); Bot-Partien und Fingerabdrücke unverändert.
- Neu: LuneSmoke (-luneSmoke <Ordner>), Selbsttest eines Players nach
  lune_smoke.gd. StartScreenTests führt ihn im Editor aus.
- Tests: EditMode 29/29, PlayMode 14/14 (StartScreenTests 26/26), Verify
  718/0, Fingerabdrücke DCE95C96556F3A1A und 7B8E43AEFC0D60C5 unverändert.
  macOS-Player: Selbsttest 26/26, Bilder in
  docs/port/screenshots/u4/startbildschirm/. Ein zusammenhängender
  PlayMode-Lauf auf dem Endstand und ein Player-Benchmark stehen aus, weil
  der Mac zugeklappt schlief (siehe U4-NETWORK.md).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 05:37:51 +02:00

229 lines
11 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using Luneraria.View;
using UnityEngine;
using UnityEngine.UIElements;
namespace Luneraria.UI
{
/// <summary>
/// Smoke test of a player build through the start screen (after lune_smoke.gd, plus the way
/// back to the menu): start screen, "Allein spielen", planning, Startaufstellung and Bereit,
/// combat, "Menü" → "Zum Menü", start screen again, a second offline match. It presses the
/// real buttons (LuneButton.Click, as Godot's smoke emits `pressed`), photographs each step
/// from the screen, writes report.json and quits with 0 (pass) or 1. Nothing here changes
/// rules or presentation. Run: &lt;player&gt; -luneSmoke &lt;folder&gt;. The PlayMode test runs it
/// in the editor without photos.
/// </summary>
public sealed class LuneSmoke : MonoBehaviour
{
/// <summary>The whole run takes about 20 s; after this the smoke test gives up and reports.</summary>
const float Limit = 120f;
public LuneNetPlay Net;
/// <summary>Folder for the photos and report.json; empty: neither (tests).</summary>
public string Folder = "";
public bool QuitWhenDone = true;
public bool Done { get; private set; }
public readonly List<string> Passed = new List<string>();
public readonly List<string> Failed = new List<string>();
readonly List<string> _photos = new List<string>();
float _started;
/// <summary>Adds the smoke test when the command line asks for it (lune_main.gd: --lune-smoke).</summary>
public static void StartIfRequested(LuneNetPlay net)
{
string[] args = Environment.GetCommandLineArgs();
int at = Array.IndexOf(args, "-luneSmoke");
if (at < 0)
return;
var smoke = net.gameObject.AddComponent<LuneSmoke>();
smoke.Net = net;
smoke.Folder = at + 1 < args.Length && !args[at + 1].StartsWith("-") ? args[at + 1] : Path.Combine(Application.persistentDataPath, "smoke");
// It runs unattended; it must not pause when its window loses focus.
Application.runInBackground = true;
}
IEnumerator Start()
{
_started = Time.realtimeSinceStartup;
LuneGame game = Net.Game;
LuneMatchDriver driver = Net.Driver;
yield return new WaitForSecondsRealtime(1.5f);
// The start screen comes first; the scene's match waits.
Check("Startbildschirm sichtbar", Shown(Net.Menu));
Check("Partie wartet: Spielcontroller und Treiber aus", !game.enabled && !driver.enabled);
Check("vor dem Start kein HUD", game.Hud == null);
LuneButton alone = Button(Net.Menu, "ALLEIN SPIELEN");
Check("„Allein spielen“ bedienbar", Pressable(alone));
yield return Photo("00-startbildschirm");
// Offline starts a match in planning, the own seat on the chosen lane (Mid: seat 1).
LuneMatchRunner waiting = driver.Runner;
alone?.Click();
yield return new WaitForSecondsRealtime(1.5f);
Check("Offline: Startbildschirm verborgen", !Shown(Net.Menu));
Check("Offline: Spielcontroller und Treiber laufen", game.enabled && driver.enabled);
Check("Offline: HUD sichtbar", game.Hud != null && Shown(game.Hud.Root));
Check("Offline: neue Partie, Sitz 1 (Mid)", driver.Runner != waiting && driver.Runner.HumanSlot == 1);
Check("Offline: Planung", driver.Runner.Current == LuneMatchRunner.Stage.Planning);
Check("Offline: lokale Sitzung", Net.Session != null && !Net.Session.IsNetworked());
yield return Photo("01-planung");
// The HUD plays the match: Startaufstellung and Bereit lead into the combat.
LuneButton starter = Button(game.Hud != null ? game.Hud.Root : null, "Startaufstellung");
Check("„Startaufstellung“ bedienbar", Pressable(starter));
int roster = driver.Runner.Match.Players[1].Draft.Roster.Count;
starter?.Click();
yield return new WaitForSecondsRealtime(.5f);
Check("Startaufstellung gekauft (3 Einheiten)", driver.Runner.Match.Players[1].Draft.Roster.Count == roster + 3);
LuneButton ready = Button(game.Hud != null ? game.Hud.Root : null, "BEREIT");
Check("„Bereit“ bedienbar", Pressable(ready));
ready?.Click();
float waited = 0f;
while (driver.Runner.Current != LuneMatchRunner.Stage.Combat && waited < 10f)
{
waited += Time.unscaledDeltaTime;
yield return null;
}
Check("Bereit: Kampf beginnt", driver.Runner.Current == LuneMatchRunner.Stage.Combat);
yield return new WaitForSecondsRealtime(4f);
Check("Kampf: Figuren im Feld", Net.View.Units.Any());
yield return Photo("02-kampf");
// "Menü" asks; "Zum Menü" returns to the start screen over the empty map.
LuneButton menu = Button(game.Hud != null ? game.Hud.Root : null, "Menü");
Check("„Menü“ bedienbar", Pressable(menu));
menu?.Click();
yield return new WaitForSecondsRealtime(.5f);
LuneButton back = Button(game.Hud != null ? game.Hud.Root : null, "ZUM MENÜ");
Check("Verlassen-Dialog mit „Zum Menü“", Pressable(back));
yield return Photo("03-verlassen");
back?.Click();
yield return new WaitForSecondsRealtime(1f);
Check("Zurück: Startbildschirm sichtbar", Shown(Net.Menu));
Check("Zurück: HUD verborgen", game.Hud != null && !Shown(game.Hud.Root));
Check("Zurück: Spielcontroller und Treiber aus", !game.enabled && !driver.enabled);
Check("Zurück: Sitzung beendet", Net.Session == null);
Check("Zurück: keine Figuren im Feld", !Net.View.Units.Any());
Check("Zurück: Kamera ruht", Net.Rig != null && !Net.Rig.Interactive);
alone = Button(Net.Menu, "ALLEIN SPIELEN");
Check("Zurück: „Allein spielen“ bedienbar", Pressable(alone));
yield return Photo("04-zurueck-im-startbildschirm");
// The start screen works again: a second offline match.
waiting = driver.Runner;
alone?.Click();
yield return new WaitForSecondsRealtime(1.5f);
Check("Zweite Partie: Startbildschirm verborgen, HUD sichtbar", !Shown(Net.Menu) && game.Hud != null && Shown(game.Hud.Root));
Check("Zweite Partie: neue Partie in der Planung", driver.Runner != waiting && driver.Runner.Current == LuneMatchRunner.Stage.Planning && driver.enabled && game.enabled);
yield return Photo("05-zweite-partie");
Finish();
}
void Update()
{
// A stuck step (or an exception in the run) must still end with a report.
if (!Done && _started > 0f && Time.realtimeSinceStartup - _started > Limit)
{
Check("Lauf innerhalb von " + Limit + " s beendet", false);
Finish();
}
}
void Check(string name, bool ok)
{
(ok ? Passed : Failed).Add(name);
Debug.Log("LUNE_SMOKE " + (ok ? "ok " : "FEHLER ") + name);
}
/// <summary>Displayed in a panel, with no hidden ancestor.</summary>
static bool Shown(VisualElement element)
{
if (element == null || element.panel == null)
return false;
for (VisualElement node = element; node != null; node = node.parent)
{
if (node.style.display == DisplayStyle.None || node.resolvedStyle.display == DisplayStyle.None)
return false;
}
return true;
}
/// <summary>
/// Shown and enabled; in a player with a screen also laid out and on top at its centre,
/// so a mouse click there reaches it.
/// </summary>
static bool Pressable(LuneButton button)
{
if (button == null || button.Disabled || !Shown(button))
return false;
if (Application.isBatchMode)
return true;
Rect bound = button.worldBound;
if (float.IsNaN(bound.width) || bound.width <= 0f)
return false;
VisualElement picked = button.panel.Pick(bound.center);
return picked != null && (picked == button || button.Contains(picked));
}
/// <summary>The first shown button with this text (icon buttons carry it in a label).</summary>
static LuneButton Button(VisualElement root, string text)
{
if (root == null)
return null;
return root.Query<LuneButton>().ToList().FirstOrDefault(b => Shown(b) && (b.Text == text || b.Query<Label>().ToList().Any(l => l.text == text)));
}
IEnumerator Photo(string label)
{
if (string.IsNullOrEmpty(Folder))
yield break;
yield return new WaitForEndOfFrame();
Texture2D shot = ScreenCapture.CaptureScreenshotAsTexture();
Directory.CreateDirectory(Folder);
string path = Path.Combine(Folder, label + ".jpg");
File.WriteAllBytes(path, shot.EncodeToJPG(92));
Destroy(shot);
_photos.Add(label + ".jpg");
Debug.Log("LUNE_SMOKE_SHOT " + path);
}
void Finish()
{
if (Done)
return;
Done = true;
bool ok = Failed.Count == 0;
var text = new StringBuilder("{\n");
text.AppendFormat(" \"build\": \"{0} player, {1}, {2}\",\n", Application.platform, Application.unityVersion, Debug.isDebugBuild ? "development" : "release");
text.AppendFormat(" \"device\": \"{0}; {1}; {2}\",\n", SystemInfo.deviceModel, SystemInfo.processorType, SystemInfo.graphicsDeviceName);
text.AppendFormat(" \"graphics_api\": \"{0}\",\n \"resolution\": \"{1}x{2}\",\n", SystemInfo.graphicsDeviceType, Screen.width, Screen.height);
text.AppendFormat(CultureInfo.InvariantCulture, " \"seconds\": {0:0.0},\n", Time.realtimeSinceStartup - _started);
text.Append(" \"passed\": [" + string.Join(", ", Passed.Select(Quote)) + "],\n");
text.Append(" \"failed\": [" + string.Join(", ", Failed.Select(Quote)) + "],\n");
text.Append(" \"photos\": [" + string.Join(", ", _photos.Select(Quote)) + "],\n");
text.AppendFormat(" \"result\": \"{0}\"\n}}\n", ok ? "PASS" : "FAIL");
Debug.Log("LUNE_SMOKE_" + (ok ? "PASS" : "FAIL") + " " + Passed.Count + " ok, " + Failed.Count + " Fehler");
if (!string.IsNullOrEmpty(Folder))
{
Directory.CreateDirectory(Folder);
File.WriteAllText(Path.Combine(Folder, "report.json"), text.ToString());
}
if (QuitWhenDone)
Application.Quit(ok ? 0 : 1);
}
static string Quote(string value)
{
return "\"" + value.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\"";
}
}
}