-- | Server and client game state types and operations.
module Game.LambdaHack.Server.ServerOptions
  ( ServerOptions(..), RNGs(..), defServerOptions
  ) where

import Prelude ()

import Game.LambdaHack.Core.Prelude

import           Data.Binary
import qualified System.Random as R

import Game.LambdaHack.Client (ClientOptions (..), defClientOptions)
import Game.LambdaHack.Definition.Defs
import Game.LambdaHack.Common.Faction
import Game.LambdaHack.Content.ModeKind

-- | Options that affect the behaviour of the server (including game rules).
data ServerOptions = ServerOptions
  { ServerOptions -> Bool
sknowMap         :: Bool
  , ServerOptions -> Bool
sknowEvents      :: Bool
  , ServerOptions -> Bool
sknowItems       :: Bool
  , ServerOptions -> Bool
sniff            :: Bool
  , ServerOptions -> Bool
sallClear        :: Bool
  , ServerOptions -> Bool
sboostRandomItem :: Bool
  , ServerOptions -> Maybe (GroupName ModeKind)
sgameMode        :: Maybe (GroupName ModeKind)
  , ServerOptions -> Bool
sautomateAll     :: Bool
  , ServerOptions -> Bool
skeepAutomated   :: Bool
  , ServerOptions -> Maybe StdGen
sdungeonRng      :: Maybe R.StdGen
  , ServerOptions -> Maybe StdGen
smainRng         :: Maybe R.StdGen
  , ServerOptions -> Bool
snewGameSer      :: Bool
  , ServerOptions -> Challenge
scurChalSer      :: Challenge
  , ServerOptions -> Bool
sdumpInitRngs    :: Bool
  , ServerOptions -> String
ssavePrefixSer   :: String
  , ServerOptions -> Bool
sdbgMsgSer       :: Bool
  , ServerOptions -> Bool
sshowItemSamples :: Bool
  , ServerOptions -> ClientOptions
sclientOptions   :: ClientOptions
      -- The client debug inside server debug only holds the client commandline
      -- options and is never updated with config options, etc.
  }
  deriving Int -> ServerOptions -> ShowS
[ServerOptions] -> ShowS
ServerOptions -> String
(Int -> ServerOptions -> ShowS)
-> (ServerOptions -> String)
-> ([ServerOptions] -> ShowS)
-> Show ServerOptions
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ServerOptions] -> ShowS
$cshowList :: [ServerOptions] -> ShowS
show :: ServerOptions -> String
$cshow :: ServerOptions -> String
showsPrec :: Int -> ServerOptions -> ShowS
$cshowsPrec :: Int -> ServerOptions -> ShowS
Show

data RNGs = RNGs
  { RNGs -> Maybe StdGen
dungeonRandomGenerator  :: Maybe R.StdGen
  , RNGs -> Maybe StdGen
startingRandomGenerator :: Maybe R.StdGen
  }

instance Show RNGs where
  show :: RNGs -> String
show RNGs{..} =
    let args :: [String]
args = [ String -> (StdGen -> String) -> Maybe StdGen -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe "" (\gen :: StdGen
gen -> "--setDungeonRng \"" String -> ShowS
forall a. [a] -> [a] -> [a]
++ StdGen -> String
forall a. Show a => a -> String
show StdGen
gen String -> ShowS
forall a. [a] -> [a] -> [a]
++ "\"")
                       Maybe StdGen
dungeonRandomGenerator
               , String -> (StdGen -> String) -> Maybe StdGen -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe "" (\gen :: StdGen
gen -> "--setMainRng \"" String -> ShowS
forall a. [a] -> [a] -> [a]
++ StdGen -> String
forall a. Show a => a -> String
show StdGen
gen String -> ShowS
forall a. [a] -> [a] -> [a]
++ "\"")
                       Maybe StdGen
startingRandomGenerator ]
    in [String] -> String
unwords [String]
args

instance Binary ServerOptions where
  put :: ServerOptions -> Put
put ServerOptions{..} = do
    Bool -> Put
forall t. Binary t => t -> Put
put Bool
sknowMap
    Bool -> Put
forall t. Binary t => t -> Put
put Bool
sknowEvents
    Bool -> Put
forall t. Binary t => t -> Put
put Bool
sknowItems
    Bool -> Put
forall t. Binary t => t -> Put
put Bool
sniff
    Bool -> Put
forall t. Binary t => t -> Put
put Bool
sallClear
    Bool -> Put
forall t. Binary t => t -> Put
put Bool
sboostRandomItem
    Maybe (GroupName ModeKind) -> Put
forall t. Binary t => t -> Put
put Maybe (GroupName ModeKind)
sgameMode
    Bool -> Put
forall t. Binary t => t -> Put
put Bool
sautomateAll
    Bool -> Put
forall t. Binary t => t -> Put
put Bool
skeepAutomated
    Challenge -> Put
forall t. Binary t => t -> Put
put Challenge
scurChalSer
    String -> Put
forall t. Binary t => t -> Put
put String
ssavePrefixSer
    Bool -> Put
forall t. Binary t => t -> Put
put Bool
sdbgMsgSer
    Bool -> Put
forall t. Binary t => t -> Put
put Bool
sshowItemSamples
    ClientOptions -> Put
forall t. Binary t => t -> Put
put ClientOptions
sclientOptions
  get :: Get ServerOptions
get = do
    Bool
sknowMap <- Get Bool
forall t. Binary t => Get t
get
    Bool
