Skip to content

Commit

Permalink
Output types corrected
Browse files Browse the repository at this point in the history
  • Loading branch information
ezemtsov committed Jul 5, 2019
1 parent be72be6 commit e887d9d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 44 deletions.
6 changes: 3 additions & 3 deletions src/Game.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ()

Expand Down
5 changes: 2 additions & 3 deletions src/Router.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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 ()
Expand Down
31 changes: 13 additions & 18 deletions src/TypesGameOutput.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
33 changes: 13 additions & 20 deletions web/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -92,40 +85,40 @@ 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 = [];
cleanGrid();
drawSnackbar('New game started');
break;
case 'UpdatePlayer':
game.updatePlayer(data.Player);
game.updatePlayer(data);
break;
}
};
Expand Down

0 comments on commit e887d9d

Please sign in to comment.