manage deleted refered player, participant, objective, sector

This commit is contained in:
Maxime Réaux 2026-02-05 16:17:18 +01:00
parent 7afbb5ea1d
commit df846b8e4b
6 changed files with 322 additions and 80 deletions

View file

@ -2,6 +2,10 @@ from __future__ import annotations
from uuid import uuid4
from typing import Any, Dict, List
from warchron.model.exception import (
DeletionRequiresConfirmation,
UpdateRequiresConfirmation,
)
from warchron.model.campaign_participant import CampaignParticipant
from warchron.model.sector import Sector
from warchron.model.round import Round
@ -94,12 +98,47 @@ class Campaign:
part.set_theme(theme)
def remove_campaign_participant(self, participant_id: str) -> None:
# TODO manage choices referring to it
# TODO manage battles referring to it
del self.participants[participant_id]
rounds_blocking: list[Round] = []
for rnd in self.rounds:
if rnd.has_choice_with_participant(
participant_id
) or rnd.has_battle_with_participant(participant_id):
rounds_blocking.append(rnd)
if not rounds_blocking:
del self.participants[participant_id]
return
def cleanup() -> None:
for rnd in rounds_blocking:
rnd.clear_participant_references(participant_id)
rnd.remove_choice(participant_id)
del self.participants[participant_id]
rounds_str = ", ".join(
str(self.get_round_index(rnd.id)) for rnd in rounds_blocking
)
raise DeletionRequiresConfirmation(
message=(
f"This participant is used in round(s): {rounds_str}.\n"
"Related choices and battles will be cleared.\n"
"Do you want to continue?"
),
cleanup_action=cleanup,
)
# Sector methods
def has_sector(self, sector_id: str) -> bool:
return sector_id in self.sectors
def has_sector_with_objective(self, objective_id: str) -> bool:
return any(
sect.major_objective_id == objective_id
or sect.minor_objective_id == objective_id
or sect.influence_objective_id == objective_id
for sect in self.sectors.values()
)
def add_sector(
self, name: str, round_id: str, major_id: str, minor_id: str, influence_id: str
) -> Sector:
@ -130,16 +169,75 @@ class Campaign:
influence_id: str,
) -> None:
sect = self.get_sector(sector_id)
sect.set_name(name)
sect.set_round(round_id)
sect.set_major(major_id)
sect.set_minor(minor_id)
sect.set_influence(influence_id)
old_round_id = sect.round_id
def apply_update() -> None:
sect.set_name(name)
sect.set_round(round_id)
sect.set_major(major_id)
sect.set_minor(minor_id)
sect.set_influence(influence_id)
if old_round_id == round_id:
apply_update()
return
affected_rounds: list[Round] = []
for rnd in self.rounds:
if rnd.id == old_round_id and (
rnd.has_choice_with_sector(sector_id)
or rnd.has_battle_with_sector(sector_id)
):
affected_rounds.append(rnd)
if not affected_rounds:
apply_update()
return
def cleanup_and_update() -> None:
for rnd in affected_rounds:
rnd.clear_sector_references(sector_id)
rnd.remove_battle(sector_id)
apply_update()
rounds_str = ", ".join(
str(self.get_round_index(rnd.id)) for rnd in affected_rounds
)
raise UpdateRequiresConfirmation(
message=(
f"Changing the round of this sector will affect round(s): {rounds_str}."
"\nRelated battles and choices will be cleared.\n"
"Do you want to continue?"
),
apply_update=cleanup_and_update,
)
def remove_sector(self, sector_id: str) -> None:
# TODO manage choices referring to it
# TODO manage battles referring to it
del self.sectors[sector_id]
rounds_blocking: list[Round] = []
for rnd in self.rounds:
if rnd.has_battle_with_sector(sector_id) or rnd.has_choice_with_sector(
sector_id
):
rounds_blocking.append(rnd)
if not rounds_blocking:
del self.sectors[sector_id]
return
def cleanup() -> None:
for rnd in rounds_blocking:
rnd.clear_sector_references(sector_id)
rnd.remove_battle(sector_id)
del self.sectors[sector_id]
rounds_str = ", ".join(
str(self.get_round_index(rnd.id)) for rnd in rounds_blocking
)
raise DeletionRequiresConfirmation(
message=(
f"This sector is used in round(s): {rounds_str}.\n"
"Battles and choices using this sector will be cleared.\n"
"Do you want to continue?"
),
cleanup_action=cleanup,
)
def get_sectors_in_round(self, round_id: str) -> List[Sector]:
sectors = [s for s in self.sectors.values() if s.round_id == round_id]