Spaces:
Sleeping
Sleeping
| """ | |
| Dropdown Manager for D'n'D Campaign Manager | |
| Handles dropdown creation and auto-population logic | |
| """ | |
| import gradio as gr | |
| from typing import List, Tuple, Optional | |
| from src.agents.character_agent import CharacterAgent | |
| from src.agents.campaign_agent import CampaignAgent | |
| class DropdownManager: | |
| """Manages dropdowns and their auto-population""" | |
| def __init__(self, character_agent: CharacterAgent, campaign_agent: CampaignAgent): | |
| self.character_agent = character_agent | |
| self.campaign_agent = campaign_agent | |
| def get_character_dropdown_choices(self) -> List[str]: | |
| """Get character choices for dropdown (returns labels)""" | |
| try: | |
| characters = self.character_agent.list_characters() | |
| if not characters: | |
| return [] | |
| # Create dropdown choices with nice labels | |
| choices = [] | |
| for char in characters: | |
| label = f"{char.name} ({char.race.value} {char.character_class.value}, Lvl {char.level})" | |
| choices.append(label) | |
| return choices | |
| except Exception as e: | |
| return [] | |
| def get_character_choices_for_checkboxgroup(self) -> List[Tuple[str, str]]: | |
| """Get list of characters for checkbox selection (label, value pairs)""" | |
| try: | |
| characters = self.character_agent.list_characters() | |
| if not characters: | |
| return [] | |
| # Create choices as "Name (Race Class, Level X)" -> ID | |
| choices = [] | |
| for char in characters: | |
| label = f"{char.name} ({char.race.value} {char.character_class.value}, Level {char.level})" | |
| choices.append((label, char.id)) | |
| return choices | |
| except Exception as e: | |
| return [] | |
| def get_character_id_from_label(self, label: str) -> str: | |
| """Extract character ID from dropdown label""" | |
| try: | |
| # Parse the label to get character name | |
| if not label: | |
| return "" | |
| name = label.split(" (")[0] if " (" in label else label | |
| # Find character by name | |
| characters = self.character_agent.list_characters() | |
| for char in characters: | |
| if char.name == name: | |
| return char.id | |
| return "" | |
| except Exception as e: | |
| return "" | |
| def get_campaign_dropdown_choices(self) -> List[str]: | |
| """Get campaign choices for dropdown""" | |
| try: | |
| campaigns = self.campaign_agent.list_campaigns() | |
| if not campaigns: | |
| return [] | |
| choices = [] | |
| for campaign in campaigns: | |
| label = f"{campaign.name} ({campaign.theme.value}, Session {campaign.current_session})" | |
| choices.append(label) | |
| return choices | |
| except Exception as e: | |
| return [] | |
| def get_campaign_id_from_label(self, label: str) -> str: | |
| """Extract campaign ID from dropdown label""" | |
| try: | |
| if not label: | |
| return "" | |
| name = label.split(" (")[0] if " (" in label else label | |
| campaigns = self.campaign_agent.list_campaigns() | |
| for campaign in campaigns: | |
| if campaign.name == name: | |
| return campaign.id | |
| return "" | |
| except Exception as e: | |
| return "" | |
| def refresh_character_dropdown(self) -> gr.update: | |
| """Refresh character dropdown choices""" | |
| choices = self.get_character_dropdown_choices() | |
| return gr.update(choices=choices, value=None) | |
| def refresh_campaign_dropdown(self) -> gr.update: | |
| """Refresh campaign dropdown choices""" | |
| choices = self.get_campaign_dropdown_choices() | |
| return gr.update(choices=choices, value=None) | |
| def refresh_character_checkboxgroup(self) -> gr.update: | |
| """Refresh character checkbox group choices""" | |
| choices = self.get_character_choices_for_checkboxgroup() | |
| if not choices: | |
| return gr.update(choices=[], value=[]) | |
| return gr.update(choices=choices, value=[]) | |