2018-05-25 10:40:35 -07:00
|
|
|
import Go.Types exposing (..)
|
|
|
|
import Go.Game exposing (verify)
|
|
|
|
import Go.Decoders exposing (decodeUpdatestring)
|
|
|
|
import Go.Ws exposing (..)
|
2018-05-25 12:06:15 -07:00
|
|
|
import Go.View exposing (..)
|
2018-05-25 10:40:35 -07:00
|
|
|
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
|
2018-05-25 12:06:15 -07:00
|
|
|
view m = div []
|
|
|
|
[ text (toString m.currentColor)
|
|
|
|
, renderBoard m.sessionSize m.board
|
|
|
|
]
|
2018-05-25 10:40:35 -07:00
|
|
|
|
|
|
|
update : Msg -> Model -> (Model, Cmd Msg)
|
|
|
|
update msg model = case msg of
|
2018-05-25 12:06:15 -07:00
|
|
|
Place indx -> case verify (indx, model.sessionColor) model of
|
2018-05-25 10:40:35 -07:00
|
|
|
Nothing -> ( { model | error = Just "Can't place piece" }, Cmd.none)
|
2018-05-25 10:51:38 -07:00
|
|
|
Just c -> ( { model | board = c::model.board }, sendMove model c)
|
2018-05-25 10:40:35 -07:00
|
|
|
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
|
|
|
|
|
2018-05-25 12:25:14 -07:00
|
|
|
main = Html.programWithFlags { init = init, update = update, subscriptions = subscriptions, view = view }
|