author | unc0rr |
Sun, 10 Feb 2013 22:58:16 +0400 | |
changeset 8495 | f1223400b473 |
parent 8482 | 5656a73fe3c3 |
child 8502 | fb8d914e76d8 |
permissions | -rw-r--r-- |
5143
649d87819682
Start implementation of archivements/ratings on server side: replay saving routine
unc0rr
parents:
diff
changeset
|
1 |
{-# LANGUAGE ScopedTypeVariables #-} |
649d87819682
Start implementation of archivements/ratings on server side: replay saving routine
unc0rr
parents:
diff
changeset
|
2 |
module OfficialServer.GameReplayStore where |
649d87819682
Start implementation of archivements/ratings on server side: replay saving routine
unc0rr
parents:
diff
changeset
|
3 |
|
649d87819682
Start implementation of archivements/ratings on server side: replay saving routine
unc0rr
parents:
diff
changeset
|
4 |
import Data.Time |
649d87819682
Start implementation of archivements/ratings on server side: replay saving routine
unc0rr
parents:
diff
changeset
|
5 |
import Control.Exception as E |
649d87819682
Start implementation of archivements/ratings on server side: replay saving routine
unc0rr
parents:
diff
changeset
|
6 |
import qualified Data.Map as Map |
649d87819682
Start implementation of archivements/ratings on server side: replay saving routine
unc0rr
parents:
diff
changeset
|
7 |
import Data.Sequence() |
649d87819682
Start implementation of archivements/ratings on server side: replay saving routine
unc0rr
parents:
diff
changeset
|
8 |
import System.Log.Logger |
5996
2c72fe81dd37
Convert boolean variable + a bunch of fields which make sense only while game is going on into Maybe + structure
unc0rr
parents:
5143
diff
changeset
|
9 |
import Data.Maybe |
6040 | 10 |
import Data.Unique |
11 |
import Control.Monad |
|
8479
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8423
diff
changeset
|
12 |
import Data.List |
8482 | 13 |
import qualified Data.ByteString as B |
14 |
import System.Directory |
|
6040 | 15 |
--------------- |
16 |
import CoreTypes |
|
8479
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8423
diff
changeset
|
17 |
import EngineInteraction |
6040 | 18 |
|
5143
649d87819682
Start implementation of archivements/ratings on server side: replay saving routine
unc0rr
parents:
diff
changeset
|
19 |
|
649d87819682
Start implementation of archivements/ratings on server side: replay saving routine
unc0rr
parents:
diff
changeset
|
20 |
saveReplay :: RoomInfo -> IO () |
8423 | 21 |
saveReplay r = do |
5996
2c72fe81dd37
Convert boolean variable + a bunch of fields which make sense only while game is going on into Maybe + structure
unc0rr
parents:
5143
diff
changeset
|
22 |
let gi = fromJust $ gameInfo r |
8423 | 23 |
when (allPlayersHaveRegisteredAccounts gi) $ do |
24 |
time <- getCurrentTime |
|
25 |
u <- liftM hashUnique newUnique |
|
8479
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8423
diff
changeset
|
26 |
let fileName = "replays/" ++ show time ++ "-" ++ show u ++ "." ++ show (roomProto r) |
8423 | 27 |
let replayInfo = (teamsAtStart gi, Map.toList $ mapParams r, Map.toList $ params r, roundMsgs gi) |
28 |
E.catch |
|
29 |
(writeFile fileName (show replayInfo)) |
|
30 |
(\(e :: IOException) -> warningM "REPLAYS" $ "Couldn't write to " ++ fileName ++ ": " ++ show e) |
|
8479
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8423
diff
changeset
|
31 |
|
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8423
diff
changeset
|
32 |
|
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8423
diff
changeset
|
33 |
loadReplay :: Int -> IO [B.ByteString] |
8482 | 34 |
loadReplay p = E.handle (\(e :: SomeException) -> warningM "REPLAYS" "Problems reading replay" >> return []) $ do |
35 |
files <- liftM (filter (isSuffixOf ('.' : show p))) $ getDirectoryContents "replays" |
|
8479
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8423
diff
changeset
|
36 |
if (not $ null files) then |
8495 | 37 |
loadFile $ "replays/" ++ head files |
8479
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8423
diff
changeset
|
38 |
else |
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8423
diff
changeset
|
39 |
return [] |
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8423
diff
changeset
|
40 |
where |
8482 | 41 |
loadFile :: String -> IO [B.ByteString] |
8495 | 42 |
loadFile fileName = E.handle (\(e :: SomeException) -> warningM "REPLAYS" ("Problems reading " ++ fileName ++ ": " ++ show e) >> return []) $ do |
8479
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8423
diff
changeset
|
43 |
(teams, params1, params2, roundMsgs) <- liftM read $ readFile fileName |
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8423
diff
changeset
|
44 |
return $ replayToDemo teams (Map.fromList params1) (Map.fromList params2) roundMsgs |