Skip to content

Commit 78e766a

Browse files
committed
#38: Deck class rewritten to use new pickFromObjectArray
1 parent 72adc62 commit 78e766a

2 files changed

Lines changed: 18 additions & 35 deletions

File tree

200-helpersGeneral.gs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ function pickFromObjectArray(objectArray, property, value, remove = true) {
205205
}
206206
if (match) {
207207
if (remove)
208-
return objectArray.splice(i, 1);
208+
return objectArray.splice(i, 1)[0];
209209
else
210210
return objectArray[i];
211211
}

classes/110-Deck.gs

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)