author | Periklis Ntanasis <pntanasis@gmail.com> |
Wed, 31 Jul 2013 23:08:08 +0300 | |
branch | spacecampaign |
changeset 9424 | 9a150b7862e9 |
parent 8511 | 4f899fbce66d |
child 9662 | 47dbd9601342 |
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 |
|
8509 | 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 |
||
5143
649d87819682
Start implementation of archivements/ratings on server side: replay saving routine
unc0rr
parents:
diff
changeset
|
28 |
saveReplay :: RoomInfo -> IO () |
8423 | 29 |
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
|
30 |
let gi = fromJust $ gameInfo r |
8423 | 31 |
when (allPlayersHaveRegisteredAccounts gi) $ do |
32 |
time <- getCurrentTime |
|
33 |
u <- liftM hashUnique newUnique |
|
8479
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8423
diff
changeset
|
34 |
let fileName = "replays/" ++ show time ++ "-" ++ show u ++ "." ++ show (roomProto r) |
8423 | 35 |
let replayInfo = (teamsAtStart gi, Map.toList $ mapParams r, Map.toList $ params r, roundMsgs gi) |
36 |
E.catch |
|
37 |
(writeFile fileName (show replayInfo)) |
|
38 |
(\(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
|
39 |
|
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8423
diff
changeset
|
40 |
|
8509 | 41 |
loadReplay :: Int -> IO (Maybe CheckInfo, [B.ByteString]) |
42 |
loadReplay p = E.handle (\(e :: SomeException) -> warningM "REPLAYS" "Problems reading replay" >> return (Nothing, [])) $ do |
|
43 |
fileName <- pickReplayFile p |
|
44 |
if (not $ null fileName) then |
|
45 |
loadFile fileName |
|
8479
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8423
diff
changeset
|
46 |
else |
8509 | 47 |
return (Nothing, []) |
8479
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8423
diff
changeset
|
48 |
where |
8509 | 49 |
loadFile :: String -> IO (Maybe CheckInfo, [B.ByteString]) |
50 |
loadFile fileName = E.handle (\(e :: SomeException) -> |
|
51 |
warningM "REPLAYS" ("Problems reading " ++ fileName ++ ": " ++ show e) >> return (Nothing, [])) $ do |
|
8479
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8423
diff
changeset
|
52 |
(teams, params1, params2, roundMsgs) <- liftM read $ readFile fileName |
8509 | 53 |
return $ ( |
54 |
Just (CheckInfo fileName teams) |
|
55 |
, replayToDemo teams (Map.fromList params1) (Map.fromList params2) (reverse roundMsgs) |
|
56 |
) |
|
57 |
||
58 |
moveFailedRecord :: String -> IO () |
|
8511 | 59 |
moveFailedRecord fn = E.handle (\(e :: SomeException) -> warningM "REPLAYS" $ show e) $ |
60 |
renameFile fn ("failed/" ++ drop 8 fn) |
|
8509 | 61 |
|
62 |
||
63 |
moveCheckedRecord :: String -> IO () |
|
8511 | 64 |
moveCheckedRecord fn = E.handle (\(e :: SomeException) -> warningM "REPLAYS" $ show e) $ |
65 |
renameFile fn ("checked/" ++ drop 8 fn) |