gameServer/Utils.hs
changeset 2349 ba7a0813c532
parent 2310 581e59f123a2
child 2381 959da8402cac
equal deleted inserted replaced
2348:b39d826e1ccd 2349:ba7a0813c532
    10 import Data.ByteString.Internal (w2c)
    10 import Data.ByteString.Internal (w2c)
    11 import Numeric
    11 import Numeric
    12 import Network.Socket
    12 import Network.Socket
    13 import System.IO
    13 import System.IO
    14 import qualified Data.List as List
    14 import qualified Data.List as List
       
    15 import Control.Monad
    15 import Maybe
    16 import Maybe
    16 -------------------------------------------------
    17 -------------------------------------------------
    17 import qualified Codec.Binary.Base64 as Base64
    18 import qualified Codec.Binary.Base64 as Base64
    18 import qualified Codec.Binary.UTF8.String as UTF8
    19 import qualified Codec.Binary.UTF8.String as UTF8
    19 import CoreTypes
    20 import CoreTypes
    28 
    29 
    29 toEngineMsg :: String -> String
    30 toEngineMsg :: String -> String
    30 toEngineMsg msg = Base64.encode (fromIntegral (length msg) : (UTF8.encode msg))
    31 toEngineMsg msg = Base64.encode (fromIntegral (length msg) : (UTF8.encode msg))
    31 
    32 
    32 fromEngineMsg :: String -> Maybe String
    33 fromEngineMsg :: String -> Maybe String
    33 fromEngineMsg msg = Base64.decode msg >>= removeLength >>= return . (map w2c)
    34 fromEngineMsg msg = liftM (map w2c) (Base64.decode msg >>= removeLength)
    34 	where
    35 	where
    35 		removeLength (x:xs) = if length xs == fromIntegral x then Just xs else Nothing
    36 		removeLength (x:xs) = if length xs == fromIntegral x then Just xs else Nothing
    36 		removeLength _ = Nothing
    37 		removeLength _ = Nothing
    37 
    38 
    38 isLegalNetCommand :: String -> Bool
    39 isLegalNetCommand :: String -> Bool
    41 		decoded = fromEngineMsg msg
    42 		decoded = fromEngineMsg msg
    42 		test Nothing = False
    43 		test Nothing = False
    43 		test (Just (m:ms)) = m `Set.member` legalMessages
    44 		test (Just (m:ms)) = m `Set.member` legalMessages
    44 		test _ = False
    45 		test _ = False
    45 		legalMessages = Set.fromList $ "M#+LlRrUuDdZzAaSjJ,sFNpPwtghb12345" ++ slotMessages
    46 		legalMessages = Set.fromList $ "M#+LlRrUuDdZzAaSjJ,sFNpPwtghb12345" ++ slotMessages
    46 		slotMessages = ['\128', '\129', '\130', '\131', '\132', '\133', '\134', '\135', '\136', '\137', '\138']
    47 		slotMessages = "\128\129\130\131\132\133\134\135\136\137\138"
    47 
    48 
    48 maybeRead :: Read a => String -> Maybe a
    49 maybeRead :: Read a => String -> Maybe a
    49 maybeRead s = case reads s of
    50 maybeRead s = case reads s of
    50 	[(x, rest)] | all isSpace rest -> Just x
    51 	[(x, rest)] | all isSpace rest -> Just x
    51 	_         -> Nothing
    52 	_         -> Nothing
    72 			team : teams
    73 			team : teams
    73 		else
    74 		else
    74 			t : replaceTeam team teams
    75 			t : replaceTeam team teams
    75 
    76 
    76 illegalName :: String -> Bool
    77 illegalName :: String -> Bool
    77 illegalName str = all isSpace str
    78 illegalName = all isSpace
    78 
    79 
    79 protoNumber2ver :: Word16 -> String
    80 protoNumber2ver :: Word16 -> String
    80 protoNumber2ver 17 = "0.9.7-dev"
    81 protoNumber2ver 17 = "0.9.7-dev"
    81 protoNumber2ver 19 = "0.9.7"
    82 protoNumber2ver 19 = "0.9.7"
    82 protoNumber2ver 20 = "0.9.8-dev"
    83 protoNumber2ver 20 = "0.9.8-dev"