-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdisplay.py
More file actions
190 lines (168 loc) · 8.58 KB
/
display.py
File metadata and controls
190 lines (168 loc) · 8.58 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import json
import re
from rich.panel import Panel
from rich.console import Console
console = Console()
def _stat_modifier(score):
mod = (score - 10) // 2
if mod >= 0:
return f"+{mod}"
return str(mod)
def render_gm_text(text):
processed = text
processed = re.sub(r'\*\*(.*?)\*\*', r'[bold white]\1[/]', processed)
processed = re.sub(r'\*(.*?)\*', r'[italic cyan]\1[/]', processed)
lines = processed.split('\n')
for i in range(len(lines)):
if lines[i].startswith('#'):
content = lines[i].strip('#').strip()
lines[i] = f"[bold magenta]{content}[/]"
return "\n".join(lines)
def _render_inventory_item(item):
if isinstance(item, dict):
name = item.get('name', str(item))
desc = item.get('description', '')
if desc:
return f" [bold white]{name}[/]\n [dim]{desc}[/]"
return f" [bold white]{name}[/]"
return f" [bold white]{item}[/]"
def format_stats(db_data):
def get(key, default=""):
val = db_data.get(key, default)
if isinstance(val, str):
try:
parsed = json.loads(val)
return parsed
except (json.JSONDecodeError, TypeError):
return val
return val
hp_cur = get('current_hit_points', '?')
hp_max = get('total_hit_points', '?')
ac = get('armor_class', '?')
speed = get('speed', '?')
prof = get('proficiency_bonus', '?')
hd_count = get('hit_dice_count', '?')
hd_size = get('hit_dice_size', '?')
# Character panel
char_lines = []
char_lines.append(f"🧙 [bold white]{get('name')}[/] 🧝 {get('race')} 📖 {get('character_class')}")
char_lines.append(f"⭐ Level {get('level')} 💰 Gold {get('gold')} ✨ XP {get('xp')}")
char_lines.append(f"🎭 {get('background')} ⚖️ {get('alignment')}")
char_stats = Panel("\n".join(char_lines), title="⚔️ Character", border_style="red", expand=False)
# Combat panel
combat_lines = []
combat_lines.append(f"❤️ {hp_cur}/{hp_max} HP 🛡️ AC {ac} 🏃 Speed {speed}")
combat_lines.append(f"⭐ Proficiency +{prof} 🎲 Hit Dice {hd_count}d{hd_size}")
combat_panel = Panel("\n".join(combat_lines), title="🛡️ Combat", border_style="blue", expand=False)
# Stats panel
stats_data = get('stats')
stats_lines = []
if isinstance(stats_data, dict):
stats_lines.append(f"💪 STR {stats_data.get('str', '?')} ({_stat_modifier(stats_data.get('str', 10))}) 🏹 DEX {stats_data.get('dex', '?')} ({_stat_modifier(stats_data.get('dex', 10))})")
stats_lines.append(f"🫀 CON {stats_data.get('con', '?')} ({_stat_modifier(stats_data.get('con', 10))}) 🧠 INT {stats_data.get('int', '?')} ({_stat_modifier(stats_data.get('int', 10))})")
stats_lines.append(f"👁️ WIS {stats_data.get('wis', '?')} ({_stat_modifier(stats_data.get('wis', 10))}) ✨ CHA {stats_data.get('cha', '?')} ({_stat_modifier(stats_data.get('cha', 10))})")
else:
stats_lines.append(str(stats_data))
stats_panel = Panel("\n".join(stats_lines), title="📊 Stats", border_style="green", expand=False)
panels = [char_stats, combat_panel, stats_panel]
# Spellcasting panel (conditional)
spell_data = get('spellcasting')
if isinstance(spell_data, dict):
spell_lines = []
spell_lines.append(f"🔮 {spell_data.get('ability', '?').capitalize()} 📿 DC {spell_data.get('dc', '?')} 🪄 Attack +{spell_data.get('attack_modifier', '?')}")
cantrips = spell_data.get('cantrips', [])
if cantrips:
spell_lines.append(f"✋ {', '.join(str(c) for c in cantrips)}")
spells_known = spell_data.get('spells_known', [])
if spells_known:
spell_lines.append(f"📜 Known: {', '.join(str(s) for s in spells_known)}")
spells_prepared = spell_data.get('spells_prepared', [])
if spells_prepared:
spell_lines.append(f"📖 Prepared: {', '.join(str(s) for s in spells_prepared)}")
spellbook = spell_data.get('spellbook', [])
if spellbook:
spell_lines.append(f"📕 Spellbook: {', '.join(str(s) for s in spellbook)}")
slots = spell_data.get('slots', {})
if slots:
slot_parts = [f"Lv{k}: {v}" for k, v in slots.items()]
spell_lines.append(f"🔥 {' | '.join(slot_parts)}")
panels.append(Panel("\n".join(spell_lines), title="🔮 Spellcasting", border_style="magenta", expand=False))
# Skills & Proficiencies panel
prof_lines = []
skills = get('skills')
if isinstance(skills, list) and skills:
prof_lines.append(f"🎯 Skills: {', '.join(str(s) for s in skills)}")
saves = get('saves')
if isinstance(saves, list) and saves:
prof_lines.append(f"💫 Saves: {', '.join(str(s) for s in saves)}")
armor_prof = get('armor_proficiencies')
if isinstance(armor_prof, list) and armor_prof:
prof_lines.append(f"🛡️ Armor: {', '.join(str(a) for a in armor_prof)}")
weapon_prof = get('weapon_proficiencies')
if isinstance(weapon_prof, list) and weapon_prof:
prof_lines.append(f"⚔️ Weapons: {', '.join(str(w) for w in weapon_prof)}")
tool_prof = get('tool_proficiencies')
if isinstance(tool_prof, list) and tool_prof:
prof_lines.append(f"🔧 Tools: {', '.join(str(t) for t in tool_prof)}")
features = get('features')
if isinstance(features, list) and features:
prof_lines.append(f"🌟 Features: {', '.join(str(f) for f in features)}")
languages = get('languages')
if isinstance(languages, list) and languages:
prof_lines.append(f"🗣️ Languages: {', '.join(str(l) for l in languages)}")
if prof_lines:
panels.append(Panel("\n".join(prof_lines), title="🎯 Skills & Proficiencies", border_style="yellow", expand=False))
# Inventory & Consumables panel
inv_lines = []
inventory = get('inventory')
if isinstance(inventory, list) and inventory:
for item in inventory:
inv_lines.append(_render_inventory_item(item))
consumables = get('consumables')
if isinstance(consumables, dict) and consumables:
for name, qty in consumables.items():
inv_lines.append(f" 🧪 [bold white]{name}[/]: {qty}")
if inv_lines:
panels.append(Panel("\n".join(inv_lines), title="🎒 Inventory & Consumables", border_style="cyan", expand=False))
reputation = get('reputation')
if isinstance(reputation, dict):
rep_lines = []
for kname, factions in reputation.items():
if isinstance(factions, dict):
for faction, entries in factions.items():
if isinstance(entries, list) and entries:
rep_lines.append(f" 🏆 [bold white]{kname.title()} — {faction.title()}[/]")
for entry in entries:
if isinstance(entry, dict):
rep_lines.append(f" [bold]{entry.get('name', '?')}[/]")
desc = entry.get('description', '')
if desc:
rep_lines.append(f" [dim]{desc}[/]")
else:
rep_lines.append(f" {entry}")
if rep_lines:
panels.append(Panel("\n".join(rep_lines), title="🏆 Reputation", border_style="yellow", expand=False))
active_effects = get('active_effects')
buff_data = get('_active_buff_data')
current_thp = get('temporary_hit_points')
if isinstance(active_effects, list) and active_effects:
buff_lines = []
for spell_name in active_effects:
entries = []
if isinstance(buff_data, dict) and spell_name in buff_data:
for entry in buff_data[spell_name]:
field = entry.get('field', '?')
if field == "temporary_hit_points" and isinstance(current_thp, (int, float)):
entries.append(f"{field:>20} {int(current_thp)}")
else:
delta = entry.get('delta', 0)
sign = '+' if delta >= 0 else ''
entries.append(f"{field:>20} {sign}{delta}")
if entries:
buff_lines.append(f" ✨ [bold white]{spell_name}[/]")
for e in entries:
buff_lines.append(f" {e}")
else:
buff_lines.append(f" ✨ [bold white]{spell_name}[/]")
panels.append(Panel("\n".join(buff_lines), title="🌀 Active Effects", border_style="bright_magenta", expand=False))
return panels