figure out timing problems
This commit is contained in:
parent
324a776227
commit
a202ec17b3
6 changed files with 50 additions and 27 deletions
|
@ -2,4 +2,4 @@ setSocketPath : "/tmp/wizard.sock"
|
||||||
setMapRows : 40
|
setMapRows : 40
|
||||||
setMapColumns : 40
|
setMapColumns : 40
|
||||||
setSpawnerProbability : 0.01
|
setSpawnerProbability : 0.01
|
||||||
setFPS : 30
|
setFPS : 60
|
||||||
|
|
|
@ -11,6 +11,8 @@ import qualified Data.Aeson as A
|
||||||
import qualified Data.ByteString as B
|
import qualified Data.ByteString as B
|
||||||
import qualified Data.ByteString.Lazy.Char8 as B8
|
import qualified Data.ByteString.Lazy.Char8 as B8
|
||||||
|
|
||||||
|
import Data.UUID
|
||||||
|
|
||||||
import qualified Data.Vector.Storable as VS
|
import qualified Data.Vector.Storable as VS
|
||||||
|
|
||||||
import Foreign hiding (void)
|
import Foreign hiding (void)
|
||||||
|
@ -50,7 +52,6 @@ sendMessage msg sock = do
|
||||||
handleMessages
|
handleMessages
|
||||||
:: Game ()
|
:: Game ()
|
||||||
handleMessages = do
|
handleMessages = do
|
||||||
sock <- asks rcSocket
|
|
||||||
queue <- asks rcQueue
|
queue <- asks rcQueue
|
||||||
msgs <- liftIO $ STM.atomically $ STM.flushTQueue queue
|
msgs <- liftIO $ STM.atomically $ STM.flushTQueue queue
|
||||||
mapM_
|
mapM_
|
||||||
|
@ -61,21 +62,21 @@ handleMessage
|
||||||
:: ServerMessage
|
:: ServerMessage
|
||||||
-> Game ()
|
-> Game ()
|
||||||
handleMessage ServerQuit = do
|
handleMessage ServerQuit = do
|
||||||
state <- get
|
st <- get
|
||||||
let stateCont = scClientState state
|
let stateCont = scClientState st
|
||||||
liftIO $ STM.atomically $ do
|
liftIO $ STM.atomically $ do
|
||||||
state <- STM.readTMVar stateCont
|
stateContainer <- STM.readTMVar stateCont
|
||||||
void $ STM.swapTMVar stateCont $ state
|
void $ STM.swapTMVar stateCont $ stateContainer
|
||||||
{ clientStop = True
|
{ clientStop = True
|
||||||
}
|
}
|
||||||
put $ state
|
put $ st
|
||||||
{ scClientState = stateCont
|
{ scClientState = stateCont
|
||||||
}
|
}
|
||||||
liftIO $ putStrLn "Quitting due to server shutdown"
|
liftIO $ putStrLn "Quitting due to server shutdown"
|
||||||
|
|
||||||
handleMessage (TickUpdate slice wizard) = do
|
handleMessage (TickUpdate slice wizard) = do
|
||||||
modify' (\state@(StateContainer {..}) ->
|
modify' (\st@(StateContainer {}) ->
|
||||||
state
|
st
|
||||||
{ scWizard = wizard
|
{ scWizard = wizard
|
||||||
, scMapSlice = slice
|
, scMapSlice = slice
|
||||||
}
|
}
|
||||||
|
@ -103,8 +104,8 @@ receiveMessage sock queue = do
|
||||||
terminateGameOnSigint
|
terminateGameOnSigint
|
||||||
:: Game ()
|
:: Game ()
|
||||||
terminateGameOnSigint = do
|
terminateGameOnSigint = do
|
||||||
sock <- asks rcSocket
|
-- sock <- asks rcSocket
|
||||||
clientId <- asks rcClientUUID
|
-- clientId <- asks rcClientUUID
|
||||||
clientState <- gets scClientState
|
clientState <- gets scClientState
|
||||||
void $ liftIO $ installHandler
|
void $ liftIO $ installHandler
|
||||||
keyboardSignal
|
keyboardSignal
|
||||||
|
@ -114,9 +115,16 @@ terminateGameOnSigint = do
|
||||||
void $ STM.swapTMVar clientState $ currentState { clientStop = True }
|
void $ STM.swapTMVar clientState $ currentState { clientStop = True }
|
||||||
pure currentState
|
pure currentState
|
||||||
Vty.shutdown (clientVty currentState)
|
Vty.shutdown (clientVty currentState)
|
||||||
sendMessage (ClientMessage clientId ClientQuit) sock
|
-- partingMessage clientId sock
|
||||||
close sock
|
|
||||||
-- Raise SIGINT again so it does not get blocked
|
-- Raise SIGINT again so it does not get blocked
|
||||||
-- raiseSignal keyboardSignal
|
-- raiseSignal keyboardSignal
|
||||||
)
|
)
|
||||||
Nothing
|
Nothing
|
||||||
|
|
||||||
|
partingMessage
|
||||||
|
:: UUID
|
||||||
|
-> Socket
|
||||||
|
-> IO ()
|
||||||
|
partingMessage clientId sock = do
|
||||||
|
sendMessage (ClientMessage clientId ClientQuit) sock
|
||||||
|
-- close sock
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
module Client.Game where
|
module Client.Game where
|
||||||
|
|
||||||
|
import Control.Concurrent
|
||||||
|
|
||||||
import qualified Control.Concurrent.STM as STM
|
import qualified Control.Concurrent.STM as STM
|
||||||
|
|
||||||
|
import Control.Monad
|
||||||
|
|
||||||
import Control.Monad.Loops
|
import Control.Monad.Loops
|
||||||
|
|
||||||
import Control.Monad.RWS
|
import Control.Monad.RWS
|
||||||
|
@ -15,9 +19,12 @@ import Library.Types
|
||||||
|
|
||||||
runGame :: Game ()
|
runGame :: Game ()
|
||||||
runGame = do
|
runGame = do
|
||||||
handleMessages
|
sock <- asks rcSocket
|
||||||
|
queue <- asks rcQueue
|
||||||
|
recvThread <- liftIO $ forkIO $ forever $ receiveMessage sock queue
|
||||||
whileM_
|
whileM_
|
||||||
(not . clientStop <$> (liftIO . STM.atomically . STM.readTMVar =<< gets scClientState))
|
(not . clientStop <$> (liftIO . STM.atomically . STM.readTMVar =<< gets scClientState))
|
||||||
(do
|
(do
|
||||||
pure ()
|
handleMessages
|
||||||
)
|
)
|
||||||
|
liftIO $ killThread recvThread
|
||||||
|
|
|
@ -7,16 +7,14 @@ import qualified Control.Concurrent.STM as STM
|
||||||
|
|
||||||
import Control.Monad
|
import Control.Monad
|
||||||
|
|
||||||
import Control.Monad.IO.Class
|
|
||||||
|
|
||||||
import Control.Monad.Loops
|
|
||||||
|
|
||||||
import Control.Monad.RWS
|
import Control.Monad.RWS
|
||||||
|
|
||||||
import qualified Data.Matrix as M
|
import qualified Data.Matrix as M
|
||||||
|
|
||||||
import Graphics.Vty
|
import Graphics.Vty
|
||||||
|
|
||||||
|
import Network.Socket (close)
|
||||||
|
|
||||||
import Options.Applicative
|
import Options.Applicative
|
||||||
|
|
||||||
-- internal imports
|
-- internal imports
|
||||||
|
@ -78,6 +76,9 @@ main = do
|
||||||
)
|
)
|
||||||
initRead
|
initRead
|
||||||
initState
|
initState
|
||||||
|
partingMessage clientId sock
|
||||||
|
threadDelay $ 1 * 10 ^ 6
|
||||||
|
close sock
|
||||||
where
|
where
|
||||||
opts = info (options <**> helper)
|
opts = info (options <**> helper)
|
||||||
( fullDesc
|
( fullDesc
|
||||||
|
|
|
@ -23,6 +23,7 @@ import qualified Data.Matrix as M
|
||||||
|
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
|
|
||||||
|
import Data.UUID
|
||||||
import Data.UUID.V4
|
import Data.UUID.V4
|
||||||
|
|
||||||
import qualified Data.Vector.Storable as VS
|
import qualified Data.Vector.Storable as VS
|
||||||
|
@ -114,12 +115,13 @@ sendMessage msg sock = do
|
||||||
(\ptr -> void $ sendBuf sock ptr (VS.length msgVector))
|
(\ptr -> void $ sendBuf sock ptr (VS.length msgVector))
|
||||||
|
|
||||||
-- | receive incoming messages from clients
|
-- | receive incoming messages from clients
|
||||||
receiveMessages :: Game ()
|
receiveMessages
|
||||||
receiveMessages = do
|
:: STM.TMVar [(Maybe UUID, Socket)]
|
||||||
clientsVar <- gets scClientSockets
|
-> STM.TQueue ClientMessage
|
||||||
|
-> IO ()
|
||||||
|
receiveMessages clientsVar queue = do
|
||||||
clients <- liftIO $ STM.atomically $ STM.readTMVar clientsVar
|
clients <- liftIO $ STM.atomically $ STM.readTMVar clientsVar
|
||||||
queue <- gets scMessageQueue
|
mapM_
|
||||||
void $ liftIO $ forkIO $ mapM_
|
|
||||||
(\(_, clientSocket) -> do
|
(\(_, clientSocket) -> do
|
||||||
receiveMessage clientSocket queue
|
receiveMessage clientSocket queue
|
||||||
)
|
)
|
||||||
|
@ -159,7 +161,7 @@ receiveMessage sock queue = do
|
||||||
(pure ())
|
(pure ())
|
||||||
(\msg -> do
|
(\msg -> do
|
||||||
liftIO $ STM.atomically $ STM.writeTQueue queue msg
|
liftIO $ STM.atomically $ STM.writeTQueue queue msg
|
||||||
when (msg == IdRequest) (threadDelay $ 10 ^ 3)
|
-- when (msg == IdRequest) (threadDelay $ 10 ^ 3)
|
||||||
)
|
)
|
||||||
mmsg
|
mmsg
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,8 @@ import Control.Monad.RWS.Strict
|
||||||
|
|
||||||
import Control.Monad.Loops
|
import Control.Monad.Loops
|
||||||
|
|
||||||
|
import Control.Concurrent
|
||||||
|
|
||||||
import qualified Data.ByteString.Char8 as B8
|
import qualified Data.ByteString.Char8 as B8
|
||||||
|
|
||||||
import Data.Time
|
import Data.Time
|
||||||
|
@ -27,6 +29,9 @@ import Server.Types
|
||||||
runGame :: Game ()
|
runGame :: Game ()
|
||||||
runGame = do
|
runGame = do
|
||||||
processRequests
|
processRequests
|
||||||
|
clientsVar <- gets scClientSockets
|
||||||
|
queue <- gets scMessageQueue
|
||||||
|
recvThread <- liftIO $ forkIO $ forever $ receiveMessages clientsVar queue
|
||||||
whileM_
|
whileM_
|
||||||
(not . serverStop <$> (liftIO . STM.atomically . STM.readTMVar =<< gets scServerState))
|
(not . serverStop <$> (liftIO . STM.atomically . STM.readTMVar =<< gets scServerState))
|
||||||
(do
|
(do
|
||||||
|
@ -34,7 +39,6 @@ runGame = do
|
||||||
now <- liftIO getCurrentTime
|
now <- liftIO getCurrentTime
|
||||||
let delta = realToFrac $ diffUTCTime now before
|
let delta = realToFrac $ diffUTCTime now before
|
||||||
liftIO $ B8.hPutStr stdout $ "tick: " <> fromString (show delta) <> " \r"
|
liftIO $ B8.hPutStr stdout $ "tick: " <> fromString (show delta) <> " \r"
|
||||||
receiveMessages
|
|
||||||
handleMessages
|
handleMessages
|
||||||
updateSpawners delta
|
updateSpawners delta
|
||||||
updateWizards delta
|
updateWizards delta
|
||||||
|
@ -44,11 +48,12 @@ runGame = do
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
fps <- asks rcFPS
|
fps <- asks rcFPS
|
||||||
sendUpdates
|
-- sendUpdates
|
||||||
let remainingTime = recip (fromIntegral fps) - delta
|
let remainingTime = recip (fromIntegral fps) - delta
|
||||||
when (remainingTime > 0) $
|
when (remainingTime > 0) $
|
||||||
liftIO $ threadDelay $ floor $ remainingTime * 10 ^ 6
|
liftIO $ threadDelay $ floor $ remainingTime * 10 ^ 6
|
||||||
)
|
)
|
||||||
|
liftIO $ killThread recvThread
|
||||||
|
|
||||||
updateSpawners :: Float -> Game ()
|
updateSpawners :: Float -> Game ()
|
||||||
updateSpawners dt =
|
updateSpawners dt =
|
||||||
|
|
Loading…
Reference in a new issue