File size: 7,415 Bytes
90d81ff
f834d3b
 
 
 
 
 
886a3fc
f8e47c2
68fb7fc
6da7ff4
24e80a3
f834d3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90d81ff
 
 
 
 
11dec97
 
90d81ff
f8e47c2
90d81ff
 
 
 
 
 
 
11dec97
90d81ff
637bf4e
886a3fc
6da7ff4
11dec97
886a3fc
11dec97
886a3fc
 
19252c8
90d81ff
 
 
 
 
11dec97
 
 
 
 
 
 
 
90d81ff
11dec97
 
 
 
 
 
 
 
 
 
 
24e80a3
 
 
 
90d81ff
d78affc
90d81ff
d78affc
90d81ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6da7ff4
 
90d81ff
11dec97
f8e47c2
d78affc
f8e47c2
 
68fb7fc
24e80a3
90d81ff
 
5fe82f2
90d81ff
11dec97
 
90d81ff
 
11dec97
 
 
 
6da7ff4
 
68fb7fc
 
 
d78affc
11dec97
68fb7fc
24e80a3
90d81ff
 
 
 
11dec97
6da7ff4
 
11dec97
886a3fc
6da7ff4
 
 
 
d78affc
11dec97
68fb7fc
24e80a3
90d81ff
 
 
11dec97
90d81ff
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
import gradio as gr
import json
import os
import uuid
import time
import re
from datasets import Dataset, load_dataset
from systems.reputation_system import format_reputation_for_ui
from systems.inventory_system import get_inventory_markdown, get_inventory_images
from systems.relationship_system import get_relationships_markdown
from systems.house_points_system import format_house_points_for_ui
from systems.spell_system import get_known_spells_markdown
# ----------------------------
# SAVE / LOAD (HF DATASET)
# ----------------------------

def save_to_hf(dataset_repo, state_json, history_json):
    data = {
        "state": [state_json],
        "history": [history_json]
    }

    ds = Dataset.from_dict(data)

    ds.push_to_hub(
        dataset_repo,
        private=True
    )
    
    return "Game Saved ☁️"

def load_from_hf(dataset_repo):
    ds = load_dataset(
        dataset_repo,
        split="train"
    )

    state_json = ds[0]["state"]
    history_json = ds[0]["history"]

    return state_json, history_json

