-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.py
62 lines (40 loc) · 1.71 KB
/
decoder.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
import json
from typing import Any, Dict, List, Tuple, Union
from Node import Node
from story import Story
from utils import VarCommand
from VarStore import VarStore
def decode_var_store(data: str) -> VarStore:
return VarStore(json.loads(data))
def decode_story(data: str, store: VarStore) -> Story:
story = Story(store)
node_store: Dict[str, Node] = {}
story_dict = json.loads(data)
for node_hash, node_dict in story_dict['nodes'].items():
node = _decode_basic_node(node_dict, story)
node_store[str(node_hash)] = node
story.change_beginning(node_store[str(story_dict['beginning'])])
for ending in story_dict['endings']:
story.add_ending(node_store[str(ending)])
for node_hash, node_dict in story_dict['nodes'].items():
_decode_outgoing(node_store[str(node_hash)],
node_store, node_dict['outgoing'])
return story
def _decode_basic_node(node_dict, story: Story) -> Node:
node = story.create_node(node_dict['title'])
node.set_text(node_dict['text'])
for req in node_dict['requirements']:
node.add_requirement(_decode_var_command(req))
for change in node_dict['changes']:
node.add_change(_decode_var_command(change))
return node
def _decode_var_command(com_dict)->VarCommand:
command = VarCommand('', '', None)
command.__dict__ = com_dict
return command
def _decode_outgoing(node: Node, node_store, outgoing):
for out_node_hash, info in outgoing.items():
out_node = node_store[str(out_node_hash)]
for text, commands_dict in info.items():
commands = [_decode_var_command(com) for com in commands_dict]
node.add_outgoing(out_node, text, commands)