gameServer/OfficialServer/GameReplayStore.hs
changeset 8509 eda9f2106d8d
parent 8502 fb8d914e76d8
child 8511 4f899fbce66d
equal deleted inserted replaced
8508:f849b7b3af1d 8509:eda9f2106d8d
    15 ---------------
    15 ---------------
    16 import CoreTypes
    16 import CoreTypes
    17 import EngineInteraction
    17 import EngineInteraction
    18 
    18 
    19 
    19 
       
    20 pickReplayFile :: Int -> IO String
       
    21 pickReplayFile p = do
       
    22     files <- liftM (filter (isSuffixOf ('.' : show p))) $ getDirectoryContents "replays"
       
    23     if (not $ null files) then
       
    24         return $ "replays/" ++ head files
       
    25         else
       
    26         return ""
       
    27 
    20 saveReplay :: RoomInfo -> IO ()
    28 saveReplay :: RoomInfo -> IO ()
    21 saveReplay r = do
    29 saveReplay r = do
    22     let gi = fromJust $ gameInfo r
    30     let gi = fromJust $ gameInfo r
    23     when (allPlayersHaveRegisteredAccounts gi) $ do
    31     when (allPlayersHaveRegisteredAccounts gi) $ do
    24         time <- getCurrentTime
    32         time <- getCurrentTime
    28         E.catch
    36         E.catch
    29             (writeFile fileName (show replayInfo))
    37             (writeFile fileName (show replayInfo))
    30             (\(e :: IOException) -> warningM "REPLAYS" $ "Couldn't write to " ++ fileName ++ ": " ++ show e)
    38             (\(e :: IOException) -> warningM "REPLAYS" $ "Couldn't write to " ++ fileName ++ ": " ++ show e)
    31 
    39 
    32 
    40 
    33 loadReplay :: Int -> IO [B.ByteString]
    41 loadReplay :: Int -> IO (Maybe CheckInfo, [B.ByteString])
    34 loadReplay p = E.handle (\(e :: SomeException) -> warningM "REPLAYS" "Problems reading replay" >> return []) $ do
    42 loadReplay p = E.handle (\(e :: SomeException) -> warningM "REPLAYS" "Problems reading replay" >> return (Nothing, [])) $ do
    35     files <- liftM (filter (isSuffixOf ('.' : show p))) $ getDirectoryContents "replays"
    43     fileName <- pickReplayFile p
    36     if (not $ null files) then
    44     if (not $ null fileName) then
    37         loadFile $ "replays/" ++ head files
    45         loadFile fileName
    38         else
    46         else
    39         return []
    47         return (Nothing, [])
    40     where
    48     where
    41         loadFile :: String -> IO [B.ByteString]
    49         loadFile :: String -> IO (Maybe CheckInfo, [B.ByteString])
    42         loadFile fileName = E.handle (\(e :: SomeException) -> warningM "REPLAYS" ("Problems reading " ++ fileName ++ ": " ++ show e) >> return []) $ do
    50         loadFile fileName = E.handle (\(e :: SomeException) ->
       
    51                     warningM "REPLAYS" ("Problems reading " ++ fileName ++ ": " ++ show e) >> return (Nothing, [])) $ do
    43             (teams, params1, params2, roundMsgs) <- liftM read $ readFile fileName
    52             (teams, params1, params2, roundMsgs) <- liftM read $ readFile fileName
    44             return $ replayToDemo teams (Map.fromList params1) (Map.fromList params2) (reverse roundMsgs)
    53             return $ (
       
    54                 Just (CheckInfo fileName teams)
       
    55                 , replayToDemo teams (Map.fromList params1) (Map.fromList params2) (reverse roundMsgs)
       
    56                 )
       
    57 
       
    58 moveFailedRecord :: String -> IO ()
       
    59 moveFailedRecord fn = renameFile fn ("failed/" ++ drop 8 fn)
       
    60 
       
    61 
       
    62 moveCheckedRecord :: String -> IO ()
       
    63 moveCheckedRecord fn = renameFile fn ("checked/" ++ drop 8 fn)