split controller
This commit is contained in:
parent
701f6b3292
commit
7792a76f8e
11 changed files with 1212 additions and 987 deletions
246
src/warchron/controller/app_controller.py
Normal file
246
src/warchron/controller/app_controller.py
Normal file
|
|
@ -0,0 +1,246 @@
|
|||
from pathlib import Path
|
||||
|
||||
from PyQt6.QtWidgets import QMessageBox
|
||||
|
||||
from warchron.model.model import Model
|
||||
from warchron.model.exception import (
|
||||
DeletionForbidden,
|
||||
DeletionRequiresConfirmation,
|
||||
UpdateRequiresConfirmation,
|
||||
)
|
||||
from warchron.view.view import View
|
||||
from warchron.constants import ItemType, RefreshScope
|
||||
from warchron.controller.navigation_controller import NavigationController
|
||||
from warchron.controller.player_controller import PlayerController
|
||||
from warchron.controller.war_controller import WarController
|
||||
from warchron.controller.campaign_controller import CampaignController
|
||||
from warchron.controller.round_controller import RoundController
|
||||
|
||||
|
||||
class AppController:
|
||||
def __init__(self, model: Model, view: View) -> None:
|
||||
self.model: Model = model
|
||||
self.view: View = view
|
||||
self.navigation = NavigationController(self)
|
||||
self.players = PlayerController(self)
|
||||
self.wars = WarController(self)
|
||||
self.campaigns = CampaignController(self)
|
||||
self.rounds = RoundController(self)
|
||||
self.current_file: Path | None = None
|
||||
self.view.on_close_callback = self.on_app_close
|
||||
self.is_dirty: bool = False
|
||||
self.__connect()
|
||||
self.navigation.refresh_players_view()
|
||||
self.navigation.refresh_wars_view()
|
||||
self.update_window_title()
|
||||
self.view.on_tree_selection_changed = self.navigation.on_tree_selection_changed
|
||||
self.view.on_add_campaign = self.campaigns.add_campaign
|
||||
self.view.on_add_round = self.rounds.add_round
|
||||
|
||||
def __connect(self) -> None:
|
||||
self.view.actionExit.triggered.connect(self.view.close)
|
||||
self.view.actionNew.triggered.connect(self.new)
|
||||
self.view.actionOpen.triggered.connect(self.open_file)
|
||||
self.view.actionSave.triggered.connect(self.save)
|
||||
self.view.actionSave_as.triggered.connect(self.save_as)
|
||||
self.view.addPlayerBtn.clicked.connect(self.players.add_player)
|
||||
self.view.addWarBtn.clicked.connect(self.wars.add_war)
|
||||
self.view.addObjectiveBtn.clicked.connect(self.wars.add_objective)
|
||||
self.view.addWarParticipantBtn.clicked.connect(self.wars.add_war_participant)
|
||||
self.view.addSectorBtn.clicked.connect(self.campaigns.add_sector)
|
||||
self.view.addCampaignParticipantBtn.clicked.connect(
|
||||
self.campaigns.add_campaign_participant
|
||||
)
|
||||
self.view.on_edit_item = self.edit_item
|
||||
self.view.on_delete_item = self.delete_item
|
||||
|
||||
def on_app_close(self) -> bool:
|
||||
if self.is_dirty:
|
||||
reply = QMessageBox.question(
|
||||
self.view,
|
||||
"Unsaved changes",
|
||||
"You have unsaved changes. Do you want to save before quitting?",
|
||||
QMessageBox.StandardButton.Yes
|
||||
| QMessageBox.StandardButton.No
|
||||
| QMessageBox.StandardButton.Cancel,
|
||||
)
|
||||
if reply == QMessageBox.StandardButton.Yes:
|
||||
self.save()
|
||||
elif reply == QMessageBox.StandardButton.Cancel:
|
||||
return False
|
||||
return True
|
||||
|
||||
# Menu bar methods
|
||||
|
||||
def new(self) -> None:
|
||||
if self.is_dirty:
|
||||
reply = QMessageBox.question(
|
||||
self.view,
|
||||
"Unsaved changes",
|
||||
"Discard current campaign?",
|
||||
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
|
||||
)
|
||||
if reply != QMessageBox.StandardButton.Yes:
|
||||
return
|
||||
self.model.new()
|
||||
self.current_file = None
|
||||
self.is_dirty = False
|
||||
self.navigation.refresh_players_view()
|
||||
self.navigation.refresh_wars_view()
|
||||
self.update_window_title()
|
||||
|
||||
def open_file(self) -> None:
|
||||
if self.is_dirty:
|
||||
reply = QMessageBox.question(
|
||||
self.view,
|
||||
"Unsaved changes",
|
||||
"Discard current campaign?",
|
||||
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
|
||||
)
|
||||
if reply != QMessageBox.StandardButton.Yes:
|
||||
return
|
||||
path = self.view.ask_open_file()
|
||||
if not path:
|
||||
return
|
||||
self.model.load(path)
|
||||
self.current_file = path
|
||||
self.is_dirty = False
|
||||
self.navigation.refresh_players_view()
|
||||
self.navigation.refresh_wars_view()
|
||||
self.update_window_title()
|
||||
|
||||
def save(self) -> None:
|
||||
if not self.current_file:
|
||||
self.save_as()
|
||||
return
|
||||
self.model.save(self.current_file)
|
||||
self.is_dirty = False
|
||||
self.update_window_title()
|
||||
|
||||
def save_as(self) -> None:
|
||||
path = self.view.ask_save_file()
|
||||
if not path:
|
||||
return
|
||||
self.current_file = path
|
||||
self.model.save(path)
|
||||
self.is_dirty = False
|
||||
self.update_window_title()
|
||||
|
||||
# Display methods
|
||||
|
||||
def update_window_title(self) -> None:
|
||||
base = "WarChron"
|
||||
if self.current_file:
|
||||
base += f" - {self.current_file.name}"
|
||||
else:
|
||||
base += " - New file"
|
||||
if self.is_dirty:
|
||||
base = base + " *"
|
||||
self.view.setWindowTitle(base)
|
||||
|
||||
# Command methods
|
||||
|
||||
def edit_item(self, item_type: str, item_id: str) -> None:
|
||||
try:
|
||||
if item_type == ItemType.PLAYER:
|
||||
self.players.edit_player(item_id)
|
||||
self.navigation.refresh(RefreshScope.PLAYERS_LIST)
|
||||
elif item_type == ItemType.WAR:
|
||||
self.wars.edit_war(item_id)
|
||||
self.navigation.refresh_and_select(
|
||||
RefreshScope.WARS_TREE, item_type=ItemType.WAR, item_id=item_id
|
||||
)
|
||||
elif item_type == ItemType.CAMPAIGN:
|
||||
self.campaigns.edit_campaign(item_id)
|
||||
self.navigation.refresh_and_select(
|
||||
RefreshScope.WARS_TREE, item_type=ItemType.CAMPAIGN, item_id=item_id
|
||||
)
|
||||
elif item_type == ItemType.OBJECTIVE:
|
||||
self.wars.edit_objective(item_id)
|
||||
self.navigation.refresh(RefreshScope.WAR_DETAILS)
|
||||
elif item_type == ItemType.WAR_PARTICIPANT:
|
||||
self.wars.edit_war_participant(item_id)
|
||||
self.navigation.refresh(RefreshScope.WAR_DETAILS)
|
||||
elif item_type == ItemType.SECTOR:
|
||||
self.campaigns.edit_sector(item_id)
|
||||
self.navigation.refresh(RefreshScope.CAMPAIGN_DETAILS)
|
||||
elif item_type == ItemType.CAMPAIGN_PARTICIPANT:
|
||||
self.campaigns.edit_campaign_participant(item_id)
|
||||
self.navigation.refresh(RefreshScope.CAMPAIGN_DETAILS)
|
||||
elif item_type == ItemType.CHOICE:
|
||||
self.rounds.edit_round_choice(item_id)
|
||||
self.navigation.refresh(RefreshScope.ROUND_DETAILS)
|
||||
elif item_type == ItemType.BATTLE:
|
||||
self.rounds.edit_round_battle(item_id)
|
||||
self.navigation.refresh(RefreshScope.ROUND_DETAILS)
|
||||
self.is_dirty = True
|
||||
except UpdateRequiresConfirmation as e:
|
||||
reply = QMessageBox.question(
|
||||
self.view,
|
||||
"Confirm update",
|
||||
e.message,
|
||||
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
|
||||
)
|
||||
if reply == QMessageBox.StandardButton.Yes:
|
||||
e.apply_update()
|
||||
self.navigation.refresh(RefreshScope.CAMPAIGN_DETAILS)
|
||||
|
||||
def delete_item(self, item_type: str, item_id: str) -> None:
|
||||
reply = QMessageBox.question(
|
||||
self.view,
|
||||
"Confirm deletion",
|
||||
"Are you sure you want to delete this item?",
|
||||
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
|
||||
)
|
||||
if reply != QMessageBox.StandardButton.Yes:
|
||||
return
|
||||
try:
|
||||
if item_type == ItemType.PLAYER:
|
||||
self.model.remove_player(item_id)
|
||||
self.navigation.refresh(RefreshScope.PLAYERS_LIST)
|
||||
elif item_type == ItemType.WAR:
|
||||
self.model.remove_war(item_id)
|
||||
self.navigation.refresh(RefreshScope.WARS_TREE)
|
||||
elif item_type == ItemType.CAMPAIGN:
|
||||
war = self.model.get_war_by_campaign(item_id)
|
||||
war_id = war.id
|
||||
self.model.remove_campaign(item_id)
|
||||
self.navigation.refresh_and_select(
|
||||
RefreshScope.WARS_TREE, item_type=ItemType.WAR, item_id=war_id
|
||||
)
|
||||
elif item_type == ItemType.OBJECTIVE:
|
||||
self.model.remove_objective(item_id)
|
||||
self.navigation.refresh(RefreshScope.WAR_DETAILS)
|
||||
elif item_type == ItemType.WAR_PARTICIPANT:
|
||||
self.model.remove_war_participant(item_id)
|
||||
self.navigation.refresh(RefreshScope.WAR_DETAILS)
|
||||
elif item_type == ItemType.SECTOR:
|
||||
self.model.remove_sector(item_id)
|
||||
self.navigation.refresh(RefreshScope.CAMPAIGN_DETAILS)
|
||||
elif item_type == ItemType.CAMPAIGN_PARTICIPANT:
|
||||
self.model.remove_campaign_participant(item_id)
|
||||
self.navigation.refresh(RefreshScope.CAMPAIGN_DETAILS)
|
||||
elif item_type == ItemType.ROUND:
|
||||
camp = self.model.get_campaign_by_round(item_id)
|
||||
camp_id = camp.id
|
||||
self.model.remove_round(item_id)
|
||||
self.navigation.refresh_and_select(
|
||||
RefreshScope.WARS_TREE, item_type=ItemType.CAMPAIGN, item_id=camp_id
|
||||
)
|
||||
self.is_dirty = True
|
||||
except DeletionForbidden as e:
|
||||
QMessageBox.warning(
|
||||
self.view,
|
||||
"Deletion forbidden",
|
||||
e.reason,
|
||||
)
|
||||
except DeletionRequiresConfirmation as e:
|
||||
reply = QMessageBox.question(
|
||||
self.view,
|
||||
"Confirm deletion",
|
||||
e.message,
|
||||
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
|
||||
)
|
||||
if reply == QMessageBox.StandardButton.Yes:
|
||||
e.cleanup_action()
|
||||
self.navigation.refresh(RefreshScope.CAMPAIGN_DETAILS)
|
||||
239
src/warchron/controller/campaign_controller.py
Normal file
239
src/warchron/controller/campaign_controller.py
Normal file
|
|
@ -0,0 +1,239 @@
|
|||
from typing import List, TYPE_CHECKING
|
||||
|
||||
from PyQt6.QtWidgets import QMessageBox, QDialog
|
||||
|
||||
from warchron.constants import ItemType, RefreshScope
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from warchron.controller.app_controller import AppController
|
||||
from warchron.controller.dtos import (
|
||||
ParticipantOption,
|
||||
ObjectiveDTO,
|
||||
CampaignParticipantDTO,
|
||||
SectorDTO,
|
||||
RoundDTO,
|
||||
)
|
||||
from warchron.view.campaign_dialog import CampaignDialog
|
||||
from warchron.view.campaign_participant_dialog import CampaignParticipantDialog
|
||||
from warchron.view.sector_dialog import SectorDialog
|
||||
|
||||
|
||||
class CampaignController:
|
||||
def __init__(self, app: "AppController"):
|
||||
self.app = app
|
||||
|
||||
def _fill_campaign_details(self, campaign_id: str) -> None:
|
||||
camp = self.app.model.get_campaign(campaign_id)
|
||||
self.app.view.show_campaign_details(name=camp.name, month=camp.month)
|
||||
sectors = camp.get_all_sectors()
|
||||
war = self.app.model.get_war_by_campaign(camp.id)
|
||||
sectors_for_display: List[SectorDTO] = [
|
||||
SectorDTO(
|
||||
id=sect.id,
|
||||
name=sect.name,
|
||||
round_index=camp.get_round_index(sect.round_id),
|
||||
major=war.get_objective_name(sect.major_objective_id),
|
||||
minor=war.get_objective_name(sect.minor_objective_id),
|
||||
influence=war.get_objective_name(sect.influence_objective_id),
|
||||
)
|
||||
for sect in sectors
|
||||
]
|
||||
self.app.view.display_campaign_sectors(sectors_for_display)
|
||||
camp_parts = camp.get_all_campaign_participants()
|
||||
participants_for_display: List[CampaignParticipantDTO] = [
|
||||
CampaignParticipantDTO(
|
||||
id=p.id,
|
||||
player_name=self.app.model.get_participant_name(p.war_participant_id),
|
||||
leader=p.leader or "",
|
||||
theme=p.theme or "",
|
||||
)
|
||||
for p in camp_parts
|
||||
]
|
||||
self.app.view.display_campaign_participants(participants_for_display)
|
||||
|
||||
def _validate_campaign_inputs(self, name: str, month: int) -> bool:
|
||||
if not name.strip():
|
||||
QMessageBox.warning(
|
||||
self.app.view, "Invalid name", "Campaign name cannot be empty."
|
||||
)
|
||||
return False
|
||||
if not (1 <= month <= 12):
|
||||
QMessageBox.warning(
|
||||
self.app.view, "Invalid month", "Month must be between 1 and 12."
|
||||
)
|
||||
return False
|
||||
return True
|
||||
|
||||
def add_campaign(self) -> None:
|
||||
if not self.app.navigation.selected_war_id:
|
||||
return
|
||||
dialog = CampaignDialog(
|
||||
self.app.view,
|
||||
default_month=self.app.model.get_default_campaign_values(
|
||||
self.app.navigation.selected_war_id
|
||||
)["month"],
|
||||
)
|
||||
if dialog.exec() != QDialog.DialogCode.Accepted:
|
||||
return
|
||||
name = dialog.get_campaign_name()
|
||||
month = dialog.get_campaign_month()
|
||||
if not self._validate_campaign_inputs(name, month):
|
||||
return
|
||||
camp = self.app.model.add_campaign(
|
||||
self.app.navigation.selected_war_id, name, month
|
||||
)
|
||||
self.app.is_dirty = True
|
||||
self.app.navigation.refresh_and_select(
|
||||
RefreshScope.WARS_TREE, item_type=ItemType.CAMPAIGN, item_id=camp.id
|
||||
)
|
||||
|
||||
def edit_campaign(self, campaign_id: str) -> None:
|
||||
camp = self.app.model.get_campaign(campaign_id)
|
||||
camp_dialog = CampaignDialog(
|
||||
self.app.view, default_name=camp.name, default_month=camp.month
|
||||
)
|
||||
if camp_dialog.exec() == QDialog.DialogCode.Accepted:
|
||||
name = camp_dialog.get_campaign_name()
|
||||
month = camp_dialog.get_campaign_month()
|
||||
if not self._validate_campaign_inputs(name, month):
|
||||
return
|
||||
self.app.model.update_campaign(campaign_id, name=name, month=month)
|
||||
|
||||
# Campaign participant methods
|
||||
|
||||
def add_campaign_participant(self) -> None:
|
||||
if not self.app.navigation.selected_campaign_id:
|
||||
return
|
||||
participants = self.app.model.get_available_war_participants(
|
||||
self.app.navigation.selected_campaign_id
|
||||
)
|
||||
part_opts = [
|
||||
ParticipantOption(id=p.id, name=self.app.model.get_player_name(p.player_id))
|
||||
for p in participants
|
||||
]
|
||||
dialog = CampaignParticipantDialog(self.app.view, participants=part_opts)
|
||||
if dialog.exec() != QDialog.DialogCode.Accepted:
|
||||
return
|
||||
player_id = dialog.get_player_id()
|
||||
leader = dialog.get_participant_leader()
|
||||
theme = dialog.get_participant_theme()
|
||||
if not player_id:
|
||||
return
|
||||
self.app.model.add_campaign_participant(
|
||||
self.app.navigation.selected_campaign_id, player_id, leader, theme
|
||||
)
|
||||
self.app.is_dirty = True
|
||||
self.app.navigation.refresh(RefreshScope.CAMPAIGN_DETAILS)
|
||||
|
||||
def edit_campaign_participant(self, participant_id: str) -> None:
|
||||
camp_part = self.app.model.get_campaign_participant(participant_id)
|
||||
war_part = self.app.model.get_war_participant(camp_part.war_participant_id)
|
||||
player = self.app.model.get_player(war_part.player_id)
|
||||
part_opt = [ParticipantOption(id=player.id, name=player.name)]
|
||||
camp_part_dialog = CampaignParticipantDialog(
|
||||
self.app.view,
|
||||
participants=part_opt,
|
||||
default_participant_id=camp_part.id,
|
||||
default_leader=camp_part.leader,
|
||||
default_theme=camp_part.theme,
|
||||
editable_player=False,
|
||||
)
|
||||
if camp_part_dialog.exec() == QDialog.DialogCode.Accepted:
|
||||
leader = camp_part_dialog.get_participant_leader()
|
||||
theme = camp_part_dialog.get_participant_theme()
|
||||
self.app.model.update_campaign_participant(
|
||||
participant_id, leader=leader, theme=theme
|
||||
)
|
||||
|
||||
# Sector methods
|
||||
|
||||
def _validate_sector_inputs(
|
||||
self, name: str, round_id: str, major_id: str, minor_id: str, influence_id: str
|
||||
) -> bool:
|
||||
|
||||
if not name.strip():
|
||||
QMessageBox.warning(
|
||||
self.app.view, "Invalid name", "Sector name cannot be empty."
|
||||
)
|
||||
return False
|
||||
# allow same objectives in different fields?
|
||||
return True
|
||||
|
||||
def add_sector(self) -> None:
|
||||
if not self.app.navigation.selected_campaign_id:
|
||||
return
|
||||
war = self.app.model.get_war_by_campaign(
|
||||
self.app.navigation.selected_campaign_id
|
||||
)
|
||||
camp = self.app.model.get_campaign(self.app.navigation.selected_campaign_id)
|
||||
rounds = camp.get_all_rounds()
|
||||
rnd_objs: List[RoundDTO] = [
|
||||
RoundDTO(id=rnd.id, index=camp.get_round_index(rnd.id)) for rnd in rounds
|
||||
]
|
||||
objectives = war.get_all_objectives()
|
||||
obj_dtos: List[ObjectiveDTO] = [
|
||||
ObjectiveDTO(id=obj.id, name=obj.name, description=obj.description)
|
||||
for obj in objectives
|
||||
]
|
||||
dialog = SectorDialog(
|
||||
self.app.view, default_name="", rounds=rnd_objs, objectives=obj_dtos
|
||||
)
|
||||
if dialog.exec() != QDialog.DialogCode.Accepted:
|
||||
return
|
||||
name = dialog.get_sector_name()
|
||||
round_id = dialog.get_round_id()
|
||||
major_id = dialog.get_major_id()
|
||||
minor_id = dialog.get_minor_id()
|
||||
influence_id = dialog.get_influence_id()
|
||||
if not self._validate_sector_inputs(
|
||||
name, round_id, major_id, minor_id, influence_id
|
||||
):
|
||||
return
|
||||
self.app.model.add_sector(
|
||||
self.app.navigation.selected_campaign_id,
|
||||
name,
|
||||
round_id,
|
||||
major_id,
|
||||
minor_id,
|
||||
influence_id,
|
||||
)
|
||||
self.app.is_dirty = True
|
||||
self.app.navigation.refresh(RefreshScope.CAMPAIGN_DETAILS)
|
||||
|
||||
def edit_sector(self, sector_id: str) -> None:
|
||||
sect = self.app.model.get_sector(sector_id)
|
||||
camp = self.app.model.get_campaign_by_sector(sector_id)
|
||||
war = self.app.model.get_war_by_campaign(camp.id)
|
||||
rounds = camp.get_all_rounds()
|
||||
rnd_dto: List[RoundDTO] = [
|
||||
RoundDTO(id=rnd.id, index=i) for i, rnd in enumerate(rounds, start=1)
|
||||
]
|
||||
objectives = war.get_all_objectives()
|
||||
obj_dto: List[ObjectiveDTO] = [
|
||||
ObjectiveDTO(id=obj.id, name=obj.name, description=obj.description)
|
||||
for obj in objectives
|
||||
]
|
||||
sect_dialog = SectorDialog(
|
||||
self.app.view,
|
||||
default_name=sect.name,
|
||||
rounds=rnd_dto,
|
||||
default_round_id=sect.round_id,
|
||||
objectives=obj_dto,
|
||||
default_major_id=sect.major_objective_id,
|
||||
default_minor_id=sect.minor_objective_id,
|
||||
default_influence_id=sect.influence_objective_id,
|
||||
)
|
||||
if sect_dialog.exec() == QDialog.DialogCode.Accepted:
|
||||
name = sect_dialog.get_sector_name()
|
||||
round_id = sect_dialog.get_round_id()
|
||||
major_id = sect_dialog.get_major_id()
|
||||
minor_id = sect_dialog.get_minor_id()
|
||||
influence_id = sect_dialog.get_influence_id()
|
||||
self.app.model.update_sector(
|
||||
sector_id,
|
||||
name=name,
|
||||
round_id=round_id,
|
||||
major_id=major_id,
|
||||
minor_id=minor_id,
|
||||
influence_id=influence_id,
|
||||
)
|
||||
|
|
@ -1,903 +0,0 @@
|
|||
from typing import List
|
||||
from pathlib import Path
|
||||
|
||||
from PyQt6.QtWidgets import QMessageBox, QDialog
|
||||
|
||||
from warchron.model.model import Model
|
||||
from warchron.model.exception import (
|
||||
DeletionForbidden,
|
||||
DeletionRequiresConfirmation,
|
||||
UpdateRequiresConfirmation,
|
||||
)
|
||||
from warchron.view.view import View
|
||||
from warchron.constants import ItemType, RefreshScope
|
||||
from warchron.controller.dtos import (
|
||||
ParticipantOption,
|
||||
TreeSelection,
|
||||
WarDTO,
|
||||
WarParticipantDTO,
|
||||
ObjectiveDTO,
|
||||
CampaignDTO,
|
||||
CampaignParticipantDTO,
|
||||
SectorDTO,
|
||||
RoundDTO,
|
||||
ChoiceDTO,
|
||||
BattleDTO,
|
||||
)
|
||||
from warchron.view.player_dialog import PlayerDialog
|
||||
from warchron.view.war_dialog import WarDialog
|
||||
from warchron.view.campaign_dialog import CampaignDialog
|
||||
from warchron.view.objective_dialog import ObjectiveDialog
|
||||
from warchron.view.war_participant_dialog import WarParticipantDialog
|
||||
from warchron.view.campaign_participant_dialog import CampaignParticipantDialog
|
||||
from warchron.view.sector_dialog import SectorDialog
|
||||
from warchron.view.choices_dialog import ChoicesDialog
|
||||
from warchron.view.battles_dialog import BattlesDialog
|
||||
|
||||
|
||||
class Controller:
|
||||
def __init__(self, model: Model, view: View) -> None:
|
||||
self.model: Model = model
|
||||
self.view: View = view
|
||||
self.current_file: Path | None = None
|
||||
self.selected_war_id: str | None = None
|
||||
self.selected_campaign_id: str | None = None
|
||||
self.selected_round_id: str | None = None
|
||||
self.view.on_close_callback = self.on_app_close
|
||||
self.is_dirty: bool = False
|
||||
self.__connect()
|
||||
self.refresh_players_view()
|
||||
self.refresh_wars_view()
|
||||
self.update_window_title()
|
||||
self.update_actions_state()
|
||||
self.view.on_tree_selection_changed = self.on_tree_selection_changed
|
||||
self.view.on_add_campaign = self.add_campaign
|
||||
self.view.on_add_round = self.add_round
|
||||
|
||||
def __connect(self) -> None:
|
||||
self.view.actionExit.triggered.connect(self.view.close)
|
||||
self.view.actionNew.triggered.connect(self.new)
|
||||
self.view.actionOpen.triggered.connect(self.open_file)
|
||||
self.view.actionSave.triggered.connect(self.save)
|
||||
self.view.actionSave_as.triggered.connect(self.save_as)
|
||||
self.view.addPlayerBtn.clicked.connect(self.add_player)
|
||||
self.view.addWarBtn.clicked.connect(self.add_war)
|
||||
self.view.addObjectiveBtn.clicked.connect(self.add_objective)
|
||||
self.view.addWarParticipantBtn.clicked.connect(self.add_war_participant)
|
||||
self.view.addSectorBtn.clicked.connect(self.add_sector)
|
||||
self.view.addCampaignParticipantBtn.clicked.connect(
|
||||
self.add_campaign_participant
|
||||
)
|
||||
self.view.on_edit_item = self.edit_item
|
||||
self.view.on_delete_item = self.delete_item
|
||||
|
||||
def on_app_close(self) -> bool:
|
||||
if self.is_dirty:
|
||||
reply = QMessageBox.question(
|
||||
self.view,
|
||||
"Unsaved changes",
|
||||
"You have unsaved changes. Do you want to save before quitting?",
|
||||
QMessageBox.StandardButton.Yes
|
||||
| QMessageBox.StandardButton.No
|
||||
| QMessageBox.StandardButton.Cancel,
|
||||
)
|
||||
if reply == QMessageBox.StandardButton.Yes:
|
||||
self.save()
|
||||
elif reply == QMessageBox.StandardButton.Cancel:
|
||||
return False
|
||||
return True
|
||||
|
||||
# Menu bar methods
|
||||
|
||||
def new(self) -> None:
|
||||
if self.is_dirty:
|
||||
reply = QMessageBox.question(
|
||||
self.view,
|
||||
"Unsaved changes",
|
||||
"Discard current campaign?",
|
||||
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
|
||||
)
|
||||
if reply != QMessageBox.StandardButton.Yes:
|
||||
return
|
||||
self.model.new()
|
||||
self.current_file = None
|
||||
self.is_dirty = False
|
||||
self.refresh_players_view()
|
||||
self.refresh_wars_view()
|
||||
self.update_window_title()
|
||||
|
||||
def open_file(self) -> None:
|
||||
if self.is_dirty:
|
||||
reply = QMessageBox.question(
|
||||
self.view,
|
||||
"Unsaved changes",
|
||||
"Discard current campaign?",
|
||||
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
|
||||
)
|
||||
if reply != QMessageBox.StandardButton.Yes:
|
||||
return
|
||||
path = self.view.ask_open_file()
|
||||
if not path:
|
||||
return
|
||||
self.model.load(path)
|
||||
self.current_file = path
|
||||
self.is_dirty = False
|
||||
self.refresh_players_view()
|
||||
self.refresh_wars_view()
|
||||
self.update_window_title()
|
||||
|
||||
def save(self) -> None:
|
||||
if not self.current_file:
|
||||
self.save_as()
|
||||
return
|
||||
self.model.save(self.current_file)
|
||||
self.is_dirty = False
|
||||
self.update_window_title()
|
||||
|
||||
def save_as(self) -> None:
|
||||
path = self.view.ask_save_file()
|
||||
if not path:
|
||||
return
|
||||
self.current_file = path
|
||||
self.model.save(path)
|
||||
self.is_dirty = False
|
||||
self.update_window_title()
|
||||
|
||||
# Display methods
|
||||
|
||||
def update_window_title(self) -> None:
|
||||
base = "WarChron"
|
||||
if self.current_file:
|
||||
base += f" - {self.current_file.name}"
|
||||
else:
|
||||
base += " - New file"
|
||||
if self.is_dirty:
|
||||
base = base + " *"
|
||||
self.view.setWindowTitle(base)
|
||||
|
||||
def refresh_players_view(self) -> None:
|
||||
players = self.model.get_all_players()
|
||||
players_for_display: List[ParticipantOption] = [
|
||||
ParticipantOption(id=p.id, name=p.name) for p in players
|
||||
]
|
||||
self.view.display_players(players_for_display)
|
||||
|
||||
def refresh_wars_view(self) -> None:
|
||||
wars: List[WarDTO] = [
|
||||
WarDTO(
|
||||
id=w.id,
|
||||
name=w.name,
|
||||
year=w.year,
|
||||
_campaigns=[
|
||||
CampaignDTO(
|
||||
id=c.id,
|
||||
name=c.name,
|
||||
month=c.month,
|
||||
_rounds=[
|
||||
RoundDTO(id=r.id, index=c.get_round_index(r.id))
|
||||
for r in c.get_all_rounds()
|
||||
],
|
||||
)
|
||||
for c in w.get_all_campaigns()
|
||||
],
|
||||
)
|
||||
for w in self.model.get_all_wars()
|
||||
]
|
||||
self.view.display_wars_tree(wars)
|
||||
|
||||
def _fill_war_details(self, war_id: str) -> None:
|
||||
war = self.model.get_war(war_id)
|
||||
self.view.show_war_details(name=war.name, year=war.year)
|
||||
objectives = war.get_all_objectives()
|
||||
objectives_for_display: List[ObjectiveDTO] = [
|
||||
ObjectiveDTO(id=obj.id, name=obj.name, description=obj.description)
|
||||
for obj in objectives
|
||||
]
|
||||
self.view.display_war_objectives(objectives_for_display)
|
||||
war_parts = war.get_all_war_participants()
|
||||
participants_for_display: List[WarParticipantDTO] = [
|
||||
WarParticipantDTO(
|
||||
id=p.id,
|
||||
player_name=self.model.get_player_name(p.player_id),
|
||||
faction=p.faction,
|
||||
)
|
||||
for p in war_parts
|
||||
]
|
||||
self.view.display_war_participants(participants_for_display)
|
||||
|
||||
def _fill_campaign_details(self, campaign_id: str) -> None:
|
||||
camp = self.model.get_campaign(campaign_id)
|
||||
self.view.show_campaign_details(name=camp.name, month=camp.month)
|
||||
sectors = camp.get_all_sectors()
|
||||
war = self.model.get_war_by_campaign(camp.id)
|
||||
sectors_for_display: List[SectorDTO] = [
|
||||
SectorDTO(
|
||||
id=sect.id,
|
||||
name=sect.name,
|
||||
round_index=camp.get_round_index(sect.round_id),
|
||||
major=war.get_objective_name(sect.major_objective_id),
|
||||
minor=war.get_objective_name(sect.minor_objective_id),
|
||||
influence=war.get_objective_name(sect.influence_objective_id),
|
||||
)
|
||||
for sect in sectors
|
||||
]
|
||||
self.view.display_campaign_sectors(sectors_for_display)
|
||||
camp_parts = camp.get_all_campaign_participants()
|
||||
participants_for_display: List[CampaignParticipantDTO] = [
|
||||
CampaignParticipantDTO(
|
||||
id=p.id,
|
||||
player_name=self.model.get_participant_name(p.war_participant_id),
|
||||
leader=p.leader or "",
|
||||
theme=p.theme or "",
|
||||
)
|
||||
for p in camp_parts
|
||||
]
|
||||
self.view.display_campaign_participants(participants_for_display)
|
||||
|
||||
def _fill_round_details(self, round_id: str) -> None:
|
||||
rnd = self.model.get_round(round_id)
|
||||
camp = self.model.get_campaign_by_round(round_id)
|
||||
self.view.show_round_details(index=camp.get_round_index(round_id))
|
||||
participants = self.model.get_round_participants(round_id)
|
||||
sectors = camp.get_sectors_in_round(round_id)
|
||||
choices_for_display: List[ChoiceDTO] = []
|
||||
for part in participants:
|
||||
choice = rnd.get_choice(part.id)
|
||||
if not choice:
|
||||
choice = self.model.create_choice(
|
||||
round_id=rnd.id, participant_id=part.id
|
||||
)
|
||||
priority_name = (
|
||||
camp.get_sector_name(choice.priority_sector_id)
|
||||
if choice.priority_sector_id is not None
|
||||
else ""
|
||||
)
|
||||
secondary_name = (
|
||||
camp.get_sector_name(choice.secondary_sector_id)
|
||||
if choice.secondary_sector_id is not None
|
||||
else ""
|
||||
)
|
||||
choices_for_display.append(
|
||||
ChoiceDTO(
|
||||
id=choice.participant_id,
|
||||
participant_name=self.model.get_participant_name(
|
||||
part.war_participant_id
|
||||
),
|
||||
priority_sector=priority_name,
|
||||
secondary_sector=secondary_name,
|
||||
comment=choice.comment,
|
||||
)
|
||||
)
|
||||
self.view.display_round_choices(choices_for_display)
|
||||
battles_for_display: List[BattleDTO] = []
|
||||
for sect in sectors:
|
||||
battle = rnd.get_battle(sect.id)
|
||||
if not battle:
|
||||
battle = self.model.create_battle(round_id=rnd.id, sector_id=sect.id)
|
||||
if battle.player_1_id:
|
||||
camp_part = camp.participants[battle.player_1_id]
|
||||
player_1_name = self.model.get_participant_name(
|
||||
camp_part.war_participant_id
|
||||
)
|
||||
else:
|
||||
player_1_name = ""
|
||||
if battle.player_2_id:
|
||||
camp_part = camp.participants[battle.player_2_id]
|
||||
player_2_name = self.model.get_participant_name(
|
||||
camp_part.war_participant_id
|
||||
)
|
||||
else:
|
||||
player_2_name = ""
|
||||
if battle.winner_id:
|
||||
camp_part = camp.participants[battle.winner_id]
|
||||
winner_name = self.model.get_participant_name(
|
||||
camp_part.war_participant_id
|
||||
)
|
||||
else:
|
||||
winner_name = ""
|
||||
battles_for_display.append(
|
||||
BattleDTO(
|
||||
id=battle.sector_id,
|
||||
sector_name=camp.get_sector_name(battle.sector_id),
|
||||
player_1=player_1_name,
|
||||
player_2=player_2_name,
|
||||
winner=winner_name,
|
||||
score=battle.score,
|
||||
victory_condition=battle.victory_condition,
|
||||
comment=battle.comment,
|
||||
)
|
||||
)
|
||||
self.view.display_round_battles(battles_for_display)
|
||||
|
||||
def on_tree_selection_changed(self, selection: TreeSelection | None) -> None:
|
||||
self.selected_war_id = None
|
||||
self.selected_campaign_id = None
|
||||
self.selected_round_id = None
|
||||
if selection:
|
||||
item_type = selection.type
|
||||
item_id = selection.id
|
||||
if item_type == ItemType.WAR:
|
||||
self.selected_war_id = item_id
|
||||
self.view.show_details(ItemType.WAR)
|
||||
self._fill_war_details(item_id)
|
||||
elif item_type == ItemType.CAMPAIGN:
|
||||
self.selected_campaign_id = item_id
|
||||
self.view.show_details(ItemType.CAMPAIGN)
|
||||
self._fill_campaign_details(item_id)
|
||||
elif item_type == ItemType.ROUND:
|
||||
self.selected_round_id = item_id
|
||||
self.view.show_details(ItemType.ROUND)
|
||||
self._fill_round_details(item_id)
|
||||
else:
|
||||
self.view.show_details(None)
|
||||
self.update_actions_state()
|
||||
return
|
||||
self.update_actions_state()
|
||||
|
||||
def update_actions_state(self) -> None:
|
||||
self.view.set_add_campaign_enabled(self.selected_war_id is not None)
|
||||
self.view.set_add_round_enabled(self.selected_campaign_id is not None)
|
||||
|
||||
def refresh(self, scope: RefreshScope) -> None:
|
||||
match scope:
|
||||
case RefreshScope.PLAYERS_LIST:
|
||||
self.refresh_players_view()
|
||||
case RefreshScope.WARS_TREE:
|
||||
self.refresh_wars_view()
|
||||
case RefreshScope.WAR_DETAILS:
|
||||
if self.selected_war_id:
|
||||
self.view.show_details(ItemType.WAR)
|
||||
self._fill_war_details(self.selected_war_id)
|
||||
case RefreshScope.CAMPAIGN_DETAILS:
|
||||
if self.selected_campaign_id:
|
||||
self.view.show_details(ItemType.CAMPAIGN)
|
||||
self._fill_campaign_details(self.selected_campaign_id)
|
||||
case RefreshScope.ROUND_DETAILS:
|
||||
if self.selected_round_id:
|
||||
self.view.show_details(ItemType.ROUND)
|
||||
self._fill_round_details(self.selected_round_id)
|
||||
self.update_window_title()
|
||||
|
||||
# Common command methods
|
||||
|
||||
def refresh_and_select(
|
||||
self, scope: RefreshScope, *, item_type: ItemType, item_id: str
|
||||
) -> None:
|
||||
self.refresh(scope)
|
||||
self.view.select_tree_item(item_type=item_type, item_id=item_id)
|
||||
|
||||
def edit_item(self, item_type: str, item_id: str) -> None:
|
||||
try:
|
||||
if item_type == ItemType.PLAYER:
|
||||
self.edit_player(item_id)
|
||||
self.refresh(RefreshScope.PLAYERS_LIST)
|
||||
elif item_type == ItemType.WAR:
|
||||
self.edit_war(item_id)
|
||||
self.refresh_and_select(
|
||||
RefreshScope.WARS_TREE, item_type=ItemType.WAR, item_id=item_id
|
||||
)
|
||||
elif item_type == ItemType.CAMPAIGN:
|
||||
self.edit_campaign(item_id)
|
||||
self.refresh_and_select(
|
||||
RefreshScope.WARS_TREE, item_type=ItemType.CAMPAIGN, item_id=item_id
|
||||
)
|
||||
elif item_type == ItemType.OBJECTIVE:
|
||||
self.edit_objective(item_id)
|
||||
self.refresh(RefreshScope.WAR_DETAILS)
|
||||
elif item_type == ItemType.WAR_PARTICIPANT:
|
||||
self.edit_war_participant(item_id)
|
||||
self.refresh(RefreshScope.WAR_DETAILS)
|
||||
elif item_type == ItemType.SECTOR:
|
||||
self.edit_sector(item_id)
|
||||
self.refresh(RefreshScope.CAMPAIGN_DETAILS)
|
||||
elif item_type == ItemType.CAMPAIGN_PARTICIPANT:
|
||||
self.edit_campaign_participant(item_id)
|
||||
self.refresh(RefreshScope.CAMPAIGN_DETAILS)
|
||||
elif item_type == ItemType.CHOICE:
|
||||
self.edit_round_choice(item_id)
|
||||
self.refresh(RefreshScope.ROUND_DETAILS)
|
||||
elif item_type == ItemType.BATTLE:
|
||||
self.edit_round_battle(item_id)
|
||||
self.refresh(RefreshScope.ROUND_DETAILS)
|
||||
self.is_dirty = True
|
||||
except UpdateRequiresConfirmation as e:
|
||||
reply = QMessageBox.question(
|
||||
self.view,
|
||||
"Confirm update",
|
||||
e.message,
|
||||
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
|
||||
)
|
||||
if reply == QMessageBox.StandardButton.Yes:
|
||||
e.apply_update()
|
||||
self.refresh(RefreshScope.CAMPAIGN_DETAILS)
|
||||
|
||||
def delete_item(self, item_type: str, item_id: str) -> None:
|
||||
reply = QMessageBox.question(
|
||||
self.view,
|
||||
"Confirm deletion",
|
||||
"Are you sure you want to delete this item?",
|
||||
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
|
||||
)
|
||||
if reply != QMessageBox.StandardButton.Yes:
|
||||
return
|
||||
try:
|
||||
if item_type == ItemType.PLAYER:
|
||||
self.model.remove_player(item_id)
|
||||
self.refresh(RefreshScope.PLAYERS_LIST)
|
||||
elif item_type == ItemType.WAR:
|
||||
self.model.remove_war(item_id)
|
||||
self.refresh(RefreshScope.WARS_TREE)
|
||||
elif item_type == ItemType.CAMPAIGN:
|
||||
war = self.model.get_war_by_campaign(item_id)
|
||||
war_id = war.id
|
||||
self.model.remove_campaign(item_id)
|
||||
self.refresh_and_select(
|
||||
RefreshScope.WARS_TREE, item_type=ItemType.WAR, item_id=war_id
|
||||
)
|
||||
elif item_type == ItemType.OBJECTIVE:
|
||||
self.model.remove_objective(item_id)
|
||||
self.refresh(RefreshScope.WAR_DETAILS)
|
||||
elif item_type == ItemType.WAR_PARTICIPANT:
|
||||
self.model.remove_war_participant(item_id)
|
||||
self.refresh(RefreshScope.WAR_DETAILS)
|
||||
elif item_type == ItemType.SECTOR:
|
||||
self.model.remove_sector(item_id)
|
||||
self.refresh(RefreshScope.CAMPAIGN_DETAILS)
|
||||
elif item_type == ItemType.CAMPAIGN_PARTICIPANT:
|
||||
self.model.remove_campaign_participant(item_id)
|
||||
self.refresh(RefreshScope.CAMPAIGN_DETAILS)
|
||||
elif item_type == ItemType.ROUND:
|
||||
camp = self.model.get_campaign_by_round(item_id)
|
||||
camp_id = camp.id
|
||||
self.model.remove_round(item_id)
|
||||
self.refresh_and_select(
|
||||
RefreshScope.WARS_TREE, item_type=ItemType.CAMPAIGN, item_id=camp_id
|
||||
)
|
||||
self.is_dirty = True
|
||||
except DeletionForbidden as e:
|
||||
QMessageBox.warning(
|
||||
self.view,
|
||||
"Deletion forbidden",
|
||||
e.reason,
|
||||
)
|
||||
except DeletionRequiresConfirmation as e:
|
||||
reply = QMessageBox.question(
|
||||
self.view,
|
||||
"Confirm deletion",
|
||||
e.message,
|
||||
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
|
||||
)
|
||||
if reply == QMessageBox.StandardButton.Yes:
|
||||
e.cleanup_action()
|
||||
self.refresh(RefreshScope.CAMPAIGN_DETAILS)
|
||||
|
||||
# Player methods
|
||||
|
||||
def _validate_player_inputs(self, name: str) -> bool:
|
||||
if not name.strip():
|
||||
QMessageBox.warning(
|
||||
self.view, "Invalid name", "Player name cannot be empty."
|
||||
)
|
||||
return False
|
||||
return True
|
||||
|
||||
def add_player(self) -> None:
|
||||
dialog = PlayerDialog(self.view)
|
||||
result = dialog.exec() # modal blocking dialog
|
||||
if result == QDialog.DialogCode.Accepted:
|
||||
name = dialog.get_player_name()
|
||||
if not self._validate_player_inputs(name):
|
||||
return
|
||||
self.model.add_player(name)
|
||||
self.is_dirty = True
|
||||
self.refresh(RefreshScope.PLAYERS_LIST)
|
||||
|
||||
def edit_player(self, player_id: str) -> None:
|
||||
play = self.model.get_player(player_id)
|
||||
player_dialog = PlayerDialog(self.view, default_name=play.name)
|
||||
if player_dialog.exec() == QDialog.DialogCode.Accepted:
|
||||
name = player_dialog.get_player_name()
|
||||
if not self._validate_player_inputs(name):
|
||||
return
|
||||
self.model.update_player(player_id, name=name)
|
||||
|
||||
# War methods
|
||||
|
||||
def _validate_war_inputs(self, name: str, year: int) -> bool:
|
||||
if not name.strip():
|
||||
QMessageBox.warning(self.view, "Invalid name", "War name cannot be empty.")
|
||||
return False
|
||||
if not (1970 <= year <= 3000):
|
||||
QMessageBox.warning(
|
||||
self.view, "Invalid year", "Year must be between 1970 and 3000."
|
||||
)
|
||||
return False
|
||||
return True
|
||||
|
||||
def add_war(self) -> None:
|
||||
dialog = WarDialog(
|
||||
self.view, default_year=self.model.get_default_war_values()["year"]
|
||||
)
|
||||
result = dialog.exec() # modal blocking dialog
|
||||
if result == QDialog.DialogCode.Accepted:
|
||||
name = dialog.get_war_name()
|
||||
year = dialog.get_war_year()
|
||||
if not self._validate_war_inputs(name, year):
|
||||
return
|
||||
war = self.model.add_war(name, year)
|
||||
self.is_dirty = True
|
||||
self.refresh_and_select(
|
||||
RefreshScope.WARS_TREE, item_type=ItemType.WAR, item_id=war.id
|
||||
)
|
||||
|
||||
def edit_war(self, war_id: str) -> None:
|
||||
war = self.model.get_war(war_id)
|
||||
war_dialog = WarDialog(self.view, default_name=war.name, default_year=war.year)
|
||||
if war_dialog.exec() == QDialog.DialogCode.Accepted:
|
||||
name = war_dialog.get_war_name()
|
||||
year = war_dialog.get_war_year()
|
||||
if not self._validate_war_inputs(name, year):
|
||||
return
|
||||
self.model.update_war(war_id, name=name, year=year)
|
||||
|
||||
# Objective methods
|
||||
|
||||
def _validate_objective_inputs(self, name: str, description: str) -> bool:
|
||||
if not name.strip():
|
||||
QMessageBox.warning(
|
||||
self.view, "Invalid name", "Objective name cannot be empty."
|
||||
)
|
||||
return False
|
||||
return True
|
||||
|
||||
def add_objective(self) -> None:
|
||||
if not self.selected_war_id:
|
||||
return
|
||||
dialog = ObjectiveDialog(self.view)
|
||||
if dialog.exec() != QDialog.DialogCode.Accepted:
|
||||
return
|
||||
name = dialog.get_objective_name()
|
||||
description = dialog.get_objective_description()
|
||||
if not self._validate_objective_inputs(name, description):
|
||||
return
|
||||
self.model.add_objective(self.selected_war_id, name, description)
|
||||
self.is_dirty = True
|
||||
self.refresh(RefreshScope.WAR_DETAILS)
|
||||
|
||||
def edit_objective(self, objective_id: str) -> None:
|
||||
obj = self.model.get_objective(objective_id)
|
||||
obj_dialog = ObjectiveDialog(
|
||||
self.view, default_name=obj.name, default_description=obj.description
|
||||
)
|
||||
if obj_dialog.exec() == QDialog.DialogCode.Accepted:
|
||||
name = obj_dialog.get_objective_name()
|
||||
description = obj_dialog.get_objective_description()
|
||||
if not self._validate_objective_inputs(name, description):
|
||||
return
|
||||
self.model.update_objective(
|
||||
objective_id, name=name, description=description
|
||||
)
|
||||
|
||||
# War participant methods
|
||||
|
||||
def add_war_participant(self) -> None:
|
||||
if not self.selected_war_id:
|
||||
return
|
||||
players = self.model.get_available_players(self.selected_war_id)
|
||||
play_opts: List[ParticipantOption] = [
|
||||
ParticipantOption(id=p.id, name=p.name) for p in players
|
||||
]
|
||||
dialog = WarParticipantDialog(self.view, players=play_opts)
|
||||
if dialog.exec() != QDialog.DialogCode.Accepted:
|
||||
return
|
||||
player_id = dialog.get_player_id()
|
||||
faction = dialog.get_participant_faction()
|
||||
if not player_id:
|
||||
return
|
||||
self.model.add_war_participant(self.selected_war_id, player_id, faction)
|
||||
self.is_dirty = True
|
||||
self.refresh(RefreshScope.WAR_DETAILS)
|
||||
|
||||
def edit_war_participant(self, participant_id: str) -> None:
|
||||
war_part = self.model.get_war_participant(participant_id)
|
||||
player = self.model.get_player(war_part.player_id)
|
||||
play_opt = ParticipantOption(id=player.id, name=player.name)
|
||||
war_part_dialog = WarParticipantDialog(
|
||||
self.view,
|
||||
players=[play_opt],
|
||||
default_player_id=war_part.id,
|
||||
default_faction=war_part.faction,
|
||||
editable_player=False,
|
||||
)
|
||||
if war_part_dialog.exec() == QDialog.DialogCode.Accepted:
|
||||
faction = war_part_dialog.get_participant_faction()
|
||||
self.model.update_war_participant(participant_id, faction=faction)
|
||||
|
||||
# Campaign methods
|
||||
|
||||
def _validate_campaign_inputs(self, name: str, month: int) -> bool:
|
||||
if not name.strip():
|
||||
QMessageBox.warning(
|
||||
self.view, "Invalid name", "Campaign name cannot be empty."
|
||||
)
|
||||
return False
|
||||
if not (1 <= month <= 12):
|
||||
QMessageBox.warning(
|
||||
self.view, "Invalid month", "Month must be between 1 and 12."
|
||||
)
|
||||
return False
|
||||
return True
|
||||
|
||||
def add_campaign(self) -> None:
|
||||
if not self.selected_war_id:
|
||||
return
|
||||
dialog = CampaignDialog(
|
||||
self.view,
|
||||
default_month=self.model.get_default_campaign_values(self.selected_war_id)[
|
||||
"month"
|
||||
],
|
||||
)
|
||||
if dialog.exec() != QDialog.DialogCode.Accepted:
|
||||
return
|
||||
name = dialog.get_campaign_name()
|
||||
month = dialog.get_campaign_month()
|
||||
if not self._validate_campaign_inputs(name, month):
|
||||
return
|
||||
camp = self.model.add_campaign(self.selected_war_id, name, month)
|
||||
self.is_dirty = True
|
||||
self.refresh_and_select(
|
||||
RefreshScope.WARS_TREE, item_type=ItemType.CAMPAIGN, item_id=camp.id
|
||||
)
|
||||
|
||||
def edit_campaign(self, campaign_id: str) -> None:
|
||||
camp = self.model.get_campaign(campaign_id)
|
||||
camp_dialog = CampaignDialog(
|
||||
self.view, default_name=camp.name, default_month=camp.month
|
||||
)
|
||||
if camp_dialog.exec() == QDialog.DialogCode.Accepted:
|
||||
name = camp_dialog.get_campaign_name()
|
||||
month = camp_dialog.get_campaign_month()
|
||||
if not self._validate_campaign_inputs(name, month):
|
||||
return
|
||||
self.model.update_campaign(campaign_id, name=name, month=month)
|
||||
|
||||
# Campaign participant methods
|
||||
|
||||
def add_campaign_participant(self) -> None:
|
||||
if not self.selected_campaign_id:
|
||||
return
|
||||
participants = self.model.get_available_war_participants(
|
||||
self.selected_campaign_id
|
||||
)
|
||||
part_opts = [
|
||||
ParticipantOption(id=p.id, name=self.model.get_player_name(p.player_id))
|
||||
for p in participants
|
||||
]
|
||||
dialog = CampaignParticipantDialog(self.view, participants=part_opts)
|
||||
if dialog.exec() != QDialog.DialogCode.Accepted:
|
||||
return
|
||||
player_id = dialog.get_player_id()
|
||||
leader = dialog.get_participant_leader()
|
||||
theme = dialog.get_participant_theme()
|
||||
if not player_id:
|
||||
return
|
||||
self.model.add_campaign_participant(
|
||||
self.selected_campaign_id, player_id, leader, theme
|
||||
)
|
||||
self.is_dirty = True
|
||||
self.refresh(RefreshScope.CAMPAIGN_DETAILS)
|
||||
|
||||
def edit_campaign_participant(self, participant_id: str) -> None:
|
||||
camp_part = self.model.get_campaign_participant(participant_id)
|
||||
war_part = self.model.get_war_participant(camp_part.war_participant_id)
|
||||
player = self.model.get_player(war_part.player_id)
|
||||
part_opt = [ParticipantOption(id=player.id, name=player.name)]
|
||||
camp_part_dialog = CampaignParticipantDialog(
|
||||
self.view,
|
||||
participants=part_opt,
|
||||
default_participant_id=camp_part.id,
|
||||
default_leader=camp_part.leader,
|
||||
default_theme=camp_part.theme,
|
||||
editable_player=False,
|
||||
)
|
||||
if camp_part_dialog.exec() == QDialog.DialogCode.Accepted:
|
||||
leader = camp_part_dialog.get_participant_leader()
|
||||
theme = camp_part_dialog.get_participant_theme()
|
||||
self.model.update_campaign_participant(
|
||||
participant_id, leader=leader, theme=theme
|
||||
)
|
||||
|
||||
# Sector methods
|
||||
|
||||
def _validate_sector_inputs(
|
||||
self, name: str, round_id: str, major_id: str, minor_id: str, influence_id: str
|
||||
) -> bool:
|
||||
|
||||
if not name.strip():
|
||||
QMessageBox.warning(
|
||||
self.view, "Invalid name", "Sector name cannot be empty."
|
||||
)
|
||||
return False
|
||||
# allow same objectives in different fields?
|
||||
return True
|
||||
|
||||
def add_sector(self) -> None:
|
||||
if not self.selected_campaign_id:
|
||||
return
|
||||
war = self.model.get_war_by_campaign(self.selected_campaign_id)
|
||||
camp = self.model.get_campaign(self.selected_campaign_id)
|
||||
rounds = camp.get_all_rounds()
|
||||
rnd_objs: List[RoundDTO] = [
|
||||
RoundDTO(id=rnd.id, index=camp.get_round_index(rnd.id)) for rnd in rounds
|
||||
]
|
||||
objectives = war.get_all_objectives()
|
||||
obj_dtos: List[ObjectiveDTO] = [
|
||||
ObjectiveDTO(id=obj.id, name=obj.name, description=obj.description)
|
||||
for obj in objectives
|
||||
]
|
||||
dialog = SectorDialog(
|
||||
self.view, default_name="", rounds=rnd_objs, objectives=obj_dtos
|
||||
)
|
||||
if dialog.exec() != QDialog.DialogCode.Accepted:
|
||||
return
|
||||
name = dialog.get_sector_name()
|
||||
round_id = dialog.get_round_id()
|
||||
major_id = dialog.get_major_id()
|
||||
minor_id = dialog.get_minor_id()
|
||||
influence_id = dialog.get_influence_id()
|
||||
if not self._validate_sector_inputs(
|
||||
name, round_id, major_id, minor_id, influence_id
|
||||
):
|
||||
return
|
||||
self.model.add_sector(
|
||||
self.selected_campaign_id, name, round_id, major_id, minor_id, influence_id
|
||||
)
|
||||
self.is_dirty = True
|
||||
self.refresh(RefreshScope.CAMPAIGN_DETAILS)
|
||||
|
||||
def edit_sector(self, sector_id: str) -> None:
|
||||
sect = self.model.get_sector(sector_id)
|
||||
camp = self.model.get_campaign_by_sector(sector_id)
|
||||
war = self.model.get_war_by_campaign(camp.id)
|
||||
rounds = camp.get_all_rounds()
|
||||
rnd_dto: List[RoundDTO] = [
|
||||
RoundDTO(id=rnd.id, index=i) for i, rnd in enumerate(rounds, start=1)
|
||||
]
|
||||
objectives = war.get_all_objectives()
|
||||
obj_dto: List[ObjectiveDTO] = [
|
||||
ObjectiveDTO(id=obj.id, name=obj.name, description=obj.description)
|
||||
for obj in objectives
|
||||
]
|
||||
sect_dialog = SectorDialog(
|
||||
self.view,
|
||||
default_name=sect.name,
|
||||
rounds=rnd_dto,
|
||||
default_round_id=sect.round_id,
|
||||
objectives=obj_dto,
|
||||
default_major_id=sect.major_objective_id,
|
||||
default_minor_id=sect.minor_objective_id,
|
||||
default_influence_id=sect.influence_objective_id,
|
||||
)
|
||||
if sect_dialog.exec() == QDialog.DialogCode.Accepted:
|
||||
name = sect_dialog.get_sector_name()
|
||||
round_id = sect_dialog.get_round_id()
|
||||
major_id = sect_dialog.get_major_id()
|
||||
minor_id = sect_dialog.get_minor_id()
|
||||
influence_id = sect_dialog.get_influence_id()
|
||||
self.model.update_sector(
|
||||
sector_id,
|
||||
name=name,
|
||||
round_id=round_id,
|
||||
major_id=major_id,
|
||||
minor_id=minor_id,
|
||||
influence_id=influence_id,
|
||||
)
|
||||
|
||||
# Round methods
|
||||
|
||||
def add_round(self) -> None:
|
||||
if not self.selected_campaign_id:
|
||||
return
|
||||
rnd = self.model.add_round(self.selected_campaign_id)
|
||||
self.is_dirty = True
|
||||
self.refresh_and_select(
|
||||
RefreshScope.WARS_TREE, item_type=ItemType.ROUND, item_id=rnd.id
|
||||
)
|
||||
|
||||
# Choice methods
|
||||
|
||||
def edit_round_choice(self, choice_id: str) -> None:
|
||||
round_id = self.selected_round_id
|
||||
if not round_id:
|
||||
return
|
||||
war = self.model.get_war_by_round(round_id)
|
||||
camp = self.model.get_campaign_by_round(round_id)
|
||||
rnd = camp.get_round(round_id)
|
||||
sectors = camp.get_sectors_in_round(round_id)
|
||||
sect_opts: List[SectorDTO] = [
|
||||
SectorDTO(
|
||||
id=sect.id,
|
||||
name=sect.name,
|
||||
round_index=camp.get_round_index(sect.round_id),
|
||||
major=war.get_objective_name(sect.major_objective_id),
|
||||
minor=war.get_objective_name(sect.minor_objective_id),
|
||||
influence=war.get_objective_name(sect.influence_objective_id),
|
||||
)
|
||||
for sect in sectors
|
||||
]
|
||||
choice = rnd.get_choice(choice_id)
|
||||
if not choice:
|
||||
return
|
||||
participant = camp.participants[choice.participant_id]
|
||||
player = self.model.get_player_from_campaign_participant(participant)
|
||||
part_opt = ParticipantOption(id=participant.id, name=player.name)
|
||||
dialog = ChoicesDialog(
|
||||
self.view,
|
||||
participants=[part_opt],
|
||||
default_participant_id=participant.id,
|
||||
sectors=sect_opts,
|
||||
default_priority_id=choice.priority_sector_id,
|
||||
default_secondary_id=choice.secondary_sector_id,
|
||||
default_comment=choice.comment,
|
||||
)
|
||||
if dialog.exec() != QDialog.DialogCode.Accepted:
|
||||
return
|
||||
self.model.update_choice(
|
||||
round_id=round_id,
|
||||
participant_id=participant.id,
|
||||
priority_sector_id=dialog.get_priority_id(),
|
||||
secondary_sector_id=dialog.get_secondary_id(),
|
||||
comment=dialog.get_comment(),
|
||||
)
|
||||
|
||||
# Battle methods
|
||||
|
||||
def edit_round_battle(self, battle_id: str) -> None:
|
||||
round_id = self.selected_round_id
|
||||
if not round_id:
|
||||
return
|
||||
war = self.model.get_war_by_round(round_id)
|
||||
camp = self.model.get_campaign_by_round(round_id)
|
||||
rnd = camp.get_round(round_id)
|
||||
participants = camp.get_all_campaign_participants()
|
||||
battle = rnd.get_battle(battle_id)
|
||||
if not battle:
|
||||
return
|
||||
sect = camp.sectors[battle.sector_id]
|
||||
sect_dto = SectorDTO(
|
||||
id=sect.id,
|
||||
name=sect.name,
|
||||
round_index=camp.get_round_index(sect.round_id),
|
||||
major=war.get_objective_name(sect.major_objective_id),
|
||||
minor=war.get_objective_name(sect.minor_objective_id),
|
||||
influence=war.get_objective_name(sect.influence_objective_id),
|
||||
)
|
||||
|
||||
part_opts: List[ParticipantOption] = []
|
||||
for participant in participants:
|
||||
player = self.model.get_player_from_campaign_participant(participant)
|
||||
part_opts.append(ParticipantOption(id=participant.id, name=player.name))
|
||||
dialog = BattlesDialog(
|
||||
self.view,
|
||||
sectors=[sect_dto],
|
||||
default_sector_id=sect.id,
|
||||
players=part_opts,
|
||||
default_player_1_id=battle.player_1_id,
|
||||
default_player_2_id=battle.player_2_id,
|
||||
default_winner_id=battle.winner_id,
|
||||
default_score=battle.score,
|
||||
default_victory_condition=battle.victory_condition,
|
||||
default_comment=battle.comment,
|
||||
)
|
||||
if dialog.exec() != QDialog.DialogCode.Accepted:
|
||||
return
|
||||
self.model.update_battle(
|
||||
round_id=round_id,
|
||||
sector_id=sect.id,
|
||||
player_1_id=dialog.get_player_1_id(),
|
||||
player_2_id=dialog.get_player_2_id(),
|
||||
winner_id=dialog.get_winner_id(),
|
||||
score=dialog.get_score(),
|
||||
victory_condition=dialog.get_victory_condition(),
|
||||
comment=dialog.get_comment(),
|
||||
)
|
||||
111
src/warchron/controller/navigation_controller.py
Normal file
111
src/warchron/controller/navigation_controller.py
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
from typing import List, TYPE_CHECKING
|
||||
|
||||
from warchron.constants import ItemType, RefreshScope
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from warchron.controller.app_controller import AppController
|
||||
from warchron.controller.dtos import (
|
||||
TreeSelection,
|
||||
ParticipantOption,
|
||||
WarDTO,
|
||||
CampaignDTO,
|
||||
RoundDTO,
|
||||
)
|
||||
|
||||
|
||||
class NavigationController:
|
||||
def __init__(self, app: "AppController"):
|
||||
self.app: AppController = app
|
||||
self.selected_war_id: str | None = None
|
||||
self.selected_campaign_id: str | None = None
|
||||
self.selected_round_id: str | None = None
|
||||
self.update_actions_state()
|
||||
|
||||
# Display methods
|
||||
|
||||
def refresh_players_view(self) -> None:
|
||||
players = self.app.model.get_all_players()
|
||||
players_for_display: List[ParticipantOption] = [
|
||||
ParticipantOption(id=p.id, name=p.name) for p in players
|
||||
]
|
||||
self.app.view.display_players(players_for_display)
|
||||
|
||||
def refresh_wars_view(self) -> None:
|
||||
wars: List[WarDTO] = [
|
||||
WarDTO(
|
||||
id=w.id,
|
||||
name=w.name,
|
||||
year=w.year,
|
||||
_campaigns=[
|
||||
CampaignDTO(
|
||||
id=c.id,
|
||||
name=c.name,
|
||||
month=c.month,
|
||||
_rounds=[
|
||||
RoundDTO(id=r.id, index=c.get_round_index(r.id))
|
||||
for r in c.get_all_rounds()
|
||||
],
|
||||
)
|
||||
for c in w.get_all_campaigns()
|
||||
],
|
||||
)
|
||||
for w in self.app.model.get_all_wars()
|
||||
]
|
||||
self.app.view.display_wars_tree(wars)
|
||||
|
||||
def refresh(self, scope: RefreshScope) -> None:
|
||||
match scope:
|
||||
case RefreshScope.PLAYERS_LIST:
|
||||
self.app.navigation.refresh_players_view()
|
||||
case RefreshScope.WARS_TREE:
|
||||
self.app.navigation.refresh_wars_view()
|
||||
case RefreshScope.WAR_DETAILS:
|
||||
if self.selected_war_id:
|
||||
self.app.view.show_details(ItemType.WAR)
|
||||
self.app.wars._fill_war_details(self.selected_war_id)
|
||||
case RefreshScope.CAMPAIGN_DETAILS:
|
||||
if self.selected_campaign_id:
|
||||
self.app.view.show_details(ItemType.CAMPAIGN)
|
||||
self.app.campaigns._fill_campaign_details(self.selected_campaign_id)
|
||||
case RefreshScope.ROUND_DETAILS:
|
||||
if self.selected_round_id:
|
||||
self.app.view.show_details(ItemType.ROUND)
|
||||
self.app.rounds._fill_round_details(self.selected_round_id)
|
||||
self.app.update_window_title()
|
||||
|
||||
def refresh_and_select(
|
||||
self, scope: RefreshScope, *, item_type: ItemType, item_id: str
|
||||
) -> None:
|
||||
self.refresh(scope)
|
||||
self.app.view.select_tree_item(item_type=item_type, item_id=item_id)
|
||||
|
||||
# Commands methods
|
||||
|
||||
def on_tree_selection_changed(self, selection: TreeSelection | None) -> None:
|
||||
self.selected_war_id = None
|
||||
self.selected_campaign_id = None
|
||||
self.selected_round_id = None
|
||||
if selection:
|
||||
item_type = selection.type
|
||||
item_id = selection.id
|
||||
if item_type == ItemType.WAR:
|
||||
self.selected_war_id = item_id
|
||||
self.app.view.show_details(ItemType.WAR)
|
||||
self.app.wars._fill_war_details(item_id)
|
||||
elif item_type == ItemType.CAMPAIGN:
|
||||
self.selected_campaign_id = item_id
|
||||
self.app.view.show_details(ItemType.CAMPAIGN)
|
||||
self.app.campaigns._fill_campaign_details(item_id)
|
||||
elif item_type == ItemType.ROUND:
|
||||
self.selected_round_id = item_id
|
||||
self.app.view.show_details(ItemType.ROUND)
|
||||
self.app.rounds._fill_round_details(item_id)
|
||||
else:
|
||||
self.app.view.show_details(None)
|
||||
self.update_actions_state()
|
||||
return
|
||||
self.update_actions_state()
|
||||
|
||||
def update_actions_state(self) -> None:
|
||||
self.app.view.set_add_campaign_enabled(self.selected_war_id is not None)
|
||||
self.app.view.set_add_round_enabled(self.selected_campaign_id is not None)
|
||||
42
src/warchron/controller/player_controller.py
Normal file
42
src/warchron/controller/player_controller.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
from typing import TYPE_CHECKING
|
||||
|
||||
from PyQt6.QtWidgets import QMessageBox, QDialog
|
||||
|
||||
from warchron.constants import RefreshScope
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from warchron.controller.app_controller import AppController
|
||||
from warchron.view.player_dialog import PlayerDialog
|
||||
|
||||
|
||||
class PlayerController:
|
||||
def __init__(self, app: "AppController"):
|
||||
self.app: AppController = app
|
||||
|
||||
def _validate_player_inputs(self, name: str) -> bool:
|
||||
if not name.strip():
|
||||
QMessageBox.warning(
|
||||
self.app.view, "Invalid name", "Player name cannot be empty."
|
||||
)
|
||||
return False
|
||||
return True
|
||||
|
||||
def add_player(self) -> None:
|
||||
dialog = PlayerDialog(self.app.view)
|
||||
result = dialog.exec() # modal blocking dialog
|
||||
if result == QDialog.DialogCode.Accepted:
|
||||
name = dialog.get_player_name()
|
||||
if not self._validate_player_inputs(name):
|
||||
return
|
||||
self.app.model.add_player(name)
|
||||
self.app.is_dirty = True
|
||||
self.app.navigation.refresh(RefreshScope.PLAYERS_LIST)
|
||||
|
||||
def edit_player(self, player_id: str) -> None:
|
||||
play = self.app.model.get_player(player_id)
|
||||
player_dialog = PlayerDialog(self.app.view, default_name=play.name)
|
||||
if player_dialog.exec() == QDialog.DialogCode.Accepted:
|
||||
name = player_dialog.get_player_name()
|
||||
if not self._validate_player_inputs(name):
|
||||
return
|
||||
self.app.model.update_player(player_id, name=name)
|
||||
201
src/warchron/controller/round_controller.py
Normal file
201
src/warchron/controller/round_controller.py
Normal file
|
|
@ -0,0 +1,201 @@
|
|||
from typing import List, TYPE_CHECKING
|
||||
|
||||
from PyQt6.QtWidgets import QDialog
|
||||
|
||||
from warchron.constants import ItemType, RefreshScope
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from warchron.controller.app_controller import AppController
|
||||
from warchron.controller.dtos import ParticipantOption, SectorDTO, ChoiceDTO, BattleDTO
|
||||
|
||||
from warchron.view.choices_dialog import ChoicesDialog
|
||||
from warchron.view.battles_dialog import BattlesDialog
|
||||
|
||||
|
||||
class RoundController:
|
||||
def __init__(self, app: "AppController"):
|
||||
self.app = app
|
||||
|
||||
def _fill_round_details(self, round_id: str) -> None:
|
||||
rnd = self.app.model.get_round(round_id)
|
||||
camp = self.app.model.get_campaign_by_round(round_id)
|
||||
self.app.view.show_round_details(index=camp.get_round_index(round_id))
|
||||
participants = self.app.model.get_round_participants(round_id)
|
||||
sectors = camp.get_sectors_in_round(round_id)
|
||||
choices_for_display: List[ChoiceDTO] = []
|
||||
for part in participants:
|
||||
choice = rnd.get_choice(part.id)
|
||||
if not choice:
|
||||
choice = self.app.model.create_choice(
|
||||
round_id=rnd.id, participant_id=part.id
|
||||
)
|
||||
priority_name = (
|
||||
camp.get_sector_name(choice.priority_sector_id)
|
||||
if choice.priority_sector_id is not None
|
||||
else ""
|
||||
)
|
||||
secondary_name = (
|
||||
camp.get_sector_name(choice.secondary_sector_id)
|
||||
if choice.secondary_sector_id is not None
|
||||
else ""
|
||||
)
|
||||
choices_for_display.append(
|
||||
ChoiceDTO(
|
||||
id=choice.participant_id,
|
||||
participant_name=self.app.model.get_participant_name(
|
||||
part.war_participant_id
|
||||
),
|
||||
priority_sector=priority_name,
|
||||
secondary_sector=secondary_name,
|
||||
comment=choice.comment,
|
||||
)
|
||||
)
|
||||
self.app.view.display_round_choices(choices_for_display)
|
||||
battles_for_display: List[BattleDTO] = []
|
||||
for sect in sectors:
|
||||
battle = rnd.get_battle(sect.id)
|
||||
if not battle:
|
||||
battle = self.app.model.create_battle(
|
||||
round_id=rnd.id, sector_id=sect.id
|
||||
)
|
||||
if battle.player_1_id:
|
||||
camp_part = camp.participants[battle.player_1_id]
|
||||
player_1_name = self.app.model.get_participant_name(
|
||||
camp_part.war_participant_id
|
||||
)
|
||||
else:
|
||||
player_1_name = ""
|
||||
if battle.player_2_id:
|
||||
camp_part = camp.participants[battle.player_2_id]
|
||||
player_2_name = self.app.model.get_participant_name(
|
||||
camp_part.war_participant_id
|
||||
)
|
||||
else:
|
||||
player_2_name = ""
|
||||
if battle.winner_id:
|
||||
camp_part = camp.participants[battle.winner_id]
|
||||
winner_name = self.app.model.get_participant_name(
|
||||
camp_part.war_participant_id
|
||||
)
|
||||
else:
|
||||
winner_name = ""
|
||||
battles_for_display.append(
|
||||
BattleDTO(
|
||||
id=battle.sector_id,
|
||||
sector_name=camp.get_sector_name(battle.sector_id),
|
||||
player_1=player_1_name,
|
||||
player_2=player_2_name,
|
||||
winner=winner_name,
|
||||
score=battle.score,
|
||||
victory_condition=battle.victory_condition,
|
||||
comment=battle.comment,
|
||||
)
|
||||
)
|
||||
self.app.view.display_round_battles(battles_for_display)
|
||||
|
||||
def add_round(self) -> None:
|
||||
if not self.app.navigation.selected_campaign_id:
|
||||
return
|
||||
rnd = self.app.model.add_round(self.app.navigation.selected_campaign_id)
|
||||
self.app.is_dirty = True
|
||||
self.app.navigation.refresh_and_select(
|
||||
RefreshScope.WARS_TREE, item_type=ItemType.ROUND, item_id=rnd.id
|
||||
)
|
||||
|
||||
# Choice methods
|
||||
|
||||
def edit_round_choice(self, choice_id: str) -> None:
|
||||
round_id = self.app.navigation.selected_round_id
|
||||
if not round_id:
|
||||
return
|
||||
war = self.app.model.get_war_by_round(round_id)
|
||||
camp = self.app.model.get_campaign_by_round(round_id)
|
||||
rnd = camp.get_round(round_id)
|
||||
sectors = camp.get_sectors_in_round(round_id)
|
||||
sect_opts: List[SectorDTO] = [
|
||||
SectorDTO(
|
||||
id=sect.id,
|
||||
name=sect.name,
|
||||
round_index=camp.get_round_index(sect.round_id),
|
||||
major=war.get_objective_name(sect.major_objective_id),
|
||||
minor=war.get_objective_name(sect.minor_objective_id),
|
||||
influence=war.get_objective_name(sect.influence_objective_id),
|
||||
)
|
||||
for sect in sectors
|
||||
]
|
||||
choice = rnd.get_choice(choice_id)
|
||||
if not choice:
|
||||
return
|
||||
participant = camp.participants[choice.participant_id]
|
||||
player = self.app.model.get_player_from_campaign_participant(participant)
|
||||
part_opt = ParticipantOption(id=participant.id, name=player.name)
|
||||
dialog = ChoicesDialog(
|
||||
self.app.view,
|
||||
participants=[part_opt],
|
||||
default_participant_id=participant.id,
|
||||
sectors=sect_opts,
|
||||
default_priority_id=choice.priority_sector_id,
|
||||
default_secondary_id=choice.secondary_sector_id,
|
||||
default_comment=choice.comment,
|
||||
)
|
||||
if dialog.exec() != QDialog.DialogCode.Accepted:
|
||||
return
|
||||
self.app.model.update_choice(
|
||||
round_id=round_id,
|
||||
participant_id=participant.id,
|
||||
priority_sector_id=dialog.get_priority_id(),
|
||||
secondary_sector_id=dialog.get_secondary_id(),
|
||||
comment=dialog.get_comment(),
|
||||
)
|
||||
|
||||
# Battle methods
|
||||
|
||||
def edit_round_battle(self, battle_id: str) -> None:
|
||||
round_id = self.app.navigation.selected_round_id
|
||||
if not round_id:
|
||||
return
|
||||
war = self.app.model.get_war_by_round(round_id)
|
||||
camp = self.app.model.get_campaign_by_round(round_id)
|
||||
rnd = camp.get_round(round_id)
|
||||
participants = camp.get_all_campaign_participants()
|
||||
battle = rnd.get_battle(battle_id)
|
||||
if not battle:
|
||||
return
|
||||
sect = camp.sectors[battle.sector_id]
|
||||
sect_dto = SectorDTO(
|
||||
id=sect.id,
|
||||
name=sect.name,
|
||||
round_index=camp.get_round_index(sect.round_id),
|
||||
major=war.get_objective_name(sect.major_objective_id),
|
||||
minor=war.get_objective_name(sect.minor_objective_id),
|
||||
influence=war.get_objective_name(sect.influence_objective_id),
|
||||
)
|
||||
|
||||
part_opts: List[ParticipantOption] = []
|
||||
for participant in participants:
|
||||
player = self.app.model.get_player_from_campaign_participant(participant)
|
||||
part_opts.append(ParticipantOption(id=participant.id, name=player.name))
|
||||
dialog = BattlesDialog(
|
||||
self.app.view,
|
||||
sectors=[sect_dto],
|
||||
default_sector_id=sect.id,
|
||||
players=part_opts,
|
||||
default_player_1_id=battle.player_1_id,
|
||||
default_player_2_id=battle.player_2_id,
|
||||
default_winner_id=battle.winner_id,
|
||||
default_score=battle.score,
|
||||
default_victory_condition=battle.victory_condition,
|
||||
default_comment=battle.comment,
|
||||
)
|
||||
if dialog.exec() != QDialog.DialogCode.Accepted:
|
||||
return
|
||||
self.app.model.update_battle(
|
||||
round_id=round_id,
|
||||
sector_id=sect.id,
|
||||
player_1_id=dialog.get_player_1_id(),
|
||||
player_2_id=dialog.get_player_2_id(),
|
||||
winner_id=dialog.get_winner_id(),
|
||||
score=dialog.get_score(),
|
||||
victory_condition=dialog.get_victory_condition(),
|
||||
comment=dialog.get_comment(),
|
||||
)
|
||||
161
src/warchron/controller/war_controller.py
Normal file
161
src/warchron/controller/war_controller.py
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
from typing import List, TYPE_CHECKING
|
||||
|
||||
from PyQt6.QtWidgets import QMessageBox, QDialog
|
||||
|
||||
from warchron.constants import ItemType, RefreshScope
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from warchron.controller.app_controller import AppController
|
||||
from warchron.controller.dtos import (
|
||||
ParticipantOption,
|
||||
WarParticipantDTO,
|
||||
ObjectiveDTO,
|
||||
)
|
||||
from warchron.view.war_dialog import WarDialog
|
||||
from warchron.view.objective_dialog import ObjectiveDialog
|
||||
from warchron.view.war_participant_dialog import WarParticipantDialog
|
||||
|
||||
|
||||
class WarController:
|
||||
def __init__(self, app: "AppController"):
|
||||
self.app = app
|
||||
|
||||
def _fill_war_details(self, war_id: str) -> None:
|
||||
war = self.app.model.get_war(war_id)
|
||||
self.app.view.show_war_details(name=war.name, year=war.year)
|
||||
objectives = war.get_all_objectives()
|
||||
objectives_for_display: List[ObjectiveDTO] = [
|
||||
ObjectiveDTO(id=obj.id, name=obj.name, description=obj.description)
|
||||
for obj in objectives
|
||||
]
|
||||
self.app.view.display_war_objectives(objectives_for_display)
|
||||
war_parts = war.get_all_war_participants()
|
||||
participants_for_display: List[WarParticipantDTO] = [
|
||||
WarParticipantDTO(
|
||||
id=p.id,
|
||||
player_name=self.app.model.get_player_name(p.player_id),
|
||||
faction=p.faction,
|
||||
)
|
||||
for p in war_parts
|
||||
]
|
||||
self.app.view.display_war_participants(participants_for_display)
|
||||
|
||||
def _validate_war_inputs(self, name: str, year: int) -> bool:
|
||||
if not name.strip():
|
||||
QMessageBox.warning(
|
||||
self.app.view, "Invalid name", "War name cannot be empty."
|
||||
)
|
||||
return False
|
||||
if not (1970 <= year <= 3000):
|
||||
QMessageBox.warning(
|
||||
self.app.view, "Invalid year", "Year must be between 1970 and 3000."
|
||||
)
|
||||
return False
|
||||
return True
|
||||
|
||||
def add_war(self) -> None:
|
||||
dialog = WarDialog(
|
||||
self.app.view, default_year=self.app.model.get_default_war_values()["year"]
|
||||
)
|
||||
result = dialog.exec() # modal blocking dialog
|
||||
if result == QDialog.DialogCode.Accepted:
|
||||
name = dialog.get_war_name()
|
||||
year = dialog.get_war_year()
|
||||
if not self._validate_war_inputs(name, year):
|
||||
return
|
||||
war = self.app.model.add_war(name, year)
|
||||
self.app.is_dirty = True
|
||||
self.app.navigation.refresh_and_select(
|
||||
RefreshScope.WARS_TREE, item_type=ItemType.WAR, item_id=war.id
|
||||
)
|
||||
|
||||
def edit_war(self, war_id: str) -> None:
|
||||
war = self.app.model.get_war(war_id)
|
||||
war_dialog = WarDialog(
|
||||
self.app.view, default_name=war.name, default_year=war.year
|
||||
)
|
||||
if war_dialog.exec() == QDialog.DialogCode.Accepted:
|
||||
name = war_dialog.get_war_name()
|
||||
year = war_dialog.get_war_year()
|
||||
if not self._validate_war_inputs(name, year):
|
||||
return
|
||||
self.app.model.update_war(war_id, name=name, year=year)
|
||||
|
||||
# Objective methods
|
||||
|
||||
def _validate_objective_inputs(self, name: str, description: str) -> bool:
|
||||
if not name.strip():
|
||||
QMessageBox.warning(
|
||||
self.app.view, "Invalid name", "Objective name cannot be empty."
|
||||
)
|
||||
return False
|
||||
return True
|
||||
|
||||
def add_objective(self) -> None:
|
||||
if not self.app.navigation.selected_war_id:
|
||||
return
|
||||
dialog = ObjectiveDialog(self.app.view)
|
||||
if dialog.exec() != QDialog.DialogCode.Accepted:
|
||||
return
|
||||
name = dialog.get_objective_name()
|
||||
description = dialog.get_objective_description()
|
||||
if not self._validate_objective_inputs(name, description):
|
||||
return
|
||||
self.app.model.add_objective(
|
||||
self.app.navigation.selected_war_id, name, description
|
||||
)
|
||||
self.app.is_dirty = True
|
||||
self.app.navigation.refresh(RefreshScope.WAR_DETAILS)
|
||||
|
||||
def edit_objective(self, objective_id: str) -> None:
|
||||
obj = self.app.model.get_objective(objective_id)
|
||||
obj_dialog = ObjectiveDialog(
|
||||
self.app.view, default_name=obj.name, default_description=obj.description
|
||||
)
|
||||
if obj_dialog.exec() == QDialog.DialogCode.Accepted:
|
||||
name = obj_dialog.get_objective_name()
|
||||
description = obj_dialog.get_objective_description()
|
||||
if not self._validate_objective_inputs(name, description):
|
||||
return
|
||||
self.app.model.update_objective(
|
||||
objective_id, name=name, description=description
|
||||
)
|
||||
|
||||
# War participant methods
|
||||
|
||||
def add_war_participant(self) -> None:
|
||||
if not self.app.navigation.selected_war_id:
|
||||
return
|
||||
players = self.app.model.get_available_players(
|
||||
self.app.navigation.selected_war_id
|
||||
)
|
||||
play_opts: List[ParticipantOption] = [
|
||||
ParticipantOption(id=p.id, name=p.name) for p in players
|
||||
]
|
||||
dialog = WarParticipantDialog(self.app.view, players=play_opts)
|
||||
if dialog.exec() != QDialog.DialogCode.Accepted:
|
||||
return
|
||||
player_id = dialog.get_player_id()
|
||||
faction = dialog.get_participant_faction()
|
||||
if not player_id:
|
||||
return
|
||||
self.app.model.add_war_participant(
|
||||
self.app.navigation.selected_war_id, player_id, faction
|
||||
)
|
||||
self.app.is_dirty = True
|
||||
self.app.navigation.refresh(RefreshScope.WAR_DETAILS)
|
||||
|
||||
def edit_war_participant(self, participant_id: str) -> None:
|
||||
war_part = self.app.model.get_war_participant(participant_id)
|
||||
player = self.app.model.get_player(war_part.player_id)
|
||||
play_opt = ParticipantOption(id=player.id, name=player.name)
|
||||
war_part_dialog = WarParticipantDialog(
|
||||
self.app.view,
|
||||
players=[play_opt],
|
||||
default_player_id=war_part.id,
|
||||
default_faction=war_part.faction,
|
||||
editable_player=False,
|
||||
)
|
||||
if war_part_dialog.exec() == QDialog.DialogCode.Accepted:
|
||||
faction = war_part_dialog.get_participant_faction()
|
||||
self.app.model.update_war_participant(participant_id, faction=faction)
|
||||
Loading…
Add table
Add a link
Reference in a new issue