-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
84 lines (63 loc) · 2.86 KB
/
tools.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
import random
import re
from colorama import Fore, init
from df_engine.core import Context, Actor
from text import HELP_, WANDS, FACULTIES, NAVIGATOR, HAGRID_SPEECH
init(autoreset=True)
def choose_hagrid_greeting(ctx: Context) -> str:
"""Returns greeting depends on user grade"""
if ctx.misc:
return HAGRID_SPEECH[1]
return HAGRID_SPEECH[0]
def get_grade(ctx: Context, actor: Actor, *args, **kwargs) -> tuple:
"""Returns transition route depends on user grade"""
if ctx.misc:
return "second_year", "kings_cross"
return "first_year", "ollivander_shop"
def overwrite_response(ctx: Context, current_response: str, nav_commands: list) -> Context:
"""Overwrites response with nav hints"""
hint = "\n".join(nav_commands)
ctx.current_node.response = f"{Fore.GREEN}{current_response}\n{Fore.YELLOW}{hint}"
ctx.overwrite_current_node_in_processing(ctx.current_node)
return ctx
def get_help(ctx: Context, actor: Actor, *args, **kwargs) -> Context:
"""Returns help hint"""
if re.search(r"(?i)(help)", ctx.last_request):
ctx.current_node.response = f"{ctx.last_response} \n{Fore.CYAN}{HELP_}"
ctx.overwrite_current_node_in_processing(ctx.current_node)
return ctx
def pickup_wand(ctx: Context, actor: Actor, *args, **kwargs) -> Context:
"""Puts the wand into ctx.misc"""
if re.search(r"[1-3]", ctx.last_request):
ctx.misc["wand"] = WANDS[int(ctx.last_request)]
return ctx
def sorting_hat(ctx: Context) -> str:
"""Declares a faculty and save into ctx.misc"""
faculty = FACULTIES[random.randint(1, len(FACULTIES))]
ctx.misc["faculty"] = faculty
return f"Congrats! The hat chose the {faculty}!\n\t An unforgettable year awaits you!"
navi_hints = {
"start_node": ["start", "help"],
"ollivander_shop": WANDS,
"fallback_node": ["back", "help"],
"hogwarts_school": ["hat", "back", "help"],
"sorting_hat": ["next", "back", "help"],
}
def format_selected_nav_commands(source, required_commands=None) -> list:
"""Returns selected nav commands"""
if not required_commands:
required_commands = source
return [f"\t[{key}] > {value}" for key, value in source.items() if key in required_commands]
def get_navi_hint(ctx: Context, actor: Actor, *args, **kwargs) -> Context:
if not ctx.last_request == "help":
current_response = ctx.current_node.response
if ctx.last_label[1] == "start_node":
current_response = choose_hagrid_greeting(ctx)
elif ctx.last_label[1] == "sorting_hat":
current_response = sorting_hat(ctx)
if ctx.last_label[1] == "ollivander_shop":
nav_commands = format_selected_nav_commands(WANDS)
else:
nav_commands = format_selected_nav_commands(NAVIGATOR, navi_hints[ctx.last_label[1]])
overwrite_response(ctx, current_response, nav_commands)
return ctx