billstclair / elm-websocket-framework / WebSocketFramework.ServerInterface

Functions that connect the client code to the server.

Server Constructors

makeServer : (Json.Encode.Value -> Platform.Cmd.Cmd msg) -> WebSocketFramework.Types.MessageEncoder message -> WebSocketFramework.Types.ServerUrl -> msg -> WebSocketFramework.Types.ServerInterface gamestate player message msg

Make a client connection to a real WebSocket server.

The msg will usually be a no-operation message. It is only used to fill a slot in the returned ServerInterface. That slot is only used by the proxy server.

makeProxyServer : WebSocketFramework.Types.ServerMessageProcessor gamestate player message -> (WebSocketFramework.Types.ServerInterface gamestate player message msg -> message -> msg) -> WebSocketFramework.Types.ServerInterface gamestate player message msg

Make a client connection to a proxy server.

No WebSocket connection will be used to send messages.

fullMessageProcessor : WebSocketFramework.Types.EncodeDecode message -> WebSocketFramework.Types.ServerMessageProcessor gamestate player message -> WebSocketFramework.Types.ServerState gamestate player -> message -> ( WebSocketFramework.Types.ServerState gamestate player, Maybe message )

Simulate a round-trip through the message encoder, decoder, and message processor.

Returns a function that takes a request message, encodes it, decodes it, processes it into a response, encodes and decodes that, then returns the result.

Usually passed as the first arg to makeProxyServer.

Sending a Message

send : WebSocketFramework.Types.ServerInterface gamestate player message msg -> message -> Platform.Cmd.Cmd msg

Return a Cmd to send a message through a server interface.

State Access

getServer : WebSocketFramework.Types.ServerInterface gamestate player message msg -> WebSocketFramework.Types.ServerUrl

Return the server URL from inside a ServerInterface.

GameId and PlayerId validity checking

checkOnlyGameid : WebSocketFramework.Types.ServerState gamestate player -> message -> WebSocketFramework.Types.GameId -> Result (WebSocketFramework.Types.ErrorRsp message) gamestate

Check that the passed GameId is in the ServerState's game dict.

If it is, return the gamestate. Otherwise wrap the message in an ErrorRsp.

checkGameid : WebSocketFramework.Types.ModeChecker gamestate message -> WebSocketFramework.Types.ServerState gamestate player -> message -> WebSocketFramework.Types.GameId -> Result (WebSocketFramework.Types.ErrorRsp message) gamestate

Check that the passed GameId is in the ServerState's game dict and that it satisifed the ModeChecker.

If it does, return the gamestate. Otherwise wrap the message in an ErrorRsp.

checkPlayerid : WebSocketFramework.Types.ServerState gamestate player -> message -> WebSocketFramework.Types.PlayerId -> Result (WebSocketFramework.Types.ErrorRsp message) (WebSocketFramework.Types.PlayerInfo player)

Check that the passed PlayerId is in the ServerState's player dict.

If it is, return the PlayerInfo record for the player.

Otherwise wrap the message in an ErrorRsp.

dummyGameid : WebSocketFramework.Types.GameId

"<gameid>"

Game and Player addition and removal

addGame : WebSocketFramework.Types.GameId -> gamestate -> WebSocketFramework.Types.ServerState gamestate player -> WebSocketFramework.Types.ServerState gamestate player

Add a game to a ServerState.

Adds the game ID to the added games list in ServerState.changes, so that the server code will update its tables.

addPlayer : WebSocketFramework.Types.PlayerId -> WebSocketFramework.Types.PlayerInfo player -> WebSocketFramework.Types.ServerState gamestate player -> WebSocketFramework.Types.ServerState gamestate player

Add a player to a ServerState.

Adds the player ID to the added players list in ServerState.changes, so that the server code will update its tables.

removeGame : WebSocketFramework.Types.GameId -> WebSocketFramework.Types.ServerState gamestate player -> WebSocketFramework.Types.ServerState gamestate player

Remove a game and its players from a ServerState.

Adds the game ID to the removed games list in ServerState.changes, so that the server code will update its tables.

removePlayer : WebSocketFramework.Types.PlayerId -> WebSocketFramework.Types.ServerState gamestate player -> WebSocketFramework.Types.ServerState gamestate player

