Compare commits

...

2 Commits

3 changed files with 10 additions and 7 deletions

8
Go.elm
View File

@@ -1,6 +1,6 @@
import Go.Types exposing (..)
import Go.Game exposing (verify)
import Go.Decoders exposing (decodeUpdatestring)
import Go.Decoders exposing (decodeUpdateString)
import Go.Ws exposing (..)
import Go.View exposing (..)
import WebSocket
@@ -28,7 +28,7 @@ initDummy = (Model
view : Model -> Html Msg
view m = div []
[ text (toString m.currentColor)
[ text (toString m.board)
, renderBoard m.sessionSize m.board
]
@@ -37,13 +37,13 @@ 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
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
WebSocket.listen (wsUrl m) Update
main = Html.programWithFlags { init = init, update = update, subscriptions = subscriptions, view = view }

View File

@@ -30,5 +30,5 @@ decodeUpdate = decode pair
|> required "turn" decodeColor
|> required "board" (Decode.list decodeCell)
decodeUpdatestring : String -> Result String Update
decodeUpdatestring = Decode.decodeString decodeUpdate
decodeUpdateString : String -> Result String Update
decodeUpdateString = Decode.decodeString decodeUpdate

View File

@@ -2,6 +2,9 @@ module Go.Ws exposing (..)
import Go.Types exposing (..)
import WebSocket
wsUrl : Model -> String
wsUrl m = (m.sessionUrl ++ "/game/" ++ (toString m.sessionId))
encodeCell : Cell -> String
encodeCell ((x, y), c) = "place "
++ (toString x) ++ " "
@@ -9,4 +12,4 @@ encodeCell ((x, y), c) = "place "
++ (toString c)
sendMove : Model -> Cell -> Cmd Msg
sendMove m c = WebSocket.send m.sessionUrl (encodeCell c)
sendMove m c = WebSocket.send (wsUrl m) (encodeCell c)