|
23 | 23 | </div>
|
24 | 24 | <script>
|
25 | 25 |
|
26 |
| -function getRestrictionById(restrictions, id) { |
27 |
| - for (i = 0; i < restrictions.length; i++) { |
28 |
| - if (restrictions[i].id == id) { |
29 |
| - return restrictions[i]; |
30 |
| - } |
31 |
| - } |
32 |
| - return null; |
| 26 | +function makeIdMap(json) { |
| 27 | + const out = new Map(); |
| 28 | + json.data.forEach(d => { |
| 29 | + out.set(d.id, d); |
| 30 | + }); |
| 31 | + return out; |
33 | 32 | }
|
34 | 33 |
|
35 | 34 | async function buildFormatsView() {
|
36 |
| -const desiredFormats = new Set(['eternal', 'standard', 'startup']); |
37 |
| -
|
38 |
| -//const [snapshotsResponse, restrictionsResponse] = await Promise.all( |
39 |
| -const [r1, r2, r3, r4, r5] = await Promise.all( |
40 |
| - [ |
41 |
| - fetch('http://localhost:3000/api/v3/public/snapshots?filter[active]=true').then(data => data.json()), |
42 |
| - fetch('http://localhost:3000/api/v3/public/restrictions').then(data => data.json()), |
43 |
| - fetch('http://localhost:3000/api/v3/public/card_cycles?sort=-date_release').then(data => data.json()), |
44 |
| - fetch('http://localhost:3000/api/v3/public/card_sets?sort=-date_release').then(data => data.json()), |
45 |
| - fetch('http://localhost:3000/api/v3/public/formats').then(data => data.json()), |
46 |
| - ] |
47 |
| -); |
48 |
| -
|
49 |
| -const restrictions = r2.data; |
50 |
| -const cycles = r3.data; |
51 |
| -const cyclesById = new Map(); |
52 |
| -cycles.forEach(c => { |
53 |
| - cyclesById.set(c.id, c); |
54 |
| -}); |
55 |
| -const sets = r4.data; |
56 |
| -const setsById = new Map(); |
57 |
| -sets.forEach(s => { |
58 |
| - setsById.set(s.id, s); |
59 |
| -}); |
60 |
| -const formatsById = new Map(); |
61 |
| -r5.data.forEach(f => { |
62 |
| - formatsById.set(f.id, f); |
63 |
| -}); |
64 |
| -
|
65 |
| - const formats = r1.data.filter(snapshot => desiredFormats.has(snapshot.attributes.format_id)).sort((a, b) => (a.attributes.format_id < b.attributes.format_id) ? 1 : -1); |
66 |
| -
|
67 |
| - formats.forEach(format => { |
68 |
| - const legalCycleIds = new Set(format.attributes.card_cycle_ids); |
69 |
| - const incompleteCycleIds = new Set(); |
70 |
| - // TODO(plural): Accommodate multiple sets from an incomplete cycle and sort them by descending release date. |
71 |
| - const incompleteSetIds = new Set(); |
72 |
| - format.attributes.card_set_ids.forEach(s => { |
73 |
| - if (!legalCycleIds.has(setsById.get(s).attributes.card_cycle_id)) { |
74 |
| - incompleteCycleIds.add(setsById.get(s).attributes.card_cycle_id); |
75 |
| - incompleteSetIds.add(s); |
76 |
| - } |
77 |
| - }); |
78 |
| -
|
79 |
| - $('#formats').append(`<div class="col-sm-4" id="${format.id}"><h2>${formatsById.get(format.attributes.format_id).attributes.name} Format</h2></div>`); |
80 |
| - $(`#${format.id}`).append(`<h4>Current Ban List</h4>`); |
81 |
| - if (format.attributes.restriction_id) { |
82 |
| - const r = getRestrictionById(restrictions, format.attributes.restriction_id); |
83 |
| - $(`#${format.id}`).append(`<p><strong><a href="#">${format.attributes.restriction_id}</a></strong><br />active as of <strong>${r.attributes.date_start}</strong></p>`); |
84 |
| - } else { |
85 |
| - $(`#${format.id}`).append(`<p><strong>None<br /> </p>`); |
| 35 | + const desiredFormats = new Set(['eternal', 'standard', 'startup']); |
| 36 | +
|
| 37 | + //const [snapshotsResponse, restrictionsResponse] = await Promise.all( |
| 38 | + const [r1, r2, r3, r4, r5] = await Promise.all( |
| 39 | + [ |
| 40 | + fetch('{{ v3_api_url }}/api/v3/public/snapshots?filter[active]=true').then(data => data.json()), |
| 41 | + fetch('{{ v3_api_url }}/api/v3/public/restrictions').then(data => data.json()), |
| 42 | + fetch('{{ v3_api_url }}/api/v3/public/card_cycles?sort=-date_release').then(data => data.json()), |
| 43 | + fetch('{{ v3_api_url }}/api/v3/public/card_sets?sort=-date_release').then(data => data.json()), |
| 44 | + fetch('{{ v3_api_url }}/api/v3/public/formats').then(data => data.json()), |
| 45 | + ] |
| 46 | + ); |
| 47 | +
|
| 48 | + const restrictions = makeIdMap(r2); |
| 49 | + const cycles = r3.data; |
| 50 | + const cyclesById = makeIdMap(r3); |
| 51 | + const sets = r4.data; |
| 52 | + const setsById = makeIdMap(r4); |
| 53 | + const formatsById = makeIdMap(r5); |
| 54 | +
|
| 55 | + const formats = r1.data.filter(snapshot => desiredFormats.has(snapshot.attributes.format_id)).sort((a, b) => (a.attributes.format_id < b.attributes.format_id) ? 1 : -1); |
| 56 | +
|
| 57 | + formats.forEach(format => { |
| 58 | + const legalCycleIds = new Set(format.attributes.card_cycle_ids); |
| 59 | + const incompleteCycleIds = new Set(); |
| 60 | + // TODO(plural): Accommodate multiple sets from an incomplete cycle and sort them by descending release date. |
| 61 | + const incompleteSetIds = new Set(); |
| 62 | + format.attributes.card_set_ids.forEach(s => { |
| 63 | + if (!legalCycleIds.has(setsById.get(s).attributes.card_cycle_id)) { |
| 64 | + incompleteCycleIds.add(setsById.get(s).attributes.card_cycle_id); |
| 65 | + incompleteSetIds.add(s); |
86 | 66 | }
|
| 67 | + }); |
87 | 68 |
|
88 |
| - $(`#${format.id}`).append(`<h4>Corp Decklists</h4>`); |
89 |
| - $(`#${format.id}`).append(`<ul id="${format.id}_corp_decklists"></ul>`); |
90 |
| - // TODO(plural): Wire up current restriction in the decklist search after making a legacy code for that as well. |
91 |
| - const packs = format.attributes.card_set_ids.map(s => setsById.get(s).attributes.legacy_code).sort(); |
92 |
| - $(`#${format.id}_corp_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'corp', 'packs': packs})}">Any Corp</a></li>`); |
93 |
| - $(`#${format.id}_corp_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'haas-bioroid', 'packs': packs})}"><span class="icon icon-haas-bioroid influence-haas-bioroid"></span> Haas-Bioroid</a></li>`); |
94 |
| - $(`#${format.id}_corp_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'jinteki', 'packs': packs})}"><span class="icon icon-jinteki influence-jinteki"></span> Jinteki</a></li>`); |
95 |
| - $(`#${format.id}_corp_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'nbn', 'packs': packs})}"><span class="icon icon-nbn influence-nbn"></span> NBN</a></li>`); |
96 |
| - $(`#${format.id}_corp_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'weyland-consortium', 'packs': packs})}"><span class="icon icon-weyland-consortium influence-weyland-consortium"></span> Weyland Consortium</a></li>`); |
97 |
| -
|
98 |
| - $(`#${format.id}`).append(`<h4>Runner Decklists</h4>`); |
99 |
| - $(`#${format.id}`).append(`<ul id="${format.id}_runner_decklists"></ul>`); |
100 |
| - $(`#${format.id}_runner_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'runner', 'packs': packs})}">Any Runner</a></li>`); |
101 |
| - $(`#${format.id}_runner_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'anarch', 'packs': packs})}"><span class="icon icon-anarch influence-anarch"></span> Anarch</a></li>`); |
102 |
| - $(`#${format.id}_runner_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'criminal', 'packs': packs})}"><span class="icon icon-criminal influence-criminal"></span> Criminal</a></li>`); |
103 |
| - $(`#${format.id}_runner_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'shaper', 'packs': packs})}"><span class="icon icon-shaper influence-shaper"></span> Shaper</a></li>`); |
104 |
| -
|
105 |
| - // Even though there are mini-faction cards in other sets, the IDs are only in Data & Destiny. |
106 |
| - if (legalCycleIds.has('data_and_destiny')) { |
107 |
| - $(`#${format.id}_runner_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'adam', 'packs': packs})}"><span class="icon icon-adam influence-adam"></span> Adam</a></li>`); |
108 |
| - $(`#${format.id}_runner_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'apex', 'packs': packs})}"><span class="icon icon-apex influence-apex"></span> Apex</a></li>`); |
109 |
| - $(`#${format.id}_runner_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'sunny-lebeau', 'packs': packs})}"><span class="icon icon-sunny-lebeau influence-sunny-lebeau"></span> Sunny Lebeau</a></li>`); |
110 |
| - } |
111 |
| - |
112 |
| - $(`#${format.id}`).append(`<h4>Legal Card Cycles - <a href="${Routing.generate('cards_find', {q: 'e:' + packs.join('|') } )}">${format.attributes.num_cards} Unique Cards</a></h4>`); |
113 |
| -
|
114 |
| - $(`#${format.id}`).append(`<ul id="${format.id}_cycles"></ul>`); |
115 |
| - cycles.forEach(cycle => { |
116 |
| - if (legalCycleIds.has(cycle.id)) { |
117 |
| - $(`#${format.id}_cycles`).append(`<li><a href="${Routing.generate('cards_cycle', {cycle_code: cycle.attributes.legacy_code})}">${cycle.attributes.name}</a></li>`); |
118 |
| - } else if (incompleteCycleIds.has(cycle.id)) { |
119 |
| - const incompleteListId = `${format.id}_cycles_${cycle.id}`; |
120 |
| - if (!$(`#${incompleteListId}`).length) { |
121 |
| - $(`#${format.id}_cycles`).append(`<li>${cycle.attributes.name}<ul id=${incompleteListId}></ul></li>`); |
122 |
| - } |
123 |
| - sets.forEach(set => { |
124 |
| - if (set.attributes.card_cycle_id == cycle.id && incompleteSetIds.has(set.id)) { |
125 |
| - $(`#${incompleteListId}`).append(`<li><a href="${Routing.generate('cards_list', {pack_code: set.attributes.legacy_code})}">${set.attributes.name}</a></li>`); |
126 |
| - } |
127 |
| - }); |
| 69 | + $('#formats').append(`<div class="col-sm-4" id="${format.id}"><h2>${formatsById.get(format.attributes.format_id).attributes.name} Format</h2></div>`); |
| 70 | + $(`#${format.id}`).append(`<h4>Current Ban List</h4>`); |
| 71 | + if (format.attributes.restriction_id) { |
| 72 | + const r = restrictions.get(format.attributes.restriction_id); |
| 73 | + $(`#${format.id}`).append(`<p><strong><a href="#">${r.attributes.name}</a></strong><br />active as of <strong>${r.attributes.date_start}</strong></p>`); |
| 74 | + } else { |
| 75 | + $(`#${format.id}`).append(`<p><strong>None<br /> </p>`); |
| 76 | + } |
| 77 | +
|
| 78 | + $(`#${format.id}`).append(`<h4>Corp Decklists</h4>`); |
| 79 | + $(`#${format.id}`).append(`<ul id="${format.id}_corp_decklists"></ul>`); |
| 80 | + // TODO(plural): Wire up current restriction in the decklist search after making a legacy code for that as well. |
| 81 | + const packs = format.attributes.card_set_ids.map(s => setsById.get(s).attributes.legacy_code).sort(); |
| 82 | + $(`#${format.id}_corp_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'corp', 'packs': packs})}">Any Corp</a></li>`); |
| 83 | + $(`#${format.id}_corp_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'haas-bioroid', 'packs': packs})}"><span class="icon icon-haas-bioroid influence-haas-bioroid"></span> Haas-Bioroid</a></li>`); |
| 84 | + $(`#${format.id}_corp_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'jinteki', 'packs': packs})}"><span class="icon icon-jinteki influence-jinteki"></span> Jinteki</a></li>`); |
| 85 | + $(`#${format.id}_corp_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'nbn', 'packs': packs})}"><span class="icon icon-nbn influence-nbn"></span> NBN</a></li>`); |
| 86 | + $(`#${format.id}_corp_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'weyland-consortium', 'packs': packs})}"><span class="icon icon-weyland-consortium influence-weyland-consortium"></span> Weyland Consortium</a></li>`); |
| 87 | +
|
| 88 | + $(`#${format.id}`).append(`<h4>Runner Decklists</h4>`); |
| 89 | + $(`#${format.id}`).append(`<ul id="${format.id}_runner_decklists"></ul>`); |
| 90 | + $(`#${format.id}_runner_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'runner', 'packs': packs})}">Any Runner</a></li>`); |
| 91 | + $(`#${format.id}_runner_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'anarch', 'packs': packs})}"><span class="icon icon-anarch influence-anarch"></span> Anarch</a></li>`); |
| 92 | + $(`#${format.id}_runner_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'criminal', 'packs': packs})}"><span class="icon icon-criminal influence-criminal"></span> Criminal</a></li>`); |
| 93 | + $(`#${format.id}_runner_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'shaper', 'packs': packs})}"><span class="icon icon-shaper influence-shaper"></span> Shaper</a></li>`); |
| 94 | +
|
| 95 | + // Even though there are mini-faction cards in other sets, the IDs are only in Data & Destiny. |
| 96 | + if (legalCycleIds.has('data_and_destiny')) { |
| 97 | + $(`#${format.id}_runner_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'adam', 'packs': packs})}"><span class="icon icon-adam influence-adam"></span> Adam</a></li>`); |
| 98 | + $(`#${format.id}_runner_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'apex', 'packs': packs})}"><span class="icon icon-apex influence-apex"></span> Apex</a></li>`); |
| 99 | + $(`#${format.id}_runner_decklists`).append(`<li><a href="${Routing.generate('decklists_list', {type:'find', 'faction':'sunny-lebeau', 'packs': packs})}"><span class="icon icon-sunny-lebeau influence-sunny-lebeau"></span> Sunny Lebeau</a></li>`); |
| 100 | + } |
| 101 | + |
| 102 | + $(`#${format.id}`).append(`<h4>Legal Card Cycles - <a href="${Routing.generate('cards_find', {q: 'e:' + packs.join('|') } )}">${format.attributes.num_cards} Unique Cards</a></h4>`); |
| 103 | +
|
| 104 | + $(`#${format.id}`).append(`<ul id="${format.id}_cycles"></ul>`); |
| 105 | + cycles.forEach(cycle => { |
| 106 | + if (legalCycleIds.has(cycle.id)) { |
| 107 | + $(`#${format.id}_cycles`).append(`<li><a href="${Routing.generate('cards_cycle', {cycle_code: cycle.attributes.legacy_code})}">${cycle.attributes.name}</a></li>`); |
| 108 | + } else if (incompleteCycleIds.has(cycle.id)) { |
| 109 | + const incompleteListId = `${format.id}_cycles_${cycle.id}`; |
| 110 | + if (!$(`#${incompleteListId}`).length) { |
| 111 | + $(`#${format.id}_cycles`).append(`<li>${cycle.attributes.name}<ul id=${incompleteListId}></ul></li>`); |
128 | 112 | }
|
129 |
| - }); |
| 113 | + sets.forEach(set => { |
| 114 | + if (set.attributes.card_cycle_id == cycle.id && incompleteSetIds.has(set.id)) { |
| 115 | + $(`#${incompleteListId}`).append(`<li><a href="${Routing.generate('cards_list', {pack_code: set.attributes.legacy_code})}">${set.attributes.name}</a></li>`); |
| 116 | + } |
| 117 | + }); |
| 118 | + } |
130 | 119 | });
|
| 120 | + }); |
131 | 121 | }
|
132 | 122 | buildFormatsView();
|
133 | 123 | </script>
|
|
0 commit comments