9 import Network.Socket |
9 import Network.Socket |
10 import System.IO |
10 import System.IO |
11 import qualified Data.List as List |
11 import qualified Data.List as List |
12 import Control.Monad |
12 import Control.Monad |
13 import qualified Codec.Binary.Base64 as Base64 |
13 import qualified Codec.Binary.Base64 as Base64 |
|
14 import qualified Data.ByteString.Lazy as BL |
|
15 import qualified Text.Show.ByteString as BS |
14 import qualified Data.ByteString.Char8 as B |
16 import qualified Data.ByteString.Char8 as B |
15 import qualified Data.ByteString as BW |
17 import qualified Data.ByteString as BW |
16 ------------------------------------------------- |
18 ------------------------------------------------- |
17 import CoreTypes |
19 import CoreTypes |
18 |
20 |
32 where |
34 where |
33 removeLength (x:xs) = if length xs == fromIntegral x then Just xs else Nothing |
35 removeLength (x:xs) = if length xs == fromIntegral x then Just xs else Nothing |
34 removeLength _ = Nothing |
36 removeLength _ = Nothing |
35 |
37 |
36 checkNetCmd :: B.ByteString -> (Bool, Bool) |
38 checkNetCmd :: B.ByteString -> (Bool, Bool) |
37 checkNetCmd = check . liftM B.unpack . fromEngineMsg |
39 checkNetCmd msg = check decoded |
38 where |
40 where |
|
41 decoded = fromEngineMsg msg |
39 check Nothing = (False, False) |
42 check Nothing = (False, False) |
40 check (Just (m:_)) = (m `Set.member` legalMessages, m == '+') |
43 check (Just ms) | B.length ms > 0 = let m = B.head ms in (m `Set.member` legalMessages, m == '+') |
41 check _ = (False, False) |
44 | otherwise = (False, False) |
42 legalMessages = Set.fromList $ "M#+LlRrUuDdZzAaSjJ,sFNpPwtghbc12345" ++ slotMessages |
45 legalMessages = Set.fromList $ "M#+LlRrUuDdZzAaSjJ,sFNpPwtghbc12345" ++ slotMessages |
43 slotMessages = "\128\129\130\131\132\133\134\135\136\137\138" |
46 slotMessages = "\128\129\130\131\132\133\134\135\136\137\138" |
44 |
47 |
45 maybeRead :: Read a => String -> Maybe a |
48 maybeRead :: Read a => String -> Maybe a |
46 maybeRead s = case reads s of |
49 maybeRead s = case reads s of |
70 tm : ts |
73 tm : ts |
71 else |
74 else |
72 t : replaceTeam tm ts |
75 t : replaceTeam tm ts |
73 |
76 |
74 illegalName :: B.ByteString -> Bool |
77 illegalName :: B.ByteString -> Bool |
75 illegalName b = null s || all isSpace s || isSpace (head s) || isSpace (last s) |
78 illegalName s = B.null s || B.all isSpace s || isSpace (B.head s) || isSpace (B.last s) |
76 where |
|
77 s = B.unpack b |
|
78 |
79 |
79 protoNumber2ver :: Word16 -> B.ByteString |
80 protoNumber2ver :: Word16 -> B.ByteString |
80 protoNumber2ver v = Map.findWithDefault "Unknown" v vermap |
81 protoNumber2ver v = Map.findWithDefault "Unknown" v vermap |
81 where |
82 where |
82 vermap = Map.fromList [ |
83 vermap = Map.fromList [ |
113 unfoldrE f b = |
114 unfoldrE f b = |
114 case f b of |
115 case f b of |
115 Right (a, new_b) -> let (a', b') = unfoldrE f new_b in (a : a', b') |
116 Right (a, new_b) -> let (a', b') = unfoldrE f new_b in (a : a', b') |
116 Left new_b -> ([], new_b) |
117 Left new_b -> ([], new_b) |
117 |
118 |
118 showB :: Show a => a -> B.ByteString |
119 showB :: (BS.Show a) => a -> B.ByteString |
119 showB = B.pack .show |
120 showB = B.concat . BL.toChunks . BS.show |
|
121 |
|
122 readInt_ :: (Num a) => B.ByteString -> a |
|
123 readInt_ str = |
|
124 case B.readInt str of |
|
125 Just (i, t) | B.null t -> fromIntegral i |
|
126 _ -> 0 |