From e887d9d22711a76a0a4a475e32e88ef5689de1d4 Mon Sep 17 00:00:00 2001 From: Evgeny Zemtsov Date: Fri, 5 Jul 2019 12:01:41 +0200 Subject: [PATCH] Output types corrected --- src/Game.hs | 6 +++--- src/Router.hs | 5 ++--- src/TypesGameOutput.hs | 31 +++++++++++++------------------ web/index.js | 33 +++++++++++++-------------------- 4 files changed, 31 insertions(+), 44 deletions(-) diff --git a/src/Game.hs b/src/Game.hs index fe37bf8..a889630 100644 --- a/src/Game.hs +++ b/src/Game.hs @@ -99,7 +99,7 @@ updatePlayerAction client newPlayer state = do return s' -- Share update with other clients readMVar state >>= \s -> do - let ctrlMsg = O.UpdatePlayer (O.Player newPlayer) + let ctrlMsg = O.UpdatePlayer newPlayer broadcast (encode ctrlMsg) s postMoveAction :: Client -> G.Cell -> GameAction @@ -110,11 +110,11 @@ postMoveAction (player, conn) cell state = do return s' -- Share the move with other clients readMVar state >>= \s -> do - let ctrlMsg = O.SetCell (O.Cell cell) + let ctrlMsg = O.SetCell cell broadcast (encode ctrlMsg) s -- Then check if player won if winSituation s - then let ctrlMsg = O.Win (O.Player player) + then let ctrlMsg = O.Win player in broadcast (encode ctrlMsg) s else return () diff --git a/src/Router.hs b/src/Router.hs index 7b53e13..e6e16c8 100644 --- a/src/Router.hs +++ b/src/Router.hs @@ -112,7 +112,7 @@ startRouter rState pending = do modifyMVar_ gState $ \s -> do let s' = Game.addClient client s -- Warn everybody - let ctrlMsg = O.Connected (O.Player player) + let ctrlMsg = O.Connected player Game.broadcast (encode ctrlMsg) s' return s' @@ -140,8 +140,7 @@ disconnect client gState = do let s' = removeClient client s in return (s',s') -- Broadcast that one of the players is disconnected - let ctrlMsg = O.Disconnected $ - O.Player (fst client) + let ctrlMsg = O.Disconnected (fst client) broadcast (encode ctrlMsg) s incorrectMessage :: ByteString -> IO () diff --git a/src/TypesGameOutput.hs b/src/TypesGameOutput.hs index f69a3a7..ea5fd05 100644 --- a/src/TypesGameOutput.hs +++ b/src/TypesGameOutput.hs @@ -10,12 +10,12 @@ import qualified TypesGame as G -- Structure for output messages data Message = - Connected Data - | Disconnected Data - | SetSession Data - | SetCell Data - | UpdatePlayer Data - | Win Data + Connected G.Player + | Disconnected G.Player + | UpdatePlayer G.Player + | SetSession SessionData + | SetCell G.Cell + | Win G.Player | Clean deriving (Generic, Show) instance ToJSON Message where @@ -25,15 +25,10 @@ instance ToJSON Message where , contentsFieldName = "data" } } -data Data = - Player G.Player - | Cell G.Cell - | SessionData { session :: G.SessionId - , me :: G.Player - , players :: [G.Player] - , history :: [G.Cell] } - deriving (Generic, Show) -instance ToJSON Data where - toJSON = genericToJSON defaultOptions - { allNullaryToStringTag = True - , sumEncoding = ObjectWithSingleField } +data SessionData = SessionData + { session :: G.SessionId + , me :: G.Player + , players :: [G.Player] + , history :: [G.Cell] + } deriving (Generic, Show) +instance ToJSON SessionData diff --git a/web/index.js b/web/index.js index 06836c6..8fd42c9 100644 --- a/web/index.js +++ b/web/index.js @@ -64,13 +64,6 @@ class Game { focusCell(this.lastMove); //Fill player list refillPlayerList(this.players); - - // // Set default token - // let playersN = this.players.length; - // // tokens are defined once as global alias to token class elements - // let tokensN = Array.from(tokens).length; - // let playerTokenId = (playersN - 1) % tokensN; - } connect() { let game = this; @@ -92,32 +85,32 @@ class Game { let data = ctrlMsg.data; switch (ctrlMsg.message) { case 'Connected': - drawSnackbar(data.Player.name + ' connected'); - game.rememberPlayer(data.Player); + drawSnackbar(data.name + ' connected'); + game.rememberPlayer(data); break; case 'Disconnected': - drawSnackbar(data.Player.name + ' disconnected'); - game.forgetPlayer(data.Player); + drawSnackbar(data.name + ' disconnected'); + game.forgetPlayer(data); break; case 'SetCell': unfocusCell(game.lastMove); - game.history.push(data.Cell); - fillCell(data.Cell); - focusCell(data.Cell); + game.history.push(data); + fillCell(data); + focusCell(data); break; case 'Win': - drawSnackbar(data.Player.name + ' won!'); + drawSnackbar(data.name + ' won!'); break; case 'SetSession': console.log('Recieved session data'); - game.players = data.SessionData.players; - game.history = data.SessionData.history; + game.players = data.players; + game.history = data.history; game.replayHistory(); - game.me = data.SessionData.me; + game.me = data.me; toggleTokenSelector(game.me.token); - window.history.pushState(null, null, data.SessionData.session); + window.history.pushState(null, null, data.session); break; case 'Clean': game.history = []; @@ -125,7 +118,7 @@ class Game { drawSnackbar('New game started'); break; case 'UpdatePlayer': - game.updatePlayer(data.Player); + game.updatePlayer(data); break; } };