The main engine that runs the logic for the game. Should be entirely separate from the View and should have no knowledge of how the player is using it.

CYOA Utility Functions

  • Adds amount to counter with key 'key'. Does nothing and logs an error if this counter hasn't been defined using setCounter yet.

    Parameters

    • key: string
    • amount: number

    Returns void

  • Parameters

    • key: string

    Returns number

    Value in counters with key 'key' or -1 if that key does not exist

  • Parameters

    • answerIds: number[]

      array of answer id numbers to check if answered

    Returns boolean

    true if all answers have been answers

  • Parameters

    • question: default

      The question to insert

    • index: number

      The index before the question you want to insert. For example if you are on index 3 and you want to insert a question at index 6, put index 5

    Returns void

  • Inserts question as the next question the player will see

    Parameters

    • question: default

      The question to insert

    Returns void

  • Removes question from the list of questions.

    Parameters

    • questionId: number

      The id of the question to remove

    Returns void

  • Sets counter value with key 'key' to 'amount'

    Parameters

    • key: string
    • amount: number

    Returns void

  • Parameters

    • url: string

    Returns void

  • Parameters

    • url: string

    Returns void

  • Sets new image for player candidate from url

    Parameters

    • url: string

    Returns void

  • Sets new name for player candidate

    Parameters

    • firstName: string
    • lastName: string

    Returns void

  • Sets new image for running mate (shown on banner)

    Parameters

    • url: string

    Returns void

  • Sets new name for running mate (shown on banner)

    Parameters

    • firstName: string
    • lastName: string

    Returns void

  • Parameters

    • scenarioName: string

    Returns void

Core

  • Applies all the answer effects of an AnswerModel and then updates each state in the scenario

    Parameters

    • selectedAnswer: null | default

      The answer to apply

    Returns void

  • Loads a ScenarioModel into the engine

    Parameters

    • newScenario: default

      The ScenarioModel to load

    • asObserver: boolean

      If this is true, then the margins are also loaded and the GameState is set to Election. Used for when you want to view the map without actually playing the game

    Returns void

  • Finalizes loading the scenario after loadScenario is called. Sets which candidate is the player and who their running mate is.

    Parameters

    • newSideIndex: number

      Used to lookup a side in the ScenarioModel's scenarioSides. Loads that sides questions into the game.

    • runningMateId: number

      The id of the running mate of the player.

    • isShuffled: boolean

    Returns void

  • Calls the onAchievementUnlocked method with the achievementName and the current ScenarioController. The Engine does not handle the logic of if an achivement is already unlocked, or storing achievements. That is left to the host site.

    Parameters

    • achievementName: string

    Returns void

  • Updates the polling in each state. Used for example after the player chooses a question and those margins need to be updated.

    Returns void

Debug

  • Parameters

    • stateId: number

    Returns Map<number, number>

  • Returns string

    Returns a string of the opinions of each state for each candidate

  • Parameters

    • stateId: number

    Returns string

Ending Utility Functions

  • Returns true if the candidate with candidateId is polling highest in the state with id stateId. Can also be used midgame.

    Parameters

    • candidateId: number
    • stateId: number

    Returns boolean

  • Parameters

    • results: default

      The results of the election

    Returns number

    The player candidate's number of electoral votes, if player candidate is undefined then returns 0

  • Parameters

    • results: default

      The results of the election

    Returns number

    The player candidate's number of popular votes, if player candidate is undefined then returns 0

  • Parameters

    • results: default

      The results of the election

    Returns number

    The ratio of PV the player got (range [0, 1])

  • Returns number

    Total number of electoral votes for the scenario

  • Returns number

    Total number of popular votes for the scenario

  • Parameters

    • results: default

      The results of the election

    • amount: number

    Returns boolean

    Was the player's # of electoral votes >= amount?

  • Parameters

    • results: default

      The results of the election

    Returns boolean

    Did the player have the most PV (note not > 50%, just the most)

  • Set a new music playlist. Good to use for song switches for endings.

    Parameters

    Returns void

Other

answers: number[] = []
counterDisplayNames: Map<string, string> = ...

Holds the display names for counters, if a display name is not defined it defaults to the counter's key + value

counters: Map<string, number> = ...

Any variable in counters will be shown on screen. Only supports number variables. Use setCounter, setCounter methods

createEnding: null | (engine: Engine, results: default) => default = null

Called when the game ends and ending slides/results are calculated. Is set from the method of the same name in a scenario's logic.js

currentQuestionIndex: number = 0

Index of current question

currentScenario: null | default = null

Currently loaded ScenarioModel

gameState: GameState = GameState.Uninitialized
isShuffled: boolean = false
logToConsole: boolean = true
onAchievementUnlocked: null | (engine: Engine, achievementName: string) => void = null

To be hooked into from a host site to know when an achievement is unlocked.

onAnswerPicked: null | (engine: Engine, answerPicked: default) => void = null

Called when the an answer is chosen. Is set from the method of the same name in a scenario's logic.js. Used for CYOA purposes.

onScenarioStarted: null | (engine: Engine) => void = null

Called when the scenario side is first set (played has chose their running mate and pressed start game)

random: () => number = ...
randomState: Seed = ...
rng: number = 0.025

Range of RNG.

runningMateId: number = -1

Id of the player's running mate. -1 if unset.

scenarioController: default = ...
seed: string = ...
sideIndex: number = 0

Index of current side

useRng: boolean = true
visits: number[] = []
waitingToPickState: boolean = false

Is the player currently waiting to pick a state before moving on to the next question?

  • Adds amount to candidate global multiplier with id 'id'

    Parameters

    • id: number
    • amount: number

    Returns void

  • Sets the display name for a counter (a counter's display name defaults to its key) Useful if you want to provide special context to a counter

    Parameters

    • key: string
    • displayName: string

    Returns void

Utility

  • Returns null | default

    Returns the QuestionModel of the current question, if the currentQuestionIndex is invalid, returns null

  • Returns number

    The number of questions in this scenario, includes questions added with CYOA

  • Returns number

    Returns range for RNG. So if value returned in 0.01, then you would += (0.01 / 2) to state polls

  • Returns Set<number>

    Returns a Set of ids for candidates who have valid sides in the current ScenarioModel

  • Parameters

    • abbr: string

      The abbreviation for the state as defined in data.json

    Returns number

    The id of the state as defined in data.json

  • Returns boolean

    Returns true if the currentQuestionIndex >= number of questions in the scenario

  • Increments currentQuestionIndex by one

    Returns void