2023-12-10 05:57:48 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2023-12-09 01:49:19 +00:00
|
|
|
module Server.Game where
|
|
|
|
|
2024-03-29 22:01:41 +00:00
|
|
|
import Control.Concurrent
|
2023-12-10 05:57:48 +00:00
|
|
|
|
|
|
|
import qualified Control.Concurrent.STM as STM
|
2023-12-09 01:49:19 +00:00
|
|
|
|
2024-04-07 11:35:48 +00:00
|
|
|
import Control.Monad
|
|
|
|
|
2023-12-09 01:49:19 +00:00
|
|
|
import Control.Monad.RWS.Strict
|
|
|
|
|
|
|
|
import Control.Monad.Loops
|
|
|
|
|
2023-12-10 05:57:48 +00:00
|
|
|
import qualified Data.ByteString.Char8 as B8
|
|
|
|
|
2023-12-09 01:49:19 +00:00
|
|
|
import Data.Time
|
|
|
|
|
2023-12-10 05:57:48 +00:00
|
|
|
import Data.String (fromString)
|
|
|
|
|
|
|
|
import System.IO (stdout)
|
|
|
|
|
2023-12-09 01:49:19 +00:00
|
|
|
-- 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
|
2023-12-09 01:49:19 +00:00
|
|
|
import Server.Types
|
|
|
|
|
2023-12-10 05:57:48 +00:00
|
|
|
runGame :: Game ()
|
2023-12-09 01:49:19 +00:00
|
|
|
runGame = do
|
2023-12-10 05:57:48 +00:00
|
|
|
processRequests
|
2023-12-09 01:49:19 +00:00
|
|
|
whileM_
|
2023-12-10 05:57:48 +00:00
|
|
|
(not . serverStop <$> (liftIO . STM.atomically . STM.readTMVar =<< gets scServerState))
|
2023-12-09 01:49:19 +00:00
|
|
|
(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
|
2023-12-09 01:49:19 +00:00
|
|
|
updateSpawners delta
|
|
|
|
updateWizards delta
|
2023-12-10 05:57:48 +00:00
|
|
|
modify'
|
|
|
|
(\s -> s
|
|
|
|
{ scServerLastTick = now
|
|
|
|
}
|
|
|
|
)
|
2023-12-09 01:49:19 +00:00
|
|
|
fps <- asks rcFPS
|
2024-02-02 08:07:11 +00:00
|
|
|
sendPings
|
2023-12-09 01:49:19 +00:00
|
|
|
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
|