diff --git a/configuration.yaml b/configuration.yaml index 99180ce..385d470 100644 --- a/configuration.yaml +++ b/configuration.yaml @@ -2,4 +2,4 @@ setSocketPath : "/tmp/wizard.sock" setMapRows : 20 setMapColumns : 20 setSpawnerProbability : 0.01 -setFPS : 30 +setFPS : 5 diff --git a/src-client/Client/Communication.hs b/src-client/Client/Communication.hs index 4effbf6..82c8c99 100644 --- a/src-client/Client/Communication.hs +++ b/src-client/Client/Communication.hs @@ -5,6 +5,8 @@ import qualified Control.Concurrent.STM as STM import Control.Monad (void) +import Control.Monad.RWS + import qualified Data.Aeson as A import qualified Data.ByteString as B @@ -18,9 +20,12 @@ import Foreign hiding (void) import Network.Socket +import System.Posix.Signals + -- internal imports import Library.Types +import Client.Types connectSocket :: FilePath @@ -57,3 +62,22 @@ receiveMessage sock queue = do (putStrLn $ "received garbled data: " <> B8.unpack (B8.fromStrict msg)) (STM.atomically . STM.writeTQueue queue) mJsonMsg + +-- | Function that installs a handler on SIGINT to terminate game +terminateGameOnSigint + :: Game () +terminateGameOnSigint = do + sock <- asks rcSocket + clientId <- asks rcClientUUID + clientState <- gets scClientState + void $ liftIO $ installHandler + keyboardSignal + (CatchOnce $ do + void $ STM.atomically $ STM.swapTMVar serverState (clientState { clientStop = True }) + threadDelay (10 ^ 6) + sendMessage sock (ClientMessage clientId ClientQuit) + close sock + -- Raise SIGINT again so it does not get blocked + raiseSignal keyboardSignal + ) + Nothing diff --git a/src-client/Client/Types.hs b/src-client/Client/Types.hs index 10f8696..3cec11c 100644 --- a/src-client/Client/Types.hs +++ b/src-client/Client/Types.hs @@ -24,12 +24,13 @@ options = Options ) data ReaderContainer = ReaderContainer - { rcSocketHandle :: Socket - , rcClientUUID :: UUID + { rcSocket :: Socket + , rcClientUUID :: UUID } newtype StateContainer = StateContainer - { scWizard :: Wizard + { scWizard :: Wizard + , scClientState :: ClientState } type Game = RWST ReaderContainer String StateContainer IO diff --git a/src-client/Main.hs b/src-client/Main.hs index 4382d0b..3e49228 100644 --- a/src-client/Main.hs +++ b/src-client/Main.hs @@ -5,8 +5,6 @@ import Control.Concurrent (threadDelay) import qualified Control.Concurrent.STM as STM -import Control.Monad.Loops (whileM_) - import Options.Applicative -- internal imports diff --git a/wizard-wipeout.cabal b/wizard-wipeout.cabal index 53991c7..4a80910 100644 --- a/wizard-wipeout.cabal +++ b/wizard-wipeout.cabal @@ -46,6 +46,7 @@ executable wizard-wipeout-client , network , optparse-applicative , stm + , unix , uuid , vector , vty