gameServer/OfficialServer/extdbinterface.hs
author unc0rr
Sat, 24 Aug 2013 00:13:15 +0400
branch0.9.19
changeset 9138 9e94a9bc8c7e
parent 8924 13ac59499066
permissions -rw-r--r--
Store TrophyRace records in database
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4906
22cc9c2b5ae5 Fix even more
unc0rr
parents: 4568
diff changeset
     1
{-# LANGUAGE ScopedTypeVariables, OverloadedStrings #-}
2348
b39d826e1ccd Drop support for ghc 6.8, use 6.10 instead
unc0rr
parents: 2195
diff changeset
     2
2116
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
     3
module Main where
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
     4
2117
1ac0e10e546f Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents: 2116
diff changeset
     5
import Prelude hiding (catch)
2116
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
     6
import Control.Monad
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
     7
import Control.Exception
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
     8
import System.IO
4932
f11d80bac7ed - Take into account hlint suggestions
unc0rr
parents: 4921
diff changeset
     9
import Data.Maybe
2116
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
    10
import Database.HDBC
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
    11
import Database.HDBC.MySQL
9138
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    12
import Data.List (lookup)
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    13
import qualified Data.ByteString.Char8 as B
2116
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
    14
--------------------------
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
    15
import CoreTypes
9138
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    16
import Utils
2116
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
    17
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
    18
2172
80d34c0b9dfe Implement sending gameserver stats to webserver
unc0rr
parents: 2117
diff changeset
    19
dbQueryAccount =
8909
95542e198bc8 Check for admin role right in the sql statemenet
unc0rr
parents: 6040
diff changeset
    20
    "SELECT users.pass, users_roles.rid FROM users LEFT JOIN users_roles ON (users.uid = users_roles.uid AND users_roles.rid = 3) WHERE users.name = ?"
2116
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
    21
2172
80d34c0b9dfe Implement sending gameserver stats to webserver
unc0rr
parents: 2117
diff changeset
    22
dbQueryStats =
6040
a740069c21e3 - Add unique id to replay file name
unc0rr
parents: 4982
diff changeset
    23
    "INSERT INTO gameserver_stats (players, rooms, last_update) VALUES (?, ?, UNIX_TIMESTAMP())"
2172
80d34c0b9dfe Implement sending gameserver stats to webserver
unc0rr
parents: 2117
diff changeset
    24
9138
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    25
dbQueryAchievement =
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    26
    "INSERT INTO achievements (typeid, userid, value, filename, location) \
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    27
    \ VALUES ((SELECT id FROM achievement_types WHERE name = ?), (SELECT uid FROM users WHERE name = ?), \
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    28
    \ ?, ?, ?)"
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    29
2116
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
    30
dbInteractionLoop dbConn = forever $ do
4932
f11d80bac7ed - Take into account hlint suggestions
unc0rr
parents: 4921
diff changeset
    31
    q <- liftM read getLine
2869
93cc73dcc421 Replace tabs with spaces here too
unc0rr
parents: 2348
diff changeset
    32
    hPutStrLn stderr $ show q
4921
2efad3acbb74 Fix build of official server
unc0rr
parents: 4906
diff changeset
    33
2869
93cc73dcc421 Replace tabs with spaces here too
unc0rr
parents: 2348
diff changeset
    34
    case q of
4921
2efad3acbb74 Fix build of official server
unc0rr
parents: 4906
diff changeset
    35
        CheckAccount clId clUid clNick _ -> do
2869
93cc73dcc421 Replace tabs with spaces here too
unc0rr
parents: 2348
diff changeset
    36
                statement <- prepare dbConn dbQueryAccount
4932
f11d80bac7ed - Take into account hlint suggestions
unc0rr
parents: 4921
diff changeset
    37
                execute statement [SqlByteString clNick]
2869
93cc73dcc421 Replace tabs with spaces here too
unc0rr
parents: 2348
diff changeset
    38
                passAndRole <- fetchRow statement
93cc73dcc421 Replace tabs with spaces here too
unc0rr
parents: 2348
diff changeset
    39
                finish statement
8924
13ac59499066 update 0.9.19 with dev branch
koda
parents: 8909
diff changeset
    40
                let response =
2919
70244c730ea0 Now really fix build
unc0rr
parents: 2918
diff changeset
    41
                        if isJust passAndRole then
2869
93cc73dcc421 Replace tabs with spaces here too
unc0rr
parents: 2348
diff changeset
    42
                        (
4921
2efad3acbb74 Fix build of official server
unc0rr
parents: 4906
diff changeset
    43
                            clId,
2869
93cc73dcc421 Replace tabs with spaces here too
unc0rr
parents: 2348
diff changeset
    44
                            clUid,
93cc73dcc421 Replace tabs with spaces here too
unc0rr
parents: 2348
diff changeset
    45
                            HasAccount
4932
f11d80bac7ed - Take into account hlint suggestions
unc0rr
parents: 4921
diff changeset
    46
                                (fromSql . head . fromJust $ passAndRole)
f11d80bac7ed - Take into account hlint suggestions
unc0rr
parents: 4921
diff changeset
    47
                                (fromSql (last . fromJust $ passAndRole) == Just (3 :: Int))
2869
93cc73dcc421 Replace tabs with spaces here too
unc0rr
parents: 2348
diff changeset
    48
                        )
2919
70244c730ea0 Now really fix build
unc0rr
parents: 2918
diff changeset
    49
                        else
4921
2efad3acbb74 Fix build of official server
unc0rr
parents: 4906
diff changeset
    50
                        (clId, clUid, Guest)
4932
f11d80bac7ed - Take into account hlint suggestions
unc0rr
parents: 4921
diff changeset
    51
                print response
2869
93cc73dcc421 Replace tabs with spaces here too
unc0rr
parents: 2348
diff changeset
    52
                hFlush stdout
2116
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
    53
2869
93cc73dcc421 Replace tabs with spaces here too
unc0rr
parents: 2348
diff changeset
    54
        SendStats clients rooms ->
93cc73dcc421 Replace tabs with spaces here too
unc0rr
parents: 2348
diff changeset
    55
                run dbConn dbQueryStats [SqlInt32 $ fromIntegral clients, SqlInt32 $ fromIntegral rooms] >> return ()
9138
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    56
--StoreAchievements (B.pack fileName) (map toPair teams) info
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    57
        StoreAchievements fileName teams info -> 
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    58
            mapM_ (run dbConn dbQueryAchievement) $ (parseStats fileName teams) info
2172
80d34c0b9dfe Implement sending gameserver stats to webserver
unc0rr
parents: 2117
diff changeset
    59
9138
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    60
<<<<<<< local
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    61
        StoreAchievements {} -> return ()
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    62
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    63
=======
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    64
parseStats :: B.ByteString -> [(B.ByteString, B.ByteString)] -> [B.ByteString] -> [[SqlValue]]
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    65
parseStats fileName teams = ps
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    66
    where
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    67
    ps ("DRAW" : bs) = ps bs
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    68
    ps ("WINNERS" : n : bs) = ps $ drop (readInt_ n) bs
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    69
    ps ("ACHIEVEMENT" : typ : teamname : location : value : bs) =
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    70
        [SqlByteString typ
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    71
        , SqlByteString $ fromMaybe "" (lookup teamname teams)
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    72
        , SqlInt32 (readInt_ value)
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    73
        , SqlByteString fileName
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    74
        , SqlByteString location
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    75
        ] : ps bs
9e94a9bc8c7e Store TrophyRace records in database
unc0rr
parents: 8924
diff changeset
    76
>>>>>>> other
2116
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
    77
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
    78
dbConnectionLoop mySQLConnectionInfo =
4906
22cc9c2b5ae5 Fix even more
unc0rr
parents: 4568
diff changeset
    79
    Control.Exception.handle (\(e :: IOException) -> hPutStrLn stderr $ show e) $ handleSqlError $
2869
93cc73dcc421 Replace tabs with spaces here too
unc0rr
parents: 2348
diff changeset
    80
        bracket
93cc73dcc421 Replace tabs with spaces here too
unc0rr
parents: 2348
diff changeset
    81
            (connectMySQL mySQLConnectionInfo)
4932
f11d80bac7ed - Take into account hlint suggestions
unc0rr
parents: 4921
diff changeset
    82
            disconnect
f11d80bac7ed - Take into account hlint suggestions
unc0rr
parents: 4921
diff changeset
    83
            dbInteractionLoop
2116
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
    84
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
    85
4921
2efad3acbb74 Fix build of official server
unc0rr
parents: 4906
diff changeset
    86
--processRequest :: DBQuery -> IO String
2efad3acbb74 Fix build of official server
unc0rr
parents: 4906
diff changeset
    87
--processRequest (CheckAccount clId clUid clNick clHost) = return $ show (clclId, clUid, Guest)
2116
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
    88
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
    89
main = do
2869
93cc73dcc421 Replace tabs with spaces here too
unc0rr
parents: 2348
diff changeset
    90
        dbHost <- getLine
4982
3572eaf14340 Add dbName parameter to .ini file, fix some warnings
unc0rr
parents: 4932
diff changeset
    91
        dbName <- getLine
2869
93cc73dcc421 Replace tabs with spaces here too
unc0rr
parents: 2348
diff changeset
    92
        dbLogin <- getLine
93cc73dcc421 Replace tabs with spaces here too
unc0rr
parents: 2348
diff changeset
    93
        dbPassword <- getLine
2116
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
    94
4982
3572eaf14340 Add dbName parameter to .ini file, fix some warnings
unc0rr
parents: 4932
diff changeset
    95
        let mySQLConnectInfo = defaultMySQLConnectInfo {mysqlHost = dbHost, mysqlDatabase = dbName, mysqlUser = dbLogin, mysqlPassword = dbPassword}
2116
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff changeset
    96
2869
93cc73dcc421 Replace tabs with spaces here too
unc0rr
parents: 2348
diff changeset
    97
        dbConnectionLoop mySQLConnectInfo