Refactor options handling and storing
authorunc0rr
Mon, 20 Oct 2008 15:59:05 +0000
changeset 1383 d20e6e8928e3
parent 1382 b6ab9fea22fe
child 1384 329d3308e2e3
Refactor options handling and storing
netserver/HWProto.hs
netserver/Miscutils.hs
netserver/Opts.hs
netserver/hedgewars-server.hs
--- a/netserver/HWProto.hs	Mon Oct 20 12:58:16 2008 +0000
+++ b/netserver/HWProto.hs	Mon Oct 20 15:59:05 2008 +0000
@@ -11,7 +11,7 @@
 	where
 		hhsInfo = concatMap (\(HedgehogInfo name hat) -> [name, hat]) $ hedgehogs team
 
-answerServerMessage = [(clientOnly, ["SERVER_MESSAGE", "<h2><p align=center><a href=\"http://www.hedgewars.org/\">http://www.hedgewars.org/</a></p></h2>"])]
+answerServerMessage = [(clientOnly, ["SERVER_MESSAGE", ""])]
 answerBadCmd = [(clientOnly, ["ERROR", "Bad command, state or incorrect parameter"])]
 answerNotMaster = [(clientOnly, ["ERROR", "You cannot configure room parameters"])]
 answerBadParam = [(clientOnly, ["ERROR", "Bad parameter"])]
--- a/netserver/Miscutils.hs	Mon Oct 20 12:58:16 2008 +0000
+++ b/netserver/Miscutils.hs	Mon Oct 20 15:59:05 2008 +0000
@@ -51,13 +51,6 @@
 	}
 createRoom = (RoomInfo "" "" 0 [] "+rnd+" False Map.empty)
 
-data GlobalInfo =
-	GlobalInfo
-	{
-		isDedicated :: Bool,
-		serverMessage :: String
-	}
-
 type ClientsTransform = [ClientInfo] -> [ClientInfo]
 type RoomsTransform = [RoomInfo] -> [RoomInfo]
 type HandlesSelector = ClientInfo -> [ClientInfo] -> [RoomInfo] -> [Handle]
--- a/netserver/Opts.hs	Mon Oct 20 12:58:16 2008 +0000
+++ b/netserver/Opts.hs	Mon Oct 20 15:59:05 2008 +0000
@@ -5,26 +5,40 @@
 import Network
 import Data.Maybe ( fromMaybe )
 import Miscutils
-
-data Flag = ListenPort PortNumber
-	deriving Show
+import System.IO.Unsafe
 
-options :: [OptDescr Flag]
+data GlobalOptions =
+	GlobalOptions
+	{
+		isDedicated :: Bool,
+		serverMessage :: String,
+		listenPort :: PortNumber
+	}
+defaultMessage = "<h2><p align=center><a href=\"http://www.hedgewars.org/\">http://www.hedgewars.org/</a></p></h2>"
+defaultOptions = (GlobalOptions False defaultMessage 46631)
+
+options :: [OptDescr (GlobalOptions -> GlobalOptions)]
 options = [
-	Option ['p'] ["port"] (OptArg readPort "PORT") "listen on PORT"
+	Option ['p'] ["port"] (ReqArg readListenPort "PORT") "listen on PORT",
+	Option ['d'] ["dedicated"] (ReqArg readDedicated "BOOL") "start as dedicated (True or False)"
 	]
 
-readPort :: Maybe String -> Flag
-readPort str = ListenPort $ fromInteger (fromMaybe 46631 (maybeRead (fromMaybe "46631" str) :: Maybe Integer))
+readListenPort, readDedicated :: String -> GlobalOptions -> GlobalOptions
+readListenPort str opts = opts{listenPort = readPort}
+	where
+		readPort = fromInteger $ fromMaybe 46631 (maybeRead str :: Maybe Integer)
 
-opts :: IO [Flag]
+readDedicated str opts = opts{isDedicated = readDedicated}
+	where
+		readDedicated = fromMaybe True (maybeRead str :: Maybe Bool)
+
+opts :: IO GlobalOptions
 opts = do
 	args <- getArgs
 	case getOpt Permute options args of
-		(o, [], []) -> return o
+		(o, [], []) -> return $ foldr ($) defaultOptions o
 		(_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options))
 	where header = "Usage: newhwserv [OPTION...]"
 
-getPort :: [Flag] -> PortNumber
-getPort [] = 46631
-getPort (ListenPort a:flags) = a
+{-# NOINLINE globalOptions #-}
+globalOptions = unsafePerformIO opts
--- a/netserver/hedgewars-server.hs	Mon Oct 20 12:58:16 2008 +0000
+++ b/netserver/hedgewars-server.hs	Mon Oct 20 15:59:05 2008 +0000
@@ -87,7 +87,6 @@
 
 
 main = withSocketsDo $ do
-	flags <- opts
-	putStrLn $ "Listening on port " ++ show (getPort flags)
-	serverSocket <- listenOn $ PortNumber (getPort flags)
+	putStrLn $ "Listening on port " ++ show (listenPort globalOptions)
+	serverSocket <- listenOn $ PortNumber (listenPort globalOptions)
 	startServer serverSocket `finally` sClose serverSocket