-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-de.py
255 lines (223 loc) · 10.4 KB
/
run-de.py
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import os
import subprocess
# =================================================================================================================== #
# === CONSTANTS === #
# =================================================================================================================== #
SCREEN_WIDTH = 120
LANG_DELIM = "de"
IFLOG_JAR_PATH = "../lib/Standalone"
IFLTARGET_FILE_PATH = "../target/PostgreSQL"
NEW_ENV = os.environ.copy()
NEW_ENV["PGPASSWORD"] = "postgres"
DB_NAME = "iflogdemodb"
DB_NOTICE_PREFIX = "HINWEIS:"
IFLOG_JAR = "IFLOG.jar"
IFLTARGET_FILE = "PSQL.ifltarget"
IFLOG_FILE = "demo.ifl"
SQL_OUTPUT_FILE = "demo_psql.sql"
LOG_FILE = "demo_log.txt"
IFLOG_JAR = IFLOG_JAR_PATH + "/" + IFLOG_JAR
IFLTARGET_FILE = IFLTARGET_FILE_PATH + "/" + IFLTARGET_FILE
IFLOG_FILE = LANG_DELIM + "/" + IFLOG_FILE
SQL_OUTPUT_FILE = LANG_DELIM + "/" + SQL_OUTPUT_FILE
LOG_FILE = LANG_DELIM + "/" + LOG_FILE
# =================================================================================================================== #
# === CONSOLE OUTPUT === #
# =================================================================================================================== #
def output_command(args, out_str, err_str, suppress_newline = False):
command = ' '.join(args).replace("\n","\\n")
print("\n" + SCREEN_WIDTH*"=")
print("BEFEHL: " + command)
print(SCREEN_WIDTH*"=")
prefix_str = ""
if not suppress_newline:
prefix_str += "\n"
if (len(out_str) + len(err_str)) == 0:
print(prefix_str + "-")
else:
if len(out_str) > 0:
out_str = out_str.strip().removeprefix(DB_NOTICE_PREFIX).strip()
print(prefix_str + out_str)
if len(err_str) > 0:
err_str = err_str.strip().removeprefix(DB_NOTICE_PREFIX).strip()
print(prefix_str + err_str)
def output_map():
start_room = run_map_command("start_room")
garden = run_map_command("garden")
end_room = run_map_command("end_room")
ret_val = subprocess.run(
["psql", "-U", "postgres", "-d", DB_NAME, "-qAt", "-c", "set client_encoding=win1252",
"-c", "SELECT loc FROM character WHERE name = 'player'"],
text=True,capture_output=True,env=NEW_ENV)
cur_room = int(ret_val.stdout.strip())
if cur_room == 1:
start_room += " (HIER)"
elif cur_room == 2:
garden += " (HIER)"
elif cur_room == 3:
end_room += " (HIER)"
line_len = len(" * *")
line_len_base = len(" * A - *")
line_len_a = line_len - (line_len_base + len(start_room))
line_len_b = line_len - (line_len_base + len(garden))
line_len_c = line_len - (line_len_base + len(end_room))
print(
"\n/* **************************************\n" +
" * *\n" +
" * A - " + start_room + line_len_a * " " + " *\n" +
" * B - " + garden + line_len_b * " " + " *\n" +
" * C - " + end_room + line_len_c * " " + " *\n" +
" * *\n" +
" * ---------------- *\n" +
" * | | *\n" +
" * | C | *\n" +
" * | | *\n" +
" * --------------+------ -----| *\n" +
" * | | | *\n" +
" * | B A | *\n" +
" * | | | *\n" +
" * --------------+--------------- *\n" +
" * *\n" +
" ************************************** */" )
# =================================================================================================================== #
# === IFLOG HELPER COMMANDS === #
# =================================================================================================================== #
def run_iflog_command(command):
ret_val = None
match command:
case "help":
ret_val = subprocess.run(
["java", "-jar", IFLOG_JAR, "-h"],
text=True,capture_output=True)
case "test":
ret_val = subprocess.run(
["java", "-jar", IFLOG_JAR, IFLOG_FILE, IFLTARGET_FILE, "-s"],
text=True,capture_output=True)
case "out":
ret_val = subprocess.run(
["java", "-jar", IFLOG_JAR, IFLOG_FILE, IFLTARGET_FILE, "-o", SQL_OUTPUT_FILE],
text=True,capture_output=True)
case "out_log":
ret_val = subprocess.run(
["java", "-jar", IFLOG_JAR, IFLOG_FILE, IFLTARGET_FILE, "-o", SQL_OUTPUT_FILE, "-s", "-l", LOG_FILE],
text=True,capture_output=True)
case _:
print("Befehl " + command + " nicht gefunden!")
if(ret_val is not None):
output_command(ret_val.args, ret_val.stdout, ret_val.stderr)
# =================================================================================================================== #
# === CREATE/DROP DATABASE === #
# =================================================================================================================== #
def init():
run_psql_command("CREATE DATABASE " + DB_NAME + ";")
run_command("\i " + SQL_OUTPUT_FILE)
def destroy():
run_psql_command("DROP DATABASE " + DB_NAME + ";")
# =================================================================================================================== #
# === PSQL HELPER COMMANDS === #
# =================================================================================================================== #
def run_psql_command(command):
ret_val = subprocess.run(
["psql", "-U", "postgres", "-c", command],
text=True,capture_output=True,env=NEW_ENV)
output_command(ret_val.args, ret_val.stdout, ret_val.stderr)
def run_command(command):
ret_val = subprocess.run(
["psql", "-U", "postgres", "-d", DB_NAME, "-c", command],
text=True,capture_output=True,env=NEW_ENV)
output_command(ret_val.args, ret_val.stdout, ret_val.stderr)
def run_game_command(command, options = "-qAt", suppress_newline = False):
ret_val = subprocess.run(
["psql", "-U", "postgres", "-d", DB_NAME, options, "-c", "set client_encoding=win1252", "-c", command],
text=True,capture_output=True,env=NEW_ENV)
output_command([command], ret_val.stdout, ret_val.stderr, suppress_newline)
def run_map_command(room):
ret_room = "???"
ret_val = subprocess.run(
["psql", "-U", "postgres", "-d", DB_NAME, "-qAt", "-c", "set client_encoding=win1252",
"-c", "SELECT visited FROM room WHERE name = '" + room + "'"],
text=True,capture_output=True,env=NEW_ENV)
if ret_val.stdout.strip() != "f":
ret_val = subprocess.run(
["psql", "-U", "postgres", "-d", DB_NAME, "-qAt", "-c", "set client_encoding=win1252",
"-c", "SELECT sdesc FROM room WHERE name = '" + room + "'"],
text=True,capture_output=True,env=NEW_ENV)
ret_room = ret_val.stdout.strip()
return ret_room
def run_move_command(direction):
run_game_command(
"UPDATE character SET loc = path." + direction +
" FROM path WHERE character.name = 'player' AND character.loc = path.id;", "-qAt")
# =================================================================================================================== #
# === PROCESS INPUT === #
# =================================================================================================================== #
def process_game_input():
run_game_command("SELECT gs.msg FROM game_state gs, character c WHERE gs.id = c.game_state;")
command = input("> ")
ret = True
match command:
case "n" | "north" | "go north":
run_move_command("north")
case "s" | "south" | "go south":
run_move_command("south")
case "e" | "east" | "go east":
run_move_command("east")
case "w" | "west" | "go west":
run_move_command("west")
case "i" | "inv" | "inventory":
run_game_command("SELECT * FROM player_inv;", "-q")
run_game_command("SELECT * FROM all_inv;", "-q")
case "m" | "map":
run_game_command(
"SELECT r.id, name, sdesc, visited, north, south, east, west FROM room r INNER JOIN path p ON r.id = p.id ;", "-q")
output_map()
case "x" | "examine":
run_game_command("SELECT DoExamine();", "-qAt", True)
case "a" | "d" | "do" | "do action":
run_game_command("SELECT DoAction();", "-qAt", True)
case "q" | "quit" | "exit":
ret = False
case _:
print("Befehl " + command + " nicht gefunden!")
return ret
def process_input():
command = input("\nBefehl: ")
ret = True
match command:
case "h" | "help":
run_iflog_command("help")
case "t" | "test":
run_iflog_command("test")
case "o" | "out":
run_iflog_command("out")
case "l" | "log":
run_iflog_command("out_log")
case "i" | "init":
init()
case "d" | "destroy":
destroy()
case "r" | "reinit":
destroy()
init()
case "q" | "quit" | "exit":
ret = False
case "s" | "start":
run_game_command(
"SELECT r.sdesc || E'\n' || r.ldesc FROM character c, room r WHERE c.name = 'player' AND c.loc = r.id")
cont_game = True
while cont_game:
cont_game = process_game_input()
case _:
print("Befehl " + command + " nicht gefunden!")
return ret
# =================================================================================================================== #
# === MAIN ENTRY POINT === #
# =================================================================================================================== #
def main():
# set shell or windows won't display mutated vowels properly
subprocess.run(["chcp", "1252"],text=True,capture_output=True,shell=True)
cont = True
while cont:
cont = process_input()
if __name__ == "__main__":
main()