gameServer/Actions.hs
author unc0rr
Fri, 27 Mar 2009 18:50:18 +0000
changeset 1926 cb46fbdcaa41
parent 1925 ec923e56c444
child 1927 e2031906a347
permissions -rw-r--r--
Add simple DoS protection mechanism (although better than previous server had)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     1
module Actions where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     2
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     3
import Control.Concurrent.STM
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     4
import Control.Concurrent.Chan
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     5
import Data.IntMap
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     6
import qualified Data.IntSet as IntSet
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
     7
import qualified Data.Sequence as Seq
1839
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1834
diff changeset
     8
import System.Log.Logger
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     9
import Monad
1926
cb46fbdcaa41 Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents: 1925
diff changeset
    10
import Data.Time
cb46fbdcaa41 Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents: 1925
diff changeset
    11
import Maybe
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    12
-----------------------------
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    13
import CoreTypes
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
    14
import Utils
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    15
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    16
data Action =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    17
	AnswerThisClient [String]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    18
	| AnswerAll [String]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    19
	| AnswerAllOthers [String]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    20
	| AnswerThisRoom [String]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    21
	| AnswerOthersInRoom [String]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    22
	| AnswerLobby [String]
1923
956b6b3529bc Send server message on join
unc0rr
parents: 1921
diff changeset
    23
	| SendServerMessage
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    24
	| RoomAddThisClient Int -- roomID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    25
	| RoomRemoveThisClient
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
    26
	| RemoveTeam String
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    27
	| RemoveRoom
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
    28
	| UnreadyRoomClients
1841
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
    29
	| MoveToLobby
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    30
	| ProtocolError String
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    31
	| Warning String
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    32
	| ByeClient String
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1847
diff changeset
    33
	| KickClient Int -- clID
1879
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
    34
	| KickRoomClient Int -- clID
1921
2a09f7f786a0 - User from localhost is server admin
unc0rr
parents: 1879
diff changeset
    35
	| BanClient String -- nick
1879
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
    36
	| RemoveClientTeams Int -- clID
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    37
	| ModifyClient (ClientInfo -> ClientInfo)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    38
	| ModifyRoom (RoomInfo -> RoomInfo)
1925
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1923
diff changeset
    39
	| ModifyServerInfo (ServerInfo -> ServerInfo)
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    40
	| AddRoom String String
1834
71cb978dc85f Add working check for www account existance
unc0rr
parents: 1827
diff changeset
    41
	| CheckRegistered
1839
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1834
diff changeset
    42
	| ProcessAccountInfo AccountInfo
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    43
	| Dump
1926
cb46fbdcaa41 Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents: 1925
diff changeset
    44
	| AddClient ClientInfo
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    45
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    46
type CmdHandler = Int -> Clients -> Rooms -> [String] -> [Action]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    47
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1847
diff changeset
    48