Remove a player from a ServerState.

Adds the player ID to the removed players list in ServerState.changes, so that the server code will update its tables.

Public Games

appendPublicGames : WebSocketFramework.Types.PublicGame -> WebSocketFramework.Types.PublicGames -> WebSocketFramework.Types.PublicGames

Push a PublicGame onto a list of them.

removePublicGame : WebSocketFramework.Types.GameId -> WebSocketFramework.Types.PublicGames -> WebSocketFramework.Types.PublicGames

Remove the PublicGame with the given GameId from a list of games.

Create random game and player identifiers.

newGameid : WebSocketFramework.Types.ServerState gamestate player -> ( WebSocketFramework.Types.GameId, WebSocketFramework.Types.ServerState gamestate player )

Generate a random GameId string, ensuring that it is not already assigned to a game.

Will not be used by servers that have no concept of a game.

newPlayerid : WebSocketFramework.Types.ServerState gamestate player -> ( WebSocketFramework.Types.PlayerId, WebSocketFramework.Types.ServerState gamestate player )

Generate a random PlayerId string, ensuring that it is not already assigned to a player.

Will not be used by servers that have no concept of a player.

Statistics

getStatisticsProperty : String -> WebSocketFramework.Types.ServerState gamestate player -> Maybe Basics.Int

Return the value of a statistics property, or Nothing if statistics are not being tracked or the property is unset.

setStatisticsProperty : String -> Maybe Basics.Int -> WebSocketFramework.Types.ServerState gamestate player -> WebSocketFramework.Types.ServerState gamestate player

Set the value of a statistics property, or do nothing if statistics are not being tracked.

updateStatisticsProperty : String -> (Maybe Basics.Int -> Maybe Basics.Int) -> WebSocketFramework.Types.ServerState gamestate player -> WebSocketFramework.Types.ServerState gamestate player

Update a statistics property.

If not tracking statistics, the updater will not be called. Otherwise, it will receive Nothing if the property is not already set. If it returns Nothing, the property will be removed.

maybeUpdateStatisticsProperty : String -> (Basics.Int -> Basics.Int) -> WebSocketFramework.Types.ServerState gamestate player -> WebSocketFramework.Types.ServerState gamestate player

If tracking statistics and the property is set, update it.

Utilities

getGame : WebSocketFramework.Types.GameId -> WebSocketFramework.Types.ServerState gamestate player -> Maybe gamestate

Look up the gamestate for a GameId

updateGame : WebSocketFramework.Types.GameId -> gamestate -> WebSocketFramework.Types.ServerState gamestate player -> WebSocketFramework.Types.ServerState gamestate player

Update the gamestate for a GameId.

Use removeGame to delete a game.

gameCount : WebSocketFramework.Types.ServerState gamestate player -> Basics.Int

Return the total number of games.

getPlayer : WebSocketFramework.Types.PlayerId -> WebSocketFramework.Types.ServerState gamestate player -> Maybe (WebSocketFramework.Types.PlayerInfo player)

Look up the PlayerInfo for a PlayerId

updatePlayer : WebSocketFramework.Types.PlayerId -> WebSocketFramework.Types.PlayerInfo player -> WebSocketFramework.Types.ServerState gamestate player -> WebSocketFramework.Types.ServerState gamestate player

Update the PlayerInfo for a PlayerId.

Use removePlayer to delete a player.

getGamePlayers : WebSocketFramework.Types.GameId -> WebSocketFramework.Types.ServerState gamestate player -> List WebSocketFramework.Types.PlayerId

Get the player IDs for a game ID.

isPublicGame : WebSocketFramework.Types.GameId -> WebSocketFramework.Types.ServerState gamestate player -> Basics.Bool

Return true if the GameId is in the public games list.

isPrivateGame : WebSocketFramework.Types.GameId -> WebSocketFramework.Types.ServerState gamestate player -> Basics.Bool

Return true if the GameId is NOT in the public games list.

Errors

errorRsp : message -> String -> WebSocketFramework.Types.ErrorRsp message

Create the ErrorRsp record returned in the errors from CheckOnlyGameid, checkGameid, and checkPlayerid.