wizard-wipeout/src-server/Server/Game.hs

50 lines
1.1 KiB
Haskell
Raw Normal View History

module Server.Game where
2024-03-29 22:01:41 +00:00
import Control.Concurrent
import qualified Control.Concurrent.STM as STM
2024-04-07 11:35:48 +00:00
import Control.Monad
import Control.Monad.RWS.Strict
import Control.Monad.Loops
import qualified Data.ByteString.Char8 as B8
import Data.Time
import Data.String (fromString)
-- internal imports
2024-05-01 21:52:49 +00:00
import Server.Game.Update
2023-12-10 01:02:09 +00:00
import Server.Communication
import Server.Types
runGame :: Game ()
runGame = do
processRequests
whileM_
(not . serverStop <$> (liftIO . STM.atomically . STM.readTMVar =<< gets scServerState))
(do
before <- gets scServerLastTick
now <- liftIO getCurrentTime
let delta = realToFrac $ diffUTCTime now before
2024-05-01 20:42:49 +00:00
-- liftIO $ B8.hPutStr stdout $ "tick: " <> fromString (show delta) <> " \r"
2023-12-12 02:31:57 +00:00
handleMessages
2024-11-03 03:40:46 +00:00
-- updateSpawners delta
-- updateWizards delta
2024-12-12 08:09:17 +00:00
modify'
(\s -> s
{ scServerLastTick = now
}
)
fps <- asks rcFPS
2024-02-02 08:07:11 +00:00
sendPings
let remainingTime = recip (fromIntegral fps) - delta
when (remainingTime > 0) $
2024-11-29 10:24:07 +00:00
liftIO $ threadDelay $ floor (remainingTime * 10 ^ (6 :: Int) :: Float)
)
2024-11-29 10:24:07 +00:00
liftIO $ threadDelay (10 ^ (6 :: Int))