gameServer/OfficialServer/DBInteraction.hs
changeset 1921 2a09f7f786a0
parent 1857 b835395659e2
child 1963 58c29439225d
equal deleted inserted replaced
1920:302835d554d8 1921:2a09f7f786a0
    13 import Maybe
    13 import Maybe
    14 import System.Log.Logger
    14 import System.Log.Logger
    15 ------------------------
    15 ------------------------
    16 import CoreTypes
    16 import CoreTypes
    17 
    17 
       
    18 localAddressList = ["127.0.0.1", "0:0:0:0:0:0:0:1", "0:0:0:0:0:ffff:7f00:1"]
    18 
    19 
    19 fakeDbConnection serverInfo = do
    20 fakeDbConnection serverInfo = do
    20 	q <- readChan $ dbQueries serverInfo
    21 	q <- readChan $ dbQueries serverInfo
    21 	case q of
    22 	case q of
    22 		CheckAccount clID name -> do
    23 		CheckAccount client -> do
    23 			writeChan (coreChan serverInfo) $ ClientAccountInfo clID Guest
    24 			writeChan (coreChan serverInfo) $ ClientAccountInfo (clientUID client) $
       
    25 				if host client `elem` localAddressList then Admin else Guest
    24 
    26 
    25 	fakeDbConnection serverInfo
    27 	fakeDbConnection serverInfo
    26 
    28 
    27 
    29 
    28 -------------------------------------------------------------------
    30 -------------------------------------------------------------------
    38 	++ "WHERE users.name = ? AND users_roles.uid = users.uid"
    40 	++ "WHERE users.name = ? AND users_roles.uid = users.uid"
    39 
    41 
    40 dbInteractionLoop queries coreChan dbConn = do
    42 dbInteractionLoop queries coreChan dbConn = do
    41 	q <- readChan queries
    43 	q <- readChan queries
    42 	case q of
    44 	case q of
    43 		CheckAccount clID name -> do
    45 		CheckAccount client -> do
    44 				statement <- prepare dbConn dbQueryString
    46 				statement <- prepare dbConn dbQueryString
    45 				execute statement [SqlString name]
    47 				execute statement [SqlString $ nick client]
    46 				passAndRole <- fetchRow statement
    48 				passAndRole <- fetchRow statement
    47 				finish statement
    49 				finish statement
    48 				if isJust passAndRole then
    50 				if isJust passAndRole then
    49 					writeChan coreChan $
    51 					writeChan coreChan $
    50 							ClientAccountInfo clID $
    52 							ClientAccountInfo (clientUID client) $
    51 								HasAccount
    53 								HasAccount
    52 									(fromSql $ head $ fromJust $ passAndRole)
    54 									(fromSql $ head $ fromJust $ passAndRole)
    53 									((fromSql $ last $ fromJust $ passAndRole) == (3 :: Int))
    55 									((fromSql $ last $ fromJust $ passAndRole) == (3 :: Int))
    54 					else
    56 					else
    55 					writeChan coreChan $ ClientAccountInfo clID Guest
    57 					writeChan coreChan $ ClientAccountInfo (clientUID client) Guest
    56 			`onException`
    58 			`onException`
    57 				(unGetChan queries $ CheckAccount clID name)
    59 				(unGetChan queries q)
    58 
    60 
    59 	dbInteractionLoop queries coreChan dbConn
    61 	dbInteractionLoop queries coreChan dbConn
    60 
    62 
    61 dbConnectionLoop serverInfo = do
    63 dbConnectionLoop serverInfo = do
    62 	Control.Exception.handle (\e -> infoM "Database" $ show e) $ handleSqlError $
    64 	Control.Exception.handle (\e -> infoM "Database" $ show e) $ handleSqlError $