mirror of
https://github.com/db9979/uconsole_asw_game.git
synced 2026-12-06 21:04:18 +01:00
- Daily mission: always the short variant of a scenario that has one; only a short daily run files its best. The page names its minutes. - The mission length last chosen (start menu, lobby, browser) is kept in the settings; a first launch without settings defaults to short. - Training moves second in the main menu, ticks finished lessons (kept in the settings, never in a save), opens on the next one with a progress line, and N on the end panel starts the next lesson. Three new frigate lessons: air defence (an ESSM against a sea-skimmer), ESM (name a merchant's radar) and torpedo defence with the Nixie at HALF or FULL. - End panel of both sides: a line saying what decided the mission, read from the finished debrief recording (src/core/decisive.py); none in a lesson. The browser debrief shows the same line. - Logbook: one ribbon per scenario of the side, coloured once won without the advisor or the experimental opponent; the browser logbook shows the rack and the training dialog the ticks and the next lesson. - help.py, manual (EN/DE), shortcuts PDF and manual export updated. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
263 lines
11 KiB
Python
263 lines
11 KiB
Python
"""Guided training lessons and the campaign (its own file, never in a save)."""
|
|
|
|
import copy
|
|
import json
|
|
import os
|
|
|
|
import pygame
|
|
import pytest
|
|
|
|
from src.core import campaign as campaign_model, config, training
|
|
from src.core.campaign import CampaignState
|
|
from src.core.game import Game
|
|
from src.core.mission_definition import validate_mission
|
|
from src.core.station import Station
|
|
from src.sonar.sonar import Contact
|
|
|
|
|
|
def _menu_game(seed=11):
|
|
return Game(seed=seed, start_menu=True, audio_enabled=False)
|
|
|
|
|
|
def _key(game, key, mod=0):
|
|
game.handle_event(pygame.event.Event(pygame.KEYDOWN, key=key, mod=mod, unicode=""))
|
|
|
|
|
|
@pytest.mark.parametrize("lesson", training.LESSONS)
|
|
def test_every_lesson_is_a_valid_mission_and_starts(lesson):
|
|
definition = training.lesson_definition(lesson, 11)
|
|
assert validate_mission(definition) == []
|
|
game = _menu_game()
|
|
assert game.start_training(lesson)
|
|
assert game.training.lesson == lesson and game.training.step == 0
|
|
if lesson in ("air", "esm", "torpedo"):
|
|
assert not game.subs # no submarine in these lessons
|
|
else:
|
|
hostile = lesson == "attack" or lesson in training.BOAT_LESSONS
|
|
assert game.subs[0].side == ("hostile" if hostile else "neutral")
|
|
assert game.local_side == training.side_of(lesson)
|
|
assert (game.opfor is not None) is (lesson in training.BOAT_LESSONS)
|
|
game.draw()
|
|
|
|
|
|
def test_sonar_lesson_steps_through_and_ends_as_won(monkeypatch):
|
|
game = _menu_game()
|
|
game.start_training("sonar")
|
|
coach = game.training
|
|
game._update_training()
|
|
assert coach.step == 0
|
|
game.station = Station.SONAR
|
|
contact = Contact(1, game.subs[0].id, "passiv", "sub")
|
|
monkeypatch.setattr(game.sonar, "active_contacts", lambda: [contact])
|
|
game._update_training()
|
|
assert coach.step == 2 # station and contact passed
|
|
game.sonar.focus_locked = True
|
|
contact.player_class = "U_BOOT"
|
|
game._update_training()
|
|
assert coach.done and game.game_over and game.mission_result == "SIEG"
|
|
|
|
|
|
def test_a_lost_attack_lesson_never_reports_the_hit_step_done():
|
|
game = _menu_game()
|
|
game.start_training("attack")
|
|
coach = game.training
|
|
coach.step = len(coach.steps) - 1 # waiting for the hit
|
|
game._end_mission(False, "test")
|
|
game._update_training()
|
|
assert coach.step == len(coach.steps) - 1 and not coach.done
|
|
assert not any("training.step_done" in json.dumps(entry.text)
|
|
for entry in game.feed.entries)
|
|
|
|
|
|
def test_a_loaded_lesson_gets_its_coach_back_and_r_restarts_it():
|
|
game = _menu_game()
|
|
game.start_training("tma")
|
|
for _ in range(10):
|
|
game.update(0.5)
|
|
document = json.loads(json.dumps(game.save_state()))
|
|
other = _menu_game()
|
|
assert other._load_save_data(document)
|
|
assert other.training is not None and other.training.lesson == "tma"
|
|
other._end_mission(False, "test")
|
|
_key(other, pygame.K_r)
|
|
assert other.training.lesson == "tma" and not other.game_over
|
|
|
|
|
|
def test_training_menu_starts_the_chosen_lesson():
|
|
game = _menu_game()
|
|
while game.main_menu_entries()[game.main_menu_sel] != "training":
|
|
_key(game, pygame.K_DOWN)
|
|
_key(game, pygame.K_RETURN)
|
|
assert game.menu_screen == "training"
|
|
_key(game, pygame.K_3)
|
|
game.draw()
|
|
_key(game, pygame.K_RETURN)
|
|
assert game.training.lesson == "attack" and not game.in_menu
|
|
|
|
|
|
def _first(state):
|
|
return state.theatre.ordered()[0]["id"]
|
|
|
|
|
|
def test_campaign_results_standing_and_port():
|
|
state = CampaignState(1234)
|
|
assert state.mission_seed() % 128 == 1234 % 128
|
|
assert state.record(_first(state), won=True, ship_sunk=False, score=900,
|
|
torpedoes_left=3, damaged=["engine", "nowhere"], helo_lost=True,
|
|
incident=False, tasks_done=1, tasks_failed=0)
|
|
assert state.port and state.reputation == 50 + 15 + 3
|
|
assert state.damaged == ["engine"] and state.torpedoes == 3
|
|
assert not state.can_sail() and state.missions == 1
|
|
assert state.mission_seed() % 128 == 1234 % 128
|
|
quick = copy.deepcopy(state)
|
|
assert quick.call_at_port("quick") and quick.can_sail()
|
|
assert quick.damaged == ["engine"] and quick.helo_lost
|
|
assert quick.torpedoes == 3 + campaign_model.resupply(68) // 2
|
|
assert state.call_at_port("refit") and state.torpedoes == campaign_model.resupply(68)
|
|
assert state.damaged == [] and not state.helo_lost and state.reputation == 63
|
|
assert CampaignState.restore(state.serialize()).serialize() == state.serialize()
|
|
for key, value in (("leg", 1), ("reputation", 101), ("damaged", ["hull"]),
|
|
("status", "draw"), ("history", []), ("version", 1)):
|
|
broken = dict(state.serialize(), **{key: value})
|
|
assert not CampaignState.valid_state(broken)
|
|
sunk = CampaignState(5)
|
|
sunk.record(_first(sunk), won=False, ship_sunk=True, score=0, torpedoes_left=0,
|
|
damaged=[], helo_lost=False, incident=False, tasks_done=0, tasks_failed=0)
|
|
assert sunk.status == "lost" and not sunk.port and sunk.outcome == "sunk"
|
|
|
|
|
|
def test_campaign_file_is_atomic_and_rejects_symlinks(tmp_path, monkeypatch):
|
|
monkeypatch.setattr(config, "SAVE_DIR", str(tmp_path))
|
|
assert campaign_model.load_campaign() is None
|
|
state = CampaignState(77)
|
|
assert campaign_model.save_campaign(state)
|
|
assert campaign_model.load_campaign().serialize() == state.serialize()
|
|
path = tmp_path / campaign_model.FILE_NAME
|
|
path.write_text('{"version": NaN}')
|
|
assert campaign_model.load_campaign() is None
|
|
path.unlink()
|
|
target = tmp_path / "elsewhere.json"
|
|
target.write_text(json.dumps(state.serialize()))
|
|
os.symlink(target, path)
|
|
assert campaign_model.load_campaign() is None
|
|
|
|
|
|
def test_a_campaign_leg_carries_stock_damage_and_helicopter(tmp_path, monkeypatch):
|
|
monkeypatch.setattr(config, "SAVE_DIR", str(tmp_path))
|
|
game = _menu_game(seed=300)
|
|
assert game.new_campaign()
|
|
state = game.campaign
|
|
state.torpedoes, state.damaged, state.helo_lost = 3, ["sonar"], True
|
|
spot = state.theatre.ordered()[0]
|
|
assert game.start_campaign_leg(spot["id"])
|
|
assert game.scenario_key == spot["scenario"]
|
|
assert game.torpedo_count == 3 and game.difficulty["torpedo_count"] == 3
|
|
assert game.damage.compartments["sonar"].state == "BESCHAEDIGT"
|
|
assert game.helo.state == "VERLOREN" and game.campaign_mission
|
|
document = json.loads(json.dumps(game.save_state()))
|
|
other = _menu_game()
|
|
assert other._load_save_data(document) # a plain mission save
|
|
assert not other.campaign_mission
|
|
game._end_mission(True, "test")
|
|
saved = campaign_model.load_campaign()
|
|
assert saved.port and saved.history[0] == dict(scenario=spot["scenario"],
|
|
result="won", score=game.score)
|
|
assert saved.theatre.hotspot(spot["id"]) is None and saved.theatre.lage > 50
|
|
game.draw()
|
|
|
|
|
|
def test_campaign_menu_new_port_and_sail(tmp_path, monkeypatch):
|
|
monkeypatch.setattr(config, "SAVE_DIR", str(tmp_path))
|
|
game = _menu_game(seed=400)
|
|
game.main_menu_sel = game.main_menu_index("campaign")
|
|
_key(game, pygame.K_RETURN)
|
|
assert game.menu_screen == "campaign"
|
|
game.draw()
|
|
_key(game, pygame.K_n)
|
|
assert game.campaign is not None
|
|
_key(game, pygame.K_n) # asks before replacing
|
|
assert game._campaign_confirm_new
|
|
game.draw()
|
|
_key(game, pygame.K_ESCAPE)
|
|
game.main_menu_sel = game.main_menu_index("campaign")
|
|
_key(game, pygame.K_RETURN)
|
|
_key(game, pygame.K_DOWN) # the second hotspot
|
|
second = game.campaign.theatre.ordered()[1]
|
|
_key(game, pygame.K_RETURN) # its briefing
|
|
assert game._campaign_briefing == second["id"] and not game.campaign_mission
|
|
game.draw()
|
|
_key(game, pygame.K_ESCAPE) # back to the map
|
|
assert game._campaign_briefing is None and game.menu_screen == "campaign"
|
|
_key(game, pygame.K_RETURN)
|
|
_key(game, pygame.K_RETURN) # sail
|
|
assert game.campaign_mission and game.scenario_key == second["scenario"]
|
|
game._end_mission(False, "test")
|
|
_key(game, pygame.K_m)
|
|
game.main_menu_sel = game.main_menu_index("campaign")
|
|
_key(game, pygame.K_RETURN)
|
|
game.draw()
|
|
_key(game, pygame.K_RETURN) # the port call comes first
|
|
assert game._campaign_briefing is None
|
|
_key(game, pygame.K_2)
|
|
assert game.campaign.missions == 1 and not game.campaign.port
|
|
|
|
|
|
def test_lesson_order_helpers():
|
|
assert training.next_lesson(()) == training.LESSONS[0]
|
|
assert training.next_lesson(training.LESSONS[:3]) == training.LESSONS[3]
|
|
assert training.next_lesson(training.LESSONS) is None
|
|
assert training.following(training.LESSONS[0]) == training.LESSONS[1]
|
|
assert training.following(training.LESSONS[-1]) is None
|
|
assert training.valid_done(["air", "nope", "sonar", "air"]) == ("sonar", "air")
|
|
assert training.valid_done("sonar") == ()
|
|
assert {"air", "esm", "torpedo"} <= set(training.FRIGATE_LESSONS)
|
|
|
|
|
|
def test_training_page_opens_on_the_next_lesson_and_marks_the_done_ones():
|
|
game = _menu_game()
|
|
game._set_preference("lessons_done", ("sonar", "tma"))
|
|
game.open_training_menu()
|
|
assert game.menu_screen == "training"
|
|
assert training.LESSONS[game.menu_sel] == "attack"
|
|
game.draw()
|
|
|
|
|
|
def test_a_finished_lesson_is_remembered_and_n_starts_the_next(monkeypatch):
|
|
game = _menu_game()
|
|
game.start_training("sonar")
|
|
coach = game.training
|
|
game.station = Station.SONAR
|
|
contact = Contact(1, game.subs[0].id, "passiv", "sub")
|
|
monkeypatch.setattr(game.sonar, "active_contacts", lambda: [contact])
|
|
game._update_training()
|
|
game.sonar.focus_locked = True
|
|
contact.player_class = "U_BOOT"
|
|
game._update_training()
|
|
assert coach.done and game.game_over
|
|
assert "sonar" in game.lessons_done()
|
|
keys = game.end_keys((("R", "a"), ("M", "b"), ("Q", "c")))
|
|
assert ("N", "end.key.next_lesson") in keys
|
|
game.draw()
|
|
_key(game, pygame.K_n)
|
|
assert game.training.lesson == training.following("sonar") and not game.game_over
|
|
|
|
|
|
def test_the_air_lesson_launches_a_missile_at_the_ship():
|
|
game = _menu_game()
|
|
game.start_training("air")
|
|
assert not training.missiles_alive(game)
|
|
for _ in range(int(training.MISSILE_FIRST_S / 0.5) + 4):
|
|
game.update(0.5)
|
|
assert training.missiles_alive(game)
|
|
assert game.training.missile_alive
|
|
|
|
|
|
def test_a_hit_in_the_torpedo_lesson_ends_it_as_lost():
|
|
game = _menu_game()
|
|
game.start_training("torpedo")
|
|
assert game.enemy_torpedoes
|
|
game.damage.total = 10.0
|
|
game._update_training()
|
|
assert game.game_over and game.mission_result != "SIEG"
|
|
assert "torpedo" not in game.lessons_done()
|