gameServer/EngineInteraction.hs
changeset 6068 e18713ecf1e0
child 6069 d59745e525ec
equal deleted inserted replaced
6067:fe7990b3db66 6068:e18713ecf1e0
       
     1 module EngineInteraction where
       
     2 
       
     3 import qualified Data.Set as Set
       
     4 import qualified Data.List as List
       
     5 import Control.Monad
       
     6 import qualified Codec.Binary.Base64 as Base64
       
     7 import qualified Data.ByteString.Char8 as B
       
     8 import qualified Data.ByteString as BW
       
     9 
       
    10 
       
    11 
       
    12 toEngineMsg :: B.ByteString -> B.ByteString
       
    13 toEngineMsg msg = B.pack $ Base64.encode (fromIntegral (BW.length msg) : BW.unpack msg)
       
    14 
       
    15 
       
    16 fromEngineMsg :: B.ByteString -> Maybe B.ByteString
       
    17 fromEngineMsg msg = liftM BW.pack (Base64.decode (B.unpack msg) >>= removeLength)
       
    18     where
       
    19         removeLength (x:xs) = if length xs == fromIntegral x then Just xs else Nothing
       
    20         removeLength _ = Nothing
       
    21 
       
    22 
       
    23 checkNetCmd :: B.ByteString -> (Bool, Bool)
       
    24 checkNetCmd msg = check decoded
       
    25     where
       
    26         decoded = fromEngineMsg msg
       
    27         check Nothing = (False, False)
       
    28         check (Just ms) | B.length ms > 0 = let m = B.head ms in (m `Set.member` legalMessages, m == '+')
       
    29                         | otherwise        = (False, False)
       
    30         legalMessages = Set.fromList $ "M#+LlRrUuDdZzAaSjJ,sFNpPwtghbc12345" ++ slotMessages
       
    31         slotMessages = "\128\129\130\131\132\133\134\135\136\137\138"
       
    32