extends SceneTree ## Plays random-vs-random games with events on (godot-plan.md ยง3) and checks after every action that ## the event stream, folded up from the start, gives the state: each player's Bandwidth, each ## gateway's Firewall, each location's owner, the zone of every card, where every Anomaly stands, ## which Anomalies are face up, and their counters. Every event's "from" must match the fold too, and ## the Bandwidth safety net (State.reconcile_bandwidth) must never be needed. ## ## godot --headless --script res://tests/events.gd -- [--games N] [--seed N] var _fold: Dictionary var _seen := 0 var _errors: Array[String] = [] func _init() -> void: var games := 30 var first := 1 var args := OS.get_cmdline_user_args() var i := 0 while i < args.size(): match args[i]: "--games": i += 1 games = int(args[i]) "--seed": i += 1 first = int(args[i]) i += 1 var pool := Cards.load_cards() var rules := Rules.defaults() var started := Time.get_ticks_msec() var failed := 0 var steps := 0 var events := 0 var kinds := {} State.event_fixes = 0 for seed in range(first, first + games): Util.errors.clear() _errors = [] var err := _play(pool, rules, seed) if err == "" and not Util.errors.is_empty(): err = "engine error: %s" % "; ".join(Util.errors) if err == "" and State.event_fixes > 0: err = "the Bandwidth safety net added %d event(s)" % State.event_fixes if err != "": failed += 1 print("seed %d: %s" % [seed, err]) State.event_fixes = 0 continue steps += _fold.steps events += _seen for k in _fold.kinds: kinds[k] = kinds.get(k, 0) + _fold.kinds[k] var secs := (Time.get_ticks_msec() - started) / 1000.0 var counts := PackedStringArray() var names := kinds.keys() names.sort() for k in names: counts.append("%s %d" % [k, kinds[k]]) print("events: %s" % ", ".join(counts)) print("events: %d/%d games consistent (%d steps, %d events, %.1fs)" % [games - failed, games, steps, events, secs]) var match_err := _check_match(pool, first) if match_err != "": failed += 1 print("LocalMatch: %s" % match_err) quit(0 if failed == 0 else 1) ## LocalMatch records events and hands out yours: the bot's draws hidden, yours shown. func _check_match(pool: Dictionary, seed: int) -> String: var rules := LocalMatch.rules_from([]) var setup := {"seed": seed, "human": 0, "decks": LocalMatch.random_decks(pool, seed, rules), "rules": []} var m := LocalMatch.new(pool, setup, RandomBot.new(seed)) var you := RandomBot.new(seed + 1) var from := m.event_count() if from == 0: return "no events after setup" for n in 300: if m.is_over(): break if m.bot_to_act(): m.bot_step() else: m.act(you.choose(m.view(), m.legal(), m.human)) var mine := m.events_since(from) if mine.size() != m.event_count() - from: return "events_since(%d) has %d events, expected %d" % [from, mine.size(), m.event_count() - from] var drew := [0, 0] for ev in mine: if ev.k == "drew": drew[ev.p] += 1 if (ev.p == m.bot_side) != (ev.card == -1): return "a draw shown to the wrong player: %s" % JSON.stringify(ev) if drew[0] == 0 or drew[1] == 0: return "no draws seen: %s" % str(drew) return "" ## Plays one game, checking after every action. Returns "" or the first problem. func _play(pool: Dictionary, rules: Dictionary, seed: int) -> String: var decks := Play.deal(pool, seed, rules) var s = RulesEngine.create_game({"pool": pool, "decks": decks, "seed": seed, "rules": rules, "log": true, "events": true}) if s == null: return "create_game failed" _start_fold(s) var err := _check(s, "setup") if err != "": return err var random: Callable = Play.bots().random var players := [random.call(seed * 2 + 1), random.call(seed * 2 + 2)] var n := 0 while not s.over: n += 1 if n > Play.MAX_ACTIONS: return "didn't finish" var p: int = RulesEngine.decider(s) var legal := RulesEngine.legal_actions(s) var view := RulesEngine.view_for(s, p) if view.events != null: return "view_for keeps the events" var a: Dictionary = players[p].choose(view, legal, p) if not RulesEngine.apply_action(s, a, legal): return "illegal action" _fold.steps += 1 err = _check(s, "step %d (%s), turn %d" % [n, RulesEngine.action_key(a), s.turn]) if err != "": return err return _check_views(s) # --------------------------------------------------------------------------------------------- # The fold func _start_fold(s: Dictionary) -> void: _seen = 0 var zone := {} for c in s.cards: var kind: String = s.defs[c.name].kind zone[c.id] = "slot" if kind == "anomaly" else ("serverPile" if kind == "server" else "deck") var owner := {} # No event captures the starts; folding from them, any capture event must agree. for loc in GameMap.START_LOC: owner[loc] = GameMap.START_LOC.find(loc) for loc in GameMap.LOCATIONS: if not owner.has(loc): owner[loc] = null var at := {} var face := {} var counters := {} for p in 2: for slot in range(1, 9): var k := "%d:%d" % [p, slot] at[k] = null counters[k] = {"infection": 0, "bugs": 0, "damage": 0} if slot <= 4: face[k] = false _fold = {"bandwidth": [0, 0], "firewalls": {}, "owner": owner, "zone": zone, "at": at, "face": face, "counters": counters, "turn": 0, "active": -1, "combats": {}, "steps": 0, "kinds": {}} ## Folds the new events, then compares the fold with the state. func _check(s: Dictionary, where: String) -> String: var events: Array = s.events for i in range(_seen, events.size()): var err := _apply(s, events[i]) if err != "": return "%s: event %d %s: %s" % [where, i, JSON.stringify(events[i]), err] _seen = events.size() var err := _compare(s) return "" if err == "" else "%s: %s" % [where, err] func _zone_matches(zone: String, from: String) -> bool: return from != "" and (zone == from or zone.begins_with(from + ":")) func _apply(s: Dictionary, ev: Dictionary) -> String: var f := _fold f.kinds[ev.k] = f.kinds.get(ev.k, 0) + 1 var rk := func(r) -> String: return "%d:%d" % [r.player, r.slot] match ev.k: "turn": if ev.turn != f.turn + 1: return "turn %d after %d" % [ev.turn, f.turn] f.turn = ev.turn f.active = ev.p "bandwidth": if ev.from != f.bandwidth[ev.p]: return "from %d, but the fold has %d" % [ev.from, f.bandwidth[ev.p]] if ev.from == ev.to: return "no change" f.bandwidth[ev.p] = ev.to "firewall": if ev.from != f.firewalls.get(ev.line, 0): return "from Lv %d, but the fold has %d" % [ev.from, f.firewalls.get(ev.line, 0)] f.firewalls[ev.line] = ev.to "captured": if ev.from != f.owner[ev.loc]: return "from %s, but the fold has %s" % [ev.from, f.owner[ev.loc]] f.owner[ev.loc] = ev.p "drew": if f.zone[ev.card] != "deck": return "drawn from %s" % f.zone[ev.card] if s.cards[ev.card].owner != ev.p: return "not the drawer's card" f.zone[ev.card] = "hand" "hand", "discard": if not _zone_matches(f.zone[ev.card], ev.from): return "from %s, but the fold has %s" % [ev.from, f.zone[ev.card]] if s.cards[ev.card].owner != ev.p: return "p isn't the owner" f.zone[ev.card] = ev.k if ev.k == "discard" and ev.why == "": return "no why" "pile": if not _zone_matches(f.zone[ev.card], ev.from): return "from %s, but the fold has %s" % [ev.from, f.zone[ev.card]] f.zone[ev.card] = ev.zone "install": var was: String = f.zone[ev.card] if was != "hand" and was != "serverPile": return "installed from %s" % was f.zone[ev.card] = ("play:" + ev.at.substr(4)) if ev.at.begins_with("loc:") else "worn:" + ev.at.substr(8) "stack": if ev.kind == "execution": if f.zone[ev.card] != "hand": return "played from %s" % f.zone[ev.card] f.zone[ev.card] = "stack" "placed": if f.at[rk.call(ev.ref)] != null: return "placed, but it stands on %s" % f.at[rk.call(ev.ref)] f.at[rk.call(ev.ref)] = ev.loc "moved": if f.at[rk.call(ev.ref)] != ev.from: return "moved from %s, but it stands on %s" % [ev.from, f.at[rk.call(ev.ref)]] f.at[rk.call(ev.ref)] = ev.to "recalled", "destroyed": var from = ev.get("from", ev.get("loc")) if from == null or f.at[rk.call(ev.ref)] != from: return "left %s, but it stands on %s" % [from, f.at[rk.call(ev.ref)]] f.at[rk.call(ev.ref)] = null "flipped": if f.face[rk.call(ev.ref)]: return "already face up" f.face[rk.call(ev.ref)] = true "damaged": if ev.amount <= 0 or f.at[rk.call(ev.ref)] == null: return "bad damage" f.counters[rk.call(ev.ref)].damage += ev.amount "counter": var c: Dictionary = f.counters[rk.call(ev.ref)] if c[ev.what] != ev.from: return "%s from %d, but the fold has %d" % [ev.what, ev.from, c[ev.what]] c[ev.what] = ev.to "combat": if f.combats.has(ev.id): return "combat %d twice" % ev.id f.combats[ev.id] = ev.loc "rolled": if not f.combats.values().has(ev.loc): return "a roll with no combat there" "rerolled": if ev.dice.is_empty() and s.combat == null: return "a reroll with no combat" _: return "unknown kind" return "" ## The state's zone for every card, in the fold's terms. func _zones(s: Dictionary) -> Dictionary: var out := {} for p in 2: var pl: Dictionary = s.players[p] for zone in ["hand", "deck", "serverPile", "discard"]: for id in pl[zone]: out[id] = zone for i in pl.anomalies.size(): var av: Dictionary = pl.anomalies[i] for hw in av.hardware: out[hw] = "worn:%d:%d" % [p, i + 1] if i < 4 and av.card >= 0: out[av.card] = "slot" for loc in s.locations: var L: Dictionary = s.locations[loc] if L.server != null: out[L.server] = "play:" + loc for id in L.programs: out[id] = "play:" + loc for item in s.stack: if item.kind == "execution": out[item.card] = "stack" return out func _compare(s: Dictionary) -> String: var f := _fold for p in 2: if f.bandwidth[p] != s.players[p].bandwidth: return "%s's Bandwidth folds to %d, is %d" % [State.player_name(p), f.bandwidth[p], s.players[p].bandwidth] for line in GameMap.LINES: if f.firewalls.get(line.id, 0) != s.firewalls.get(line.id, 0): return "the Firewall on %s folds to %d, is %d" % [line.id, f.firewalls.get(line.id, 0), s.firewalls.get(line.id, 0)] for loc in GameMap.LOCATIONS: if f.owner[loc] != s.locations[loc].owner: return "%s's owner folds to %s, is %s" % [loc, f.owner[loc], s.locations[loc].owner] var zones := _zones(s) for id in f.zone: if f.zone[id] != zones.get(id, "?"): return "%s (%d) folds to %s, is in %s" % [s.cards[id].name, id, f.zone[id], zones.get(id, "?")] for p in 2: for slot in range(1, 9): var k := "%d:%d" % [p, slot] var av: Dictionary = s.players[p].anomalies[slot - 1] if f.at[k] != av.at: return "Anomaly %s folds to %s, stands on %s" % [k, f.at[k], av.at] if slot <= 4 and f.face[k] != av.faceUp: return "Anomaly %s folds to face %s, is %s" % [k, "up" if f.face[k] else "down", "up" if av.faceUp else "down"] var want := State.counters_of(av) if f.counters[k] != want: return "Anomaly %s's counters fold to %s, are %s" % [k, f.counters[k], want] if f.turn != s.turn or (not s.over and f.active != s.active): return "turn folds to %d (%d), is %d (%d)" % [f.turn, f.active, s.turn, s.active] return "" ## Each player's events: the same stream, with the other player's draws hidden. func _check_views(s: Dictionary) -> String: for p in 2: var mine := State.events_for(s, p) if mine.size() != s.events.size(): return "events_for(%d) has %d events of %d" % [p, mine.size(), s.events.size()] for i in mine.size(): var ev: Dictionary = mine[i] var real: Dictionary = s.events[i] if ev.k == "drew" and ev.p != p and ev.card != -1: return "events_for(%d) shows the other player's draw" % p if ev.k == "drew" and ev.p == p and ev.card != real.card: return "events_for(%d) hides its own draw" % p if not State.events_for(s, p, s.events.size()).is_empty(): return "events_for from the end isn't empty" return ""