11380
|
1 |
{-# LANGUAGE ScopedTypeVariables, OverloadedStrings #-}
|
11358
|
2 |
module Main where
|
|
3 |
|
11380
|
4 |
import Data.Maybe
|
|
5 |
import Data.TConfig
|
|
6 |
import qualified Data.ByteString.Char8 as B
|
|
7 |
import Database.MySQL.Simple
|
|
8 |
import Database.MySQL.Simple.QueryResults
|
|
9 |
import Database.MySQL.Simple.Result
|
|
10 |
import Control.Monad
|
|
11 |
import Control.Exception
|
|
12 |
import System.IO
|
|
13 |
import qualified Data.Map as Map
|
|
14 |
------
|
|
15 |
import OfficialServer.Glicko2
|
|
16 |
|
11358
|
17 |
|
11380
|
18 |
queryEpochDates = "SELECT epoch, todatetime, todatetime + INTERVAL 1 week FROM rating_epochs WHERE epoch = (SELECT MAX(epoch) FROM rating_epochs)"
|
|
19 |
queryPreviousRatings = "SELECT v.userid, v.rating, v.rd, v.volatility FROM rating_values as v WHERE (v.epoch = (SELECT MAX(epoch) FROM rating_epochs))"
|
|
20 |
queryGameResults =
|
|
21 |
"SELECT \
|
|
22 |
\ p.userid \
|
|
23 |
\ , p.place \
|
|
24 |
\ , o.userid \
|
|
25 |
\ , o.place \
|
|
26 |
\ , COALESCE(vp.rating, 1500) \
|
|
27 |
\ , COALESCE(vp.rd, 350) \
|
|
28 |
\ , COALESCE(vp.volatility, 0.06) \
|
|
29 |
\ , COALESCE(vo.rating, 1500) \
|
|
30 |
\ , COALESCE(vo.rd, 350) \
|
|
31 |
\ , COALESCE(vo.volatility, 0.06) \
|
|
32 |
\ FROM \
|
|
33 |
\ (SELECT epoch, todatetime FROM rating_epochs WHERE epoch = (SELECT MAX(epoch) FROM rating_epochs)) as e \
|
|
34 |
\ JOIN rating_games as g ON (g.time BETWEEN e.todatetime AND e.todatetime + INTERVAL 1 WEEK - INTERVAL 1 SECOND) \
|
|
35 |
\ JOIN rating_players as p ON (p.gameid = g.id) \
|
|
36 |
\ JOIN rating_players as o ON (p.gameid = o.gameid AND p.userid <> o.userid AND (p.place = 0 OR (p.place <> o.place))) \
|
|
37 |
\ LEFT OUTER JOIN rating_values as vp ON (vp.epoch = e.epoch AND vp.userid = p.userid) \
|
|
38 |
\ LEFT OUTER JOIN rating_values as vo ON (vo.epoch = e.epoch AND vo.userid = o.userid) \
|
|
39 |
\ GROUP BY p.userid, p.gameid, p.place \
|
|
40 |
\ ORDER BY p.userid"
|
|
41 |
|
|
42 |
--Map Int (RatingData, [GameData])
|
|
43 |
calculateRatings dbConn = do
|
|
44 |
initRatingData <- (Map.fromList . map fromDBrating) `fmap` query_ dbConn queryPreviousRatings
|
|
45 |
return ()
|
|
46 |
|
|
47 |
where
|
|
48 |
fromDBrating (a, b, c, d) = (a, (RatingData b c d, []))
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
data DBConnectInfo = DBConnectInfo {
|
|
53 |
dbHost
|
|
54 |
, dbName
|
|
55 |
, dbLogin
|
|
56 |
, dbPassword :: B.ByteString
|
11358
|
57 |
}
|
|
58 |
|
11380
|
59 |
cfgFileName :: String
|
|
60 |
cfgFileName = "hedgewars-server.ini"
|
11358
|
61 |
|
|
62 |
|
11380
|
63 |
readServerConfig :: ConnectInfo -> IO ConnectInfo
|
|
64 |
readServerConfig ci = do
|
|
65 |
cfg <- readConfig cfgFileName
|
|
66 |
return $ ci{
|
|
67 |
connectHost = value "dbHost" cfg
|
|
68 |
, connectDatabase = value "dbName" cfg
|
|
69 |
, connectUser = value "dbLogin" cfg
|
|
70 |
, connectPassword = value "dbPassword" cfg
|
|
71 |
}
|
11358
|
72 |
where
|
11380
|
73 |
value n c = fromJust2 n $ getValue n c
|
|
74 |
fromJust2 n Nothing = error $ "Missing config entry " ++ n
|
|
75 |
fromJust2 _ (Just a) = a
|
11358
|
76 |
|
11380
|
77 |
dbConnectionLoop mySQLConnectionInfo =
|
|
78 |
Control.Exception.handle (\(e :: SomeException) -> hPutStrLn stderr $ show e) $
|
|
79 |
bracket
|
|
80 |
(connect mySQLConnectionInfo)
|
|
81 |
close
|
|
82 |
calculateRatings
|
11358
|
83 |
|
11380
|
84 |
main = readServerConfig defaultConnectInfo >>= dbConnectionLoop
|