Skip to content

Commit b024bc0

Browse files
authored
Merge pull request #67 from Itangalo/1.2
1.2
2 parents 4db1b19 + 59f4184 commit b024bc0

12 files changed

Lines changed: 155 additions & 174 deletions

000-simulation.gs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ function simulate(iterations = false, mod = false) {
2525
if (mod !== false)
2626
module = mod;
2727

28+
let extraArguments = parseArguments(arguments, 2);
2829
log('Starting to build initial data.', 'system');
29-
let gameStateSeed = modules[module].buildInitialData();
30+
let gameStateSeed = modules[module].buildInitialData(...extraArguments);
3031
log('Initial data complete.', 'system');
3132

3233
/**
@@ -70,23 +71,23 @@ function simulate(iterations = false, mod = false) {
7071
}
7172

7273
// Make any customized additional processing of the game state.
73-
modules[module].preIteration();
74+
modules[module].preIteration(...extraArguments);
7475

7576
/**
7677
* Play the game until it is over.
7778
*/
7879
gameState.round = 0;
7980
log('Starting first round in iteration ' + iteration, 'system');
80-
while (!modules[module].gameOver()) {
81+
while (!modules[module].gameOver(...extraArguments)) {
8182
gameState.round++;
8283
// Call the function carrying out the actual actions in a round.
83-
modules[module].playRound();
84+
modules[module].playRound(...extraArguments);
8485
}
8586

8687
/**
8788
* Process data that should be stored for statistics.
8889
*/
89-
results.push(modules[module].buildStatistics());
90+
results.push(modules[module].buildStatistics(...extraArguments));
9091
}
9192

9293
/**

200-helpersGeneral.gs

Lines changed: 5 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -176,83 +176,6 @@ function shuffle(array) {
176176
return array;
177177
}
178178

179-
/**
180-
* Returns first object in an array matching property:value or false if no match is found.
181-
*
182-
* @param property: The property name to search in, or an array of property names if multiple
183-
* property:values pairs should be matched.
184-
* @param value: The value to match, or an array of values if multiple property:value pairs
185-
* should be matched.
186-
* @param {boolean} remove: Whether to also remove the object from the array. Defaults to true.
187-
*/
188-
function pickFromObjectArray(objectArray, property, value, remove = true) {
189-
if (objectArray.length == 0)
190-
return false;
191-
// Turn single-value search criteria into arrays.
192-
if (typeof property != 'object')
193-
property = [property];
194-
if (typeof value != 'object')
195-
value = [value];
196-
if (property.length != value.length)
197-
throw('The number of values to search for must match the number of properties.');
198-
199-
// Find the first matching object.
200-
for (let i in objectArray) {
201-
let match = true;
202-
for (let j in property) {
203-
if (objectArray[i][property[j]] != value[j])
204-
match = false;
205-
}
206-
if (match) {
207-
if (remove)
208-
return objectArray.splice(i, 1)[0];
209-
else
210-
return objectArray[i];
211-
}
212-
}
213-
return false;
214-
}
215-
216-
/**
217-
* Returns an array with all objects matching property:value in the search array.
218-
*
219-
* @param property: The property name to search in, or an array of property names if multiple
220-
* property:values pairs should be matched.
221-
* @param value: The value to match, or an array of values if multiple property:value pairs
222-
* should be matched.
223-
* @param {boolean} remove: Whether to also remove the objects from the array. Defaults to true.
224-
*/
225-
function pickAllFromObjectArray(objectArray, property, value, remove = true) {
226-
let result = [];
227-
if (objectArray.length == 0)
228-
return result;
229-
// Turn single-value search criteria into arrays.
230-
if (typeof property != 'object')
231-
property = [property];
232-
if (typeof value != 'object')
233-
value = [value];
234-
if (property.length != value.length)
235-
throw('The number of values to search for must match the number of properties.');
236-
237-
// Find matching objects.
238-
for (let i = 0; i < objectArray.length; i++) {
239-
let match = true;
240-
for (let j in property) {
241-
if (objectArray[i][property[j]] != value[j])
242-
match = false;
243-
}
244-
if (match) {
245-
if (remove) {
246-
result.push(objectArray.splice(i, 1));
247-
i--; // The indices are shifted, so we move back one step.
248-
}
249-
else
250-
result.push(objectArray[i]);
251-
}
252-
}
253-
return result;
254-
}
255-
256179
// Returns how many times 'value' occurs in 'array'.
257180
function getFrequency(array, value) {
258181
let frequency = 0;
@@ -389,7 +312,7 @@ function isIterable(obj) {
389312
function getAgentById(id) {
390313
if (!gameState.agents)
391314
return false;
392-
return pickFromObjectArray(gameState.agents, 'id', id, false);
315+
return new ObjectFilter({id: id}).findFirstInArray(gameState.agents);
393316
}
394317

395318
// Selects a and returns a random element from an array. If the array consists of
@@ -532,22 +455,18 @@ function compareObjects(o1, o2) {
532455
* @param {string} type: The type of object: cards, spaces or goods.
533456
* @param {string} method: The name of the resolver method.
534457
*/
535-
function callResolver(type, method) {
458+
function callResolver(method) {
536459
if (!method)
537460
return false;
538461

539462
if (!modules[module].resolvers) {
540463
log('Active module does not have any resolvers.', 'error');
541464
return false;
542465
}
543-
if (!modules[module].resolvers[type]) {
544-
log('Active module does not have any resolvers for ' + type + '.', 'error');
545-
return false;
546-
}
547-
if (!modules[module].resolvers[type][method]) {
548-
log('Active module does not have a resolver ' + method + ' for ' + type + '.', 'error');
466+
if (!modules[module].resolvers[method]) {
467+
log('Active module does not have a resolver ' + method + '.', 'error');
549468
return false;
550469
}
551470

552-
return modules[module].resolvers[type][method](...parseArguments(arguments, 2));
471+
return modules[module].resolvers[type][method](...parseArguments(arguments, 1));
553472
}

classes/100-Agent.gs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* special properties:
66
* - id: The unique id for the agent. Required.
77
* - strategy: Any strategy used by the agent, and called from
8-
* agent.consultStragety(method, arguments...). Methods must be
8+
* agent.consultStrategy(method, arguments...). Methods must be
99
* added to agentStrategies[module].
1010
*
1111
*/

classes/110-Deck.gs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Deck {
1616
*
1717
* @param {Array} cardDataArray: An array of objects which will be used to create cards.
1818
* Optional. Special properties used for cards:
19-
* - resolver (string): Name of method in modules[module].resolvers.cards. Called from card.resolver().
19+
* - resolver (string): Name of method in modules[module].resolvers. Called from card.resolver().
2020
*/
2121
constructor(deckData, cardDataArray = false) {
2222
// Add default settings, overwrite with provided data.
@@ -124,26 +124,37 @@ class Deck {
124124
}
125125

126126
/**
127-
* 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.
127+
* Returns the first card found matching property:value, or false if none is found.
128+
* The card is removed from the deck.
129+
*
130+
* For more complex conditions, see ObjectFilter.
129131
*/
130132
pick(property, value) {
131-
let result = pickAllFromObjectArray(this.cards, property, value);
132-
if (!result)
133+
let condition = {};
134+
condition[property] = value;
135+
let output = new ObjectFilter(condition).removeFromArray(this.cards, 1);
136+
if (output.length) {
133137
log('Could not find any card where ' + property + ' is ' + value + ' in deck ' + this.id + '.', 'notice');
134-
return result;
138+
return false;
139+
}
140+
return output[0];
135141
}
136142

137143
/**
138-
* 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.
144+
* Returns all cards matching property:value in an array (which could be empty).
145+
* The cards are removed from the deck.
146+
*
147+
* For more complex conditions, see ObjectFilter.
140148
*/
141149
pickAll(property, value) {
142-
return pickAllFromObjectArray(this.cards, property, value);
150+
let condition = {};
151+
condition[property] = value;
152+
return new ObjectFilter(condition).removeFromArray(this.cards);
143153
}
144154

145155
/**
146156
* Counts how many cards matching property:value are in the deck.
157+
* For more complex conditions, see ObjectFilter.
147158
*/
148159
countOccurances(property, value) {
149160
let occurances = 0;
@@ -188,8 +199,8 @@ class Deck {
188199

189200
/**
190201
* 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.
192202
* If no property or value is provided, the first card in the display will be returned.
203+
* For more complex conditions, see ObjectFilter.
193204
*/
194205
pickFromDisplay(property, value) {
195206
// If no property is provided, take the first card.
@@ -202,7 +213,10 @@ class Deck {
202213
c = this.display.shift();
203214
}
204215
else {
205-
c = pickFromObjectArray(this.display, property, value);
216+
let condition = {};
217+
condition[property] = value;
218+
c = new ObjectFilter(condition).removeFirstFromArray(this.display);
219+
206220
if (!c)
207221
log('Could not find any card where ' + property + ' is ' + value + ' in display of deck ' + this.id + '.', 'error');
208222
}
@@ -246,9 +260,9 @@ class Card {
246260
/**
247261
* Calls any resolver set for the card. Any arguments will be sent to the resolver.
248262
* The card needs to have a the property 'resolver' set and a corresponding
249-
* method must be placed in modules[module].resolvers.cards.
263+
* method must be placed in modules[module].resolvers.
250264
*/
251265
resolve() {
252-
return callResolver('cards', this.resolver, ...arguments);
266+
return callResolver(this.resolver, ...arguments);
253267
}
254268
}

0 commit comments

Comments
 (0)