mirror of
https://github.com/db9979/uconsole_asw_game.git
synced 2026-12-05 12:24:25 +01:00
The uConsole asks "Which unit do you play?" before every new game (frigate or hostile submarine, last choice preselected). The browser lobby first shows the unit, then its stations. A solo browser switches all its stations to the boat's six (and back) from the host bar. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
484 lines
16 KiB
Python
484 lines
16 KiB
Python
"""First delivery gate: exclusive input, safe dialogs and keyboard navigation."""
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pygame
|
|
import pytest
|
|
|
|
from src.core import config
|
|
from src.core.commands import MAP_STATIONS, event_feed_heading
|
|
from src.core.game import Game
|
|
from src.core.station import Station
|
|
|
|
|
|
def press(game, key, **attrs):
|
|
game.handle_event(pygame.event.Event(pygame.KEYDOWN, key=key, **attrs))
|
|
|
|
|
|
@pytest.fixture
|
|
def game():
|
|
return Game(seed=31415, start_menu=False)
|
|
|
|
|
|
@pytest.mark.parametrize("dialog", ["help", "nations", "save", "load", "quit"])
|
|
def test_administration_owns_input_while_real_time_keeps_running(game, dialog):
|
|
press(game, pygame.K_UP)
|
|
game._joy_turn = 1
|
|
game._map_drag = (100, 100)
|
|
game._open_administration(dialog)
|
|
before = game.sim_t, game.ship.target_speed, game.ship.target_course, game.station
|
|
for key in (pygame.K_a, pygame.K_t, pygame.K_PLUS, pygame.K_u, pygame.K_v):
|
|
press(game, key)
|
|
for event in (
|
|
pygame.event.Event(pygame.JOYAXISMOTION, axis=0, value=1.0),
|
|
pygame.event.Event(pygame.JOYBUTTONDOWN, button=0),
|
|
pygame.event.Event(pygame.MOUSEWHEEL, y=1, pos=(200, 200)),
|
|
pygame.event.Event(pygame.MOUSEBUTTONDOWN, button=1, pos=(200, 200)),
|
|
):
|
|
game.handle_event(event)
|
|
game.update(1.0)
|
|
# The overlay owns input, but the simulation never pauses behind it.
|
|
assert game.sim_t == pytest.approx(before[0] + 1.0)
|
|
assert (game.ship.target_speed, game.ship.target_course, game.station) == before[1:]
|
|
assert game.held == set()
|
|
assert game._joy_turn == 0
|
|
assert game._map_drag is None
|
|
game.draw()
|
|
press(game, pygame.K_ESCAPE)
|
|
assert not game.administration_open
|
|
|
|
|
|
@pytest.mark.parametrize("active_crew", [False, True])
|
|
@pytest.mark.parametrize("overlay", ["help", "options", "commander", "analyzer",
|
|
"nations", "save", "load", "quit"])
|
|
def test_every_overlay_keeps_simulation_and_crew_orders_live(game, overlay, active_crew):
|
|
game.commander.active_crew = active_crew
|
|
if overlay == "analyzer":
|
|
game._open_analyzer_in_game()
|
|
else:
|
|
game._open_administration(overlay)
|
|
before = game.sim_t
|
|
game.update(.25)
|
|
assert game.sim_t > before
|
|
assert game.order_course(123) == "ok"
|
|
|
|
|
|
def test_p_key_does_not_pause(game):
|
|
before = game.sim_t
|
|
press(game, pygame.K_p)
|
|
assert not hasattr(game, "paused")
|
|
game.update(.5)
|
|
assert game.sim_t == pytest.approx(before + .5)
|
|
|
|
|
|
def test_help_escape_does_not_open_quit(game):
|
|
press(game, pygame.K_F1)
|
|
press(game, pygame.K_ESCAPE)
|
|
assert not game.administration_open and not game.quit_confirm
|
|
|
|
|
|
def test_focus_loss_clears_controls_without_pausing(game):
|
|
press(game, pygame.K_UP)
|
|
game.handle_event(pygame.event.Event(pygame.WINDOWFOCUSLOST))
|
|
assert not game.held
|
|
assert game.steering_input() == (0, 0)
|
|
before = game.sim_t
|
|
game.update(.5)
|
|
assert game.sim_t == pytest.approx(before + .5)
|
|
|
|
|
|
@pytest.mark.parametrize("in_menu", [True, False])
|
|
def test_window_close_defaults_to_return_and_ignores_key_repeat(game, in_menu):
|
|
game.in_menu = in_menu
|
|
game.handle_event(pygame.event.Event(pygame.QUIT))
|
|
press(game, pygame.K_DOWN)
|
|
press(game, pygame.K_DOWN, repeat=True)
|
|
assert game.quit_selection == 1
|
|
game.handle_event(pygame.event.Event(pygame.QUIT))
|
|
assert game.running
|
|
press(game, pygame.K_ESCAPE)
|
|
game.handle_event(pygame.event.Event(pygame.QUIT))
|
|
press(game, pygame.K_RETURN)
|
|
assert game.running and not game.quit_confirm
|
|
|
|
|
|
def test_every_station_reachable_from_every_station(game):
|
|
for origin in Station:
|
|
for index, destination in enumerate(Station):
|
|
game.station = origin
|
|
press(game, pygame.K_1 + index)
|
|
assert game.station is destination
|
|
game.station = Station.BRIDGE
|
|
press(game, pygame.K_TAB, mod=pygame.KMOD_SHIFT)
|
|
assert game.station is Station.ELOKA
|
|
|
|
|
|
def test_active_station_number_cycles_only_its_real_pages(game):
|
|
game.station = Station.BRIDGE
|
|
game.sonar_page = 4
|
|
press(game, pygame.K_2)
|
|
assert game.station is Station.SONAR and game.sonar_page == 4
|
|
|
|
game.held.add(pygame.K_LEFT)
|
|
game.pinned_tooltip = {"title": "old", "lines": []}
|
|
press(game, pygame.K_2)
|
|
assert game.sonar_page == 5
|
|
assert not game.held and game.pinned_tooltip is None
|
|
press(game, pygame.K_2)
|
|
assert game.sonar_page == 0
|
|
|
|
press(game, pygame.K_1)
|
|
press(game, pygame.K_1)
|
|
assert game.station is Station.BRIDGE and game.sonar_page == 0
|
|
|
|
|
|
def test_numeric_entry_owns_station_digits(game):
|
|
game.station = Station.BRIDGE
|
|
press(game, pygame.K_u)
|
|
press(game, pygame.K_2)
|
|
assert game.station is Station.BRIDGE
|
|
assert game.input_mode == "course" and game.input_buffer == "2"
|
|
|
|
press(game, pygame.K_9)
|
|
assert game.station is Station.BRIDGE
|
|
assert game.input_buffer == "29"
|
|
|
|
|
|
def test_team_assignment_allows_teams_to_share_selected_room(game):
|
|
game.station = Station.DAMAGE
|
|
rooms = list(game.damage.compartments)
|
|
for room in rooms[:2]:
|
|
game.damage.compartments[room].state = "BESCHAEDIGT"
|
|
game.dmg_cursor = 1
|
|
press(game, pygame.K_RETURN)
|
|
assert game.damage.teams[1] == rooms[1]
|
|
press(game, pygame.K_DOWN)
|
|
press(game, pygame.K_RETURN)
|
|
assert game.damage.teams[1] == rooms[1]
|
|
assert game.damage.teams[2] == rooms[1]
|
|
press(game, pygame.K_LEFT)
|
|
press(game, pygame.K_RETURN)
|
|
assert game.damage.teams[2] == rooms[0]
|
|
press(game, pygame.K_BACKSPACE)
|
|
assert game.damage.teams[2] is None
|
|
assert game.ship.target_course == game.ship.course
|
|
|
|
|
|
def test_numeric_input_stays_live_and_invalid_value_remains_editable(game):
|
|
press(game, pygame.K_UP)
|
|
press(game, pygame.K_u)
|
|
assert not game.held
|
|
for key in (pygame.K_9, pygame.K_9, pygame.K_9, pygame.K_RETURN):
|
|
press(game, key)
|
|
assert game.input_mode == "course" and game.input_buffer == "999"
|
|
game.update(0.05)
|
|
assert game.sim_t > 0.0
|
|
for _ in range(3):
|
|
press(game, pygame.K_BACKSPACE)
|
|
press(game, pygame.K_KP9)
|
|
press(game, pygame.K_KP0)
|
|
press(game, pygame.K_KP_ENTER)
|
|
assert game.input_mode is None and game.ship.target_course == 90.0
|
|
|
|
|
|
def test_save_selection_and_overwrite_require_confirmation(game):
|
|
path = Path(config.SAVE_DIR) / "slot2.json"
|
|
press(game, pygame.K_s)
|
|
press(game, pygame.K_2)
|
|
assert not path.exists()
|
|
press(game, pygame.K_RETURN)
|
|
original = path.read_bytes()
|
|
game.score += 1
|
|
press(game, pygame.K_s)
|
|
press(game, pygame.K_2)
|
|
press(game, pygame.K_RETURN)
|
|
assert game.save_confirm and path.read_bytes() == original
|
|
press(game, pygame.K_RETURN)
|
|
assert json.loads(path.read_text())["score"] == game.score
|
|
|
|
|
|
def test_quit_save_failure_keeps_mission_and_original_file(game, monkeypatch):
|
|
game.save_to_slot(1)
|
|
path = Path(config.SAVE_DIR) / "slot1.json"
|
|
original = path.read_bytes()
|
|
|
|
def fail_replace(*args):
|
|
raise OSError("disk unavailable")
|
|
|
|
monkeypatch.setattr("src.core.game.os.replace", fail_replace)
|
|
press(game, pygame.K_ESCAPE)
|
|
press(game, pygame.K_DOWN)
|
|
press(game, pygame.K_RETURN)
|
|
press(game, pygame.K_1)
|
|
press(game, pygame.K_RETURN)
|
|
press(game, pygame.K_RETURN)
|
|
assert game.running and game.save_ui == "save"
|
|
assert path.read_bytes() == original
|
|
assert list(Path(config.SAVE_DIR).iterdir()) == [path]
|
|
|
|
|
|
def test_save_and_quit_only_after_success(game):
|
|
press(game, pygame.K_ESCAPE)
|
|
press(game, pygame.K_DOWN)
|
|
press(game, pygame.K_RETURN)
|
|
press(game, pygame.K_3)
|
|
assert game.running
|
|
press(game, pygame.K_RETURN)
|
|
assert not game.running
|
|
assert (Path(config.SAVE_DIR) / "slot3.json").exists()
|
|
|
|
|
|
def test_scenario_briefing_starts_selected_mission(game):
|
|
game.in_menu = True
|
|
press(game, pygame.K_3)
|
|
press(game, pygame.K_RETURN)
|
|
assert game.scenario_key == config.SCENARIO_ORDER[2]
|
|
press(game, pygame.K_RETURN)
|
|
assert not game.in_menu and game.scenario_key == config.SCENARIO_ORDER[2]
|
|
|
|
|
|
def test_random_scenario_accepts_default_difficulty(game):
|
|
game.in_menu = True
|
|
press(game, pygame.K_4)
|
|
press(game, pygame.K_RETURN)
|
|
assert 0 <= game.menu_sel < len(config.DIFFICULTY_FIELD_ORDER)
|
|
press(game, pygame.K_RETURN)
|
|
assert not game.in_menu and game.scenario_key == "s4_zufall"
|
|
|
|
|
|
def test_global_telegraph_reaches_astern_without_wrapping_at_flank(game):
|
|
game.station = Station.SONAR
|
|
for _ in range(10):
|
|
press(game, pygame.K_MINUS)
|
|
assert game.ship.telegraph == "ASTERN"
|
|
assert game.ship.target_speed == config.ASTERN_SPEED_KN
|
|
for _ in range(10):
|
|
press(game, pygame.K_PLUS)
|
|
assert game.ship.target_speed == config.SHIP_SPEED_MAX_KN
|
|
assert game.sonar.gain_db == 0.0
|
|
|
|
|
|
def test_save_draw_does_not_read_disk(game, monkeypatch):
|
|
press(game, pygame.K_s)
|
|
|
|
def unexpected_open(*args, **kwargs):
|
|
pytest.fail("Rendering must use cached metadata")
|
|
|
|
monkeypatch.setattr("builtins.open", unexpected_open)
|
|
game.draw_save_ui()
|
|
|
|
|
|
@pytest.mark.parametrize("dialog,draw_name", [
|
|
("help", "draw_help_overlay"), ("nations", "draw_nations_overlay"),
|
|
("save", "draw_save_ui"), ("quit", "draw_quit_overlay"),
|
|
])
|
|
def test_game_over_dialog_renders_above_end_panel(game, monkeypatch, dialog, draw_name):
|
|
order = []
|
|
game._end_mission(False, "Test")
|
|
game._open_administration(dialog)
|
|
monkeypatch.setattr(game, "draw_end_panel", lambda: order.append("end"))
|
|
monkeypatch.setattr(game, draw_name, lambda: order.append("dialog"))
|
|
game.draw()
|
|
assert order == ["end", "dialog"]
|
|
|
|
|
|
@pytest.mark.parametrize("screen", ["scenario", "difficulty", "briefing"])
|
|
def test_help_accessible_and_visible_in_menu(game, monkeypatch, screen):
|
|
game.in_menu = True
|
|
game.menu_screen = screen
|
|
rendered = []
|
|
monkeypatch.setattr(game, "draw_help_overlay", lambda: rendered.append(True))
|
|
press(game, pygame.K_F1)
|
|
game.draw()
|
|
assert rendered and game.help_open
|
|
press(game, pygame.K_ESCAPE)
|
|
assert game.in_menu and not game.administration_open
|
|
|
|
|
|
def test_quit_from_menu_never_saves_unstarted_scenario(game):
|
|
game.in_menu = True
|
|
press(game, pygame.K_3)
|
|
press(game, pygame.K_RETURN)
|
|
game.handle_event(pygame.event.Event(pygame.QUIT))
|
|
press(game, pygame.K_DOWN)
|
|
press(game, pygame.K_RETURN)
|
|
assert not game.running
|
|
assert not list(Path(config.SAVE_DIR).glob("*.json"))
|
|
|
|
|
|
def test_end_mission_cancels_live_entry_and_allows_restart(game):
|
|
press(game, pygame.K_u)
|
|
game._map_drag = (100, 100)
|
|
game._end_mission(False, "Test")
|
|
assert game.input_mode is None and game._map_drag is None
|
|
press(game, pygame.K_r)
|
|
assert not game.game_over
|
|
game.handle_event(pygame.event.Event(pygame.MOUSEMOTION, pos=(200, 200)))
|
|
assert game._map_drag is None
|
|
|
|
|
|
def test_load_from_slot_keeps_camera_and_station(game):
|
|
game.station = Station.RADIO
|
|
game.map_view.cx, game.map_view.cy = 123.0, 234.0
|
|
game.map_view.scale = 4.0
|
|
game.map_follow = False
|
|
game.save_to_slot(1)
|
|
game.reset(game.seed)
|
|
assert game.load_from_slot(1)
|
|
assert game.station is Station.RADIO
|
|
assert (game.map_view.cx, game.map_view.cy, game.map_view.scale) == (123.0, 234.0, 4.0)
|
|
assert not game.map_follow
|
|
|
|
|
|
@pytest.mark.parametrize("context", ["menu", "course", "quit"])
|
|
def test_alt_enter_never_confirms_current_action(game, monkeypatch, context):
|
|
toggles = []
|
|
monkeypatch.setattr(game, "toggle_fullscreen", lambda: toggles.append(True))
|
|
if context == "menu":
|
|
game.in_menu = True
|
|
elif context == "course":
|
|
press(game, pygame.K_u)
|
|
game.input_buffer = "180"
|
|
else:
|
|
press(game, pygame.K_ESCAPE)
|
|
game.quit_selection = 2
|
|
press(game, pygame.K_RETURN, mod=pygame.KMOD_ALT)
|
|
assert toggles and game.running
|
|
if context == "menu":
|
|
assert game.menu_screen == "scenario"
|
|
elif context == "course":
|
|
assert game.input_mode == "course"
|
|
else:
|
|
assert game.quit_confirm
|
|
|
|
|
|
def test_escape_quits_but_q_is_station_scoped_map_zoom(game):
|
|
game.station = Station.BRIDGE
|
|
before = game.map_view.scale
|
|
press(game, pygame.K_q)
|
|
assert not game.quit_confirm
|
|
assert game.map_view.scale < before
|
|
press(game, pygame.K_e)
|
|
assert game.map_view.scale == pytest.approx(before)
|
|
press(game, pygame.K_ESCAPE)
|
|
assert game.quit_confirm
|
|
|
|
|
|
@pytest.mark.parametrize("station", list(Station))
|
|
def test_map_keyboard_controls_are_limited_to_visible_map_stations(game, station):
|
|
game.station = station
|
|
scale, follow = game.map_view.scale, game.map_follow
|
|
press(game, pygame.K_q)
|
|
press(game, pygame.K_k)
|
|
if station in MAP_STATIONS:
|
|
assert game.map_view.scale < scale
|
|
assert game.map_follow is not follow
|
|
else:
|
|
assert game.map_view.scale == scale
|
|
assert game.map_follow is follow
|
|
|
|
|
|
def test_event_feed_does_not_advertise_station_commands():
|
|
for station in Station:
|
|
assert event_feed_heading(station) == "EREIGNIS-FEED"
|
|
|
|
|
|
def test_stale_held_depth_controls_never_steer_outside_bridge(game):
|
|
game.station = Station.WEAPONS
|
|
game.held.update((pygame.K_UP, pygame.K_DOWN))
|
|
game._joy_turn = 1
|
|
before = game.ship.target_course
|
|
assert game.steering_input() == (0, 0)
|
|
game.update(0.1)
|
|
assert game.ship.target_course == before
|
|
|
|
|
|
def test_quit_dialog_returns_to_main_menu_without_saving(game):
|
|
game.update(1.0)
|
|
press(game, pygame.K_ESCAPE)
|
|
press(game, pygame.K_DOWN)
|
|
press(game, pygame.K_DOWN)
|
|
press(game, pygame.K_RETURN)
|
|
assert game.running and game.in_menu and game.main_menu
|
|
assert not game.administration_open
|
|
assert not list(Path(config.SAVE_DIR).glob("*.json"))
|
|
sim_t = game.sim_t
|
|
game.update(1.0)
|
|
assert game.sim_t == sim_t
|
|
# "New game" -> unit (frigate) -> scenario list -> briefing starts a fresh mission.
|
|
press(game, pygame.K_RETURN)
|
|
assert game.menu_screen == "side"
|
|
press(game, pygame.K_RETURN)
|
|
press(game, pygame.K_2)
|
|
press(game, pygame.K_RETURN)
|
|
press(game, pygame.K_RETURN)
|
|
assert not game.in_menu and game.scenario_key == config.SCENARIO_ORDER[1]
|
|
assert game.sim_t == 0.0 and not game.game_over
|
|
|
|
|
|
def test_quit_dialog_last_entry_still_exits_without_saving(game):
|
|
press(game, pygame.K_ESCAPE)
|
|
for _ in range(3):
|
|
press(game, pygame.K_DOWN)
|
|
press(game, pygame.K_RETURN)
|
|
assert not game.running
|
|
assert not list(Path(config.SAVE_DIR).glob("*.json"))
|
|
|
|
|
|
def test_scenario_list_escape_goes_back_to_main_menu(game):
|
|
game.in_menu = True
|
|
game.main_menu = False
|
|
game.menu_screen = "scenario"
|
|
press(game, pygame.K_ESCAPE)
|
|
assert game.main_menu and not game.quit_confirm
|
|
|
|
|
|
def test_mission_end_offers_main_menu(game):
|
|
game._end_mission(False, "Test")
|
|
press(game, pygame.K_m)
|
|
assert game.in_menu and game.main_menu and game.running
|
|
|
|
|
|
def test_restart_after_custom_mission_restarts_that_mission(game):
|
|
from src.core.mission_definition import default_mission
|
|
|
|
definition = default_mission("user.restart")
|
|
definition["name"] = "Restart probe"
|
|
definition["units"]["exact"] = [dict(
|
|
id="target", profile="diesel_alt", side="hostile",
|
|
placement=dict(kind="fixed", x=100, y=100),
|
|
course_deg=10, speed_kn=4, depth_m=60)]
|
|
definition["objective"]["target_ids"] = ["target"]
|
|
assert game.start_custom_mission(definition)
|
|
game._end_mission(False, "Test")
|
|
press(game, pygame.K_r)
|
|
assert not game.game_over
|
|
assert game.custom_mission_definition["name"] == "Restart probe"
|
|
assert game.mission.name == "Restart probe" and len(game.subs) == 1
|
|
|
|
|
|
def test_defeat_with_time_left_reports_remaining_time(game, monkeypatch):
|
|
shown = []
|
|
monkeypatch.setattr("src.core.game.layout.blit_line",
|
|
lambda surface, text, *args, **kwargs: shown.append(text))
|
|
game.mission_time = 60.0
|
|
game._end_mission(False, "Test")
|
|
game.draw_end_panel()
|
|
assert "end.expired" not in shown
|
|
shown.clear()
|
|
game.mission_time = game.mission.time_limit_s
|
|
game.draw_end_panel()
|
|
assert "end.expired" in shown
|
|
|
|
|
|
def test_survive_mission_announces_time_as_progress(game):
|
|
game.mission.win_mode = "survive"
|
|
game.mission_time = game.mission.time_limit_s - 290.0
|
|
game._mission_time_warning()
|
|
assert game.msg["__u_jagd_i18n__"] == "runtime.survive.warning_minutes"
|
|
game.mission.win_mode = "sink"
|
|
game._mission_warnings.clear()
|
|
game._mission_time_warning()
|
|
assert game.msg["__u_jagd_i18n__"] == "runtime.deadline.warning_minutes"
|