gameServer/OfficialServer/DBInteraction.hs
author unc0rr
Mon, 23 Feb 2009 20:34:29 +0000
changeset 1834 71cb978dc85f
parent 1833 e901ec5644b4
child 1839 5dd4cb7fd7e5
permissions -rw-r--r--
Add working check for www account existance
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     1
module OfficialServer.DBInteraction
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     2
(
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     3
	startDBConnection,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     4
	DBQuery(HasRegistered, CheckPassword)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     5
) where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     6
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     7
import Database.HDBC
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     8
import Database.HDBC.MySQL
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     9
import System.IO
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    10
import Control.Concurrent
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    11
import Control.Exception
1833
e901ec5644b4 Add options for configuring database access
unc0rr
parents: 1804
diff changeset
    12
import Monad
1834
71cb978dc85f Add working check for www account existance
unc0rr
parents: 1833
diff changeset
    13
import Maybe
1833
e901ec5644b4 Add options for configuring database access
unc0rr
parents: 1804
diff changeset
    14
------------------------
e901ec5644b4 Add options for configuring database access
unc0rr
parents: 1804
diff changeset
    15
import CoreTypes
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    16
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    17
dbInteractionLoop queries dbConn = do
1833
e901ec5644b4 Add options for configuring database access
unc0rr
parents: 1804
diff changeset
    18
	q <- readChan queries
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    19
	case q of
1834
71cb978dc85f Add working check for www account existance
unc0rr
parents: 1833
diff changeset
    20
		HasRegistered name -> do
71cb978dc85f Add working check for www account existance
unc0rr
parents: 1833
diff changeset
    21
			statement <- prepare dbConn "SELECT uid FROM users WHERE name=?"
71cb978dc85f Add working check for www account existance
unc0rr
parents: 1833
diff changeset
    22
			execute statement [SqlString name]
71cb978dc85f Add working check for www account existance
unc0rr
parents: 1833
diff changeset
    23
			uid <- fetchRow statement
71cb978dc85f Add working check for www account existance
unc0rr
parents: 1833
diff changeset
    24
			finish statement
71cb978dc85f Add working check for www account existance
unc0rr
parents: 1833
diff changeset
    25
			putStrLn (show $ isJust uid)
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    26
		CheckPassword queryStr -> putStrLn queryStr
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    27
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    28
	dbInteractionLoop queries dbConn
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    29
1833
e901ec5644b4 Add options for configuring database access
unc0rr
parents: 1804
diff changeset
    30
dbConnectionLoop serverInfo = do
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    31
	Control.Exception.handle (\e -> print e) $ handleSqlError $
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    32
		bracket
1833
e901ec5644b4 Add options for configuring database access
unc0rr
parents: 1804
diff changeset
    33
			(connectMySQL defaultMySQLConnectInfo {mysqlHost = dbHost serverInfo, mysqlDatabase = "hedge_main", mysqlUser = dbLogin serverInfo, mysqlPassword = dbPassword serverInfo })
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    34
			(disconnect)
1833
e901ec5644b4 Add options for configuring database access
unc0rr
parents: 1804
diff changeset
    35
			(dbInteractionLoop $ dbQueries serverInfo)
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    36
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    37
	threadDelay (15 * 10^6)
1833
e901ec5644b4 Add options for configuring database access
unc0rr
parents: 1804
diff changeset
    38
	dbConnectionLoop serverInfo
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    39
1833
e901ec5644b4 Add options for configuring database access
unc0rr
parents: 1804
diff changeset
    40
startDBConnection serverInfo =
e901ec5644b4 Add options for configuring database access
unc0rr
parents: 1804
diff changeset
    41
	when (not . null $ dbHost serverInfo) ((forkIO $ dbConnectionLoop serverInfo) >> return ())