From 68f8ea4a6eb94016ea164b0491ef9d9d9202c992 Mon Sep 17 00:00:00 2001 From: Evgeny Zemtsov Date: Fri, 5 Jul 2019 11:37:30 +0200 Subject: [PATCH] Correct input types --- src/Game.hs | 4 ++-- src/TypesGameInput.hs | 24 ++++++++++-------------- web/index.js | 24 +++++++++--------------- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/src/Game.hs b/src/Game.hs index ffb409c..fe37bf8 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -86,9 +86,9 @@ type GameAction = MVar GameState -> IO () -- Function that encapsulates message processing logic gameLogic :: Client -> I.Message -> GameAction gameLogic client msg state = case msg of - I.PostMove (I.Cell cell) -> postMoveAction client cell state + I.PostMove cell -> postMoveAction client cell state + I.UpdatePlayer player -> updatePlayerAction client player state I.CleanHistory -> cleanHistoryAction state - I.UpdatePlayer (I.Player player) -> updatePlayerAction client player state _ -> print $ "Handler is not implemented: " <> show msg updatePlayerAction :: Client -> G.Player -> GameAction diff --git a/src/TypesGameInput.hs b/src/TypesGameInput.hs index 2c27d7d..538d070 100644 --- a/src/TypesGameInput.hs +++ b/src/TypesGameInput.hs @@ -19,9 +19,9 @@ noTagOptions = defaultOptions { sumEncoding = ObjectWithSingleField } data Message = - Connect Data - | PostMove Data - | UpdatePlayer Data + Connect ConnectionData + | PostMove G.Cell + | UpdatePlayer G.Player | GetHistory | CleanHistory deriving (Generic, Eq, Show) @@ -30,17 +30,13 @@ instance FromJSON Message where instance ToJSON Message where toJSON = genericToJSON messageOptions -data Data = - ConnectionData { playerName :: G.PlayerName - , mode :: ConnectionMode - , session :: Maybe G.SessionId } - | Player G.Player - | Cell G.Cell - deriving (Generic, Eq, Show) -instance FromJSON Data where - parseJSON = genericParseJSON noTagOptions -instance ToJSON Data where - toJSON = genericToJSON noTagOptions +data ConnectionData = ConnectionData + { playerName :: G.PlayerName + , mode :: ConnectionMode + , session :: Maybe G.SessionId + } deriving (Generic, Eq, Show) +instance FromJSON ConnectionData +instance ToJSON ConnectionData data ConnectionMode = NewGame diff --git a/web/index.js b/web/index.js index 48ece18..eb0158e 100644 --- a/web/index.js +++ b/web/index.js @@ -146,13 +146,11 @@ function msgMove(row, col, token) { return { method: 'PostMove', resource: { - Cell: { - coord: { - x: row, - y: col - }, - value: token - } + coord: { + x: row, + y: col + }, + value: token } }; } @@ -161,11 +159,9 @@ function msgConnect(name, mode, session) { return { method: 'Connect', resource: { - ConnectionData: { - playerName: name, - mode: mode, - session: session - } + playerName: name, + mode: mode, + session: session } }; } @@ -177,9 +173,7 @@ function msgCleanHistory() { function msgUpdatePlayer(player) { return { method: "UpdatePlayer", - resource: { - Player: player - } + resource: player }; }