sknowEvents <- Get Bool
forall t. Binary t => Get t
get
    Bool
sknowItems <- Get Bool
forall t. Binary t => Get t
get
    Bool
sniff <- Get Bool
forall t. Binary t => Get t
get
    Bool
sallClear <- Get Bool
forall t. Binary t => Get t
get
    Bool
sboostRandomItem <- Get Bool
forall t. Binary t => Get t
get
    Maybe (GroupName ModeKind)
sgameMode <- Get (Maybe (GroupName ModeKind))
forall t. Binary t => Get t
get
    Bool
sautomateAll <- Get Bool
forall t. Binary t => Get t
get
    Bool
skeepAutomated <- Get Bool
forall t. Binary t => Get t
get
    Challenge
scurChalSer <- Get Challenge
forall t. Binary t => Get t
get
    String
ssavePrefixSer <- Get String
forall t. Binary t => Get t
get
    Bool
sdbgMsgSer <- Get Bool
forall t. Binary t => Get t
get
    Bool
sshowItemSamples <- Get Bool
forall t. Binary t => Get t
get
    ClientOptions
sclientOptions <- Get ClientOptions
forall t. Binary t => Get t
get
    let sdungeonRng :: Maybe a
sdungeonRng = Maybe a
forall a. Maybe a
Nothing
        smainRng :: Maybe a
smainRng = Maybe a
forall a. Maybe a
Nothing
        snewGameSer :: Bool
snewGameSer = Bool
False
        sdumpInitRngs :: Bool
sdumpInitRngs = Bool
False
    ServerOptions -> Get ServerOptions
forall (m :: * -> *) a. Monad m => a -> m a
return (ServerOptions -> Get ServerOptions)
-> ServerOptions -> Get ServerOptions
forall a b. (a -> b) -> a -> b
$! $WServerOptions :: Bool
-> Bool
-> Bool
-> Bool
-> Bool
-> Bool
-> Maybe (GroupName ModeKind)
-> Bool
-> Bool
-> Maybe StdGen
-> Maybe StdGen
-> Bool
-> Challenge
-> Bool
-> String
-> Bool
-> Bool
-> ClientOptions
-> ServerOptions
ServerOptions{..}

instance Binary RNGs where
  put :: RNGs -> Put
put RNGs{..} = do
    String -> Put
forall t. Binary t => t -> Put
put (Maybe StdGen -> String
forall a. Show a => a -> String
show Maybe StdGen
dungeonRandomGenerator)
    String -> Put
forall t. Binary t => t -> Put
put (Maybe StdGen -> String
forall a. Show a => a -> String
show Maybe StdGen
startingRandomGenerator)
  get :: Get RNGs
get = do
    String
dg <- Get String
forall t. Binary t => Get t
get
    String
sg <- Get String
forall t. Binary t => Get t
get
    let dungeonRandomGenerator :: Maybe StdGen
dungeonRandomGenerator = String -> Maybe StdGen
forall a. Read a => String -> a
read String
dg
        startingRandomGenerator :: Maybe StdGen
startingRandomGenerator = String -> Maybe StdGen
forall a. Read a => String -> a
read String
sg
    RNGs -> Get RNGs
forall (m :: * -> *) a. Monad m => a -> m a
return (RNGs -> Get RNGs) -> RNGs -> Get RNGs
forall a b. (a -> b) -> a -> b
$! $WRNGs :: Maybe StdGen -> Maybe StdGen -> RNGs
RNGs{..}

-- | Default value of server options.
defServerOptions :: ServerOptions
defServerOptions :: ServerOptions
defServerOptions = $WServerOptions :: Bool
-> Bool
-> Bool
-> Bool
-> Bool
-> Bool
-> Maybe (GroupName ModeKind)
-> Bool
-> Bool
-> Maybe StdGen
-> Maybe StdGen
-> Bool
-> Challenge
-> Bool
-> String
-> Bool
-> Bool
-> ClientOptions
-> ServerOptions
ServerOptions
  { sknowMap :: Bool
sknowMap = Bool
False
  , sknowEvents :: Bool
sknowEvents = Bool
False
  , sknowItems :: Bool
sknowItems = Bool
False
  , sniff :: Bool
sniff = Bool
False
  , sallClear :: Bool
sallClear = Bool
False
  , sboostRandomItem :: Bool
sboostRandomItem = Bool
False
  , sgameMode :: Maybe (GroupName ModeKind)
sgameMode = Maybe (GroupName ModeKind)
forall a. Maybe a
Nothing
  , sautomateAll :: Bool
sautomateAll = Bool
False
  , skeepAutomated :: Bool
skeepAutomated = Bool
False
  , sdungeonRng :: Maybe StdGen
sdungeonRng = Maybe StdGen
forall a. Maybe a
Nothing
  , smainRng :: Maybe StdGen
smainRng = Maybe StdGen
forall a. Maybe a
Nothing
  , snewGameSer :: Bool
snewGameSer = Bool
False
  , scurChalSer :: Challenge
scurChalSer = Challenge
defaultChallenge
  , sdumpInitRngs :: Bool
sdumpInitRngs = Bool
False
  , ssavePrefixSer :: String
ssavePrefixSer = ""
  , sdbgMsgSer :: Bool
sdbgMsgSer = Bool
False
  , sshowItemSamples :: Bool
sshowItemSamples = Bool
False
  , sclientOptions :: ClientOptions
sclientOptions = ClientOptions
defClientOptions
  }