Skip to content

Instantly share code, notes, and snippets.

@dasch
Created July 19, 2016 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dasch/97caed085c602fe517b72332c348e4a9 to your computer and use it in GitHub Desktop.
Save dasch/97caed085c602fe517b72332c348e4a9 to your computer and use it in GitHub Desktop.
module Foosball.Game exposing (..)
type Team = BlueTeam | RedTeam
type alias Player = String
type alias Score = Int
type Event
= PlayerAdded { team : Team, player : Player }
| PlayerRemoved { player : Player }
| ScoreAdded { team : Team, score : Score }
type alias Model =
{ redTeam : List Player
, blueTeam : List Player
, redScore : Maybe Int
, blueScore : Maybe Int
}
apply : Event -> Model -> Model
apply event model =
case event of
PlayerAdded { team, player } ->
case team of
RedTeam -> { model | redTeam = player :: model.redTeam }
BlueTeam -> { model | blueTeam = player :: model.blueTeam }
ScoreAdded { team, score } ->
case team of
RedTeam -> { model | redScore = Just score }
BlueTeam -> { model | blueScore = Just score }
addPlayer : Team -> Player -> Model -> Result Error Event
addPlayer team player game =
if game.playerCount == 4 then
err GameFull
else if List.include player game.players
err PlayerAlreadyInGame
else
ok (PlayerAdded team player)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment