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