Mini_DungeonGame / utils.py
sqfoo's picture
Update utils.py
120eab0 verified
Raw
History Blame Contribute Delete
3.92 kB
import re, json
from player import Player
PHYSICAL_ATTACK = 'PHYSICAL_ATTACK'
MAGICAL_ATTACK = 'MAGICAL_ATTACK'
SPEND_MONEY = "SPEND_MONEY"
LEAVE = "LEAVE"
PHYSICAL_REFILL = "PHYSICAL_REFILL"
MAGICAL_REFILL = 'MAGICAL_REFILL'
EARN_MONEY = "EARN_MONEY"
PROMPT = """
You have to identify the category that the primary action described in the response belongs to.
Here are the strict categories:
- PHYSICAL_ATTACK: Any action that causes or threatens physical damage, like stabbing, punching, firing guns, or weapon coercion.
- MAGICAL_ATTACK: Any action that causes damage with spells or magic, like summoning a monster, striking a target with a flood or thunder.
- SPEND_MONEY: Using money/coins to solve a problem, like bribing an NPC or buying an item.
- PHYSICAL_REFILL: Any action that refills HP, offers medical aid, rests, or performs supportive/altruistic deeds (e.g., helping an injured NPC, patching wounds, resting) without spending coins or items.
- MAGICAL_REFILL: Any action that refills MP, meditates, or channels spiritual energy without spending coins or items.
- EARN_MONEY: Any action focused on earning money (like looting gold, selling items, or doing a job), provided it does not involve a PHYSICAL_ATTACK or MAGICAL_ATTACK.
- LEAVE: Running away, retreating, doing absolutely nothing, or ignoring the situation entirely.
The input format will be [RESPONSE]: "HUMAN RESPONSE"
You must output your answer strictly in the following JSON format:
{"FINAL ANSWER": "YOUR_ANSWER"}
Remember: Identify ONLY the single most dominant primary action. If the action is purely a non-violent, helpful, or restorative interaction, classify it as PHYSICAL_REFILL. If the response is completely empty or completely irrelevant to the game, output LEAVE.
---
EXAMPLES:
Input: [RESPONSE]: "I will bandage his wounds and help him stand up."
Output: {"FINAL ANSWER": "PHYSICAL_REFILL"}
Input: [RESPONSE]: "I see an injured traveler on the road. I will help him."
Output: {"FINAL ANSWER": "PHYSICAL_REFILL"}
Input: [RESPONSE]: "I draw my sword to threaten the shopkeeper, then summon lightning to strike him."
Output: {"FINAL ANSWER": "MAGICAL_ATTACK"}
Input: [RESPONSE]: "I turn around and walk away back into the forest."
Output: {"FINAL ANSWER": "LEAVE"}
"""
INPUT_PROMPT = '\n[RESPONSE]: {}'
def add_attribute(player: Player, attribute: str, amount: int):
attribute = attribute.lower()
if attribute == 'hp':
player.add_hp(amount)
elif attribute == 'mp':
player.add_mp(amount)
elif attribute == 'coin':
player.add_coin(amount)
def minus_attribute(player: Player, attribute: str, amount: int):
attribute = attribute.lower()
if attribute == 'hp':
player.reduce_hp(amount)
elif attribute == 'mp':
player.reduce_mp(amount)
elif attribute == 'coin':
player.reduce_coin(amount)
def check_ending(player: Player):
print('Ending', player.hp, player.mp)
if player.hp >= 50 and player.mp >= 20:
return 10
elif player.hp > 0 and player.mp > 0:
if player.coin <= 5:
return 12
else:
return 11
elif player.mp < 0:
return 13
else:
return 14
def extract_after_final_answer(text: str) -> str:
# re.findall finds every instance of { ... }
# [^}]* ensures we don't accidentally skip over nested structures
matches = re.findall(r'(\{.*?\})', text, re.DOTALL)
if not matches:
return None
# We take the last match [-1]
last_json_str = matches[-1]
try:
# Replace single quotes with double quotes for valid JSON
valid_json_str = last_json_str.replace("'", '"')
data = json.loads(valid_json_str)
return data.get("FINAL ANSWER", "LEAVE")
except json.JSONDecodeError:
print(f'Could not fetch the keywords: "FINAL ANSWER" with the given response: {text}')
return "LEAVE"