-
Notifications
You must be signed in to change notification settings - Fork 3
/
questionnode.py
337 lines (299 loc) · 13.6 KB
/
questionnode.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# coding: utf-8
import curses
from parts import everyword
import time
import random
import os
#global state variable is used to track game state for meta-game functions like
#rpg and command line tricks
state = ""
class GameNode(object):
#game node object, contains text and major behaviours
def __init__(self, query, options):
self.query = query
self.options = options
self.answer_map = {}
# self.tricks_dict = {}
# self.tricks_list = []
def text_wrapping(self, word, standardscreen):
"""simple textwrapper, check how much space is left on the x-axis
if a word's length is longer than the remaining space, adds a newline"""
maxx = standardscreen.getmaxyx()[1] - standardscreen.getyx()[1]
word = " " + word
if len(word) + 1 > maxx:
standardscreen.addstr("\n")
return word
def pretty_printing(self, text, standardscreen):
"""splits text, a string, into a list of strings, checks dicts for them,
and changes print colour accordingly"""
words = text.split(" ")
for i in words:
i = self.text_wrapping(i, standardscreen)
standardscreen.addstr(i[:self.begin_punc_check(i)])
self.colour_lookup(
i[self.begin_punc_check(i):self.safe_end_punc(i)[0]],
standardscreen)
standardscreen.addstr(i[self.safe_end_punc(i)[1]:])
standardscreen.refresh()
def end_punc_check(self, word):
"""check end of word and count punctuation"""
if word[-1:] in ["'", ".", "!", ":", ";", ")", ",", "(", "?", "-"]:
return self.end_punc_check(word[:-1]) -1
return 0
def safe_end_punc(self, word):
"""corrects end_punc_check for weird string slicing behaviour"""
count = self.end_punc_check(word)
if count == 0:
return (None,len(word))
else:
return (count,count)
def begin_punc_check(self, word):
"""check beginning of word and counts punctuation"""
if word[:1] in ["'", ".", "!", ":", ";", ")", ",", "(", "?" "-"]:
return self.begin_punc_check(word[1:]) +1
return 0
def colour_lookup(self, word, standardscreen):
"""goes against a nltk dictionary to get part of speech and change
colour accordingly"""
if curses.has_colors():
try:
part = everyword[word.strip(" ")]
if part in ["WP", "WRB", "TO", "CC", "EX", "WDT"]:
standardscreen.addstr(word, curses.color_pair(1))
if part in ["NN", "NNS", "NNP"]:
standardscreen.addstr(word, curses.color_pair(2))
if part in ["JJ", "DT", "JJR", "JJS"]:
standardscreen.addstr(word, curses.color_pair(3))
if part in ["VBD", "VBG", "VBZ", "VB", "VBP", "VBN"]:
standardscreen.addstr(word, curses.color_pair(4))
if part in ["RB", "RP", "MD"]:
standardscreen.addstr(word, curses.color_pair(5))
if part in ["PRP", "IN", "PRP$"]:
standardscreen.addstr(word, curses.color_pair(6))
if part in ["LS", "CD", "POS", "$", "-NONE-"]:
standardscreen.addstr(word)
except KeyError:
standardscreen.addstr(word)
else:
standardscreen.addstr(word)
def play(self, standardscreen, wrong, trick):
"""main play function, gets user input and returns next node"""
global state
answer = self.prompt_input(standardscreen)
while not answer in self.answer_map:
if answer.startswith(trick.tricks):
trick.play(standardscreen, wrong, trick, answer)
else:
wrong.play(standardscreen, wrong, trick)
answer = self.prompt_input(standardscreen)
else:
state = self.answer_map[answer]
return self.answer_map[answer].play(standardscreen, wrong, trick)
def prompt_input(self, standardscreen):
"""prints the query and options using pretty printing methods, and
prompts for user input"""
standardscreen.clear()
standardscreen.addstr("\n")
for i in self.query:
self.pretty_printing(i, standardscreen)
standardscreen.addstr("\n\n ")
for i in self.options:
self.pretty_printing(i, standardscreen)
standardscreen.addstr("\n ")
standardscreen.addstr(standardscreen.getmaxyx()[0]-2,5,"$ ")
answer = standardscreen.getstr(
standardscreen.getmaxyx()[0]-2,7).decode(encoding = "utf-8")
standardscreen.refresh()
return answer
class NoAnswerNode(GameNode):
def __init__(self, query):
self.query = query
self.answer_map = {}
def play(self, standardscreen, wrong, trick):
"""main play function displays text and returns next node"""
global state
standardscreen.clear()
standardscreen.addstr("\n")
for i in self.query:
self.pretty_printing(i, standardscreen)
standardscreen.addstr("\n\n ")
answer = standardscreen.getch(
standardscreen.getmaxyx()[0]-2,5)
standardscreen.refresh()
state = self.answer_map["0"]
return self.answer_map["0"].play(standardscreen, wrong, trick)
class GameEnd(GameNode):
def play(self, standardscreen, wrong, trick):
"""prints errors if there are any remaining errors, and asks the final
ending question"""
while wrong.eternity < 17:
wrong.play(standardscreen, wrong, trick)
else:
answer = self.prompt_input(standardscreen)
while answer not in ["0", "1"]:
answer = self.prompt_input(standardscreen)
else:
if answer == "0":
while True:
wrong.play(standardscreen, wrong, trick)
else:
wrong.glitch_out(standardscreen)
wrong.spasm(standardscreen)
class WrongAnswerHandler(GameNode):
def __init__(self, errors, glitch):
self.errors = errors
self.glitch = glitch
self.wrong = 0
self.eternity = 0
def play(self, standardscreen, wrong, trick):
"""prints error message if there are any remaining, and if not, prints
an incrementing counter"""
if self.wrong < len(self.errors):
standardscreen.clear()
standardscreen.addstr("\n ERR: Unidentified Error\n\n",
curses.color_pair(1))
self.pretty_printing(self.errors[self.wrong], standardscreen)
self.wrong += 1
standardscreen.refresh()
hang = standardscreen.getch(standardscreen.getmaxyx()[0]-2,5)
else:
standardscreen.clear()
standardscreen.addstr("\n ERR: Unidentified Error\n\n",
curses.color_pair(1))
standardscreen.addstr(" " + str(self.eternity))
self.eternity += 1
hang = standardscreen.getch(standardscreen.getmaxyx()[0]-2,5)
def spasm(self, standardscreen):
"""calls recursive loop until throws maximum recursion depth error"""
standardscreen.clear()
standardscreen.addstr("\n ERR: Unidentified Error\n\n",
curses.color_pair(1))
standardscreen.addstr(" " + str(self.eternity))
self.eternity += 1
self.spasm(standardscreen)
def indentation_manager(self, standardscreen):
pass
def glitch_out(self, standardscreen):
standardscreen.clear()
standardscreen.addstr("\n ERR: Unidentified Error\n\n",
curses.color_pair(1))
standardscreen.addstr(" " + str(self.eternity))
plain = []
probability = [1]
for i in range(40):
probability.append(0)
for char in self.glitch:
plain.append(char)
if standardscreen.getyx()[1]+1 >= standardscreen.getmaxyx()[1]-5:
standardscreen.addstr(" \n ")
else:
standardscreen.addstr(char)
standardscreen.refresh()
time.sleep(.5)
for i in range(400):
standardscreen.clear()
standardscreen.addstr("\n ERR: Unidentified Error\n\n ",
curses.color_pair(1))
if i % 10 == 0:
probability.pop()
for char in self.glitch:
if random.choice(probability) == 0:
if standardscreen.getyx()[1]+3 >= standardscreen.getmaxyx(
)[1]-5:
standardscreen.addstr(" \n ")
else:
standardscreen.addstr(char)
else:
if standardscreen.getyx()[1]+3 >= standardscreen.getmaxyx(
)[1]-5:
standardscreen.addstr(" \n ")
else:
standardscreen.addstr(char, curses.color_pair(2))
standardscreen.refresh()
time.sleep(.01)
class TrickHandler(GameNode):
"""There are two types of tricks - statements and funcs. Statements print a
response to the user, funcs contain some independent logic."""
def __init__(self, statements):
"""statements is a dict of all statements, funcs a dict of all funcs,
tricks is a tuple of all statement + func names"""
self.statements = statements
self.funcs = {"pwd": self.pwd, "ls": self.ls,
"kill": self.kill, "logout": self.logout, "echo": self.echo, "sudo":
self.sudo}
self.tricks = self.tuple_constructor()
self.answer_map = {}
def play(self, standardscreen, wrong, trick, answer):
"""checks entered string against all possible trick keywords, using
startswith, and if a match is found, forwards it to the statement
handler or calls the associated func"""
for i in self.tricks:
if answer.startswith(i):
key = i
if key in self.statements:
self.statement_handler(standardscreen, wrong, trick, key)
else:
self.funcs[key](standardscreen, wrong, trick, answer)
def tuple_constructor(self):
"""creates a tuple of all the possible tricks, both funcs and
statements"""
all_tricks = [i for i in self.statements] + [i for i in
self.funcs]
return tuple(all_tricks)
def statement_handler(self, standardscreen, wrong, trick, answer):
"""prints tricks, then pops player back to global game state"""
global state
standardscreen.clear()
standardscreen.addstr("\n")
self.pretty_printing(self.statements[answer], standardscreen)
answer = standardscreen.getch(standardscreen.getmaxyx()[0]-2,5)
standardscreen.refresh()
state.play(standardscreen, wrong, trick)
def pwd(self, standardscreen, wrong, trick, answer):
home = os.getcwd()
standardscreen.clear()
standardscreen.addstr("\n " + home)
answer = standardscreen.getch(standardscreen.getmaxyx()[0]-2,5)
standardscreen.refresh()
state.play(standardscreen, wrong, trick)
def sudo(self):
lol = "You think you're clever but really you're a rock"
standardscreen.clear()
standardscreen.addstr("\n")
self.pretty_printing(lol, standardscreen)
answer = standardscreen.getch(standardscreen.getmaxyx()[0]-2,5)
standardscreen.refresh()
self.answer_map["0"].play(standardscreen, wrong, trick)
def ls(self, standardscreen, wrong, trick, answer):
files = os.listdir(os.getcwd())
standardscreen.clear()
for f in files:
standardscreen.addstr("\n " + f)
answer = standardscreen.getch(standardscreen.getmaxyx()[0]-2,5)
standardscreen.refresh()
state.play(standardscreen, wrong, trick)
def kill(self, standardscreen, wrong, trick, answer):
kill = ("Maybe I underestimated you. You seem to have learned how"
" things work very quickly. Maybe too quickly.\n\n Of "
"course you would have this power.")
standardscreen.clear()
standardscreen.addstr("\n")
self.pretty_printing(kill, standardscreen)
answer = standardscreen.getch(standardscreen.getmaxyx()[0]-2,5)
standardscreen.refresh()
self.answer_map["0"].play(standardscreen, wrong, trick)
def logout(self, standardscreen, wrong, trick, answer):
standardscreen.clear()
self.answer_map["0"].play(standardscreen, wrong, trick)
def echo(self, standardscreen, wrong, trick, answer):
reply = ("You call is echoed by the space around you, but it's "
"impossible to get a sense of its dimensions. You are alone. "
"No one hears you.")
standardscreen.clear()
standardscreen.addstr("\n")
self.pretty_printing("'"+answer[5:]+"'", standardscreen)
standardscreen.addstr("\n\n ")
self.pretty_printing(reply, standardscreen)
answer = standardscreen.getch(standardscreen.getmaxyx()[0]-2,5)
standardscreen.refresh()
state.play(standardscreen, wrong, trick)