author | unc0rr |
Sat, 24 Aug 2013 10:07:34 +0400 | |
changeset 9421 | 90fe753b3654 |
parent 9409 | 6564baf7dedf |
child 9425 | 49eb707b9367 |
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 = |
8909 | 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 | 22 |
dbQueryStats = |
6040 | 23 |
"INSERT INTO gameserver_stats (players, rooms, last_update) VALUES (?, ?, UNIX_TIMESTAMP())" |
2172 | 24 |
|
9409 | 25 |
dbQueryAchievement = |
26 |
"INSERT INTO achievements (typeid, userid, value, filename, location) \ |
|
27 |
\ VALUES ((SELECT id FROM achievement_types WHERE name = ?), (SELECT uid FROM users WHERE name = ?), \ |
|
28 |
\ ?, ?, ?)" |
|
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 | 31 |
q <- liftM read getLine |
2869 | 32 |
hPutStrLn stderr $ show q |
4921 | 33 |
|
2869 | 34 |
case q of |
4921 | 35 |
CheckAccount clId clUid clNick _ -> do |
2869 | 36 |
statement <- prepare dbConn dbQueryAccount |
4932 | 37 |
execute statement [SqlByteString clNick] |
2869 | 38 |
passAndRole <- fetchRow statement |
39 |
finish statement |
|
8924 | 40 |
let response = |
2919 | 41 |
if isJust passAndRole then |
2869 | 42 |
( |
4921 | 43 |
clId, |
2869 | 44 |
clUid, |
45 |
HasAccount |
|
4932 | 46 |
(fromSql . head . fromJust $ passAndRole) |
47 |
(fromSql (last . fromJust $ passAndRole) == Just (3 :: Int)) |
|
2869 | 48 |
) |
2919 | 49 |
else |
4921 | 50 |
(clId, clUid, Guest) |
4932 | 51 |
print response |
2869 | 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 | 54 |
SendStats clients rooms -> |
55 |
run dbConn dbQueryStats [SqlInt32 $ fromIntegral clients, SqlInt32 $ fromIntegral rooms] >> return () |
|
9409 | 56 |
--StoreAchievements (B.pack fileName) (map toPair teams) info |
57 |
StoreAchievements fileName teams info -> |
|
58 |
mapM_ (run dbConn dbQueryAchievement) $ (parseStats fileName teams) info |
|
2172 | 59 |
|
9409 | 60 |
parseStats :: B.ByteString -> [(B.ByteString, B.ByteString)] -> [B.ByteString] -> [[SqlValue]] |
61 |
parseStats fileName teams = ps |
|
62 |
where |
|
9421 | 63 |
ps [] = [] |
9409 | 64 |
ps ("DRAW" : bs) = ps bs |
65 |
ps ("WINNERS" : n : bs) = ps $ drop (readInt_ n) bs |
|
66 |
ps ("ACHIEVEMENT" : typ : teamname : location : value : bs) = |
|
67 |
[SqlByteString typ |
|
68 |
, SqlByteString $ fromMaybe "" (lookup teamname teams) |
|
69 |
, SqlInt32 (readInt_ value) |
|
70 |
, SqlByteString fileName |
|
71 |
, SqlByteString location |
|
72 |
] : ps bs |
|
9421 | 73 |
ps (b:bs) = ps bs |
74 |
||
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
75 |
|
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
76 |
dbConnectionLoop mySQLConnectionInfo = |
4906 | 77 |
Control.Exception.handle (\(e :: IOException) -> hPutStrLn stderr $ show e) $ handleSqlError $ |
2869 | 78 |
bracket |
79 |
(connectMySQL mySQLConnectionInfo) |
|
4932 | 80 |
disconnect |
81 |
dbInteractionLoop |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
82 |
|
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
83 |
|
4921 | 84 |
--processRequest :: DBQuery -> IO String |
85 |
--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
|
86 |
|
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
87 |
main = do |
2869 | 88 |
dbHost <- getLine |
4982
3572eaf14340
Add dbName parameter to .ini file, fix some warnings
unc0rr
parents:
4932
diff
changeset
|
89 |
dbName <- getLine |
2869 | 90 |
dbLogin <- getLine |
91 |
dbPassword <- getLine |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
92 |
|
4982
3572eaf14340
Add dbName parameter to .ini file, fix some warnings
unc0rr
parents:
4932
diff
changeset
|
93 |
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
|
94 |
|
2869 | 95 |
dbConnectionLoop mySQLConnectInfo |