gameServer/Opts.hs
author unc0rr
Sun, 08 Mar 2009 12:55:40 +0000
changeset 1869 490005509a7b
parent 1832 1fb61a53a2c2
child 1964 dc9ea05c9d2f
permissions -rw-r--r--
patch by nemo
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     1
module Opts
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     2
(
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     3
	getOpts,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     4
) where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     5
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     6
import System
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     7
import System.Console.GetOpt
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     8
import Network
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     9
import Data.Maybe ( fromMaybe )
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    10
import CoreTypes
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    11
import Utils
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    12
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    13
options :: [OptDescr (ServerInfo -> ServerInfo)]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    14
options = [
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    15
	Option ['p'] ["port"] (ReqArg readListenPort "PORT") "listen on PORT",
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    16
	Option ['d'] ["dedicated"] (ReqArg readDedicated "BOOL") "start as dedicated (True or False)",
1832
1fb61a53a2c2 Add options for database access
unc0rr
parents: 1804
diff changeset
    17
	Option []    ["db-login"] (ReqArg readDbLogin "STRING") "database access login",
1fb61a53a2c2 Add options for database access
unc0rr
parents: 1804
diff changeset
    18
	Option []    ["db-password"] (ReqArg readDbPassword "STRING") "database access password",
1fb61a53a2c2 Add options for database access
unc0rr
parents: 1804
diff changeset
    19
	Option []    ["db-host"] (ReqArg readDbHost "STRING") "database host"
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    20
	]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    21
1832
1fb61a53a2c2 Add options for database access
unc0rr
parents: 1804
diff changeset
    22
readListenPort,
1fb61a53a2c2 Add options for database access
unc0rr
parents: 1804
diff changeset
    23
	readDedicated,
1fb61a53a2c2 Add options for database access
unc0rr
parents: 1804
diff changeset
    24
	readDbLogin,
1fb61a53a2c2 Add options for database access
unc0rr
parents: 1804
diff changeset
    25
	readDbPassword,
1fb61a53a2c2 Add options for database access
unc0rr
parents: 1804
diff changeset
    26
	readDbHost :: String -> ServerInfo -> ServerInfo
1fb61a53a2c2 Add options for database access
unc0rr
parents: 1804
diff changeset
    27
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    28
readListenPort str opts = opts{listenPort = readPort}
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    29
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    30
		readPort = fromInteger $ fromMaybe 46631 (maybeRead str :: Maybe Integer)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    31
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    32
readDedicated str opts = opts{isDedicated = readDedicated}
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    33
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    34
		readDedicated = fromMaybe True (maybeRead str :: Maybe Bool)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    35
1832
1fb61a53a2c2 Add options for database access
unc0rr
parents: 1804
diff changeset
    36
readDbLogin str opts = opts{dbLogin = str}
1fb61a53a2c2 Add options for database access
unc0rr
parents: 1804
diff changeset
    37
readDbPassword str opts = opts{dbPassword = str}
1fb61a53a2c2 Add options for database access
unc0rr
parents: 1804
diff changeset
    38
readDbHost str opts = opts{dbHost = str}
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    39
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    40
getOpts :: ServerInfo -> IO ServerInfo
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    41
getOpts opts = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    42
	args <- getArgs
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    43
	case getOpt Permute options args of
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    44
		(o, [], []) -> return $ foldr ($) opts o
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    45
		(_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options))
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    46
	where header = "Usage: newhwserv [OPTION...]"