소스 검색

Add even split faction picks and void player

* Factions are now evenly-distributed, with the remainder discarded.
* Void players can now be added for odd player numbers
Craig Fletcher 1 년 전
부모
커밋
be6f78841c
3개의 변경된 파일48개의 추가작업 그리고 13개의 파일을 삭제
  1. 6 1
      constants.js
  2. 27 12
      index.js
  3. 15 0
      utils.js

+ 6 - 1
constants.js

@@ -27,5 +27,10 @@ export const factions = [
 
 export const messages = {
   "badBinnedFaction": "At least one of the factions in your removed list does not appear in the factions list",
-  "divider": "======================================"
+  "divider": "======================================",
+  "speaker": "[SPEAKER]",
+  "voidPlayer": "[VOID]",
+  "blank": ""
 }
+
+export const voidPlayer = "Nick Locarno"

+ 27 - 12
index.js

@@ -1,9 +1,10 @@
-import { factions, messages } from "./constants.js"
-import {shuffle, splitArray, checkRemovedCount, annotateArray, getRandomItem } from "./utils.js"
+import { factions, messages, voidPlayer } from "./constants.js"
+import {shuffle, splitArray, checkRemovedCount, annotateArray, getRandomItem, removeFromArray, insertEntriesRandomlyIntoArray } from "./utils.js"
 
 // Game options
-const players = ["Fletch", "Al", "Mike", "Ash", "Maddie"]
-const binnedFactions = ["Argent Flight"]
+const players = ["Fletch", "Al", "Mouse", "Ash", "Maddie"]
+const binnedFactions = ["Clan of Saar", "Xxcha Kingdom"]
+const voidPlayerCount = 1;
 
 // Remove any binned factions
 const factionsToUse = factions.filter(faction => !binnedFactions.includes(faction))
@@ -13,8 +14,19 @@ 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)
+// Ensure all players get an equal number of choices by calculating the number
+// of factions that should be removed for an even split
+const numFactionsToBin = factionsToUse.length % players.length
+
+// Shuffle/randomise the factions, use the number from above to make it an even
+// split, then split them into groups equal to the number of players
+const groupedFactions = splitArray(
+  removeFromArray(
+    shuffle(factionsToUse),
+    numFactionsToBin
+  ),
+  players.length
+)
 
 // Shuffle/randomise the order of players
 const orderedPlayers = shuffle(players)
@@ -25,13 +37,16 @@ const selections = annotateArray(orderedPlayers, groupedFactions)
 // Pick speaker
 const speaker = getRandomItem(orderedPlayers)
 
+// Insert Void player(s) when required (to keep an even map)
+const playerLayout = insertEntriesRandomlyIntoArray(orderedPlayers, voidPlayerCount, voidPlayer)
 // Output all this to console
-orderedPlayers.forEach(player => {
-  const playerFactions = selections[player].join(", ");
-  console.log(`Player: ${player}`)
-  if (speaker === player) {
-    console.log("[SPEAKER]")
-  }
+playerLayout.forEach((player, index) => {
+  const isSpeaker = player === speaker ? messages.speaker : messages.blank
+  const isVoid = player === voidPlayer ? messages.voidPlayer : messages.blank
+  const playerNumber = index + 1
+  const playerFactions = (selections[player] || []).join(", ");
+
+  console.log(`Player ${playerNumber}: ${player} ${[isSpeaker, isVoid].join(" ")}`)
   console.log(`Factions: ${playerFactions}`)
   console.log(messages.divider)
 })

+ 15 - 0
utils.js

@@ -35,3 +35,18 @@ export function getRandomItem(array) {
   const index =  Math.floor(Math.random() * array.length)
   return array[index]
 }
+
+export function removeFromArray(array, numToRemove) {
+  return numToRemove ? array.slice(0, -numToRemove) : array
+}
+
+export function insertEntriesRandomlyIntoArray(array, count, entry = {}) {
+  return Array.from({ length: count }).reduce((acc, _) => {
+
+    // Generate a random index to insert the value
+    const randomIndex = Math.floor(Math.random() * (array.length + 1));
+
+    // Insert the value into the array at the random index
+    return [...acc.slice(0, randomIndex), entry, ...acc.slice(randomIndex)];
+  }, array)
+}