index.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { factions, messages, voidPlayer } from "./constants.js"
  2. import {shuffle, splitArray, checkRemovedCount, annotateArray, getRandomItem, removeFromArray, insertEntriesRandomlyIntoArray } from "./utils.js"
  3. // Game options
  4. const players = ["Fletch", "Al", "Mouse", "Ash", "Maddie"]
  5. const binnedFactions = ["Clan of Saar", "Xxcha Kingdom"]
  6. const voidPlayerCount = 1;
  7. // Remove any binned factions
  8. const factionsToUse = factions.filter(faction => !binnedFactions.includes(faction))
  9. // Just in case a binned faction was spelled wrong or something
  10. if (!checkRemovedCount(factions, factionsToUse, binnedFactions)) {
  11. throw new Error(messages.badBinnedFaction)
  12. }
  13. // Ensure all players get an equal number of choices by calculating the number
  14. // of factions that should be removed for an even split
  15. const numFactionsToBin = factionsToUse.length % players.length
  16. // Shuffle/randomise the factions, use the number from above to make it an even
  17. // split, then split them into groups equal to the number of players
  18. const groupedFactions = splitArray(
  19. removeFromArray(
  20. shuffle(factionsToUse),
  21. numFactionsToBin
  22. ),
  23. players.length
  24. )
  25. // Shuffle/randomise the order of players
  26. const orderedPlayers = shuffle(players)
  27. // Assign factions
  28. const selections = annotateArray(orderedPlayers, groupedFactions)
  29. // Pick speaker
  30. const speaker = getRandomItem(orderedPlayers)
  31. // Insert Void player(s) when required (to keep an even map)
  32. const playerLayout = insertEntriesRandomlyIntoArray(orderedPlayers, voidPlayerCount, voidPlayer)
  33. // Output all this to console
  34. playerLayout.forEach((player, index) => {
  35. const isSpeaker = player === speaker ? messages.speaker : messages.blank
  36. const isVoid = player === voidPlayer ? messages.voidPlayer : messages.blank
  37. const playerNumber = index + 1
  38. const playerFactions = (selections[player] || []).join(", ");
  39. console.log(`Player ${playerNumber}: ${player} ${[isSpeaker, isVoid].join(" ")}`)
  40. console.log(`Factions: ${playerFactions}`)
  41. console.log(messages.divider)
  42. })