------------------------------------------------------------------------------
-- | Handles and responds to a request to read the game state. This will read
-- the state from the PostgreSQL database, format it to JSON, and send it back
-- to the client.
gameStateHandler :: Handler b LokaService ()
gameStateHandler = do
  gameId <- getParam "gameId"
  maybe (writeBS $ jsonError "Invalid game ID") (\gameIdValid -> do
    gameState <- liftIO . getGameState $ ((read $ B.unpack gameIdValid) :: Integer)
    writeBS $ LB.toStrict $ encode $ JsonGameState (map (uncurry JsonAction) gameState)
      $ collapseGameState gameState)
    gameId

------------------------------------------------------------------------------
-- | Handles and responds to a request to make an action. This takes in the
-- current game state from the PostgreSQL database, adds an action if the
-- action is valid, writes the new action the database, and then sends back
-- the updated state.
gameStateHandler :: Handler b LokaService ()
gameStateHandler = do
  gameId <- getParam "gameId"
  maybe (writeBS $ jsonError "Invalid game ID") (\gameIdValid -> do
    gameState <- liftIO . getGameState $ ((read $ B.unpack gameIdValid) :: Integer)
    writeBS $ LB.toStrict $ encode $ JsonGameState (map (uncurry JsonAction) gameState)
      $ collapseGameState gameState)
    gameId

------------------------------------------------------------------------------
-- | Handles and responds to a request to make an action. This takes in the
-- current game state from the PostgreSQL database, adds an action if the
-- action is valid, writes the new action the database, and then sends back
-- the updated state.
gameStateHandler = do
  gameId <- getParam "gameId"
  maybe (writeBS $ jsonError "Invalid game ID") (\gameIdValid -> do
    gameState <- liftIO . getGameState $ ((read $ B.unpack gameIdValid) :: Integer)
    writeBS $ LB.toStrict $ encode $ JsonGameState (map (uncurry JsonAction) gameState)
      $ collapseGameState gameState)
    gameId

------------------------------------------------------------------------------
-- | Handles and responds to a request to make an action. This takes in the
-- current game state from the PostgreSQL database, adds an action if the
-- action is valid, writes the new action the database, and then sends back
-- the updated state.