-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflatten.jq
More file actions
196 lines (182 loc) · 6.32 KB
/
flatten.jq
File metadata and controls
196 lines (182 loc) · 6.32 KB
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
# Flatten CBN-style copy-from inheritance for .data[]
# Assumptions:
# - no mod layering/self-override chains
# - inheritance is resolved by id
#
# Usage:
# jq -f scripts/flatten-copy-from.jq _test/all.json
#
# Output:
# flattened .data array
def trunc0:
if . >= 0 then floor else ceil end;
def parse_numbered_unit:
capture("^\\s*(?<num>-?(?:[0-9]+(?:\\.[0-9]+)?|\\.[0-9]+))\\s*(?<unit>.+?)\\s*$")?;
def arr_eq($a; $b):
if ($a | type) != ($b | type) then false
elif ($a | type) == "array" then
($a | length) == ($b | length)
and all(range(0; $a | length); arr_eq($a[.]; $b[.]))
else
$a == $b
end;
def to_dmg_array:
if . == null then []
elif (type == "array") then .
elif (type == "object" and has("damage_type") and has("amount")) then [.]
else []
end;
def as_array:
if type == "array" then . else [] end;
def merge_relative_damage($base; $delta):
reduce ($delta | to_dmg_array)[] as $d
($base | to_dmg_array;
(map(.damage_type) | index($d.damage_type)) as $i
| if $i == null then . + [$d]
else .[$i].amount = ((.[$i].amount // 0) + ($d.amount // 0))
end
);
def merge_proportional_damage($base; $factor):
reduce ($factor | to_dmg_array)[] as $d
($base | to_dmg_array;
(map(.damage_type) | index($d.damage_type)) as $i
| if $i == null then .
else .[$i].amount = ((.[$i].amount // 0) * ($d.amount // 1))
end
);
def apply_relative:
reduce ((.relative // {}) | keys[]) as $k
(.;
. as $root
| if ($root.relative[$k] | type) == "number" then
if ($root[$k] | type) == "number" then
.[$k] = (($root[$k] // 0) + $root.relative[$k])
elif ($root[$k] | type) == "string" then
($root[$k] | parse_numbered_unit) as $m
| if $m == null then .
else .[$k] = (((($m.num | tonumber) + $root.relative[$k]) | tostring) + " " + $m.unit)
end
else
.[$k] = (($root[$k] // 0) + $root.relative[$k])
end
elif (($k == "damage" or $k == "ranged_damage") and ($root[$k] != null)) then
.[$k] = merge_relative_damage($root[$k]; $root.relative[$k])
elif ($k == "armor" and $root.type == "MONSTER" and ($root[$k] | type) == "object") then
.[$k] = ($root[$k] // {})
| reduce (($root.relative[$k] // {}) | keys[]) as $k2
(.;
.[$k][$k2] = ((.[$k][$k2] // 0) + ($root.relative[$k][$k2] // 0))
)
elif ($k == "qualities" and ($root[$k] | type) == "array") then
.[$k] = (
reduce (($root.relative[$k] // []))[] as $q
(($root[$k] // []);
(map(.[0]) | index($q[0])) as $i
| if $i == null then .
else .[$i][1] = ((.[$i][1] // 0) + ($q[1] // 0))
end
)
)
else .
end
)
| del(.relative);
def apply_proportional:
reduce ((.proportional // {}) | keys[]) as $k
(.;
. as $root
| if ($root.proportional[$k] | type) == "number" then
if ($root[$k] | type) == "number" then
.[$k] = ((($root[$k] // 0) * $root.proportional[$k]) | trunc0)
elif ($root[$k] | type) == "string" then
($root[$k] | parse_numbered_unit) as $m
| if $m == null then .
else .[$k] = (((($m.num | tonumber) * $root.proportional[$k]) | tostring) + " " + $m.unit)
end
elif ($k == "attack_cost" and ($root[$k] == null)) then
.[$k] = ((100 * $root.proportional[$k]) | trunc0)
else
.[$k] = ((($root[$k] // 0) * $root.proportional[$k]) | trunc0)
end
elif (($k == "damage" or $k == "ranged_damage") and ($root[$k] != null)) then
.[$k] = merge_proportional_damage($root[$k]; $root.proportional[$k])
elif ($k == "armor" and $root.type == "MONSTER" and ($root[$k] | type) == "object") then
.[$k] = ($root[$k] // {})
| reduce (($root.proportional[$k] // {}) | keys[]) as $k2
(.;
.[$k][$k2] =
(((.[$k][$k2] // 0) * ($root.proportional[$k][$k2] // 1)) | trunc0)
)
else .
end
)
| del(.proportional);
def apply_extend:
reduce ((.extend // {}) | keys[]) as $k
(.;
. as $root
| if ($root.extend[$k] | type) == "array" then
if $k == "flags" then
.[$k] = (($root[$k] | as_array) + (($root.extend[$k]) - ($root[$k] | as_array)))
else
.[$k] = (($root[$k] | as_array) + $root.extend[$k])
end
else .
end
)
| del(.extend);
def apply_delete:
reduce ((.delete // {}) | keys[]) as $k
(.;
. as $root
| if ($root.delete[$k] | type) == "array" then
.[$k] = (
($root[$k] | as_array)
| map(select(. as $x | (($root.delete[$k] | any(arr_eq(.; $x))) | not)))
)
else
del(.[$k])
end
)
| del(.delete);
def apply_special_merges($parent_props; $obj; $ret):
($ret
| if ($parent_props.vitamins | type) == "array" and ($obj.vitamins | type) == "array" then
.vitamins = [
($parent_props.vitamins[] | select(([ $obj.vitamins[] | .[0] ] | index(.[0])) == null)),
($obj.vitamins[])
]
else .
end
| if $obj.type == "vehicle" and ($parent_props.parts | type) == "array" and ($obj.parts | type) == "array" then
.parts = (($parent_props.parts | as_array) + ($obj.parts | as_array))
else .
end
);
def flatten($idx; $id; $stack):
if ($stack | index($id)) != null then
($idx[$id])
else
($idx[$id]) as $obj
| if $obj == null then null
else ($obj["copy-from"] // null) as $pid
| if $pid == null or $idx[$pid] == null or $pid == $id then
$obj
else
(flatten($idx; $pid; $stack + [$id])) as $parent
| ($parent | del(.abstract)) as $parent_props
| ($parent_props + $obj) as $ret
| apply_special_merges($parent_props; $obj; $ret)
| apply_relative
| apply_proportional
| apply_extend
| apply_delete
end
end
end;
.data as $rows
| ($rows | map(select((.id | type) == "string") | { key: .id, value: . }) | from_entries) as $idx
| $rows
| map(
if (.id | type) == "string" then flatten($idx; .id; []) else . end
)