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

54 lines
1.2 KiB
Haskell
Raw Normal View History

{-# LANGUAGE OverloadedStrings #-}
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)
import System.IO (stdout)
-- 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
updateSpawners delta
updateWizards delta
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) $
liftIO $ threadDelay $ floor $ remainingTime * 10 ^ 6
)
2024-03-29 22:01:41 +00:00
liftIO $ threadDelay (10 ^ 6)
-- liftIO $ killThread recvThread