GoUI/Go.elm

50 lines
1.4 KiB
Elm

import Go.Types exposing (..)
import Go.Game exposing (verify)
import Go.Decoders exposing (decodeUpdateString)
import Go.Ws exposing (..)
import Go.View exposing (..)
import WebSocket
import Html exposing (Html, div, text)
init : Flags -> (Model, Cmd Msg)
init flags = (Model
(if flags.black then Black else White)
flags.url
flags.id
flags.size
Nothing
Nothing
[], Cmd.none)
initDummy : (Model, Cmd Msg)
initDummy = (Model
Black
"ws://localhost:3000"
"debug"
9
Nothing
Nothing
[], Cmd.none)
view : Model -> Html Msg
view m = div []
[ text (toString m.board)
, renderBoard m.sessionSize m.board
]
update : Msg -> Model -> (Model, Cmd Msg)
update msg model = case msg of
Place indx -> case verify (indx, model.sessionColor) model of
Nothing -> ( { model | error = Just "Can't place piece" }, Cmd.none)
Just c -> ( { model | board = c::model.board }, sendMove model c)
Update s -> case decodeUpdateString s of
Ok (c, xs) -> ( { model | board = xs, currentColor = Just c }, Cmd.none)
Err s -> ( { model | error = Just "Can't parse server response" }, Cmd.none)
DismissError -> ({ model | error = Nothing }, Cmd.none)
subscriptions : Model -> Sub Msg
subscriptions m =
WebSocket.listen (wsUrl m) Update
main = Html.programWithFlags { init = init, update = update, subscriptions = subscriptions, view = view }