1804
|
1 |
module Actions where
|
|
2 |
|
|
3 |
import Control.Concurrent.STM
|
|
4 |
import Control.Concurrent.Chan
|
|
5 |
import Data.IntMap
|
|
6 |
import qualified Data.IntSet as IntSet
|
1813
|
7 |
import qualified Data.Sequence as Seq
|
1804
|
8 |
import Monad
|
|
9 |
-----------------------------
|
|
10 |
import CoreTypes
|
1813
|
11 |
import Utils
|
1804
|
12 |
|
|
13 |
data Action =
|
|
14 |
AnswerThisClient [String]
|
|
15 |
| AnswerAll [String]
|
|
16 |
| AnswerAllOthers [String]
|
|
17 |
| AnswerThisRoom [String]
|
|
18 |
| AnswerOthersInRoom [String]
|
|
19 |
| AnswerLobby [String]
|
|
20 |
| RoomAddThisClient Int -- roomID
|
|
21 |
| RoomRemoveThisClient
|
1813
|
22 |
| RemoveTeam String
|
1804
|
23 |
| RemoveRoom
|
1811
|
24 |
| UnreadyRoomClients
|
1804
|
25 |
| ProtocolError String
|
|
26 |
| Warning String
|
|
27 |
| ByeClient String
|
|
28 |
| ModifyClient (ClientInfo -> ClientInfo)
|
|
29 |
| ModifyRoom (RoomInfo -> RoomInfo)
|
|
30 |
| AddRoom String String
|
|
31 |
| Dump
|
|
32 |
|
|
33 |
type CmdHandler = Int -> Clients -> Rooms -> [String] -> [Action]
|
|
34 |
|
|
35 |
|
|
36 |
processAction :: (Int, ServerInfo, Clients, Rooms) -> Action -> IO (Int, ServerInfo, Clients, Rooms)
|
|
37 |
|
|
38 |
|
|
39 |
processAction (clID, serverInfo, clients, rooms) (AnswerThisClient msg) = do
|
|
40 |
writeChan (sendChan $ clients ! clID) msg
|
|
41 |
return (clID, serverInfo, clients, rooms)
|
|
42 |
|
|
43 |
|
|
44 |
processAction (clID, serverInfo, clients, rooms) (AnswerAll msg) = do
|
|
45 |
mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) (keys clients)
|
|
46 |
return (clID, serverInfo, clients, rooms)
|
|
47 |
|
|
48 |
|
|
49 |
processAction (clID, serverInfo, clients, rooms) (AnswerAllOthers msg) = do
|
|
50 |
mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) $ Prelude.filter (/= clID) (keys clients)
|
|
51 |
return (clID, serverInfo, clients, rooms)
|
|
52 |
|
|
53 |
|
|
54 |
processAction (clID, serverInfo, clients, rooms) (AnswerThisRoom msg) = do
|
|
55 |
mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) roomClients
|
|
56 |
return (clID, serverInfo, clients, rooms)
|
|
57 |
where
|
|
58 |
roomClients = IntSet.elems $ playersIDs room
|
|
59 |
room = rooms ! rID
|
|
60 |
rID = roomID client
|
|
61 |
client = clients ! clID
|
|
62 |
|
|
63 |
|
|
64 |
processAction (clID, serverInfo, clients, rooms) (AnswerOthersInRoom msg) = do
|
|
65 |
mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) $ Prelude.filter (/= clID) roomClients
|
|
66 |
return (clID, serverInfo, clients, rooms)
|
|
67 |
where
|
|
68 |
roomClients = IntSet.elems $ playersIDs room
|
|
69 |
room = rooms ! rID
|
|
70 |
rID = roomID client
|
|
71 |
client = clients ! clID
|
|
72 |
|
|
73 |
|
|
74 |
processAction (clID, serverInfo, clients, rooms) (AnswerLobby msg) = do
|
|
75 |
mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) roomClients
|
|
76 |
return (clID, serverInfo, clients, rooms)
|
|
77 |
where
|
|
78 |
roomClients = IntSet.elems $ playersIDs room
|
|
79 |
room = rooms ! 0
|
|
80 |
|
|
81 |
|
|
82 |
processAction (clID, serverInfo, clients, rooms) (ProtocolError msg) = do
|
|
83 |
writeChan (sendChan $ clients ! clID) ["ERROR", msg]
|
|
84 |
return (clID, serverInfo, clients, rooms)
|
|
85 |
|
|
86 |
|
|
87 |
processAction (clID, serverInfo, clients, rooms) (Warning msg) = do
|
|
88 |
writeChan (sendChan $ clients ! clID) ["WARNING", msg]
|
|
89 |
return (clID, serverInfo, clients, rooms)
|
|
90 |
|
|
91 |
|
|
92 |
processAction (clID, serverInfo, clients, rooms) (ByeClient msg) = do
|
|
93 |
mapM_ (processAction (clID, serverInfo, clients, rooms)) $ answerOthersQuit ++ answerInformRoom
|
|
94 |
writeChan (sendChan $ clients ! clID) ["BYE"]
|
|
95 |
return (
|
|
96 |
0,
|
|
97 |
serverInfo,
|
|
98 |
delete clID clients,
|
1823
|
99 |
adjust (\r -> r{
|
|
100 |
playersIDs = IntSet.delete clID (playersIDs r),
|
|
101 |
playersIn = (playersIn r) - 1,
|
|
102 |
readyPlayers = if isReady client then readyPlayers r - 1 else readyPlayers r
|
|
103 |
}) rID rooms
|
1804
|
104 |
)
|
|
105 |
where
|
|
106 |
client = clients ! clID
|
|
107 |
rID = roomID client
|
|
108 |
clientNick = nick client
|
|
109 |
answerInformRoom =
|
|
110 |
if roomID client /= 0 then
|
|
111 |
if not $ Prelude.null msg then
|
|
112 |
[AnswerThisRoom ["LEFT", clientNick, msg]]
|
|
113 |
else
|
|
114 |
[AnswerThisRoom ["LEFT", clientNick]]
|
|
115 |
else
|
|
116 |
[]
|
|
117 |
answerOthersQuit =
|
|
118 |
if not $ Prelude.null clientNick then
|
|
119 |
if not $ Prelude.null msg then
|
|
120 |
[AnswerAll ["LOBBY:LEFT", clientNick, msg]]
|
|
121 |
else
|
|
122 |
[AnswerAll ["LOBBY:LEFT", clientNick]]
|
|
123 |
else
|
|
124 |
[]
|
|
125 |
|
|
126 |
|
|
127 |
processAction (clID, serverInfo, clients, rooms) (ModifyClient func) = do
|
|
128 |
return (clID, serverInfo, adjust func clID clients, rooms)
|
|
129 |
|
|
130 |
|
|
131 |
processAction (clID, serverInfo, clients, rooms) (ModifyRoom func) = do
|
|
132 |
return (clID, serverInfo, clients, adjust func rID rooms)
|
|
133 |
where
|
|
134 |
rID = roomID $ clients ! clID
|
|
135 |
|
|
136 |
|
|
137 |
processAction (clID, serverInfo, clients, rooms) (RoomAddThisClient rID) = do
|
|
138 |
processAction (
|
|
139 |
clID,
|
|
140 |
serverInfo,
|
|
141 |
adjust (\cl -> cl{roomID = rID}) clID clients,
|
|
142 |
adjust (\r -> r{playersIDs = IntSet.insert clID (playersIDs r), playersIn = (playersIn r) + 1}) rID $
|
|
143 |
adjust (\r -> r{playersIDs = IntSet.delete clID (playersIDs r)}) 0 rooms
|
|
144 |
) joinMsg
|
|
145 |
where
|
|
146 |
client = clients ! clID
|
|
147 |
joinMsg = if rID == 0 then
|
|
148 |
AnswerAllOthers ["LOBBY:JOINED", nick client]
|
|
149 |
else
|
|
150 |
AnswerThisRoom ["JOINED", nick client]
|
|
151 |
|
|
152 |
|
|
153 |
processAction (clID, serverInfo, clients, rooms) (RoomRemoveThisClient) = do
|
|
154 |
when (rID /= 0) $ (processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["LEFT", nick client, "part"]) >> return ()
|
|
155 |
return (
|
|
156 |
clID,
|
|
157 |
serverInfo,
|
|
158 |
adjust (\cl -> cl{roomID = 0}) clID clients,
|
1823
|
159 |
adjust (\r -> r{
|
|
160 |
playersIDs = IntSet.delete clID (playersIDs r),
|
|
161 |
playersIn = (playersIn r) - 1,
|
|
162 |
readyPlayers = if isReady client then readyPlayers r - 1 else readyPlayers r
|
|
163 |
}) rID $
|
1804
|
164 |
adjust (\r -> r{playersIDs = IntSet.insert clID (playersIDs r)}) 0 rooms
|
|
165 |
)
|
|
166 |
where
|
|
167 |
rID = roomID client
|
|
168 |
client = clients ! clID
|
|
169 |
|
|
170 |
|
|
171 |
processAction (clID, serverInfo, clients, rooms) (AddRoom roomName roomPassword) = do
|
|
172 |
let newServerInfo = serverInfo {nextRoomID = newID}
|
|
173 |
let room = newRoom{
|
|
174 |
roomUID = newID,
|
|
175 |
name = roomName,
|
|
176 |
password = roomPassword,
|
|
177 |
roomProto = (clientProto client)
|
|
178 |
}
|
|
179 |
|
|
180 |
processAction (clID, serverInfo, clients, rooms) $ AnswerLobby ["ROOM", "ADD", roomName]
|
|
181 |
|
|
182 |
processAction (
|
|
183 |
clID,
|
|
184 |
newServerInfo,
|
|
185 |
adjust (\cl -> cl{isMaster = True}) clID clients,
|
|
186 |
insert newID room rooms
|
|
187 |
) $ RoomAddThisClient newID
|
|
188 |
where
|
|
189 |
newID = (nextRoomID serverInfo) - 1
|
|
190 |
client = clients ! clID
|
|
191 |
|
|
192 |
|
|
193 |
processAction (clID, serverInfo, clients, rooms) (RemoveRoom) = do
|
1811
|
194 |
processAction (clID, serverInfo, clients, rooms) $ AnswerLobby ["ROOM", "DEL", name room]
|
|
195 |
processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["ROOMABANDONED", name room]
|
1804
|
196 |
return (clID,
|
|
197 |
serverInfo,
|
|
198 |
Data.IntMap.map (\cl -> if roomID cl == rID then cl{roomID = 0, isMaster = False} else cl) clients,
|
1811
|
199 |
delete rID $ adjust (\r -> r{playersIDs = IntSet.union (playersIDs room) (playersIDs r)}) 0 rooms
|
1804
|
200 |
)
|
|
201 |
where
|
1811
|
202 |
room = rooms ! rID
|
1804
|
203 |
rID = roomID client
|
|
204 |
client = clients ! clID
|
|
205 |
|
1813
|
206 |
|
1811
|
207 |
processAction (clID, serverInfo, clients, rooms) (UnreadyRoomClients) = do
|
|
208 |
processAction (clID, serverInfo, clients, rooms) $ AnswerThisRoom ("NOT_READY" : roomPlayers)
|
|
209 |
return (clID,
|
|
210 |
serverInfo,
|
|
211 |
Data.IntMap.map (\cl -> if roomID cl == rID then cl{isReady = False} else cl) clients,
|
|
212 |
rooms)
|
|
213 |
where
|
|
214 |
room = rooms ! rID
|
|
215 |
rID = roomID client
|
|
216 |
client = clients ! clID
|
|
217 |
roomPlayers = Prelude.map (nick . (clients !)) roomPlayersIDs
|
|
218 |
roomPlayersIDs = IntSet.elems $ playersIDs room
|
|
219 |
|
|
220 |
|
1813
|
221 |
processAction (clID, serverInfo, clients, rooms) (RemoveTeam teamName) = do
|
|
222 |
newRooms <- if not $ gameinprogress room then
|
|
223 |
do
|
|
224 |
processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["REMOVE_TEAM", teamName]
|
|
225 |
return $
|
|
226 |
adjust (\r -> r{teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r}) rID rooms
|
|
227 |
else
|
|
228 |
do
|
|
229 |
processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["GAMEMSG", rmTeamMsg]
|
|
230 |
return $
|
|
231 |
adjust (\r -> r{
|
|
232 |
teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r,
|
|
233 |
leftTeams = teamName : leftTeams r,
|
|
234 |
roundMsgs = roundMsgs r Seq.|> rmTeamMsg
|
|
235 |
}) rID rooms
|
|
236 |
return (clID, serverInfo, clients, newRooms)
|
|
237 |
where
|
|
238 |
room = rooms ! rID
|
|
239 |
rID = roomID client
|
|
240 |
client = clients ! clID
|
|
241 |
rmTeamMsg = toEngineMsg $ 'F' : teamName
|
|
242 |
|
|
243 |
|
1804
|
244 |
processAction (clID, serverInfo, clients, rooms) (Dump) = do
|
|
245 |
writeChan (sendChan $ clients ! clID) ["DUMP", show serverInfo, showTree clients, showTree rooms]
|
|
246 |
return (clID, serverInfo, clients, rooms)
|
|
247 |
|