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