extends TestCase ## Issue A9: the in-game HUD, the FEED message that tells it who is standing and who ## fell, and HudState, which works out what it shows. const FRAME: float = 1.0 / 60.0 func _rules() -> MatchRules: var rules: MatchRules = MatchRules.new() rules.max_players = 4 rules.min_players = 1 rules.lobby_ticks = 6 rules.countdown_ticks = 6 rules.results_ticks = 6 return rules func test_feed_round_trips() -> void: var entries: Array[Dictionary] = [ {"victim": -3, "by": 1, "cause": "weapon", "item": 2, "place": 4, "tied": false, "victim_name": "Thole", "by_name": "Ada"}, {"victim": 1, "by": 0, "cause": "tide", "item": -1, "place": 3, "tied": true, "victim_name": "Ada", "by_name": ""}, ] var told: Dictionary = NetFeed.decode(NetFeed.encode(3, 24, entries)) assert_eq(told["standing"], 3) assert_eq(told["total"], 24) assert_eq(told["entries"].size(), 2) for index: int in 2: for key: String in entries[index]: assert_eq(told["entries"][index][key], entries[index][key], "%s of entry %d" % [key, index]) func test_feed_keeps_only_the_newest_entries_and_cuts_long_names_on_characters() -> void: var entries: Array[Dictionary] = [] for index: int in NetFeed.MAX_ENTRIES + 5: entries.append({"victim": -index - 1, "cause": "drown", "place": 24 - index, "victim_name": "ÆØÅ".repeat(20)}) var told: Dictionary = NetFeed.decode(NetFeed.encode(1, 24, entries)) assert_eq(told["entries"].size(), NetFeed.MAX_ENTRIES) assert_eq(told["entries"][NetFeed.MAX_ENTRIES - 1]["victim"], -(NetFeed.MAX_ENTRIES + 5), "the newest is last") var name: String = told["entries"][0]["victim_name"] assert_true(name.to_utf8_buffer().size() <= NetFeed.MAX_NAME_BYTES) assert_true(name.begins_with("ÆØÅ") and name.length() == NetFeed.MAX_NAME_BYTES / 2, "cut between characters: %s" % name) func test_malformed_feed_is_refused() -> void: var good: PackedByteArray = NetFeed.encode(2, 4, [{"victim": -1, "cause": "fall", "victim_name": "Kelp"}] as Array[Dictionary]) assert_false(NetFeed.decode(good).is_empty()) for cut: int in good.size(): assert_true(NetFeed.decode(good.slice(0, cut)).is_empty(), "cut at %d" % cut) var longer: PackedByteArray = good.duplicate() longer.append(0) assert_true(NetFeed.decode(longer).is_empty(), "trailing bytes") var bad_cause: PackedByteArray = good.duplicate() bad_cause[NetFeed.HEADER_BYTES + 8] = NetFeed.CAUSES.size() assert_true(NetFeed.decode(bad_cause).is_empty(), "unknown cause") var many: PackedByteArray = good.duplicate() many[3] = NetFeed.MAX_ENTRIES + 1 assert_true(NetFeed.decode(many).is_empty(), "too many entries") func test_feed_lines_are_words_and_name_you() -> void: var flint: int = ItemCatalog.code_of(String(Tuning.WEAPONS.keys()[1])) var entries: Array[Dictionary] = [ {"victim": -2, "by": 1, "cause": "weapon", "item": flint, "victim_name": "Skerry", "by_name": "Ada"}, {"victim": 1, "by": -4, "cause": "weapon", "item": -1, "victim_name": "Ada", "by_name": "Scupper"}, {"victim": -5, "by": 0, "cause": "drown", "victim_name": "Cockle"}, {"victim": -6, "by": 0, "cause": "tide", "victim_name": "Fathom"}, {"victim": -7, "by": 0, "cause": "left", "victim_name": "Lugworm"}, {"victim": -8, "by": 0, "cause": "fall", "victim_name": "Pollack"}, ] var lines: Array[Dictionary] = HudState.feed_lines(entries, 1) assert_eq(lines.size(), HudState.FEED_LINES, "only the newest") assert_eq(lines[0]["text"], "Scupper took you") assert_true(lines[0]["you"]) assert_eq(lines[1]["text"], "Cockle drowned") assert_eq(lines[2]["text"], "The tide took Fathom") assert_eq(lines[3]["text"], "Lugworm left") assert_eq(lines[4]["text"], "Pollack fell") assert_false(lines[4]["you"]) var first: Array[Dictionary] = HudState.feed_lines(entries.slice(0, 1), 1) assert_eq(first[0]["text"], "You took Skerry with %s" % HudState.item_name(ItemCatalog.get_def(flint).id).to_lower()) func test_crew_names_are_stable_and_distinct() -> void: assert_eq(CrewNames.for_bot(-1), CrewNames.NAMES[0]) var seen: Dictionary = {} for handle: int in range(-1, -49, -1): var name: String = CrewNames.for_bot(handle) assert_false(seen.has(name), "%s used twice" % name) seen[name] = true func test_offline_client_is_told_who_stands_and_who_fell() -> void: var game: ClientGame = ClientGame.new() var local: LocalServer = LocalServer.new(5, 3, _rules()) for _tick: int in 200: local.tick(game) if game.playing(): break assert_true(game.playing(), "the match began") assert_eq(game.match_size, 4) assert_eq(game.standing, 4) var flow: Match = local.loop.match_flow var bot: int = flow.bot_handles.keys()[0] var bot_player: SimEntity = flow.sim.entities.find(flow.entity_of[bot]) bot_player.health = 0 bot_player.last_cause = SimEntity.CAUSE_DROWN local.tick(game) assert_eq(game.standing, 3, "told at once") assert_eq(game.feed.size(), 1) assert_eq(game.feed[0]["victim"], bot) assert_eq(game.feed[0]["victim_name"], CrewNames.for_bot(bot)) assert_eq(game.feed[0]["cause"], "drown") assert_eq(game.bad_packets, 0) var state: HudState = HudState.from_client(game, ClientInput.new(), LocalServer.HANDLE) assert_eq(state.feed[0]["text"], "%s drowned" % CrewNames.for_bot(bot)) assert_true(state.playing) assert_eq(state.health, Tuning.PLAYER_MAX_HEALTH) assert_eq(state.slots.size(), Tuning.INVENTORY_SLOTS + 1, "the boathook and the slots") assert_true(state.slots[0]["selected"], "the boathook is held at first") assert_false(state.tide.is_empty()) assert_eq(state.tide["phase"], 1) assert_true(state.tide["next"] >= state.tide["sea"]) func test_from_match_reads_the_server_player_and_build_mode() -> void: var loop: ServerLoop = ServerLoop.new(5, 1, 3, _rules()) loop.play_humans_by_script() while not loop.match_flow.in_match(): loop.run_tick() for _tick: int in 30: loop.run_tick() var flow: Match = loop.match_flow var handle: int = flow.participants.filter(func(h: int) -> bool: return h > 0)[0] var me: SimEntity = flow.sim.entities.find(flow.entity_of[handle]) me.materials[Materials.STONE] = 5 var state: HudState = HudState.from_match(flow, handle, true, StructureSet.WALL, Materials.STONE) assert_true(state.playing) assert_eq(state.build["kind"], "wall") assert_eq(state.build["material"], "stone") assert_false(state.build["affordable"], "5 stone isn't a wall") me.materials[Materials.STONE] = 50 assert_true(HudState.from_match(flow, handle, true, StructureSet.WALL, Materials.STONE).build["affordable"]) assert_true(HudState.from_match(flow, handle).build.is_empty(), "not building") assert_eq(state.standing, 4) assert_false(is_nan(float(state.tide["feet"]))) func test_hud_shows_only_what_the_state_holds() -> void: var hud: Hud = Hud.new() var empty: HudState = HudState.new() hud.show_state(empty) assert_eq(_texts(hud), [] as Array[String], "nothing to show before a match") var state: HudState = HudState.new() state.playing = true state.health = 20 state.armor = 50 state.standing = 7 state.total = 24 state.tide = {"phase": 2, "phases": 6, "stage": Tide.HOLD, "sea": 3.0, "next": 6.5, "rise_in": 42.0, "feet": 4.0} state.slots = [{"selected": true, "empty": false, "name": "Boathook", "count": 1, "rounds": -1, "reserve": -1, "tier": -1}] as Array[Dictionary] state.feed = [{"text": "Kelp drowned", "you": false}] as Array[Dictionary] hud.show_state(state) var texts: Array[String] = _texts(hud) assert_true(texts.has("7 of 24 standing"), "%s" % [texts]) assert_true(texts.has("Kelp drowned")) assert_true(texts.has("Driftwood 0")) hud.show_state(empty) texts = _texts(hud) assert_false(texts.has("Kelp drowned"), "an empty feed clears the lines") hud.free() func test_session_shows_a_hud_while_playing_offline() -> void: var session: ClientSession = ClientSession.new() session.input.autoplay = true session._ready() session.play_offline(2, 3, _rules()) for _frame: int in 60 * 20: session.pump(FRAME) if session.game.playing(): break assert_true(session.game.playing()) var state: HudState = session.hud_state() assert_true(state.playing) assert_eq(state.total, 4) session.hud.show_state(state) assert_true(_texts(session.hud).has("4 of 4 standing")) session.free() ## The text of every label shown: visible itself and in every parent up to the HUD. func _texts(node: Node) -> Array[String]: var found: Array[String] = [] var item: CanvasItem = node as CanvasItem if item != null and not item.visible: return found var label: Label = node as Label if label != null: found.append(label.text) for child: Node in node.get_children(): found.append_array(_texts(child)) return found