@pretty-chitty/core
    Preparing search index...

    Class Turn<T, P, R>

    A Turn is the heart of pretty-chitty. It represents multiple actions or sequences of events from a single player (or possibly not player).

    The core of a Turn is the async fn provided. This fn will run all of this logic for that turn, prompt the user for actions, take random number draws and possibly create one or more sub-turns.

    The general pattern will look something like this:

    setup.createTurn([rootChit, playerChit], playerChit, async (turn) => {

    // take a random number
    const randomDraw = await turn.rng();

    // ask the turn's player to make a decision.
    await turn.pick([Chit.pick([chit1,chit2], async chosenChit => {
    // do something with chosen chit
    })]);
    });

    There are a couple of VERY important details here:

    1. Your turn implemention should persist no state itself.
    2. Your turn is going to "lock" one or more chits. Those chits will only be editable by this turn (until the turn is done or a sub-turn briefly locks those same chits)
    3. If a user needs to "undo" something, it will automatically reset all of the locked chits to their original locked state and re-evaluate the turn from its beginning to easily handle a single "step back".
    4. As a result, all random numbers need to go through this turn's rng() so they are persisted correctly.
    5. All state should be either ephemeral or stored on the chits themselves. Remember that your turn's function may re-evaluate at any point!
    6. It will automatically handle prompting a user for confirmation before drawing a random number OR before changing the active player.

    Type Parameters

    Index

    Accessors

    • get rootChit(): R

      The root chit instance of the game. All chits in the game have this chit somewhere in its hierarchy

      Returns R

    Constructors

    Methods

    • Generates a random number from 0 to 1. This number will persisted in state and will be consistent if the turn needs to be re-run. Drawing a random number prevents normal resets.

      If the player can perform a reset, this will confirm with them first.

      Parameters

      • message: string = "confirm draw or roll"

      Returns Promise<number>

      A number from 0-1

    • Generates a series of random numbers from 0 to 1. This is a helper method to help batch up a lot of random draws since each rng() call must be awaited.

      Parameters

      • count: number
      • message: string = "confirm draw or roll"

      Returns Promise<() => number>

      A parameterless function that will return the next random number in the list. If you try to select too many random numbers, that method will throw.

    • Add a game log message to the last flush step

      Parameters

      • message: string

      Returns void

    • Append a game log message to the last flush step that has a game log message

      Parameters

      • message: string | ((previous: string) => string)

      Returns void

    • Scan all of the chits managed by this turn. If any of them have changed, group them together into a "ClockStep". If any new chits appear, then add them to our lookup. If any chits are deleted (orphaned), then we have to identify those as well.

      Parameters

      • force: boolean = false

      Returns void

    • Creates a sub-turn. This is useful for a few reasons:

      1. It is the only way to change game control flow from our current player to a different player
      2. It creates a new "reset" point for backing out turns

      It takes an async function which it will execute. For simplicity, the result of that function will be the result of this function.

      It is okay to have multiple turns going at once, but there are some rules:

      1. If there is an ongoing sub turn that hasn't resolved, you cannot prompt or change chits in this turn.
      2. There is only one ongoing subturn allowed per player.

      Type Parameters

      • A

      Parameters

      • chits: Chit[]

        The chits to lock. These are the only chits that will be allowed to be modified by this turn. If there are never any concurrent turns, this can safely be the root chit (although there maybe minor performance implications)

      • player: P

        The player who will be prompted for choices during this turn.

      • cb: (turn: Turn<A, P, R>) => Promise<A>

        The async function that will be the logic of this turn. The function takes a new turn instance

      Returns Promise<A>

      Whatever the final result of cb() is

    • Type Parameters

      • A

      Parameters

      • players: P[]
      • chits: (p: P) => Chit[]
      • action: (p: P, turn: Turn<A, P, R>) => Promise<A>

      Returns Promise<A[]>

    • Basic selection prompt. All chits will appear as "selected" on the respective client. Upon clicking one, the prompt will resolve itself.

      Parameters

      • chits: Chit[]

        The list of chits that can be selected from

      Returns Promise<Chit>

      The chit that the player selected

    • If a player has gotten themselves into a corner - i.e. no valid moves - this prompt will simply inform them of that and allow them to undo.

      Parameters

      • Optionalmessage: string
      • Optionalhelp: string

      Returns Promise<void>

    • More complex selection prompt. Useful in Typescript when you want to handle a response to multiple types of chits. This will allow a different typed callback per set of chits. This also allows the insertion of "buttons"

      Parameters

      • Optionalmessage: string | Picks

        (optional) The message to show describing the prompt in a few words

      • Optionalhelp: string | Picks

        (optional) A detailed help message to show

      • Optionalpicks: Picks

        An array of "picks" - each of which can have its own typesafe callback

      Returns Promise<void>

    Properties

    id: string
    player?: P