summaryrefslogtreecommitdiff
blob: 5962b6d1790e35f1d334a1c3fd941ca4473ba072 (plain)
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
import sys
import argparse

import tonkadur

parser = argparse.ArgumentParser(
    description = (
        "Tonkadur Python Interpreter"
    )
)

parser.add_argument(
    '-f',
    '--file',
    dest='world_file',
    type = str,
    help = 'Wyrd JSON file to load.',
)

def display_text (text):
    str_content = ""

    if (not (text['effect'] is None)):
        str_content += "{(" + str(text['effect']) + ") "

    for c in text['content']:
        if (isinstance(c, str)):
            str_content += c
        else:
            str_content += display_text(c)

    if (not (text['effect'] is None)):
        str_content += "}"
    return str_content

args = parser.parse_args()
state = tonkadur.Tonkadur(args.world_file)

try:
    while True:
        result = state.run()
        result_category = result['category']

        if (result_category == "end"):
            print("Program ended")
            break
        elif (result_category == "display"):
            print(display_text(result['content']))
        elif (result_category == "prompt_integer"):
            while True:
                user_input = input(
                    display_text(result['label'])
                    + " "
                    + "["
                    + str(result['min'])
                    + ", "
                    + str(result['max'])
                    + "] "
                )
                user_input = int(user_input)
                if (
                    (user_input >= result['min'])
                    and (user_input <= result['max'])
                ):
                    break
                else:
                    print("Value not within range.")
            state.store_integer(user_input)

        elif (result_category == "prompt_string"):
            while True:
                user_input = input(
                    display_text(result['label'])
                    + " "
                    + "["
                    + str(result['min'])
                    + ", "
                    + str(result['max'])
                    + "] "
                )
                if (
                    (len(user_input) >= result['min'])
                    and (len(user_input) <= result['max'])
                ):
                    break
                else:
                    print("Input size not within range.")
            state.store_string(user_input)

        elif (result_category == "prompt_command"):
            while True:
                user_input = input(
                    display_text(result['label'])
                    + " "
                    + "["
                    + str(result['min'])
                    + ", "
                    + str(result['max'])
                    + "] "
                )
                if (
                    (len(user_input) >= result['min'])
                    and (len(user_input) <= result['max'])
                ):
                    break
                else:
                    print("Input size not within range.")
            state.store_command(user_input.split(' '))

        elif (result_category == "assert"):
            print(
                "Assert failed at line "
                + str(result['line'])
                + ":"
                + str(display_text(result['message']))
            )
            print(str(state.memory))
        elif (result_category == "resolve_choice"):
            current_choice = 0;

            for choice in result['options']:
                if (choice["category"] == "text_option"):
                    print(
                        str(current_choice)
                        + ". "
                        + display_text(choice["label"])
                    )
                current_choice += 1

            user_choice = input("Your choice? ")
            state.resolve_choice_to(int(user_choice))

except:
    print("failed at line " + str(state.program_counter) + ".\n")
    print(str(state.memory))
    raise
print(str(state.memory))