New checker of engine messages which is aware of glued together messages
authorunc0rr
Sun, 10 Feb 2013 01:27:32 +0400
changeset 8484 99c14f14f788
parent 8483 d5fd4d7a0bcc
child 8485 7cae79214537
New checker of engine messages which is aware of glued together messages
gameServer/EngineInteraction.hs
gameServer/HWProtoInRoomState.hs
--- a/gameServer/EngineInteraction.hs	Sat Feb 09 00:11:14 2013 +0400
+++ b/gameServer/EngineInteraction.hs	Sun Feb 10 01:27:32 2013 +0400
@@ -8,6 +8,7 @@
 import qualified Data.ByteString.Char8 as B
 import qualified Data.ByteString as BW
 import qualified Data.Map as Map
+import qualified Data.List as L
 import Data.Word
 import Data.Bits
 import Control.Arrow
@@ -27,13 +28,19 @@
         removeLength _ = Nothing
 
 
-checkNetCmd :: B.ByteString -> (Bool, Bool)
+splitMessages :: B.ByteString -> [B.ByteString]
+splitMessages = L.unfoldr (\b -> if B.null b then Nothing else Just $ B.splitAt (1 + fromIntegral (BW.head b)) b)
+
+
+checkNetCmd :: B.ByteString -> (B.ByteString, B.ByteString)
 checkNetCmd msg = check decoded
     where
-        decoded = fromEngineMsg msg
-        check Nothing = (False, False)
-        check (Just ms) | B.length ms > 0 = let m = B.head ms in (m `Set.member` legalMessages, m == '+')
-                        | otherwise        = (False, False)
+        decoded = liftM splitMessages $ fromEngineMsg msg
+        check Nothing = (B.empty, B.empty)
+        check (Just msgs) = let (a, b) = (filter isLegal msgs, filter isNonEmpty a) in (encode a, encode b)
+        encode = B.pack . Base64.encode . BW.unpack . B.concat
+        isLegal = flip Set.member legalMessages . B.head
+        isNonEmpty = (/=) '+' . B.head
         legalMessages = Set.fromList $ "M#+LlRrUuDdZzAaSjJ,sNpPwtghbc12345" ++ slotMessages
         slotMessages = "\128\129\130\131\132\133\134\135\136\137\138"
 
--- a/gameServer/HWProtoInRoomState.hs	Sat Feb 09 00:11:14 2013 +0400
+++ b/gameServer/HWProtoInRoomState.hs	Sun Feb 10 01:27:32 2013 +0400
@@ -216,13 +216,13 @@
     rm <- thisRoom
     chans <- roomOthersChans
 
-    if teamsInGame cl > 0 && (isJust $ gameInfo rm) && isLegal then
-        return $ AnswerClients chans ["EM", msg]
-            : [ModifyRoom (\r -> r{gameInfo = liftM (\g -> g{roundMsgs = msg : roundMsgs g}) $ gameInfo r}) | not isKeepAlive]
+    if teamsInGame cl > 0 && (isJust $ gameInfo rm) && (not $ B.null legalMsgs) then
+        return $ AnswerClients chans ["EM", legalMsgs]
+            : [ModifyRoom (\r -> r{gameInfo = liftM (\g -> g{roundMsgs = nonEmptyMsgs : roundMsgs g}) $ gameInfo r}) | not $ B.null nonEmptyMsgs]
         else
         return []
     where
-        (isLegal, isKeepAlive) = checkNetCmd msg
+        (legalMsgs, nonEmptyMsgs) = checkNetCmd msg
 
 
 handleCmd_inRoom ["ROUNDFINISHED", correctly] = do
@@ -273,6 +273,7 @@
         else
             [ModifyRoom (\r -> r{isRegisteredOnly = not $ isRegisteredOnly r})]
 
+
 handleCmd_inRoom ["ROOM_NAME", newName] = do
     cl <- thisClient
     rs <- allRoomInfos
@@ -324,6 +325,7 @@
     where
         engineMsg cl = toEngineMsg $ B.concat ["b", nick cl, "(team): ", msg, "\x20\x20"]
 
+
 handleCmd_inRoom ["BAN", banNick] = do
     (thisClientId, rnc) <- ask
     maybeClientId <- clientByNick banNick