Spaces:
Runtime error
Runtime error
Create dialogue_system.py
Browse files- dialogue_system.py +81 -0
dialogue_system.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# dialogue_system.py
|
| 2 |
+
from dataclasses import dataclass
|
| 3 |
+
from typing import Dict, List
|
| 4 |
+
import random
|
| 5 |
+
from game_engine import GauchoCharacter
|
| 6 |
+
|
| 7 |
+
@dataclass
|
| 8 |
+
class DialogNode:
|
| 9 |
+
id: str
|
| 10 |
+
text: str
|
| 11 |
+
options: List[Dict[str, str]]
|
| 12 |
+
cultural_requirement: str = None
|
| 13 |
+
crystal_trigger: str = None
|
| 14 |
+
|
| 15 |
+
class DynamicNarrative:
|
| 16 |
+
"""Sistema de diálogos que se adapta a los valores recolectados"""
|
| 17 |
+
def __init__(self):
|
| 18 |
+
self.dialog_tree = self._build_cultural_dialog_tree()
|
| 19 |
+
self.current_node = "root"
|
| 20 |
+
|
| 21 |
+
def _build_cultural_dialog_tree(self) -> Dict[str, DialogNode]:
|
| 22 |
+
return {
|
| 23 |
+
"root": DialogNode(
|
| 24 |
+
id="root",
|
| 25 |
+
text="¡Che, ¿viste lo que pasa con estos algoritmos?¡Están desdibujando nuestra esencia!",
|
| 26 |
+
options=[
|
| 27 |
+
{"text": "Voy a recuperar los cristales", "target": "mision_principal"},
|
| 28 |
+
{"text": "¿Tenés algún dato útil?", "target": "pistas_culturales"},
|
| 29 |
+
{"text": "Hablemos de nuestra identidad", "target": "reflexion_filosofica"}
|
| 30 |
+
]
|
| 31 |
+
),
|
| 32 |
+
"mision_principal": DialogNode(
|
| 33 |
+
id="mision_principal",
|
| 34 |
+
text="Cada cristal que recuperés fortalecerá nuestra resistencia cultural. ¿Por cuál querés empezar?",
|
| 35 |
+
options=[
|
| 36 |
+
{"text": "Solidaridad", "target": "solidaridad_quest"},
|
| 37 |
+
{"text": "Creatividad", "target": "creatividad_quest"},
|
| 38 |
+
{"text": "Volver", "target": "root"}
|
| 39 |
+
],
|
| 40 |
+
cultural_requirement="basic"
|
| 41 |
+
),
|
| 42 |
+
"solidaridad_quest": DialogNode(
|
| 43 |
+
id="solidaridad_quest",
|
| 44 |
+
text="Para recuperar la Solidaridad, necesitás ayudar a tres comunidades diferentes. ¿Listo para el desafío?",
|
| 45 |
+
options=[
|
| 46 |
+
{"text": "Acepto el reto", "target": "solidaridad_1"},
|
| 47 |
+
{"text": "Necesito prepararme", "target": "mision_principal"}
|
| 48 |
+
],
|
| 49 |
+
crystal_trigger="Solidaridad"
|
| 50 |
+
)
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
def get_current_dialog(self, player: GauchoCharacter) -> DialogNode:
|
| 54 |
+
node = self.dialog_tree[self.current_node]
|
| 55 |
+
return self._filter_options(node, player)
|
| 56 |
+
|
| 57 |
+
def _filter_options(self, node: DialogNode, player: GauchoCharacter) -> DialogNode:
|
| 58 |
+
"""Filtra opciones basadas en progreso del jugador"""
|
| 59 |
+
filtered_options = []
|
| 60 |
+
for option in node.options:
|
| 61 |
+
target_node = self.dialog_tree.get(option['target'], None)
|
| 62 |
+
if target_node:
|
| 63 |
+
if target_node.cultural_requirement == "basic" and not any(player.inventory['cristales'].values()):
|
| 64 |
+
continue
|
| 65 |
+
if target_node.crystal_trigger and not player.inventory['cristales'][target_node.crystal_trigger]:
|
| 66 |
+
continue
|
| 67 |
+
filtered_options.append(option)
|
| 68 |
+
return DialogNode(
|
| 69 |
+
id=node.id,
|
| 70 |
+
text=node.text,
|
| 71 |
+
options=filtered_options,
|
| 72 |
+
cultural_requirement=node.cultural_requirement,
|
| 73 |
+
crystal_trigger=node.crystal_trigger
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
def select_option(self, option_index: int, player: GauchoCharacter) -> str:
|
| 77 |
+
current_node = self.get_current_dialog(player)
|
| 78 |
+
if 0 <= option_index < len(current_node.options):
|
| 79 |
+
self.current_node = current_node.options[option_index]['target']
|
| 80 |
+
return self.get_current_dialog(player).text
|
| 81 |
+
return current_node.text
|