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.

Advanced Modding Functions

CYOA Utility Functions

Core

Debug

Ending Utility Functions

Other

Utility

Utility?

Advanced Modding Functions

  • Gets a flag in the user's local storage. Can be used for unlockables in mods, persisting data. Does not save between accounts. Only works if the scenario has already been loaded in (should always work) Safer than accessing localStorage yourself (handles edgecases, makes sure the flag name is unique to your scenario)

    Parameters

    • flag: string

    Returns boolean

  • Removes custom view with name viewName

    Parameters

    • viewName: string

    Returns void

  • Sets a flag in the user's local storage. Can be used for unlockables in mods, persisting data. Does not save between accounts. Only works if the scenario has already been loaded in (should always work) Safer than accessing localStorage yourself (handles edgecases, makes sure the flag name is unique to your scenario)

    Parameters

    • flag: string
    • value: boolean

    Returns void

CYOA Utility Functions

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

    Parameters

    • id: number
    • amount: number

    Returns void

  • 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

    • answerIds: number[]

      array of answer id numbers to check if any answered

    Returns boolean

    true if any answers have been answers

  • Parameters

    • question: QuestionModel

      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

  • Removes question from the list of questions.

    Parameters

    • questionId: number

      The id of the question to remove

    Returns void

  • Overrides both texts on the bottom banner

    Parameters

    • line1: string
    • line2: string

    Returns void

  • Override line 1 of the banner at the bottom

    Parameters

    • line1: string

    Returns void

  • Override line 2 of the banner at the bottom

    Parameters

    • line2: string

    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

  • Enables or disables a question by id

    Parameters

    • id: number
    • isEnabled: boolean

    Returns void

  • Parameters

    • scenarioName: string

    Returns void

  • Triggers a midgame VP switch

    Returns void

Core

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

    Parameters

    Returns void

  • Called when calculating the results of a game, returns an EndingModel to create slides out of

    Parameters

    • overrideEv: null | Map<number, number> = null

      for editor, when user changes electoral votes, changes the ending based on the ev

    • overridePv: null | Map<number, number> = null

      for editor, when user changes popular votes, changes ending based on pv

    Returns EndingModel

  • Loads a ScenarioModel into the engine

    Parameters

    • newScenario: ScenarioModel

      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

  • Returns number

    Total number of electoral votes for the scenario

  • Returns number

    Total number of popular votes for the scenario

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: FinalResultsModel) => EndingModel = 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

currentQuestionNumber: number = 1

Display number of current question, also how many questions the player has actually answered

currentScenario: null | ScenarioModel = null

Currently loaded ScenarioModel

customViews: Map<string, CustomViewCreator> = ...
difficulty: Difficulty = Difficulty.Normal
gameState: GameState = GameState.Uninitialized
initialScenarioName: null | string = null
isShuffled: boolean = false
localizations: Map<string, string> = ...
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: AnswerModel) => 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.

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

Called when the user presses "next"

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.

runningMateWeight: number = 0.5

How much a running mate contributes to opinion (1 is the same as a normal candidate)

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

Index of current side

tooltips: Map<string, string> = ...
useRng: boolean = true
visitBonus: number = 0.02

How much visiting a state boosts that state's opinion of you

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

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

waitingToSelectNewRunningMate: boolean = false
  • Adds tooltips to any string

    Parameters

    • s: string
    • positionRelative: boolean = true

    Returns string

  • Enables or disables an answer by id

    Parameters

    • id: number
    • isEnabled: boolean

    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

  • Get a string you've defined previously to be rendered as another string Returns the same string if a localization is not set yet

    Parameters

    • key: string

    Returns string

    The localization

  • Returns number

    The number of ENABLED 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

  • Increments question index until an enabled question is found

    Returns void

  • Returns boolean

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

  • Increments the current question index at least once, and keeps going if the current question is disabled

    Returns void

  • Set a string you want to be rendered in game as another string. May not work for all strings in game, but will be updated until it does. You probably want to call all these in onScenarioStarted or the optional "onCandidateSelectionStarted" function.

    Parameters

    • oldValue: string
    • newValue: string

    Returns void

Utility?