some little optimizations

This commit is contained in:
nek0 2023-12-12 03:31:57 +01:00
parent 9691a0412a
commit 087ae2d072
2 changed files with 20 additions and 14 deletions

View file

@ -22,7 +22,6 @@ import qualified Data.Matrix as M
import Data.Maybe
import Data.UUID
import Data.UUID.V4
import qualified Data.Vector.Storable as VS
@ -115,24 +114,30 @@ sendMessage msg sock = do
msgVector
(\ptr -> void $ sendBuf sock ptr (VS.length msgVector))
-- | process incoming messages from clients
processMessages :: Game ()
processMessages = do
-- | receive incoming messages from clients
receiveMessages :: Game ()
receiveMessages = do
clientsVar <- gets scClientSockets
clients <- liftIO $ STM.atomically $ STM.readTMVar clientsVar
queue <- gets scMessageQueue
serverState <- get
readerContainer <- ask
mapM_
(\(uuid, clientSocket) -> void $ liftIO $ forkIO $ do
void $ liftIO $ forkIO $ mapM_
(\(_, clientSocket) -> do
receiveMessage clientSocket queue
msgs <- liftIO $ STM.atomically $ STM.flushTQueue queue
mapM_
(handleMessage serverState readerContainer)
msgs
)
clients
-- | handle received messages
handleMessages :: Game ()
handleMessages = do
queue <- gets scMessageQueue
serverState <- get
readerContainer <- ask
msgs <- liftIO $ STM.atomically $ STM.flushTQueue queue
void $ liftIO $ do
mapM_
(handleMessage serverState readerContainer)
msgs
-- | receive a 'ClientMessage'
receiveMessage
:: Socket

View file

@ -1,7 +1,7 @@
{-# LANGUAGE OverloadedStrings #-}
module Server.Game where
import Control.Concurrent (threadDelay, killThread)
import Control.Concurrent (threadDelay)
import qualified Control.Concurrent.STM as STM
@ -34,7 +34,8 @@ runGame = do
now <- liftIO getCurrentTime
let delta = realToFrac $ diffUTCTime now before
liftIO $ B8.hPutStr stdout $ "tick: " <> fromString (show delta) <> " \r"
processMessages
receiveMessages
handleMessages
updateSpawners delta
updateWizards delta
modify'