File size: 3,920 Bytes
0f3ad1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120eab0
0f3ad1b
 
 
 
 
 
120eab0
0f3ad1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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"