More work on best time ghost feature
authorunc0rr
Thu, 25 Feb 2016 23:17:52 +0300
changeset 11575 db7743e2fad1
parent 11573 8fd1808b12ed
child 11576 134113bff264
More work on best time ghost feature
gameServer/Actions.hs
gameServer/CoreTypes.hs
gameServer/HWProtoCore.hs
gameServer/HWProtoInRoomState.hs
gameServer/OfficialServer/extdbinterface.hs
gameServer/Utils.hs
gameServer/Votes.hs
--- a/gameServer/Actions.hs	Wed Feb 24 22:37:03 2016 +0300
+++ b/gameServer/Actions.hs	Thu Feb 25 23:17:52 2016 +0300
@@ -728,6 +728,26 @@
     processAction $ AnswerClients chans ["CHAT", "[random]", i !! n]
 
 
+processAction (LoadGhost location) = do
+    points <- io $ loadFile (B.unpack $ "ghosts/" `B.append` sanitizeName location)
+    ri <- clientRoomA
+    rnc <- gets roomsClients
+    thisRoomChans <- liftM (map sendChan) $ roomClientsS ri
+    -- inject ghost points into map
+    rm <- io $ room'sM rnc id ri
+    cl <- client's id
+    mapM processAction $ map (replaceChans thisRoomChans) $ answerFullConfigParams cl (mapParams rm) (params rm)
+    return ()
+    where
+    loadFile :: String -> IO [Int]
+    loadFile fileName = E.handle (\(e :: SomeException) -> return []) $ do
+        points <- liftM read $ readFile fileName
+        return (points `deepseq` points)
+    replaceChans chans (AnswerClients _ msg) = AnswerClients chans msg
+    replaceChans _ a = a
+{-
+        let a = map (replaceChans chans) $ answerFullConfigParams cl mp p
+-}
 #if defined(OFFICIAL_SERVER)
 processAction SaveReplay = do
     ri <- clientRoomA
@@ -846,4 +866,4 @@
     forM_ (actions `deepseq` actions) processAction
 
 processAction CheckVotes =
-    checkVotes >>= mapM_ processAction
\ No newline at end of file
+    checkVotes >>= mapM_ processAction
--- a/gameServer/CoreTypes.hs	Wed Feb 24 22:37:03 2016 +0300
+++ b/gameServer/CoreTypes.hs	Thu Feb 25 23:17:52 2016 +0300
@@ -93,6 +93,7 @@
     | CheckFailed B.ByteString
     | CheckSuccess [B.ByteString]
     | Random [ClientChan] [B.ByteString]
+    | LoadGhost B.ByteString
     | QueryReplay B.ByteString
     | ShowReplay B.ByteString
     | Cleanup
@@ -237,7 +238,7 @@
         roomBansList :: ![B.ByteString],
         mapParams :: !(Map.Map B.ByteString B.ByteString),
         params :: !(Map.Map B.ByteString [B.ByteString]),
-        roomSaves :: !(Map.Map B.ByteString (Map.Map B.ByteString B.ByteString, Map.Map B.ByteString [B.ByteString]))
+        roomSaves :: !(Map.Map B.ByteString (B.ByteString, Map.Map B.ByteString B.ByteString, Map.Map B.ByteString [B.ByteString]))
     }
 
 newRoom :: RoomInfo
--- a/gameServer/HWProtoCore.hs	Wed Feb 24 22:37:03 2016 +0300
+++ b/gameServer/HWProtoCore.hs	Thu Feb 25 23:17:52 2016 +0300
@@ -67,7 +67,7 @@
         h "DELEGATE" n | not $ B.null n = handleCmd ["DELEGATE", n]
         h "SAVEROOM" n | not $ B.null n = handleCmd ["SAVEROOM", n]
         h "LOADROOM" n | not $ B.null n = handleCmd ["LOADROOM", n]
-        h "SAVE" n | not $ B.null n = handleCmd ["SAVE", n]
+        h "SAVE" n | not $ B.null n = let (sn, ln) = B.break (== ' ') n in if B.null ln then return [] else handleCmd ["SAVE", sn, B.tail ln]
         h "DELETE" n | not $ B.null n = handleCmd ["DELETE", n]
         h "STATS" _ = handleCmd ["STATS"]
         h "PART" m | not $ B.null m = handleCmd ["PART", m]
