mwg API
    Preparing search index...

    Function campaignChain

    • Points a [campaign] at simulation.Campaign: the order comes out of the content, the playing stays the game's.

      first_scenario opens the campaign, and the first declared scenario opens it when the attribute is not set. Each scenario's next_scenario is where it goes when it is won, and a scenario that ends the chain leaves it: the campaign finishes. A scenario that decides for itself keeps its decision, because a result carrying next is passed through untouched - which is exactly what a runtime [endlevel] does when it overrides the authored chain.

      Malformed chains throw by name rather than being guessed at: no scenarios, the same id twice, or a first_scenario naming something the campaign does not have.

      Type Parameters

      Parameters

      Returns MwlCampaignChain<State, Result>

      import { campaignChain } from '@datamoc/mw_games/mwl';
      import { Campaign } from '@datamoc/mw_games/simulation';

      const definition = {
      id: 'prologue',
      firstScenario: 'opening',
      scenarios: [{ id: 'opening', nextScenario: 'siege' }, { id: 'siege' }],
      };

      const chain = campaignChain(definition, {
      run: (scenario, state) => ({ outcome: 'completed', state, result: scenario.id }),
      });

      const campaign = new Campaign({ levels: chain.levels, start: chain.start, state: { gold: 100 } });
      campaign.playCurrent();
      console.log(campaign.currentLevel); // 'siege'
      campaign.playCurrent();
      console.log(campaign.currentLevel); // null - the chain is done