1+ // globals classe, selfId, collections, decksArtifacts, userArtifacts, decks, addArtifact, clearArtifacts, removeCard, removeAllCards, addCard, lastOpenedDialog
12settings . register ( {
23 name : 'Disable Deck Storage' ,
34 key : 'underscript.storage.disable' ,
@@ -15,6 +16,12 @@ settings.register({
1516
1617onPage ( 'Decks' , function deckStorage ( ) {
1718 if ( settings . value ( 'underscript.storage.disable' ) ) return ;
19+ const mockLastOpenedDialog = {
20+ close ( ) { } ,
21+ } ;
22+ let templastOpenedDialog ;
23+ style . add ( '.btn-storage { margin-top: 5px; margin-right: 8px; width: 36px; }' ) ;
24+
1825 function getFromLibrary ( id , shiny , library ) {
1926 return library . find ( card => card . id === id && ( shiny === undefined || card . shiny === shiny ) ) ;
2027 }
@@ -38,43 +45,51 @@ onPage('Decks', function deckStorage() {
3845 let pending = [ ] ;
3946
4047 function processNext ( ) {
41- const card = pending . shift ( ) ;
42- if ( card ) {
43- if ( card . action === 'validate' ) {
48+ if ( lastOpenedDialog === mockLastOpenedDialog ) {
49+ lastOpenedDialog = templastOpenedDialog ;
50+ }
51+ const job = pending . shift ( ) ;
52+ if ( job ) {
53+ console . log ( 'Processing' , job ) ;
54+ if ( job . action === 'validate' ) {
4455 loadDeck ( loading ) ;
4556 if ( ! pending . length ) {
4657 loading = null ;
4758 }
48- } else if ( card . action === 'clear' ) {
59+ return ;
60+ } else if ( job . action === 'clear' ) {
4961 removeAllCards ( ) ;
50- } else if ( card . action === 'remove' ) {
51- removeCard ( parseInt ( card . id ) , card . shiny === true ) ;
62+ } else if ( job . action === 'remove' ) {
63+ removeCard ( parseInt ( job . id ) , job . shiny === true ) ;
64+ } else if ( job . action === 'clearArtifacts' ) {
65+ clearArtifacts ( ) ;
66+ } else if ( job . action === 'addArtifact' ) {
67+ debug ( `Adding artifact: ${ job . id } ` ) ;
68+ templastOpenedDialog = lastOpenedDialog ;
69+ lastOpenedDialog = mockLastOpenedDialog ;
70+ addArtifact ( job . id ) ;
5271 } else {
53- addCard ( parseInt ( card . id ) , card . shiny === true ) ;
54- if ( ! pending . length ) {
55- pending . push ( { action : 'validate' } )
56- }
72+ addCard ( parseInt ( job . id ) , job . shiny === true ) ;
73+ }
74+ if ( ! pending . length ) {
75+ pending . push ( { action : 'validate' } )
5776 }
5877 }
5978 }
6079
6180 eventManager . on ( 'Deck:postChange' , ( data ) => {
62- if ( ! [ 'addCard' , 'removeCard' , 'removeAllCards' ] . includes ( data . action ) ) return ;
81+ if ( ! [ 'addCard' , 'removeCard' , 'removeAllCards' , 'clearArtifacts' , 'addArtifact' ] . includes ( data . action ) ) return ;
6382 if ( data . status === "error" ) {
6483 pending = [ ] ;
6584 return ;
6685 }
6786 processNext ( ) ;
6887 } ) ;
6988
70- for ( let i = 1 , x = Math . max ( parseInt ( settings . value ( 'underscript.storage.rows' ) ) , 1 ) * 3 ; i <= x ; i ++ ) {
71- buttons . push ( $ ( `<button>${ i } </button>` )
72- . addClass ( 'btn btn-sm btn-danger' )
73- . css ( {
74- 'margin-top' : '5px' ,
75- 'margin-right' : '2px' ,
76- width : '58px' ,
77- } ) ) ;
89+ for ( let i = 1 , x = Math . max ( parseInt ( settings . value ( 'underscript.storage.rows' ) ) , 1 ) * 4 ; i <= x ; i ++ ) {
90+ buttons . push ( $ ( '<button>' )
91+ . text ( i )
92+ . addClass ( 'btn btn-sm btn-danger btn-storage' ) ) ;
7893 }
7994
8095 buttons . forEach ( ( button ) => {
@@ -88,37 +103,47 @@ onPage('Decks', function deckStorage() {
88103
89104 eventManager . on ( 'Deck:Soul' , loadStorage ) ;
90105
106+ function fixDeck ( id ) {
107+ const key = getKey ( id ) ;
108+ const deck = JSON . parse ( localStorage . getItem ( key ) ) ;
109+ if ( ! deck ) return ;
110+ if ( ! deck . hasOwnProperty ( 'cards' ) ) {
111+ localStorage . setItem ( key , JSON . stringify ( {
112+ cards : deck ,
113+ artifacts : [ ] ,
114+ } ) ) ;
115+ }
116+ }
117+
91118 function saveDeck ( i ) {
92- const soul = $ ( '#selectClasses' ) . find ( ':selected' ) . text ( ) ;
93- const deck = [ ] ;
94- $ ( `#deckCards li` ) . each ( ( i , e ) => {
95- const card = {
96- id : parseInt ( $ ( e ) . attr ( 'id' ) ) ,
97- } ;
98- if ( $ ( e ) . hasClass ( ' shiny' ) ) {
119+ const deck = {
120+ cards : [ ] ,
121+ artifacts : [ ] ,
122+ } ;
123+ decks [ classe ] . forEach ( ( { id , shiny } ) => {
124+ const card = { id , } ;
125+ if ( shiny ) {
99126 card . shiny = true ;
100127 }
101- deck . push ( card ) ;
128+ deck . cards . push ( card ) ;
102129 } ) ;
103- if ( ! deck . length ) return ;
104- localStorage . setItem ( `underscript.deck.${ selfId } .${ soul } .${ i } ` , JSON . stringify ( deck ) ) ;
130+ decksArtifacts [ classe ] . forEach ( ( { id } ) => deck . artifacts . push ( id ) ) ;
131+ if ( ! deck . cards . length && ! deck . artifacts . length ) return ;
132+ localStorage . setItem ( getKey ( i ) , JSON . stringify ( deck ) ) ;
105133 }
106134
107135 function loadDeck ( i ) {
108136 if ( i === null ) return ;
109137 debug ( 'loading' ) ;
110138 pending = [ ] ; // Clear pending
111139 loading = i ;
112- const soul = $ ( '#selectClasses' ) . find ( ':selected' ) . text ( ) ;
113- let deck = getDeck ( `underscript.deck.${ selfId } .${ soul } .${ i } ` , true ) ;
114- const cDeck = $ ( `#deckCards li` ) ;
140+ let deck = getDeck ( i , true ) ;
141+ const cDeck = decks [ classe ] ;
115142
116143 if ( cDeck . length ) {
117144 const builtDeck = [ ] ;
118145 // Build deck options
119- cDeck . each ( ( i , e ) => {
120- const id = parseInt ( $ ( e ) . attr ( 'id' ) ) ;
121- const shiny = $ ( e ) . hasClass ( 'shiny' ) ;
146+ cDeck . forEach ( ( { id, shiny} ) => {
122147 builtDeck . push ( {
123148 id, shiny,
124149 action : 'remove' ,
@@ -140,7 +165,7 @@ onPage('Decks', function deckStorage() {
140165 // Check what we need to do
141166 if ( ! removals . length && ! temp . length ) { // There's nothing
142167 debug ( 'Finished' ) ;
143- return ;
168+ deck = [ ] ;
144169 } else if ( removals . length > 13 ) { // Too much to do (Cards in deck + 1)
145170 pending . push ( {
146171 action : 'clear' ,
@@ -151,15 +176,50 @@ onPage('Decks', function deckStorage() {
151176 }
152177 }
153178 pending . push ( ...deck ) ;
179+ if ( ! matchingArtifacts ( i ) ) {
180+ debug ( 'Loading Artifacts' ) ;
181+ pending . push ( {
182+ action : 'clearArtifacts' ,
183+ } ) ;
184+ getArtifacts ( i ) . forEach ( ( id ) => {
185+ pending . push ( {
186+ id,
187+ action : 'addArtifact' ,
188+ } )
189+ } ) ;
190+ }
154191 processNext ( ) ;
155192 }
156193
157- function getDeck ( key , trim ) {
194+
195+ function matchingArtifacts ( id ) {
196+ const dArts = getArtifacts ( id ) ;
197+ const cArts = decksArtifacts [ classe ] ;
198+ return ! dArts . length || dArts . length === cArts . length && cArts . every ( ( { id : id1 } ) => ! ! ~ dArts . indexOf ( id1 ) ) ;
199+ }
200+
201+ function getKey ( id ) {
202+ const soul = classe ;
203+ return `underscript.deck.${ selfId } .${ soul } .${ id } ` ;
204+ }
205+
206+ function getDeck ( id , trim ) {
207+ fixDeck ( id ) ;
208+ const key = getKey ( id ) ;
158209 const deck = JSON . parse ( localStorage . getItem ( key ) ) ;
210+ if ( ! deck ) return null ;
159211 if ( trim ) {
160- return deck . filter ( ( { id, shiny} ) => getCardData ( id , shiny ) !== null ) ;
212+ return deck . cards . filter ( ( { id, shiny} ) => getCardData ( id , shiny ) !== null ) ;
161213 }
162- return deck ;
214+ return deck . cards ;
215+ }
216+
217+ function getArtifacts ( id ) {
218+ fixDeck ( id ) ;
219+ const key = getKey ( id ) ;
220+ const deck = JSON . parse ( localStorage . getItem ( key ) ) ;
221+ if ( ! deck ) return [ ] ;
222+ return deck . artifacts || [ ] ;
163223 }
164224
165225 function cards ( list ) {
@@ -172,6 +232,17 @@ onPage('Decks', function deckStorage() {
172232 return names . join ( '<br />' ) ;
173233 }
174234
235+ function artifacts ( id ) {
236+ const list = [ ] ;
237+ getArtifacts ( id ) . forEach ( ( art ) => {
238+ const artifact = userArtifacts . find ( ( { id : artID } ) => artID === art ) ;
239+ if ( artifact ) {
240+ list . push ( `<span class="${ artifact . legendary ? 'yellow' : '' } "><img style="height: 16px;" src="images/artifacts/${ artifact . image } .png" /> ${ artifact . name } </span>` ) ;
241+ }
242+ } ) ;
243+ return list . join ( ', ' ) ;
244+ }
245+
175246 function loadStorage ( ) {
176247 for ( let i = 0 ; i < buttons . length ; i ++ ) {
177248 loadButton ( i ) ;
@@ -180,7 +251,7 @@ onPage('Decks', function deckStorage() {
180251
181252 function loadButton ( i ) {
182253 const soul = classe ;
183- const deckKey = `underscript.deck. ${ selfId } . ${ soul } . ${ i } ` ;
254+ const deckKey = getKey ( i ) ;
184255 const nameKey = `${ deckKey } .name` ;
185256 const button = buttons [ i ] ;
186257 button . off ( '.deckStorage' ) ; // Remove any lingering events
@@ -196,11 +267,12 @@ onPage('Decks', function deckStorage() {
196267 function hoverButton ( e ) {
197268 let text = '' ;
198269 if ( e . type === 'mouseenter' ) {
199- const deck = getDeck ( deckKey ) ;
270+ const deck = getDeck ( i ) ;
200271 if ( deck ) {
201272 text = `
202273 <div id="deckName">${ localStorage . getItem ( nameKey ) || ( `${ soul } -${ i + 1 } ` ) } </div>
203274 <div><input id="deckNameInput" maxlength="28" style="border: 0; border-bottom: 2px solid #00617c; background: #000; width: 100%; display: none;" type="text" placeholder="${ soul } -${ i + 1 } " value="${ localStorage . getItem ( nameKey ) || '' } "></div>
275+ <div style="font-size: 13px;">${ artifacts ( i ) } </div>
204276 <div>Click to load (${ deck . length } )</div>
205277 <div style="font-size: 13px;">${ cards ( deck ) } </div>
206278 <div style="font-style: italic; color: #b3b3b3;">
0 commit comments