Skip to content

Commit fb3fef5

Browse files
committed
Bug fix in default value setting (caused by empty string).
1 parent f3122ae commit fb3fef5

6 files changed

Lines changed: 34 additions & 22 deletions

File tree

200-helpersGeneral.gs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,18 @@ function getHighestProperty(obj) {
481481
* Internally used helper functions.
482482
*/
483483

484+
/**
485+
* Combines default values and overwrite values, bypassing problems with empty strings.
486+
*/
487+
function applyDefaults(defaults, overwrites) {
488+
let result = {};
489+
Object.assign(result, defaults);
490+
Object.assign(result, overwrites);
491+
for (let i in defaults)
492+
if (result[i] === '') result[i] = defaults[i];
493+
return result;
494+
}
495+
484496
/**
485497
* Processes arguments object into an array, skipping the number of specified first items.
486498
*/

classes/110-Deck.gs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class Deck {
1919
* - resolver (string): Name of method in modules[module].resolvers.cards. Called from card.resolver().
2020
*/
2121
constructor(deckData, cardDataArray = false) {
22-
// Add default settings.
23-
Object.assign(this, global.defaults.deck);
24-
// Build basic data and verify required properties.
22+
// Add default settings, overwrite with provided data.
23+
Object.assign(this, applyDefaults(global.defaults.deck, deckData));
24+
// Verify that an ID is present.
2525
Object.assign(this, deckData);
2626
if (this.id === undefined)
2727
throw('Decks must have an id property set.');

classes/120-Track.gs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323
*/
2424
class Track {
2525
constructor(trackData, spacesDataArray = false) {
26-
// Add default settings.
27-
Object.assign(this, global.defaults.track);
28-
// Build basic data and verify required properties.
29-
Object.assign(this, trackData);
26+
// Add default settings, overwrite with provided data.
27+
let o = Object.assign(this, applyDefaults(global.defaults.track, trackData));
28+
// Verify that an ID is present.
3029
if (this.id === undefined)
3130
throw('Tracks must have an id property set.');
3231

@@ -46,7 +45,6 @@ class Track {
4645
this.constructSpace(s);
4746
}
4847
}
49-
5048
// Build a graph of how the spaces connect, if advanced movement is used.
5149
if (this.gridMovement)
5250
this.buildGraph();
@@ -55,7 +53,6 @@ class Track {
5553
this.spaceMapping = {};
5654
for (let i in this.spaces)
5755
this.spaceMapping[this.spaces[i].id] = i;
58-
5956
// Object used to track where on the track pawns are or are going.
6057
this.pawnIndices = {};
6158
}

classes/130-Market.gs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@
3131
// @TODO: Consider a way to track agents' increase/decrease in resources when buying.
3232
class Market {
3333
constructor(marketData, goodsDataArray = false) {
34-
// Add default settings.
35-
Object.assign(this, global.defaults.market);
36-
// Build basic data and verify required properties.
37-
Object.assign(this, marketData);
34+
// Add default settings, overwrite with provided data.
35+
Object.assign(this, applyDefaults(global.defaults.market, marketData));
36+
// Verify that an ID is present.
3837
if (this.id === undefined)
3938
throw('Markets must have an id property set.');
4039

@@ -303,8 +302,7 @@ class Goods {
303302
if (!market instanceof Market)
304303
throw('Goods must be added to a proper market.');
305304
// Add default settings, overwrite with provided data.
306-
Object.assign(this, global.defaults.goods);
307-
Object.assign(this, goodsData);
305+
Object.assign(this, applyDefaults(global.defaults.goods, goodsData));
308306

309307
if (this.id === undefined)
310308
throw('Goods must have an id property set.');

classes/140-DiceRoll.gs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DiceRoll {
2727
}
2828

2929
/**
30-
* Treats die results as if only the n first dice are present, until
30+
* Treats dice results as if only the n first dice are present, until
3131
* 'unlock' function is called.
3232
* @return The DiceRoll object.
3333
*/
@@ -88,8 +88,8 @@ class DiceRoll {
8888
}
8989

9090
/**
91-
* Returns an array of all straighst in the dice results, optionally only counting
92-
* results between 'lowest' and 'highest'. Result is on the form [[1, 2], [5, 6]].
91+
* Returns an array of all straights in the dice results, optionally only counting
92+
* results between 'lowest' and 'highest'. Return is on the form [[1, 2], [5, 6]].
9393
*/
9494
getStraights(lowest, highest) {
9595
return getStraights(this.result, lowest, highest);
@@ -106,11 +106,11 @@ class DiceRoll {
106106
/**
107107
* Returns the highest number of equal dice with value above 'threshold'.
108108
*/
109-
countEquals(threshold = Number.NEGATIVE_INFINITY) {
109+
getHighestFrequency(threshold = false) {
110110
let distribution = getFrequencies(this.result);
111111
let max = 0;
112112
for (let i in distribution) {
113-
if (i >= threshold && distribution[i] > max)
113+
if (distribution[i] > max && (threshold === false || i >= threshold))
114114
max = distribution[i];
115115
}
116116
return max;

tests/300-tests.gs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,12 @@ tests.diceRoll.theLot = function() {
395395
d.result = [1, 2, 3, 4, 4];
396396
if (d.sum() != 14)
397397
return 'Dice sum does not work correctly.';
398-
if (d.countEquals(4) != 2)
399-
return 'countEquals does not work correctly.';
398+
d.result = [1, 1, 1, 2, 3, 4, 4, 'a', 'a', 'a', 'a'];
399+
if (d.getHighestFrequency() != 4)
400+
return 'countEquals does not count non-numeric results.';
401+
if (d.getHighestFrequency(0) != 3)
402+
return 'countEquals does not exclude non-numeric results when a threshold is set.';
403+
if (d.getHighestFrequency(4) != 2)
404+
return 'countEquals does not apply threshold correctly.';
400405

401406
}

0 commit comments

Comments
 (0)