author | nemo |
Mon, 09 Dec 2013 10:59:38 -0500 | |
changeset 9769 | 5814e0c47c99 |
parent 9450 | 2084b1b7839c |
child 9868 | 53d1b92db6ce |
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 |
||
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 |
73 |
StoreAchievements fileName teams info -> |
|
74 |
mapM_ (run dbConn dbQueryAchievement) $ (parseStats 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 |
|
9409 | 80 |
parseStats :: B.ByteString -> [(B.ByteString, B.ByteString)] -> [B.ByteString] -> [[SqlValue]] |
81 |
parseStats fileName teams = ps |
|
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 |
|
94 |
] : ps bs |
|
9421 | 95 |
ps (b:bs) = ps bs |
96 |
||
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
97 |
|
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
98 |
dbConnectionLoop mySQLConnectionInfo = |
4906 | 99 |
Control.Exception.handle (\(e :: IOException) -> hPutStrLn stderr $ show e) $ handleSqlError $ |
2869 | 100 |
bracket |
101 |
(connectMySQL mySQLConnectionInfo) |
|
4932 | 102 |
disconnect |
103 |
dbInteractionLoop |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
104 |
|
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
105 |
|
4921 | 106 |
--processRequest :: DBQuery -> IO String |
107 |
--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
|
108 |
|
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
109 |
main = do |
2869 | 110 |
dbHost <- getLine |
4982
3572eaf14340
Add dbName parameter to .ini file, fix some warnings
unc0rr
parents:
4932
diff
changeset
|
111 |
dbName <- getLine |
2869 | 112 |
dbLogin <- getLine |
113 |
dbPassword <- getLine |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
114 |
|
4982
3572eaf14340
Add dbName parameter to .ini file, fix some warnings
unc0rr
parents:
4932
diff
changeset
|
115 |
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
|
116 |
|
2869 | 117 |
dbConnectionLoop mySQLConnectInfo |