2023-12-07 23:45:33 +00:00
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
2023-12-08 04:19:32 +00:00
|
|
|
module Server.Types where
|
|
|
|
|
2023-12-10 05:57:48 +00:00
|
|
|
import Control.Concurrent (ThreadId)
|
|
|
|
|
|
|
|
import qualified Control.Concurrent.STM as STM
|
|
|
|
|
2023-12-08 04:19:32 +00:00
|
|
|
import Control.Monad.RWS.Strict
|
2023-12-07 23:45:33 +00:00
|
|
|
|
|
|
|
import qualified Data.Aeson as Aeson
|
2023-12-08 04:19:32 +00:00
|
|
|
|
|
|
|
import Data.Time.Clock
|
|
|
|
|
2023-12-07 23:45:33 +00:00
|
|
|
import GHC.Generics
|
|
|
|
|
2023-12-09 12:58:59 +00:00
|
|
|
import Network.Socket
|
|
|
|
|
2023-12-08 04:19:32 +00:00
|
|
|
import Options.Applicative as O
|
|
|
|
|
2023-12-10 05:57:48 +00:00
|
|
|
--internal imports
|
|
|
|
|
2023-12-08 04:19:32 +00:00
|
|
|
import Library.Types
|
|
|
|
|
2023-12-08 01:26:29 +00:00
|
|
|
data Settings = Settings
|
|
|
|
{ setSocketPath :: FilePath
|
|
|
|
, setMapRows :: Int
|
|
|
|
, setMapColumns :: Int
|
|
|
|
, setSpawnerProbability :: Float
|
2023-12-08 04:19:32 +00:00
|
|
|
, setFPS :: Int
|
2023-12-07 23:45:33 +00:00
|
|
|
}
|
|
|
|
deriving (Show, Generic)
|
|
|
|
|
2023-12-08 01:26:29 +00:00
|
|
|
instance Aeson.FromJSON Settings
|
|
|
|
|
2023-12-08 04:19:32 +00:00
|
|
|
newtype Options = Options
|
2023-12-08 01:26:29 +00:00
|
|
|
{ optConfLoc :: FilePath
|
|
|
|
}
|
2023-12-08 04:19:32 +00:00
|
|
|
|
|
|
|
options :: O.Parser Options
|
|
|
|
options = Options
|
|
|
|
<$> strOption
|
|
|
|
( long "configuration"
|
|
|
|
<> short 'c'
|
|
|
|
<> metavar "FILEPATH"
|
|
|
|
<> help "Location of the configuration YAML-file"
|
|
|
|
)
|
|
|
|
|
|
|
|
data ReaderContainer = ReaderContainer
|
2023-12-10 01:02:09 +00:00
|
|
|
{ rcMap :: ServerMap
|
|
|
|
, rcFPS :: Int
|
|
|
|
, rcMainSocket :: Socket
|
2023-12-08 04:19:32 +00:00
|
|
|
}
|
|
|
|
deriving (Eq, Show)
|
|
|
|
|
|
|
|
data StateContainer = StateContainer
|
|
|
|
{ scSpawners :: [ Spawner ]
|
|
|
|
, scPlayers :: [ Wizard ]
|
2023-12-10 05:57:48 +00:00
|
|
|
, scServerState :: STM.TMVar ServerState
|
2023-12-08 04:19:32 +00:00
|
|
|
, scServerLastTick :: UTCTime
|
2023-12-10 05:57:48 +00:00
|
|
|
, scClientSockets :: STM.TMVar [ (Socket, Maybe ThreadId) ]
|
2023-12-08 04:19:32 +00:00
|
|
|
}
|
|
|
|
|
2023-12-10 01:02:09 +00:00
|
|
|
-- | Data object for storing the server's state
|
|
|
|
data ServerState = ServerState
|
|
|
|
{ serverGameOver :: Bool -- ^ global game over state (only one contestant left)
|
|
|
|
, serverStop :: Bool -- ^ Server shutdown
|
|
|
|
}
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
2023-12-10 05:57:48 +00:00
|
|
|
type Game = RWST ReaderContainer String StateContainer IO
|