author | unc0rr |
Sun, 29 Dec 2013 15:08:52 +0400 | |
changeset 9878 | 163944282710 |
parent 9868 | 53d1b92db6ce |
child 9884 | 6e09ca662fa3 |
permissions | -rw-r--r-- |
4906 | 1 |
{-# LANGUAGE ScopedTypeVariables, OverloadedStrings #-} |
2348 | 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 | 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 |
9409 | 12 |
import Data.List (lookup) |
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 |
9409 | 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 | 19 |
dbQueryAccount = |
9435 | 20 |
"SELECT users.pass, \ |
21 |
\ (SELECT COUNT(users_roles.rid) FROM users_roles WHERE users.uid = users_roles.uid AND users_roles.rid = 3), \ |
|
22 |
\ (SELECT COUNT(users_roles.rid) FROM users_roles WHERE users.uid = users_roles.uid AND users_roles.rid = 13) \ |
|
23 |
\ FROM users WHERE users.name = ?" |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
24 |
|
2172 | 25 |
dbQueryStats = |
6040 | 26 |
"INSERT INTO gameserver_stats (players, rooms, last_update) VALUES (?, ?, UNIX_TIMESTAMP())" |
2172 | 27 |
|
9409 | 28 |
dbQueryAchievement = |
9868 | 29 |
"INSERT INTO achievements (time, typeid, userid, value, filename, location, protocol) \ |
9427 | 30 |
\ VALUES (?, (SELECT id FROM achievement_types WHERE name = ?), (SELECT uid FROM users WHERE name = ?), \ |
9868 | 31 |
\ ?, ?, ?, ?)" |
9409 | 32 |
|
9446 | 33 |
dbQueryReplayFilename = "SELECT filename FROM achievements WHERE id = ?" |
34 |
||
35 |
||
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
36 |
dbInteractionLoop dbConn = forever $ do |
4932 | 37 |
q <- liftM read getLine |
2869 | 38 |
hPutStrLn stderr $ show q |
4921 | 39 |
|
2869 | 40 |
case q of |
4921 | 41 |
CheckAccount clId clUid clNick _ -> do |
2869 | 42 |
statement <- prepare dbConn dbQueryAccount |
4932 | 43 |
execute statement [SqlByteString clNick] |
9435 | 44 |
result <- fetchRow statement |
2869 | 45 |
finish statement |
8924 | 46 |
let response = |
9437 | 47 |
if isJust result then let [pass, adm, contr] = fromJust result in |
2869 | 48 |
( |
4921 | 49 |
clId, |
2869 | 50 |
clUid, |
51 |
HasAccount |
|
9435 | 52 |
(fromSql pass) |
53 |
(fromSql adm == Just (1 :: Int)) |
|
54 |
(fromSql contr == Just (1 :: Int)) |
|
2869 | 55 |
) |
2919 | 56 |
else |
4921 | 57 |
(clId, clUid, Guest) |
4932 | 58 |
print response |
2869 | 59 |
hFlush stdout |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
60 |
|
9446 | 61 |
GetReplayName clId clUid fileId -> do |
62 |
statement <- prepare dbConn dbQueryReplayFilename |
|
63 |
execute statement [SqlByteString fileId] |
|
64 |
result <- fetchRow statement |
|
65 |
finish statement |
|
9450 | 66 |
let fn = if (isJust result) then fromJust . fromSql . head . fromJust $ result else "" |
9446 | 67 |
print (clId, clUid, ReplayName fn) |
68 |
hFlush stdout |
|
69 |
||
2869 | 70 |
SendStats clients rooms -> |
71 |
run dbConn dbQueryStats [SqlInt32 $ fromIntegral clients, SqlInt32 $ fromIntegral rooms] >> return () |
|
9409 | 72 |
--StoreAchievements (B.pack fileName) (map toPair teams) info |
9868 | 73 |
StoreAchievements p fileName teams info -> |
74 |
mapM_ (run dbConn dbQueryAchievement) $ (parseStats p fileName teams) info |
|
2172 | 75 |
|
9446 | 76 |
|
9425
49eb707b9367
Extract time from file name, assuming it is stored in 'replay' folder
unc0rr
parents:
9421
diff
changeset
|
77 |
readTime = read . B.unpack . B.take 19 . B.drop 8 |
49eb707b9367
Extract time from file name, assuming it is stored in 'replay' folder
unc0rr
parents:
9421
diff
changeset
|
78 |
|
9446 | 79 |
|
9868 | 80 |
parseStats :: Word16 -> B.ByteString -> [(B.ByteString, B.ByteString)] -> [B.ByteString] -> [[SqlValue]] |
81 |
parseStats p fileName teams = ps |
|
9409 | 82 |
where |
9425
49eb707b9367
Extract time from file name, assuming it is stored in 'replay' folder
unc0rr
parents:
9421
diff
changeset
|
83 |
time = readTime fileName |
9421 | 84 |
ps [] = [] |
9409 | 85 |
ps ("DRAW" : bs) = ps bs |
86 |
ps ("WINNERS" : n : bs) = ps $ drop (readInt_ n) bs |
|
87 |
ps ("ACHIEVEMENT" : typ : teamname : location : value : bs) = |
|
9425
49eb707b9367
Extract time from file name, assuming it is stored in 'replay' folder
unc0rr
parents:
9421
diff
changeset
|
88 |
[ SqlUTCTime time |
49eb707b9367
Extract time from file name, assuming it is stored in 'replay' folder
unc0rr
parents:
9421
diff
changeset
|
89 |
, SqlByteString typ |
9409 | 90 |
, SqlByteString $ fromMaybe "" (lookup teamname teams) |
91 |
, SqlInt32 (readInt_ value) |
|
92 |
, SqlByteString fileName |
|
93 |
, SqlByteString location |
|
9868 | 94 |
, SqlInt32 p |
9409 | 95 |
] : ps bs |
9421 | 96 |
ps (b:bs) = ps bs |
97 |
||
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
98 |
|
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
99 |
dbConnectionLoop mySQLConnectionInfo = |
4906 | 100 |
Control.Exception.handle (\(e :: IOException) -> hPutStrLn stderr $ show e) $ handleSqlError $ |
2869 | 101 |
bracket |
102 |
(connectMySQL mySQLConnectionInfo) |
|
4932 | 103 |
disconnect |
104 |
dbInteractionLoop |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
105 |
|
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
106 |
|
4921 | 107 |
--processRequest :: DBQuery -> IO String |
108 |
--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
|
109 |
|
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
110 |
main = do |
2869 | 111 |
dbHost <- getLine |
4982
3572eaf14340
Add dbName parameter to .ini file, fix some warnings
unc0rr
parents:
4932
diff
changeset
|
112 |
dbName <- getLine |
2869 | 113 |
dbLogin <- getLine |
114 |
dbPassword <- getLine |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
115 |
|
4982
3572eaf14340
Add dbName parameter to .ini file, fix some warnings
unc0rr
parents:
4932
diff
changeset
|
116 |
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
|
117 |
|
2869 | 118 |
dbConnectionLoop mySQLConnectInfo |