--- a/gameServer/HWProtoInRoomState.hs	Wed Feb 24 22:37:03 2016 +0300
+++ b/gameServer/HWProtoInRoomState.hs	Thu Feb 25 23:17:52 2016 +0300
@@ -492,8 +492,8 @@
         return [AnswerClients [sendChan cl] ["CHAT", "[server]", "vote: 'yes' or 'no'"]]
 
 
-handleCmd_inRoom ["SAVE", stateName] = serverAdminOnly $ do
-    return [ModifyRoom $ \r -> r{roomSaves = Map.insert stateName (mapParams r, params r) (roomSaves r)}]
+handleCmd_inRoom ["SAVE", stateName, location] = serverAdminOnly $ do
+    return [ModifyRoom $ \r -> r{roomSaves = Map.insert stateName (location, mapParams r, params r) (roomSaves r)}]
 
 handleCmd_inRoom ["DELETE", stateName] = serverAdminOnly $ do
     return [ModifyRoom $ \r -> r{roomSaves = Map.delete stateName (roomSaves r)}]
--- a/gameServer/OfficialServer/extdbinterface.hs	Wed Feb 24 22:37:03 2016 +0300
+++ b/gameServer/OfficialServer/extdbinterface.hs	Thu Feb 25 23:17:52 2016 +0300
@@ -140,13 +140,13 @@
         ps bs
     ps ("GHOST_POINTS" : n : bs) = do
         let pointsNum = readInt_ n
-        (loc, time) <- get
-        res <- io $ query dbConn dbQueryBestTime $ Only loc
+        (location, time) <- get
+        res <- io $ query dbConn dbQueryBestTime $ Only location
         let bestTime = case res of
                 [Only a] -> a
                 _ -> maxBound :: Int
         when (time < bestTime) $ do
-            -- store it
+            io $ writeFile (B.unpack $ "ghosts/" `B.append` sanitizeName location) $ show (map readInt_ $ take (2 * pointsNum) bs)
             return ()
         ps (drop (2 * pointsNum) bs)
     ps (b:bs) = ps bs
--- a/gameServer/Utils.hs	Wed Feb 24 22:37:03 2016 +0300
+++ b/gameServer/Utils.hs	Thu Feb 25 23:17:52 2016 +0300
@@ -241,3 +241,8 @@
 deleteFirstsBy2          :: (a -> b -> Bool) -> [a] -> [b] -> [a]
 deleteFirstsBy2 eq       =  foldl (flip (deleteBy2 (flip eq)))
 
+sanitizeName :: B.ByteString -> B.ByteString
+sanitizeName = B.map sc
+    where
+        sc c | isAlphaNum c = c
+             | otherwise = '_'
--- a/gameServer/Votes.hs	Wed Feb 24 22:37:03 2016 +0300
+++ b/gameServer/Votes.hs	Thu Feb 25 23:17:52 2016 +0300
@@ -95,17 +95,14 @@
         let rs = Map.lookup roomSave (roomSaves rm)
         case rs of
              Nothing -> return []
-             Just (mp, p) -> do
+             Just (location, mp, p) -> do
                  cl <- thisClient
                  chans <- roomClientsChans
-                 let a = map (replaceChans chans) $ answerFullConfigParams cl mp p
-                 return $ 
-                    (ModifyRoom $ \r -> r{params = p, mapParams = mp})
-                    : SendUpdateOnThisRoom
-                    : a
-        where
-            replaceChans chans (AnswerClients _ msg) = AnswerClients chans msg
-            replaceChans _ a = a
+                 return $
+                    [ModifyRoom $ \r -> r{params = p, mapParams = mp}
+                    , AnswerClients chans ["CHAT", "[server]", location]
+                    , SendUpdateOnThisRoom
+                    , LoadGhost location]
     act (VotePause) = do
         rm <- thisRoom
         chans <- roomClientsChans