gameServer/ServerCore.hs
author nemo
Sun, 21 Jun 2009 22:43:38 +0000
changeset 2185 cf8f98e75bf9
parent 2184 f59f80e034b1
child 2349 ba7a0813c532
permissions -rw-r--r--
Localise words "Fuel" and "Remaining" - add shot counter to deagle.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     1
module ServerCore where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     2
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     3
import Network
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     4
import Control.Concurrent
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     5
import Control.Concurrent.STM
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     6
import Control.Concurrent.Chan
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     7
import Control.Monad
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     8
import qualified Data.IntMap as IntMap
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     9
import System.Log.Logger
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    10
--------------------------------------
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    11
import CoreTypes
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    12
import NetRoutines
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    13
import Utils
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    14
import HWProtoCore
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    15
import Actions
1833
e901ec5644b4 Add options for configuring database access
unc0rr
parents: 1804
diff changeset
    16
import OfficialServer.DBInteraction
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    17
1839
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1833
diff changeset
    18
2173
98cde8645e21 Send stats every minute
unc0rr
parents: 2172
diff changeset
    19
timerLoop :: Int -> Chan CoreMessage -> IO()
2184
f59f80e034b1 Oops, fix database process interaction
unc0rr
parents: 2173
diff changeset
    20
timerLoop tick messagesChan = threadDelay (30 * 10^6) >> (writeChan messagesChan $ TimerAction tick) >> timerLoop (tick + 1) messagesChan
1927
e2031906a347 Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents: 1926
diff changeset
    21
1839
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1833
diff changeset
    22
firstAway (_, a, b, c) = (a, b, c)
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1833
diff changeset
    23
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    24
reactCmd :: ServerInfo -> Int -> [String] -> Clients -> Rooms -> IO (ServerInfo, Clients, Rooms)
1841
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
    25
reactCmd serverInfo clID cmd clients rooms =
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
    26
	liftM firstAway $ foldM processAction (clID, serverInfo, clients, rooms) $ handleCmd clID clients rooms cmd
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    27
1839
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1833
diff changeset
    28
mainLoop :: ServerInfo -> Clients -> Rooms -> IO ()
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1833
diff changeset
    29
mainLoop serverInfo clients rooms = do
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1833
diff changeset
    30
	r <- readChan $ coreChan serverInfo
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    31
	
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    32
	(newServerInfo, mClients, mRooms) <-
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    33
		case r of
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    34
			Accept ci -> do
1841
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
    35
				liftM firstAway $ processAction
1926
cb46fbdcaa41 Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents: 1841
diff changeset
    36
					(clientUID ci, serverInfo, clients, rooms) (AddClient ci)
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    37
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    38
			ClientMessage (clID, cmd) -> do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    39
				debugM "Clients" $ (show clID) ++ ": " ++ (show cmd)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    40
				if clID `IntMap.member` clients then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    41
					reactCmd serverInfo clID cmd clients rooms
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    42
					else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    43
					do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    44
					debugM "Clients" "Message from dead client"
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    45
					return (serverInfo, clients, rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    46
2116
dec7ead2d178 Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents: 1927
diff changeset
    47
			ClientAccountInfo (clID, info) ->
1839
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1833
diff changeset
    48
				if clID `IntMap.member` clients then
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1833
diff changeset
    49
					liftM firstAway $ processAction
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1833
diff changeset
    50
						(clID, serverInfo, clients, rooms)
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1833
diff changeset
    51
						(ProcessAccountInfo info)
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1833
diff changeset
    52
					else
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1833
diff changeset
    53
					do
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1833
diff changeset
    54
					debugM "Clients" "Got info for dead client"
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1833
diff changeset
    55
					return (serverInfo, clients, rooms)
1927
e2031906a347 Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents: 1926
diff changeset
    56
2173
98cde8645e21 Send stats every minute
unc0rr
parents: 2172
diff changeset
    57
			TimerAction tick ->
2172
80d34c0b9dfe Implement sending gameserver stats to webserver
unc0rr
parents: 2116
diff changeset
    58
				liftM firstAway $
2173
98cde8645e21 Send stats every minute
unc0rr
parents: 2172
diff changeset
    59
					foldM processAction (0, serverInfo, clients, rooms) $
98cde8645e21 Send stats every minute
unc0rr
parents: 2172
diff changeset
    60
						PingAll : if even tick then [StatsAction] else []
2172
80d34c0b9dfe Implement sending gameserver stats to webserver
unc0rr
parents: 2116
diff changeset
    61
1839
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1833
diff changeset
    62
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    63
	{-			let hadRooms = (not $ null rooms) && (null mrooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    64
					in unless ((not $ isDedicated serverInfo) && ((null clientsIn) || hadRooms)) $
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    65
						mainLoop serverInfo acceptChan messagesChan clientsIn mrooms -}
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    66
1839
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1833
diff changeset
    67
	mainLoop newServerInfo mClients mRooms
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    68
1927
e2031906a347 Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents: 1926
diff changeset
    69
startServer :: ServerInfo -> Socket -> IO ()
e2031906a347 Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents: 1926
diff changeset
    70
startServer serverInfo serverSocket = do
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    71
	putStrLn $ "Listening on port " ++ show (listenPort serverInfo)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    72
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    73
	forkIO $
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    74
		acceptLoop
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    75
			serverSocket
1927
e2031906a347 Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents: 1926
diff changeset
    76
			(coreChan serverInfo)
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    77
			0
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    78
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    79
	return ()
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    80
	
2173
98cde8645e21 Send stats every minute
unc0rr
parents: 2172
diff changeset
    81
	forkIO $ timerLoop 0 $ coreChan serverInfo
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    82
1833
e901ec5644b4 Add options for configuring database access
unc0rr
parents: 1804
diff changeset
    83
	startDBConnection $ serverInfo
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    84
2184
f59f80e034b1 Oops, fix database process interaction
unc0rr
parents: 2173
diff changeset
    85
	forkIO $ mainLoop serverInfo IntMap.empty (IntMap.singleton 0 newRoom)
f59f80e034b1 Oops, fix database process interaction
unc0rr
parents: 2173
diff changeset
    86
f59f80e034b1 Oops, fix database process interaction
unc0rr
parents: 2173
diff changeset
    87
	forever $ threadDelay (60 * 60 * 10^6) >> putStrLn "***"