-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgenerator.rb
141 lines (126 loc) · 3.59 KB
/
generator.rb
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
require 'yaml'
require 'set'
class Generator
ABBREVIATIONS = {
"Knight" => "KGT",
"Monk" => "MNK",
"Thief" => "THF",
"Black-Mage" => "BLM",
"White-Mage" => "WHM",
"Blue-Mage" => "BLU",
"Mystic-Knight" => "MYS",
"Time-Mage" => "TIM",
"Summoner" => "SUM",
"Red-Mage" => "RDM",
"Berserker" => "BER",
"Beastmaster" => "BST",
"Geomancer" => "GEO",
"Ninja" => "NIN",
"Bard" => "BRD",
"Ranger" => "RAN",
"Samurai" => "SAM",
"Dragoon" => "DRG",
"Dancer" => "DNC",
"Chemist" => "CHM",
"Freelancer" => "FRE",
"Mime" => "MIM",
"Cannoneer" => "CAN",
"Gladiator" => "GLD",
"Oracle" => "ORC"
}
def initialize(jobs)
@output = ""
@tags = Set.new()
@job_data = YAML.load(File.read('data/jobs.yaml'))
jobs.map{|j| j.gsub(" ", "-")}.each do |job|
if job == "debug"
add_all_tags
@tags << "debug"
break
end
@tags << job
@job_data['Jobs'][job].each do |tag|
@tags << tag
end
end
end
def add_all_tags
@job_data['Jobs'].each do |job, subtags|
@tags << job
subtags.each do |tag|
@tags << tag
end
end
end
def get_walkthrough(beginning_node='begin')
generate!(beginning_node) if @output == ""
@output
end
def generate!(beginning_node='begin')
handle_node(beginning_node, nil)
end
def handle_node(nodename, previous_node)
node = YAML.load(File.read("data/nodes/#{nodename}.yaml"))
node_header = node.keys.first
node_data = node[node_header]
if previous_node && !node_data['Metadata']['previous-nodes'].include?(previous_node)
print "WARNING: unexpected node transition #{previous_node} => #{nodename}\n"
end
emit_header(node_header)
node_data.each do |condition, hints|
condition = condition.gsub('`', '')
next if condition == "Metadata"
if condition == "Generic" || condition_true?(condition)
hints.each do |hint|
emit_list_element(hint, condition)
end
elsif @tags.include?("debug") && !condition_true?(condition)
print "WARNING: condition unreachable: #{condition} in node #{nodename}\n" unless condition.include?("NOT")
end
end
emit_paragraph_break
next_node = node_data['Metadata']['next-node']
handle_node(next_node, nodename) unless next_node == 'end'
end
# todo: support and, or with more than 2 elements
def condition_true?(condition)
token = condition.split.first
if token == 'NOT'
return !condition_true?(condition.split[1..-1].join(" "))
elsif token == 'UNION'
subconditions = condition.split[1..-1]
return subconditions.all?{|c| condition_true?(c)}
elsif token == 'INTERSECTION'
subconditions = condition.split[1..-1]
return subconditions.detect{|c| condition_true?(c)}
else
return @tags.include?(condition)
end
end
def emit_header(contents)
@output << "## #{contents}\n"
end
def emit_list_element(contents, condition)
@output << "* #{format_condition(condition)}#{contents}\n"
end
def emit_paragraph_break()
@output << "\n"
end
def format_condition(condition)
tokens = condition.split(" ")
abbreviations = tokens.map{|tok| ABBREVIATIONS[tok]}.compact
if abbreviations.length == 0
""
else
if tokens.first == 'UNION'
"[#{abbreviations.join('+')}] "
elsif tokens.first == 'INTERSECTION'
"[#{abbreviations.join('|')}] "
elsif tokens.first == 'NOT'
"[!#{abbreviations.join(',')}] "
else
"[#{abbreviations.join(' ')}] "
end
end
end
end