@@ -125,32 +125,21 @@ class Deck {
125125
126126 /**
127127 * Picks the first card found matching property:value, or false if none is found.
128+ * If multiple property:value pairs should be matched, provide them as arrays.
128129 */
129130 pick ( property , value ) {
130- for ( let i in this . cards ) {
131- if ( this . cards [ i ] [ property ] == value ) {
132- let c = this . cards [ i ] ;
133- this . cards . splice ( i , 1 ) ;
134- return c ;
135- }
136- }
137- log ( 'Could not find any card where ' + property + ' is ' + value + ' in deck ' + this . id + '.' , 'notice' ) ;
138- return false ;
131+ let result = pickAllFromObjectArray ( this . cards , property , value ) ;
132+ if ( ! result )
133+ log ( 'Could not find any card where ' + property + ' is ' + value + ' in deck ' + this . id + '.' , 'notice' ) ;
134+ return result ;
139135 }
140136
141137 /**
142138 * Picks all cards matching property:value, returned in an array (can be empty).
139+ * If multiple property:value pairs should be matched, provide them as arrays.
143140 */
144141 pickAll ( property , value ) {
145- let picked = [ ] ;
146- for ( let i = this . cards . length - 1 ; i >= 0 ; i -- ) {
147- if ( this . cards [ i ] [ property ] == value ) {
148- let c = this . cards [ i ] ;
149- this . cards . splice ( i , 1 ) ;
150- picked . push ( c ) ;
151- }
152- }
153- return picked ;
142+ return pickAllFromObjectArray ( this . cards , property , value ) ;
154143 }
155144
156145 /**
@@ -199,33 +188,27 @@ class Deck {
199188
200189 /**
201190 * Picks the first card found matching property:value from the display, or false if none is found.
191+ * If multiple property:value pairs should be matched, provide them as arrays.
202192 * If no property or value is provided, the first card in the display will be returned.
203193 */
204194 pickFromDisplay ( property , value ) {
205195 // If no property is provided, take the first card.
196+ let c = false ;
206197 if ( property === undefined ) {
207198 if ( ! this . display . length ) {
208199 log ( 'Tried to pick the first card in the display of deck ' + this . id + ', but the display is empty.' , 'error' ) ;
209200 return false ;
210201 }
211- let c = this . display . shift ( ) ;
212- if ( this . autoFillDisplay )
213- this . fillDisplay ( ) ;
214- return c ;
202+ c = this . display . shift ( ) ;
215203 }
216-
217- // Search for a card matching property:value.
218- for ( let i in this . display ) {
219- if ( this . display [ i ] [ property ] == value ) {
220- let c = this . display [ i ] ;
221- this . display . splice ( i , 1 ) ;
222- if ( this . autoFillDisplay )
223- this . fillDisplay ( ) ;
224- return c ;
225- }
204+ else {
205+ c = pickFromObjectArray ( this . display , property , value ) ;
206+ if ( ! c )
207+ log ( 'Could not find any card where ' + property + ' is ' + value + ' in display of deck ' + this . id + '.' , 'error' ) ;
226208 }
227- log ( 'Could not find any card where ' + property + ' is ' + value + ' in display of deck ' + this . id + '.' , 'error' ) ;
228- return false ;
209+ if ( this . autoFillDisplay )
210+ this . fillDisplay ( ) ;
211+ return c ;
229212 }
230213}
231214
0 commit comments