replaceID a (b, c, d, e) = (a, c, d, e)
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    49
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    50
processAction :: (Int, ServerInfo, Clients, Rooms) -> Action -> IO (Int, ServerInfo, Clients, Rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    51
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    52
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    53
processAction (clID, serverInfo, clients, rooms) (AnswerThisClient msg) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    54
	writeChan (sendChan $ clients ! clID) msg
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    55
	return (clID, serverInfo, clients, rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    56
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    57
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    58
processAction (clID, serverInfo, clients, rooms) (AnswerAll msg) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    59
	mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) (keys clients)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    60
	return (clID, serverInfo, clients, rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    61
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    62
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    63
processAction (clID, serverInfo, clients, rooms) (AnswerAllOthers msg) = do
1879
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
    64
	mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) $
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
    65
		Prelude.filter (\id' -> (id' /= clID) && (logonPassed $ clients ! id')) (keys clients)
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    66
	return (clID, serverInfo, clients, rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    67
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    68
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    69
processAction (clID, serverInfo, clients, rooms) (AnswerThisRoom msg) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    70
	mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) roomClients
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    71
	return (clID, serverInfo, clients, rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    72
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    73
		roomClients = IntSet.elems $ playersIDs room
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    74
		room = rooms ! rID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    75
		rID = roomID client
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    76
		client = clients ! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    77
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    78
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    79
processAction (clID, serverInfo, clients, rooms) (AnswerOthersInRoom msg) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    80
	mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) $ Prelude.filter (/= clID) roomClients
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    81
	return (clID, serverInfo, clients, rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    82
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    83
		roomClients = IntSet.elems $ playersIDs room
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    84
		room = rooms ! rID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    85
		rID = roomID client
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    86
		client = clients ! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    87
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    88
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    89
processAction (clID, serverInfo, clients, rooms) (AnswerLobby msg) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    90
	mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) roomClients
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    91
	return (clID, serverInfo, clients, rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    92
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    93
		roomClients = IntSet.elems $ playersIDs room
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    94
		room = rooms ! 0
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    95
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    96
1923
956b6b3529bc Send server message on join
unc0rr
parents: 1921
diff changeset
    97
processAction (clID, serverInfo, clients, rooms) SendServerMessage = do
956b6b3529bc Send server message on join
unc0rr
parents: 1921
diff changeset
    98
	writeChan (sendChan $ clients ! clID) $ ["SERVER_MESSAGE", serverMessage serverInfo]
956b6b3529bc Send server message on join
unc0rr
parents: 1921
diff changeset
    99
	return (clID, serverInfo, clients, rooms)
956b6b3529bc Send server message on join
unc0rr
parents: 1921
diff changeset
   100
956b6b3529bc Send server message on join
unc0rr
parents: 1921
diff changeset
   101
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   102
processAction (clID, serverInfo, clients, rooms) (ProtocolError msg) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   103
	writeChan (sendChan $ clients ! clID) ["ERROR", msg]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   104
	return (clID, serverInfo, clients, rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   105
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   106
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   107
processAction (clID, serverInfo, clients, rooms) (Warning msg) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   108
	writeChan (sendChan $ clients ! clID) ["WARNING", msg]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   109
	return (clID, serverInfo, clients, rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   110
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   111
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   112
processAction (clID, serverInfo, clients, rooms) (ByeClient msg) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   113
	mapM_ (processAction (clID, serverInfo, clients, rooms)) $ answerOthersQuit ++ answerInformRoom
1926
cb46fbdcaa41 Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents: 1925
diff changeset
   114
	writeChan (sendChan $ clients ! clID) ["BYE", msg]
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   115
	return (
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   116
			0,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   117
			serverInfo,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   118
			delete clID clients,
1823
1938ef375350 Fix ready players accounting
unc0rr
parents: 1813
diff changeset
   119
			adjust (\r -> r{
1938ef375350 Fix ready players accounting
unc0rr
parents: 1813
diff changeset
   120
					playersIDs = IntSet.delete clID (playersIDs r),
1938ef375350 Fix ready players accounting
unc0rr
parents: 1813
diff changeset
   121
					playersIn = (playersIn r) - 1,
1938ef375350 Fix ready players accounting
unc0rr
parents: 1813
diff changeset
   122
					readyPlayers = if isReady client then readyPlayers r - 1 else readyPlayers r
1938ef375350 Fix ready players accounting
unc0rr
parents: 1813
diff changeset
   123
					}) rID rooms
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   124
			)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   125
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   126
		client = clients ! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   127
		rID = roomID client
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   128
		clientNick = nick client
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   129
		answerInformRoom =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   130
			if roomID client /= 0 then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   131
				if not $ Prelude.null msg then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   132
					[AnswerThisRoom ["LEFT", clientNick, msg]]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   133
				else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   134
					[AnswerThisRoom ["LEFT", clientNick]]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   135
			else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   136
				[]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   137
		answerOthersQuit =
1846
24d0074d4eed Small optimization in net server
unc0rr
parents: 1841
diff changeset
   138
			if logonPassed client then
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   139
				if not $ Prelude.null msg then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   140
					[AnswerAll ["LOBBY:LEFT", clientNick, msg]]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   141
				else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   142
					[AnswerAll ["LOBBY:LEFT", clientNick]]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   143
			else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   144
				[]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   145
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   146
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   147
processAction (clID, serverInfo, clients, rooms) (ModifyClient func) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   148
	return (clID, serverInfo, adjust func clID clients, rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   149
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   150
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   151
processAction (clID, serverInfo, clients, rooms) (ModifyRoom func) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   152
	return (clID, serverInfo, clients, adjust func rID rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   153
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   154
		rID = roomID $ clients ! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   155
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   156
1925
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1923
diff changeset
   157
processAction (clID, serverInfo, clients, rooms) (ModifyServerInfo func) = do
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1923
diff changeset
   158
	return (clID, func serverInfo, clients, rooms)
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1923
diff changeset
   159
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1923
diff changeset
   160
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   161
processAction (clID, serverInfo, clients, rooms) (RoomAddThisClient rID) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   162
	processAction (
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   163
		clID,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   164
		serverInfo,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   165
		adjust (\cl -> cl{roomID = rID}) clID clients,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   166
		adjust (\r -> r{playersIDs = IntSet.insert clID (playersIDs r), playersIn = (playersIn r) + 1}) rID $
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   167
			adjust (\r -> r{playersIDs = IntSet.delete clID (playersIDs r)}) 0 rooms
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   168
		) joinMsg
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   169
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   170
		client = clients ! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   171
		joinMsg = if rID == 0 then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   172
				AnswerAllOthers ["LOBBY:JOINED", nick client]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   173
			else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   174
				AnswerThisRoom ["JOINED", nick client]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   175
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   176
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   177
processAction (clID, serverInfo, clients, rooms) (RoomRemoveThisClient) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   178
	when (rID /= 0) $ (processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["LEFT", nick client, "part"]) >> return ()
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   179
	return (
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   180
		clID,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   181
		serverInfo,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   182
		adjust (\cl -> cl{roomID = 0}) clID clients,
1823
1938ef375350 Fix ready players accounting
unc0rr
parents: 1813
diff changeset
   183
		adjust (\r -> r{
1938ef375350 Fix ready players accounting
unc0rr
parents: 1813
diff changeset
   184
				playersIDs = IntSet.delete clID (playersIDs r),
1938ef375350 Fix ready players accounting
unc0rr
parents: 1813
diff changeset
   185
				playersIn = (playersIn r) - 1,
1827
3bb5e22b7f9a Fix ready players number after a round
unc0rr
parents: 1823
diff changeset
   186
				readyPlayers = if isReady client then (readyPlayers r) - 1 else readyPlayers r
1823
1938ef375350 Fix ready players accounting
unc0rr
parents: 1813
diff changeset
   187
				}) rID $
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   188
			adjust (\r -> r{playersIDs = IntSet.insert clID (playersIDs r)}) 0 rooms
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   189
		)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   190
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   191
		rID = roomID client
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   192
		client = clients ! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   193
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   194
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   195
processAction (clID, serverInfo, clients, rooms) (AddRoom roomName roomPassword) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   196
	let newServerInfo = serverInfo {nextRoomID = newID}
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   197
	let room = newRoom{
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   198
			roomUID = newID,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   199
			name = roomName,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   200
			password = roomPassword,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   201
			roomProto = (clientProto client)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   202
			}
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   203
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   204
	processAction (clID, serverInfo, clients, rooms) $ AnswerLobby ["ROOM", "ADD", roomName]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   205
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   206
	processAction (
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   207
		clID,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   208
		newServerInfo,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   209
		adjust (\cl -> cl{isMaster = True}) clID clients,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   210
		insert newID room rooms
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   211
		) $ RoomAddThisClient newID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   212
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   213
		newID = (nextRoomID serverInfo) - 1
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   214
		client = clients ! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   215
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   216
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   217
processAction (clID, serverInfo, clients, rooms) (RemoveRoom) = do
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   218
	processAction (clID, serverInfo, clients, rooms) $ AnswerLobby ["ROOM", "DEL", name room]
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   219
	processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["ROOMABANDONED", name room]
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   220
	return (clID,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   221
		serverInfo,
1827
3bb5e22b7f9a Fix ready players number after a round
unc0rr
parents: 1823
diff changeset
   222
		Data.IntMap.map (\cl -> if roomID cl == rID then cl{roomID = 0, isMaster = False, isReady = False} else cl) clients,
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   223
		delete rID $ adjust (\r -> r{playersIDs = IntSet.union (playersIDs room) (playersIDs r)}) 0 rooms
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   224
		)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   225
	where
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   226
		room = rooms ! rID
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   227
		rID = roomID client
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   228
		client = clients ! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   229
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   230
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   231
processAction (clID, serverInfo, clients, rooms) (UnreadyRoomClients) = do
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   232
	processAction (clID, serverInfo, clients, rooms) $ AnswerThisRoom ("NOT_READY" : roomPlayers)
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   233
	return (clID,
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   234
		serverInfo,
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   235
		Data.IntMap.map (\cl -> if roomID cl == rID then cl{isReady = False} else cl) clients,
1827
3bb5e22b7f9a Fix ready players number after a round
unc0rr
parents: 1823
diff changeset
   236
		adjust (\r -> r{readyPlayers = 0}) rID rooms)
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   237
	where
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   238
		room = rooms ! rID
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   239
		rID = roomID client
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   240
		client = clients ! clID
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   241
		roomPlayers = Prelude.map (nick . (clients !)) roomPlayersIDs
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   242
		roomPlayersIDs = IntSet.elems $ playersIDs room
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   243
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   244
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   245
processAction (clID, serverInfo, clients, rooms) (RemoveTeam teamName) = do
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   246
	newRooms <-	if not $ gameinprogress room then
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   247
			do
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   248
			processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["REMOVE_TEAM", teamName]
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   249
			return $
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   250
				adjust (\r -> r{teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r}) rID rooms
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   251
		else
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   252
			do
1866
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   253
			processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["EM", rmTeamMsg]
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   254
			return $
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   255
				adjust (\r -> r{
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   256
				teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r,
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   257
				leftTeams = teamName : leftTeams r,
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   258
				roundMsgs = roundMsgs r Seq.|> rmTeamMsg
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   259
				}) rID rooms
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   260
	return (clID, serverInfo, clients, newRooms)
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   261
	where
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   262
		room = rooms ! rID
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   263
		rID = roomID client
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   264
		client = clients ! clID
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   265
		rmTeamMsg = toEngineMsg $ 'F' : teamName
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   266
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   267
1834
71cb978dc85f Add working check for www account existance
unc0rr
parents: 1827
diff changeset
   268
processAction (clID, serverInfo, clients, rooms) (CheckRegistered) = do
1921
2a09f7f786a0 - User from localhost is server admin
unc0rr
parents: 1879
diff changeset
   269
	writeChan (dbQueries serverInfo) $ CheckAccount client
1834
71cb978dc85f Add working check for www account existance
unc0rr
parents: 1827
diff changeset
   270
	return (clID, serverInfo, clients, rooms)
71cb978dc85f Add working check for www account existance
unc0rr
parents: 1827
diff changeset
   271
	where
71cb978dc85f Add working check for www account existance
unc0rr
parents: 1827
diff changeset
   272
		client = clients ! clID
71cb978dc85f Add working check for www account existance
unc0rr
parents: 1827
diff changeset
   273
1841
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
   274
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   275
processAction (clID, serverInfo, clients, rooms) (Dump) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   276
	writeChan (sendChan $ clients ! clID) ["DUMP", show serverInfo, showTree clients, showTree rooms]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   277
	return (clID, serverInfo, clients, rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   278
1841
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
   279
1839
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1834
diff changeset
   280
processAction (clID, serverInfo, clients, rooms) (ProcessAccountInfo info) = do
1923
956b6b3529bc Send server message on join
unc0rr
parents: 1921
diff changeset
   281
	processAction (clID, serverInfo, clients, rooms) SendServerMessage
1839
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1834
diff changeset
   282
	case info of
1847
2178c0fc838c Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents: 1846
diff changeset
   283
		HasAccount passwd isAdmin -> do
1839
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1834
diff changeset
   284
			infoM "Clients" $ show clID ++ " has account"
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1834
diff changeset
   285
			writeChan (sendChan $ clients ! clID) ["ASKPASSWORD"]
1847
2178c0fc838c Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents: 1846
diff changeset
   286
			return (clID, serverInfo, adjust (\cl -> cl{webPassword = passwd, isAdministrator = isAdmin}) clID clients, rooms)
1839
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1834
diff changeset
   287
		Guest -> do
5dd4cb7fd7e5 Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents: 1834
diff changeset
   288
			infoM "Clients" $ show clID ++ " is guest"
1841
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
   289
			processAction (clID, serverInfo, adjust (\cl -> cl{logonPassed = True}) clID clients, rooms) MoveToLobby
1921
2a09f7f786a0 - User from localhost is server admin
unc0rr
parents: 1879
diff changeset
   290
		Admin -> do
2a09f7f786a0 - User from localhost is server admin
unc0rr
parents: 1879
diff changeset
   291
			infoM "Clients" $ show clID ++ " is admin"
2a09f7f786a0 - User from localhost is server admin
unc0rr
parents: 1879
diff changeset
   292
			foldM processAction (clID, serverInfo, adjust (\cl -> cl{logonPassed = True, isAdministrator = True}) clID clients, rooms) [MoveToLobby, AnswerThisClient ["ADMIN_ACCESS"]]
1841
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
   293
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
   294
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
   295
processAction (clID, serverInfo, clients, rooms) (MoveToLobby) = do
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
   296
	foldM processAction (clID, serverInfo, clients, rooms) $
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
   297
		(RoomAddThisClient 0)
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
   298
		: answerLobbyNicks
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
   299
		-- ++ (answerServerMessage client clients)
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
   300
	where
1846
24d0074d4eed Small optimization in net server
unc0rr
parents: 1841
diff changeset
   301
		lobbyNicks = Prelude.map nick $ Prelude.filter logonPassed $ elems clients
1841
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
   302
		answerLobbyNicks = if not $ Prelude.null lobbyNicks then
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
   303
					[AnswerThisClient (["LOBBY:JOINED"] ++ lobbyNicks)]
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
   304
				else
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
   305
					[]
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
   306
fba7210b438b Retrieve client password from web database and ask for it
unc0rr
parents: 1839
diff changeset
   307
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1847
diff changeset
   308
processAction (clID, serverInfo, clients, rooms) (KickClient kickID) = do
1879
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   309
	liftM2 replaceID (return clID) (processAction (kickID, serverInfo, clients, rooms) $ ByeClient "Kicked")
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   310
1926
cb46fbdcaa41 Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents: 1925
diff changeset
   311
1921
2a09f7f786a0 - User from localhost is server admin
unc0rr
parents: 1879
diff changeset
   312
processAction (clID, serverInfo, clients, rooms) (BanClient banNick) = do
2a09f7f786a0 - User from localhost is server admin
unc0rr
parents: 1879
diff changeset
   313
	return (clID, serverInfo, clients, rooms)
2a09f7f786a0 - User from localhost is server admin
unc0rr
parents: 1879
diff changeset
   314
1879
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   315
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   316
processAction (clID, serverInfo, clients, rooms) (KickRoomClient kickID) = do
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   317
	writeChan (sendChan $ clients ! kickID) ["KICKED"]
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   318
	liftM2 replaceID (return clID) (processAction (kickID, serverInfo, clients, rooms) $ RoomRemoveThisClient)
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   319
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   320
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   321
processAction (clID, serverInfo, clients, rooms) (RemoveClientTeams teamsClID) = do
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   322
	liftM2 replaceID (return clID) $
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   323
		foldM processAction (teamsClID, serverInfo, clients, rooms) $ removeTeamsActions
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   324
	where
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   325
		client = clients ! teamsClID
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   326
		room = rooms ! (roomID client)
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   327
		teamsToRemove = Prelude.filter (\t -> teamowner t == nick client) $ teams room
bb114339eb4e Implement kick from room
unc0rr
parents: 1866
diff changeset
   328
		removeTeamsActions = Prelude.map (RemoveTeam . teamname) teamsToRemove
1926
cb46fbdcaa41 Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents: 1925
diff changeset
   329
cb46fbdcaa41 Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents: 1925
diff changeset
   330
cb46fbdcaa41 Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents: 1925
diff changeset
   331
processAction (clID, serverInfo, clients, rooms) (AddClient client) = do
cb46fbdcaa41 Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents: 1925
diff changeset
   332
	let updatedClients = insert (clientUID client) client clients
cb46fbdcaa41 Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents: 1925
diff changeset
   333
	infoM "Clients" ((show $ clientUID client) ++ ": new client. Time: " ++ (show $ connectTime client))
cb46fbdcaa41 Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents: 1925
diff changeset
   334
	writeChan (sendChan $ client) ["CONNECTED", "Hedgewars server http://www.hedgewars.org/"]
cb46fbdcaa41 Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents: 1925
diff changeset
   335
cb46fbdcaa41 Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents: 1925
diff changeset
   336
	let newLogins = takeWhile (\(_ , time) -> (connectTime client) `diffUTCTime` time <= 20) $ lastLogins serverInfo
cb46fbdcaa41 Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents: 1925
diff changeset
   337
cb46fbdcaa41 Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents: 1925
diff changeset
   338
	if isJust $ host client `Prelude.lookup` newLogins then
cb46fbdcaa41 Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents: 1925
diff changeset
   339
		processAction (clID, serverInfo{lastLogins = newLogins}, updatedClients, rooms) $ ByeClient "Reconnected too fast"
cb46fbdcaa41 Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents: 1925
diff changeset
   340
		else
cb46fbdcaa41 Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents: 1925
diff changeset
   341
		return (clID, serverInfo{lastLogins = (host client, connectTime client) : newLogins}, updatedClients, rooms)