Spaces:
Running
Running
| # ---------------------------- | |
| # INVENTORY & CREDITS SYSTEM | |
| # ---------------------------- | |
| def add_item(state, item_data, image_path=None): | |
| """ | |
| Keşfedilen eşyayı player state'ine ekler. | |
| Parametreler: | |
| state : Mevcut oyuncu state dict'i (doğrudan güncellenir, referans üzerinden). | |
| item_data : LLM'den gelen dict — en az {"name": "...", "visual_prompt": "..."} içermeli. | |
| image_path: VLM tarafından üretilen görselin disk yolu (opsiyonel). | |
| Dönüş: Güncellenmiş state. | |
| """ | |
| if "inventory" not in state: | |
| state["inventory"] = [] | |
| item_name = item_data.get("name", "Unknown Item").strip() | |
| # --- Duplicate koruması (aynı isimde eşya ekleme) --- | |
| existing_names = {item["name"].lower() for item in state["inventory"]} | |
| if item_name.lower() in existing_names: | |
| print(f"--- DEBUG INFO [INVENTORY]: '{item_name}' already in inventory, skipping duplicate.") | |
| return state | |
| entry = { | |
| "name": item_name, | |
| "description": item_data.get("visual_prompt", ""), | |
| "image_path": image_path, # None ise galeri bu item'ı atlar | |
| } | |
| state["inventory"].append(entry) | |
| print(f"--- DEBUG INFO [INVENTORY]: '{item_name}' added. Total items: {len(state['inventory'])}") | |
| return state | |
| def update_credits(state, delta): | |
| """ | |
| Oyuncunun kredilerini delta kadar artırır veya azaltır. | |
| Bakiye asla negatife düşmez (min 0). | |
| Parametreler: | |
| state: Oyuncu state dict'i. | |
| delta: int — pozitif (kazanç) ya da negatif (harcama). | |
| Dönüş: Güncellenmiş state. | |
| """ | |
| if "credits" not in state: | |
| state["credits"] = 0 # Güvenlik: state'de yoksa sıfırdan başlat | |
| old_balance = state["credits"] | |
| state["credits"] = max(0, old_balance + delta) | |
| direction = "earned" if delta >= 0 else "spent" | |
| print( | |
| f"--- DEBUG INFO [INVENTORY]: Credits {direction}: {delta:+d} | " | |
| f"{old_balance} → {state['credits']} credits" | |
| ) | |
| return state | |
| # ---------------------------- | |
| # UI HELPERS | |
| # ---------------------------- | |
| def get_inventory_markdown(state): | |
| """ | |
| Gradio Markdown bileşenine verilecek envanter metnini üretir. | |
| Kredi bakiyesi + eşya listesi. | |
| """ | |
| inventory = state.get("inventory", []) | |
| credits = state.get("credits", 0) | |
| lines = [f"## 💰 {credits} Credits\n"] | |
| if not inventory: | |
| lines.append("*Your pack is empty. The galaxy has yet to offer you anything worth keeping.*") | |
| else: | |
| lines.append(f"### 🎒 Inventory — {len(inventory)} item(s)\n") | |
| for i, item in enumerate(inventory, start=1): | |
| lines.append(f"**{i}. {item['name']}**") | |
| desc = item.get("description", "") | |
| if desc: | |
| short_desc = desc if len(desc) <= 90 else desc[:87] + "…" | |
| lines.append(f"*{short_desc}*") | |
| lines.append("") # boş satır (görsel aralık) | |
| return "\n".join(lines) | |
| def get_inventory_images(state): | |
| """ | |
| Gradio Gallery bileşenine verilecek (image_path, caption) tuple listesini döndürür. | |
| image_path'i None olan eşyalar atlanır. | |
| """ | |
| inventory = state.get("inventory", []) | |
| return [ | |
| (item["image_path"], item["name"]) | |
| for item in inventory | |
| if item.get("image_path") | |
| ] |