Craig Fletcher 1 жил өмнө
commit
7232d54fd0
6 өөрчлөгдсөн 145 нэмэгдсэн , 0 устгасан
  1. 1 0
      .nvmrc
  2. 31 0
      constants.js
  3. 26 0
      factions.js
  4. 37 0
      index.js
  5. 13 0
      package.json
  6. 37 0
      utils.js

+ 1 - 0
.nvmrc

@@ -0,0 +1 @@
+20

+ 31 - 0
constants.js

@@ -0,0 +1,31 @@
+export const factions = [
+  "Sardakk N'orr",
+  "Arborec",
+  "Argent Flight",
+  "Barony of Letnev",
+  "Clan of Saar",
+  "Embers of Muaat",
+  "Emirates of Hacan",
+  "Empyrean",
+  "Federation of Sol",
+  "Ghosts of Creuss",
+  "L1Z1X Mindnet",
+  "Mahact Gene-Sorcerers",
+  "Mentak Coalition",
+  "Naalu Collective",
+  "Naaz-Rokha Alliance",
+  "Nekro Virus",
+  "Nomad",
+  "Titans of Ul",
+  "Universities of Jol-Nar",
+  "Vuil'Raith Cabal",
+  "Winnu",
+  "Xxcha Kingdom",
+  "Yin Brotherhood",
+  "Yssaril Tribes",
+]
+
+export const messages = {
+  "badBinnedFaction": "At least one of the factions in your removed list does not appear in the factions list",
+  "divider": "======================================"
+}

+ 26 - 0
factions.js

@@ -0,0 +1,26 @@
+export const factions = [
+  "Sardakk N'orr",
+  "Arborec",
+  "Argent Flight",
+  "Barony of Letnev",
+  "Clan of Saar",
+  "Embers of Muaat",
+  "Emirates of Hacan",
+  "Empyrean",
+  "Federation of Sol",
+  "Ghosts of Creuss",
+  "L1Z1X Mindnet",
+  "Mahact Gene-Sorcerers",
+  "Mentak Coalition",
+  "Naalu Collective",
+  "Naaz-Rokha Alliance",
+  "Nekro Virus",
+  "Nomad",
+  "Titans of Ul",
+  "Universities of Jol-Nar",
+  "Vuil'Raith Cabal",
+  "Winnu",
+  "Xxcha Kingdom",
+  "Yin Brotherhood",
+  "Yssaril Tribes",
+]

+ 37 - 0
index.js

@@ -0,0 +1,37 @@
+import { factions, messages } from "./constants.js"
+import {shuffle, splitArray, checkRemovedCount, annotateArray, getRandomItem } from "./utils.js"
+
+// Game options
+const players = ["Fletch", "Al", "Mike", "Ash", "Maddie"]
+const binnedFactions = ["Argent Flight"]
+
+// Remove any binned factions
+const factionsToUse = factions.filter(faction => !binnedFactions.includes(faction))
+
+// Just in case a binned faction was spelled wrong or something
+if (!checkRemovedCount(factions, factionsToUse, binnedFactions)) {
+  throw new Error(messages.badBinnedFaction)
+}
+
+// Shuffle/randomise the factions then split them into groups equal to the number of players
+const groupedFactions = splitArray(shuffle(factionsToUse), players.length)
+
+// Shuffle/randomise the order of players
+const orderedPlayers = shuffle(players)
+
+// Assign factions
+const selections = annotateArray(orderedPlayers, groupedFactions)
+
+// Pick speaker
+const speaker = getRandomItem(orderedPlayers)
+
+// Output all this to console
+orderedPlayers.forEach(player => {
+  const playerFactions = selections[player].join(", ");
+  console.log(`Player: ${player}`)
+  if (speaker === player) {
+    console.log("[SPEAKER]")
+  }
+  console.log(`Factions: ${playerFactions}`)
+  console.log(messages.divider)
+})

+ 13 - 0
package.json

@@ -0,0 +1,13 @@
+{
+  "name": "ti-setup",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "keywords": [],
+  "author": "",
+  "type": "module",
+  "license": "ISC"
+}

+ 37 - 0
utils.js

@@ -0,0 +1,37 @@
+// Even-probability shuffle
+// https://en.m.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
+export function shuffle(array) {
+  for (let i = array.length - 1; i > 0; i--) {
+    let j = Math.floor(Math.random() * (i + 1));
+    [array[i], array[j]] = [array[j], array[i]];
+  }
+  return array;
+}
+
+
+export function checkRemovedCount(oldArray, newArray, removedItems) {
+  const expectedLength = oldArray.length - removedItems.length
+  return newArray.length === expectedLength
+}
+
+export function splitArray(array, numPieces) {
+  return array.reduce((splitArrays, item, index) => {
+    const position = index % numPieces;
+    return [
+      ...splitArrays.slice(0, position),
+      [...(splitArrays[position] || []), item],
+      ...splitArrays.slice(position + 1)
+    ];
+  }, Array.from({ length: numPieces }, () => []));
+}
+
+export function annotateArray(labels, data) {
+  return labels.reduce((zipped, label, index) => {
+    return {...zipped, [label]: data[index]}
+  }, {})
+}
+
+export function getRandomItem(array) {
+  const index =  Math.floor(Math.random() * array.length)
+  return array[index]
+}