Itangalo / Bot-Playtesting-Toolkit

A toolkit for simulating board games with Google spreadsheets. Intended use is board game development.
GNU General Public License v3.0
9 stars 1 forks source link

Streamline how default objects are created #55

Closed Itangalo closed 2 years ago

Itangalo commented 2 years ago

The simulate function repeats a lot of code when creating agents, decks, tracks and markets. It could probably be more streamlined, with less repetition.

Itangalo commented 2 years ago

No. The general approach became almost as long as the explicit approach, and less readable.

Saving it here anyway, if the number of classes grow in the future.

let objectTypesToCreate = {
  agents: {class: Agent},
  decks: {class: Deck, args: ['deck', 'cards']},
  tracks: {class: Track, args: ['track', 'spaces', 'pawns']},
  markets: {class: Market, args: ['market', 'goods']},
};
for (let i in objectTypesToCreate) {
  if (gameState[i]) {
    delete(gameState[i]);
    for (let d of gameStateSeed[i]) {
      if (!objectTypesToCreate[i].args) {
        new objectTypesToCreate[i].class(d);
      }
      else {
        let args = [];
        for (let a of objectTypesToCreate[i].args)
          args.push(d[a]);
        new objectTypesToCreate[i].class(...args);
      }
    }
  }
}