def load_and_enter_game(dataset_repo, from_char_screen=True):
    """
    Load saved game and switch to game screen.
    from_char_screen=True  → hide char/stat screens and show game screen.
    from_char_screen=False → already in game screen, just refresh data.
    """

    try:
        state_json, history_json = load_from_hf(dataset_repo)
        state = json.loads(state_json)
        history = json.loads(history_json) if history_json else []

        # EKSİK STATLARI DOLDUR (eski kayıtlar için)
        default_stats = {
            "strength": 5, "agility": 5, "intelligence": 5,
            "charisma": 5, "willpower": 5,
            "light_dark": 0, "order_chaos": 0, "trouble_level": 0,
            "reputation": 0,           # ← reputation axis
            "house_points": 0,
            "house": "Gryffindor", "year": 3,
            "traits": [], "aspirations": [],
            "credits": 150, "inventory": [],
            "relationships": {},
            "dynamic_npcs": {},
            "known_spells": {},
        }
        for key, default in default_stats.items():
            if key not in state:
                state[key] = default

        # Build stat display (includes traits & aspirations if present)
        trait_line = ""
        if state.get("traits"):
            trait_line = f"\n**Traits:** {' · '.join(state['traits'])}"
        asp_line = ""
        if state.get("aspirations"):
            asp_line = f"\n**Aspirations:** {' · '.join(state['aspirations'])}"

        # Statları formatla
        stats_md = (
            f"**House:** {state['house']} · **Year:** {state['year']}"
            f"{trait_line}{asp_line}\n\n---\n"
            f"**Strength:** {state['strength']}  \n"
            f"**Agility:** {state['agility']}  \n"
            f"**Intelligence:** {state['intelligence']}  \n"
            f"**Charisma:** {state['charisma']}  \n"
            f"**Willpower:** {state['willpower']}  "
        )
        
        # Inventory helpers — called AFTER state is populated
        inventory_md        = get_inventory_markdown(state)
        inventory_images    = get_inventory_images(state)
        relations_md        = get_relationships_markdown(state)
        known_spells_md     = get_known_spells_markdown(state)
        
        # Kaydedilmiş model seçimlerini al (yoksa "Auto")
        saved_model = state.get("saved_model", "Auto (Language Routing)")
        saved_image_model = state.get("saved_image_model", "Auto (Recommended)")
        
        # Eğer karakter ekranından geliyorsak, stat_screen'i gizleyip game_screen'i göster
        if from_char_screen:
            char_screen_update = gr.update(visible=False)
            stat_screen_update = gr.update(visible=False)
            game_screen_update = gr.update(visible=True)
        else:
            char_screen_update = gr.update()
            stat_screen_update = gr.update()
            game_screen_update = gr.update()
        
        return (
            json.dumps(state),          # player_state
            history,                    # chatbot
            char_screen_update,         # char_screen visibility
            stat_screen_update,         # stat_screen visibility
            game_screen_update,         # game_screen visibility
            state.get("avatar"),        # profile_pic
            f"### {state.get('name', 'Unknown')}",  # profile_info
            state["light_dark"],        # light_dark_slider
            state["order_chaos"],       # order_chaos_slider
            format_reputation_for_ui(state.get("reputation", 0)),       # reputation_text
            format_house_points_for_ui(state.get("house_points", 0)),   # house_points_text
            stats_md,                   # stat_display
            gr.update(),                # scene_image (leave as-is)
            saved_model,                # model_selector value
            saved_image_model,          # image_model_selector value
            inventory_md,
            inventory_images,
            relations_md,
            known_spells_md,
        )
    except Exception as e:
        print(f"--- DEBUG INFO [SAVE SYSTEM]: Load error: {e}")
        # Hata durumunda mevcut ekranda kal, uyarı göster (isteğe bağlı)
        empty_inventory_md     = "*Your pack is empty.*"
        empty_inventory_images = []
        if from_char_screen:
            return (
                "{}", [],
                gr.update(visible=True), gr.update(visible=False), gr.update(visible=False),
                None, "### Error loading game",
                0, 0,
                "🫥 **Unassuming** (0)",   # reputation_text fallback
                "🏠 **+0** House Points — 😐 Unproven",   # house_points_text fallback
                gr.update(),                      # stat_display
                gr.update(),                      # scene_image (was missing — shifted every field after it)
                "Auto (Language Routing)",        # model_selector
                "Auto (Recommended)",             # image_model_selector
                empty_inventory_md, empty_inventory_images,
                "*You haven't gotten close to anyone yet.*",
                "*You haven't learned any spells yet.*",
            )
        else:
            # Oyun içinde hata olursa sadece state'i değiştirme
            return (
                "{}", [],
                gr.update(visible=True), gr.update(visible=False), gr.update(visible=False),
                None, "### Error loading game",
                0, 0,
                "🫥 **Unassuming** (0)",   # reputation_text fallback
                "🏠 **+0** House Points — 😐 Unproven",   # house_points_text fallback
                gr.update(),                      # stat_display
                gr.update(),                      # scene_image (was missing — shifted every field after it)
                "Auto (Language Routing)",        # model_selector
                "Auto (Recommended)",             # image_model_selector
                empty_inventory_md, empty_inventory_images,
                "*You haven't gotten close to anyone yet.*",
                "*You haven't learned any spells yet.*",
            )

def load_in_game(dataset_repo):
    """Convenience wrapper for the in-game load button."""
    return load_and_enter_game(dataset_repo, from_char_screen=False)