gameServer/Actions.hs
author unc0rr
Wed, 18 Feb 2009 15:04:40 +0000
changeset 1804 4e78ad846fb6
child 1811 1b9e33623b7e
permissions -rw-r--r--
New game server: - Incomplete implementation - More robust, no memory leaks, better architecture for easy features addition - Incompatible with current client
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
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     7
import Monad
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     8
-----------------------------
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     9
import CoreTypes
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    10
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    11
data Action =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    12
	AnswerThisClient [String]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    13
	| AnswerAll [String]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    14
	| AnswerAllOthers [String]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    15
	| AnswerThisRoom [String]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    16
	| AnswerOthersInRoom [String]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    17
	| AnswerLobby [String]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    18
	| RoomAddThisClient Int -- roomID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    19
	| RoomRemoveThisClient
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    20
	| RemoveRoom
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    21
	| ProtocolError String
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    22
	| Warning String
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    23
	| ByeClient String
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    24
	| ModifyClient (ClientInfo -> ClientInfo)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    25
	| ModifyRoom (RoomInfo -> RoomInfo)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    26
	| AddRoom String String
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    27
	| Dump
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    28
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    29
type CmdHandler = Int -> Clients -> Rooms -> [String] -> [Action]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    30
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    31
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    32
processAction :: (Int, ServerInfo, Clients, Rooms) -> Action -> IO (Int, ServerInfo, Clients, Rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    33
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    34
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    35
processAction (clID, serverInfo, clients, rooms) (AnswerThisClient msg) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    36
	writeChan (sendChan $ clients ! clID) msg
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    37
	return (clID, serverInfo, clients, rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    38
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    39
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    40
processAction (clID, serverInfo, clients, rooms) (AnswerAll msg) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    41
	mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) (keys clients)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    42
	return (clID, serverInfo, clients, rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    43
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    44
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    45
processAction (clID, serverInfo, clients, rooms) (AnswerAllOthers msg) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    46
	mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) $ Prelude.filter (/= clID) (keys clients)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    47
	return (clID, serverInfo, clients, rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    48
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    49
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    50
processAction (clID, serverInfo, clients, rooms) (AnswerThisRoom msg) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    51
	mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) roomClients
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    52
	return (clID, serverInfo, clients, rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    53
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    54
		roomClients = IntSet.elems $ playersIDs room
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    55
		room = rooms ! rID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    56
		rID = roomID client
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    57
		client = clients ! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    58
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    59
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    60
processAction (clID, serverInfo, clients, rooms) (AnswerOthersInRoom msg) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    61
	mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) $ Prelude.filter (/= clID) roomClients
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    62
	return (clID, serverInfo, clients, rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    63
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    64
		roomClients = IntSet.elems $ playersIDs room
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    65
		room = rooms ! rID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    66
		rID = roomID client
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    67
		client = clients ! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    68
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    69
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    70
processAction (clID, serverInfo, clients, rooms) (AnswerLobby msg) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    71
	mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) roomClients
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    72
	return (clID, serverInfo, clients, rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    73
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    74
		roomClients = IntSet.elems $ playersIDs room
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    75
		room = rooms ! 0
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    76
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    77
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    78
processAction (clID, serverInfo, clients, rooms) (ProtocolError msg) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    79
	writeChan (sendChan $ clients ! clID) ["ERROR", msg]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    80
	return (clID, serverInfo, clients, rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    81
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    82
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    83
processAction (clID, serverInfo, clients, rooms) (Warning msg) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    84
	writeChan (sendChan $ clients ! clID) ["WARNING", msg]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    85
	return (clID, serverInfo, clients, rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    86
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    87
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    88
processAction (clID, serverInfo, clients, rooms) (ByeClient msg) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    89
	mapM_ (processAction (clID, serverInfo, clients, rooms)) $ answerOthersQuit ++ answerInformRoom
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    90
	writeChan (sendChan $ clients ! clID) ["BYE"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    91
	return (
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    92
			0,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    93
			serverInfo,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    94
			delete clID clients,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    95
			adjust (\r -> r{playersIDs = IntSet.delete clID (playersIDs r), playersIn = (playersIn r) - 1}) rID rooms
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    96
			)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    97
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    98
		client = clients ! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    99
		rID = roomID client
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   100
		clientNick = nick client
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   101
		answerInformRoom =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   102
			if roomID client /= 0 then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   103
				if not $ Prelude.null msg then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   104
					[AnswerThisRoom ["LEFT", clientNick, msg]]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   105
				else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   106
					[AnswerThisRoom ["LEFT", clientNick]]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   107
			else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   108
				[]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   109
		answerOthersQuit =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   110
			if not $ Prelude.null clientNick then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   111
				if not $ Prelude.null msg then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   112
					[AnswerAll ["LOBBY:LEFT", clientNick, msg]]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   113
				else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   114
					[AnswerAll ["LOBBY:LEFT", clientNick]]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   115
			else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   116
				[]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   117
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   118
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   119
processAction (clID, serverInfo, clients, rooms) (ModifyClient func) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   120
	return (clID, serverInfo, adjust func clID clients, rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   121
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   122
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   123
processAction (clID, serverInfo, clients, rooms) (ModifyRoom func) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   124
	return (clID, serverInfo, clients, adjust func rID rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   125
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   126
		rID = roomID $ clients ! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   127
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   128
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   129
processAction (clID, serverInfo, clients, rooms) (RoomAddThisClient rID) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   130
	processAction (
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   131
		clID,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   132
		serverInfo,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   133
		adjust (\cl -> cl{roomID = rID}) clID clients,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   134
		adjust (\r -> r{playersIDs = IntSet.insert clID (playersIDs r), playersIn = (playersIn r) + 1}) rID $
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   135
			adjust (\r -> r{playersIDs = IntSet.delete clID (playersIDs r)}) 0 rooms
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   136
		) joinMsg
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   137
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   138
		client = clients ! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   139
		joinMsg = if rID == 0 then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   140
				AnswerAllOthers ["LOBBY:JOINED", nick client]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   141
			else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   142
				AnswerThisRoom ["JOINED", nick client]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   143
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   144
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   145
processAction (clID, serverInfo, clients, rooms) (RoomRemoveThisClient) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   146
	when (rID /= 0) $ (processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["LEFT", nick client, "part"]) >> return ()
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   147
	return (
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   148
		clID,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   149
		serverInfo,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   150
		adjust (\cl -> cl{roomID = 0}) clID clients,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   151
		adjust (\r -> r{playersIDs = IntSet.delete clID (playersIDs r), playersIn = (playersIn r) - 1}) rID $
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   152
			adjust (\r -> r{playersIDs = IntSet.insert clID (playersIDs r)}) 0 rooms
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   153
		)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   154
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   155
		rID = roomID client
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   156
		client = clients ! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   157
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   158
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   159
processAction (clID, serverInfo, clients, rooms) (AddRoom roomName roomPassword) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   160
	let newServerInfo = serverInfo {nextRoomID = newID}
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   161
	let room = newRoom{
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   162
			roomUID = newID,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   163
			name = roomName,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   164
			password = roomPassword,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   165
			roomProto = (clientProto client)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   166
			}
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   167
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   168
	processAction (clID, serverInfo, clients, rooms) $ AnswerLobby ["ROOM", "ADD", roomName]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   169
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   170
	processAction (
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   171
		clID,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   172
		newServerInfo,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   173
		adjust (\cl -> cl{isMaster = True}) clID clients,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   174
		insert newID room rooms
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   175
		) $ RoomAddThisClient newID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   176
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   177
		newID = (nextRoomID serverInfo) - 1
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   178
		client = clients ! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   179
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   180
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   181
processAction (clID, serverInfo, clients, rooms) (RemoveRoom) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   182
	processAction (clID, serverInfo, clients, rooms) $ AnswerLobby ["ROOM", "DEL", name clRoom]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   183
	processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["ROOMABANDONED", name clRoom]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   184
	return (clID,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   185
		serverInfo,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   186
		Data.IntMap.map (\cl -> if roomID cl == rID then cl{roomID = 0, isMaster = False} else cl) clients,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   187
		delete rID $ adjust (\r -> r{playersIDs = IntSet.union (playersIDs clRoom) (playersIDs r)}) 0 rooms
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   188
		)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   189
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   190
		clRoom = rooms ! rID
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
processAction (clID, serverInfo, clients, rooms) (Dump) = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   195
	writeChan (sendChan $ clients ! clID) ["DUMP", show serverInfo, showTree clients, showTree rooms]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   196
	return (clID, serverInfo, clients, rooms)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   197