1
1
#! /bin/bash
2
2
3
3
declare MCDB_API_URL=' https://marvelcdb.com/api/public'
4
- declare CARDS_CACHE=" ${HOME} /.mcdb/cards"
4
+ declare MCDB_CACHE=" ${HOME} /.mcdb"
5
+ declare PACKS_CACHE=" ${MCDB_CACHE} /packs.json"
6
+ declare CARDS_CACHE=" ${MCDB_CACHE} /cards"
5
7
declare ALPHABETICAL_ORDER=false
6
8
declare PACK_ORDER=false
7
9
declare RELEASE_ORDER=false
8
10
declare SUPER_ALPHABETICAL_ORDER=false
9
11
declare WAVE_ORDER=false
10
12
declare DECK_JSON
11
13
14
+ init_packs () {
15
+ if [ ! -d $MCDB_CACHE ]; then
16
+ mkdir -p $MCDB_CACHE
17
+ fi
18
+
19
+ if [ ! -f $PACKS_CACHE ]; then
20
+ curl -so " $PACKS_CACHE " " ${MCDB_API_URL} /packs/"
21
+ fi
22
+
23
+ local -a pack_positions=($( jq -r ' .[] | @text "\(.code)=\(.position)"' < $PACKS_CACHE ) )
24
+ export " ${pack_positions[@]} "
25
+ }
26
+
12
27
print_faction_color () {
13
28
local faction_code=" $1 "
14
29
case $faction_code in
@@ -37,24 +52,24 @@ print_faction_color() {
37
52
}
38
53
39
54
get_wave_number () {
40
- local pack_id= ${1 : 0 : 2}
41
- local -i pack_number= ${pack_id # 0}
55
+ local pack_code= $1
56
+ local pack_position= $( jq -nr --arg pack_code $pack_code ' env[$pack_code] ' )
42
57
43
- if [ $pack_number -lt 11 ]; then
58
+ if [ $pack_position -lt 10 ]; then
44
59
echo 1
45
- elif [ $pack_number -lt 16 ]; then
60
+ elif [ $pack_position -lt 16 ]; then
46
61
echo 2
47
- elif [ $pack_number -lt 21 ]; then
62
+ elif [ $pack_position -lt 21 ]; then
48
63
echo 3
49
- elif [ $pack_number -lt 27 ]; then
64
+ elif [ $pack_position -lt 27 ]; then
50
65
echo 4
51
- elif [ $pack_number -lt 32 ]; then
66
+ elif [ $pack_position -lt 32 ]; then
52
67
echo 5
53
- elif [ $pack_number -lt 39 ]; then
68
+ elif [ $pack_position -lt 40 ]; then
54
69
echo 6
55
- elif [ $pack_number -lt 45 ]; then
70
+ elif [ $pack_position -lt 45 ]; then
56
71
echo 7
57
- elif [ $pack_number -lt 50 ]; then
72
+ elif [ $pack_position -lt 50 ]; then
58
73
echo 8
59
74
else
60
75
echo 99
@@ -65,7 +80,8 @@ print_card() {
65
80
local card_json=" $1 "
66
81
67
82
local card_id=" $( jq -r ' .code' <<< $card_json ) "
68
- local wave_number=$( get_wave_number " $card_id " )
83
+ local pack_code=" $( jq -r ' .pack_code' <<< $card_json ) "
84
+ local wave_number=$( get_wave_number " $pack_code " )
69
85
local amount=$( jq -r --arg card_id $card_id ' .slots[$card_id]' <<< " ${DECK_JSON}" )
70
86
local faction_code=" $( jq -r ' .faction_code' <<< $card_json ) "
71
87
local card_name=" $( jq -r ' .real_name' <<< $card_json ) "
@@ -165,8 +181,8 @@ print_waves() {
165
181
local IFS='
166
182
'
167
183
for wave_json in $( jq -c ' .[]' <<< " $waves_json" ) ; do
168
- local card_id =$( jq -r ' .[0].code ' <<< " $wave_json" )
169
- local wave_number=$( get_wave_number $card_id )
184
+ local pack_code =$( jq -r ' .[0].pack_code ' <<< " $wave_json" )
185
+ local wave_number=$( get_wave_number $pack_code )
170
186
printf ' Wave #%d\n' " $wave_number "
171
187
local faction_json=$( group_by_faction " $wave_json " )
172
188
print_factions " $faction_json "
@@ -217,19 +233,19 @@ print_deck() {
217
233
elif $RELEASE_ORDER ; then
218
234
print_cards " ${card_files[@]} "
219
235
elif $WAVE_ORDER ; then
220
- local cards_json=$( jq -cn ' [inputs] | group_by(.code | .[0:2 ] | tonumber |
221
- if . < 11 then 1
236
+ local cards_json=$( jq -cn ' [inputs] | group_by(.pack_code as $pack | env[$pack ] | tonumber |
237
+ if . < 10 then 1
222
238
elif . < 16 then 2
223
239
elif . < 21 then 3
224
240
elif . < 27 then 4
225
241
elif . < 32 then 5
226
- elif . < 39 then 6
242
+ elif . < 40 then 6
227
243
elif . < 45 then 7
228
244
elif . < 50 then 8
229
245
else 99 end)' " ${card_files[@]} " )
230
246
print_waves " $cards_json "
231
247
elif $PACK_ORDER ; then
232
- local cards_json=$( jq -cn ' [inputs] | group_by(.code | .[0:2] | tonumber )' " ${card_files[@]} " )
248
+ local cards_json=$( jq -cn ' [inputs] | group_by(.pack_code as $pack | env[$pack] )' " ${card_files[@]} " )
233
249
print_packs " $cards_json "
234
250
else
235
251
local cards_json=$( jq -cn ' [inputs] | group_by(.faction_code |
@@ -245,6 +261,8 @@ print_deck() {
245
261
fi
246
262
}
247
263
264
+ init_packs
265
+
248
266
while getopts ' d:l:aprw' opt; do
249
267
case $opt in
250
268
d)
0 commit comments