author | unc0rr |
Wed, 28 Aug 2013 23:44:27 +0400 | |
changeset 9435 | 59eec19cb31a |
parent 9427 | c6c7e68de2a4 |
child 9437 | 8d1e9a9dda8e |
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 = |
9425
49eb707b9367
Extract time from file name, assuming it is stored in 'replay' folder
unc0rr
parents:
9421
diff
changeset
|
29 |
"INSERT INTO achievements (time, typeid, userid, value, filename, location) \ |
9427 | 30 |
\ VALUES (?, (SELECT id FROM achievement_types WHERE name = ?), (SELECT uid FROM users WHERE name = ?), \ |
9409 | 31 |
\ ?, ?, ?)" |
32 |
||
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
33 |
dbInteractionLoop dbConn = forever $ do |
4932 | 34 |
q <- liftM read getLine |
2869 | 35 |
hPutStrLn stderr $ show q |
4921 | 36 |
|
2869 | 37 |
case q of |
4921 | 38 |
CheckAccount clId clUid clNick _ -> do |
2869 | 39 |
statement <- prepare dbConn dbQueryAccount |
4932 | 40 |
execute statement [SqlByteString clNick] |
9435 | 41 |
result <- fetchRow statement |
2869 | 42 |
finish statement |
8924 | 43 |
let response = |
9435 | 44 |
if isJust result then let [pass, adm, contr] = fromJust $ result |
2869 | 45 |
( |
4921 | 46 |
clId, |
2869 | 47 |
clUid, |
48 |
HasAccount |
|
9435 | 49 |
(fromSql pass) |
50 |
(fromSql adm == Just (1 :: Int)) |
|
51 |
(fromSql contr == Just (1 :: Int)) |
|
2869 | 52 |
) |
2919 | 53 |
else |
4921 | 54 |
(clId, clUid, Guest) |
4932 | 55 |
print response |
2869 | 56 |
hFlush stdout |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
57 |
|
2869 | 58 |
SendStats clients rooms -> |
59 |
run dbConn dbQueryStats [SqlInt32 $ fromIntegral clients, SqlInt32 $ fromIntegral rooms] >> return () |
|
9409 | 60 |
--StoreAchievements (B.pack fileName) (map toPair teams) info |
61 |
StoreAchievements fileName teams info -> |
|
62 |
mapM_ (run dbConn dbQueryAchievement) $ (parseStats fileName teams) info |
|
2172 | 63 |
|
9425
49eb707b9367
Extract time from file name, assuming it is stored in 'replay' folder
unc0rr
parents:
9421
diff
changeset
|
64 |
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
|
65 |
|
9409 | 66 |
parseStats :: B.ByteString -> [(B.ByteString, B.ByteString)] -> [B.ByteString] -> [[SqlValue]] |
67 |
parseStats fileName teams = ps |
|
68 |
where |
|
9425
49eb707b9367
Extract time from file name, assuming it is stored in 'replay' folder
unc0rr
parents:
9421
diff
changeset
|
69 |
time = readTime fileName |
9421 | 70 |
ps [] = [] |
9409 | 71 |
ps ("DRAW" : bs) = ps bs |
72 |
ps ("WINNERS" : n : bs) = ps $ drop (readInt_ n) bs |
|
73 |
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
|
74 |
[ SqlUTCTime time |
49eb707b9367
Extract time from file name, assuming it is stored in 'replay' folder
unc0rr
parents:
9421
diff
changeset
|
75 |
, SqlByteString typ |
9409 | 76 |
, SqlByteString $ fromMaybe "" (lookup teamname teams) |
77 |
, SqlInt32 (readInt_ value) |
|
78 |
, SqlByteString fileName |
|
79 |
, SqlByteString location |
|
80 |
] : ps bs |
|
9421 | 81 |
ps (b:bs) = ps bs |
82 |
||
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
83 |
|
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
84 |
dbConnectionLoop mySQLConnectionInfo = |
4906 | 85 |
Control.Exception.handle (\(e :: IOException) -> hPutStrLn stderr $ show e) $ handleSqlError $ |
2869 | 86 |
bracket |
87 |
(connectMySQL mySQLConnectionInfo) |
|
4932 | 88 |
disconnect |
89 |
dbInteractionLoop |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
90 |
|
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
91 |
|
4921 | 92 |
--processRequest :: DBQuery -> IO String |
93 |
--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
|
94 |
|
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
95 |
main = do |
2869 | 96 |
dbHost <- getLine |
4982
3572eaf14340
Add dbName parameter to .ini file, fix some warnings
unc0rr
parents:
4932
diff
changeset
|
97 |
dbName <- getLine |
2869 | 98 |
dbLogin <- getLine |
99 |
dbPassword <- getLine |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
100 |
|
4982
3572eaf14340
Add dbName parameter to .ini file, fix some warnings
unc0rr
parents:
4932
diff
changeset
|
101 |
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
|
102 |
|
2869 | 103 |
dbConnectionLoop mySQLConnectInfo |