author | nemo |
Fri, 16 Apr 2010 15:37:04 +0000 | |
changeset 3348 | 5f3b423babcb |
parent 2919 | 70244c730ea0 |
child 3901 | 124b4755914b |
permissions | -rw-r--r-- |
2348 | 1 |
{-# LANGUAGE ScopedTypeVariables #-} |
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 |
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
9 |
import Maybe |
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 |
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
12 |
-------------------------- |
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
13 |
import CoreTypes |
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 |
|
2172 | 16 |
dbQueryAccount = |
2869 | 17 |
"SELECT users.pass, users_roles.rid FROM users LEFT JOIN users_roles ON users.uid = users_roles.uid WHERE users.name = ?" |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
18 |
|
2172 | 19 |
dbQueryStats = |
2869 | 20 |
"UPDATE gameserver_stats SET players = ?, rooms = ?, last_update = UNIX_TIMESTAMP()" |
2172 | 21 |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
22 |
dbInteractionLoop dbConn = forever $ do |
2869 | 23 |
q <- (getLine >>= return . read) |
24 |
hPutStrLn stderr $ show q |
|
25 |
||
26 |
case q of |
|
27 |
CheckAccount clUid clNick _ -> do |
|
28 |
statement <- prepare dbConn dbQueryAccount |
|
29 |
execute statement [SqlString $ clNick] |
|
30 |
passAndRole <- fetchRow statement |
|
31 |
finish statement |
|
2919 | 32 |
let response = |
33 |
if isJust passAndRole then |
|
2869 | 34 |
( |
35 |
clUid, |
|
36 |
HasAccount |
|
37 |
(fromSql $ head $ fromJust $ passAndRole) |
|
38 |
((fromSql $ last $ fromJust $ passAndRole) == (Just (3 :: Int))) |
|
39 |
) |
|
2919 | 40 |
else |
2869 | 41 |
(clUid, Guest) |
42 |
putStrLn (show response) |
|
43 |
hFlush stdout |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
44 |
|
2869 | 45 |
SendStats clients rooms -> |
46 |
run dbConn dbQueryStats [SqlInt32 $ fromIntegral clients, SqlInt32 $ fromIntegral rooms] >> return () |
|
2172 | 47 |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
48 |
|
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
49 |
dbConnectionLoop mySQLConnectionInfo = |
2869 | 50 |
Control.Exception.handle (\(_ :: IOException) -> return ()) $ handleSqlError $ |
51 |
bracket |
|
52 |
(connectMySQL mySQLConnectionInfo) |
|
53 |
(disconnect) |
|
54 |
(dbInteractionLoop) |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
55 |
|
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
56 |
|
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
57 |
processRequest :: DBQuery -> IO String |
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
58 |
processRequest (CheckAccount clUid clNick clHost) = return $ show (clUid, Guest) |
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
59 |
|
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
60 |
main = do |
2869 | 61 |
dbHost <- getLine |
62 |
dbLogin <- getLine |
|
63 |
dbPassword <- getLine |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
64 |
|
2869 | 65 |
let mySQLConnectInfo = defaultMySQLConnectInfo {mysqlHost = dbHost, mysqlDatabase = "hedge_main", mysqlUser = dbLogin, mysqlPassword = dbPassword} |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
diff
changeset
|
66 |
|
2869 | 67 |
dbConnectionLoop mySQLConnectInfo |