Implement basic communications and messages.

This commit is contained in:
2018-05-25 10:40:35 -07:00
commit b52b16e5df
6 changed files with 177 additions and 0 deletions

45
Go.elm Normal file
View File

@@ -0,0 +1,45 @@
import Go.Types exposing (..)
import Go.Game exposing (verify)
import Go.Decoders exposing (decodeUpdatestring)
import Go.Ws 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"
1
9
Nothing
Nothing
[], Cmd.none)
view : Model -> Html Msg
view m = div [] [ text (toString m.currentColor) ]
update : Msg -> Model -> (Model, Cmd Msg)
update msg model = case msg of
Place c -> case verify c model of
Nothing -> ( { model | error = Just "Can't place piece" }, Cmd.none)
Just c -> ( { model | board = c::model.board }, Cmd.none)
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 m.sessionUrl Update
main = Html.program { init = initDummy, update = update, subscriptions = subscriptions, view = view }