What is a Book?
A book is the JSON payload returned by the RGS for a single bet request. It is randomly selected from millions of pre-computed outcomes produced by the math SDK. Its most important field isevents: an ordered array of bookEvents that determines everything that happens during the round.
The order of bookEvents matters. The SDK plays them one after another using
sequence(), so placing winInfo before reveal would show win highlights before the reels spin. The math SDK controls this ordering.What is a BookEvent?
A bookEvent is one element ofbook.events. Every bookEvent has a type string field and whatever additional data that event type needs.
BookEventOfType<T>
BookEventOfType<T> is a TypeScript utility type that narrows BookEvent to the specific type matching the given type string. Use it to get full type-safety inside a handler:
bookEventHandlerMap
bookEventHandlerMap is an object where each key is a bookEvent.type string and each value is an async handler function. When playBookEvents() encounters a bookEvent, it looks up the handler by bookEvent.type and awaits it.
How playBookEvents() Works
playBookEvents() is created by packages/utils-book/src/createPlayBookUtils.ts. It iterates the book.events array and processes each bookEvent in strict sequence using sequence() — an async helper that resolves promises one at a time (unlike Promise.all(), which would fire everything at once).
playBookEvent() resolves a single bookEvent by looking up its handler in bookEventHandlerMap and awaiting it. It is also used directly in storybook stories (MODE_BASE/bookEvent/reveal) to test individual bookEvents in isolation.
Stateless vs Stateful Games
Stateless games
Stateless games
A single RGS request covers the complete round. Slots are the canonical example: one
requestBet call returns a book, playBookEvents() plays it, and the round is done.Stateless games can still be complex — they can have many bookEvent types, multiple game modes, multipliers, and bonus triggers — but everything needed to play the full round is contained in a single book.Stateful games
Stateful games
Multiple RGS requests are needed to complete a round. A mines game is the canonical example: the player reveals tiles one at a time, each reveal is a separate RGS request, and the round is only finished when the player cashes out or hits a mine.For stateful games the
resumeBet state in the XState machine is essential — it allows the game to re-enter an in-progress round after a page reload. See Game State Machine for details.Adding a New BookEvent
The recommended workflow for adding a new bookEvent type (e.g.updateGlobalMult) is:
- Add sample data to
apps/lines/src/stories/data/bonus_books.tsandbonus_events.ts - Add the story to
ModeBonusBookEvent.stories.svelte - Define the type in
typesBookEvent.tsand extend theBookEventunion - Add the handler to
bookEventHandlerMap.ts - Create or update the Svelte component that subscribes to the emitterEvents the handler broadcasts
- Test individually via
MODE_BONUS/bookEvent/updateGlobalMultin Storybook - Test in books via
MODE_BONUS/book/randomin Storybook
