Spaces:
Sleeping
Sleeping
File size: 4,156 Bytes
71b378e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
"""
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=[])
|