boardgame-0.0.0.1: Modeling boardgames

Safe HaskellNone
LanguageHaskell2010

Boardgame.Web

Description

This module is useful if you wish to create web UI through the use of WebAssembly and regular web technologies.

Our complementary boardgame.js JavaScript library can be useful as it contains the necessary JavaScript functions and some extra helper functions.

Usage example

Simple

Imagine a PositionalGame called TicTacToe, with a function newTicTacToe that instantiates it. If you wish to build a web UI for the game, you can use the default way of exposing the Haskell model to the JavaScript runtime:

main = defaultWebGame newTicTacToe

After that, the game can be started form the JavaScript runtime with the function window.boardgame.games.default().

Multiple games

If you also have another game, say Hex with a function newHex. And you want to play both from the same UI you can instead use the following method.

main = do
  addWebGame "TicTacToe" newTicTacToe
  addWebGame "Hex" newHex
  webReady

With this, the JavaScript runtime can access both games through the window.boardgame.games object. window.boardgame.games.TicTacToe() and window.boardgame.games.Hex() respectively.

The webReady call is used to invoke the window.boardgame.initialized event.

Remember

Compile with Asterius and the wasm flag active.

ahc-cabal new-build --flags="wasm"
Synopsis

Documentation

playWeb :: (ToJSON a, ToJSON c, FromJSON c, PositionalGame a c) => a -> IO () Source #

Plays a PositionalGame with the help of JavaScript FFI. The state of the game (a) needs to implement ToJSON and the coordinates (c) needs to implement FromJSON. This is because they need to be passed to and from (respectively) the JavaScript runtime.

defaultWebGame :: (ToJSON a, ToJSON c, FromJSON c, PositionalGame a c) => a -> IO () Source #

A main function for running games as a web app. Initializes the provided game as "default" and "tells" JavaScript it's ready. Then JavaScript can start games whenever.

addWebGame :: (ToJSON a, ToJSON c, FromJSON c, PositionalGame a c) => String -> a -> IO () Source #

Adds a named game to the list of games accessible from JavaScript.

webReady :: IO () Source #

Lets JavaScript know that the Haskell backend is up and running by firing the ready event.