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,
|
|
99 |
adjust (\r -> r{playersIDs = IntSet.delete clID (playersIDs r), playersIn = (playersIn r) - 1}) rID rooms
|
|
100 |
)
|
|
101 |
where
|
|
102 |
client = clients ! clID
|
|
103 |
rID = roomID client
|
|
104 |
clientNick = nick client
|
|
105 |
answerInformRoom =
|
|
106 |
if roomID client /= 0 then
|
|
107 |
if not $ Prelude.null msg then
|
|
108 |
[AnswerThisRoom ["LEFT", clientNick, msg]]
|
|
109 |
else
|
|
110 |
[AnswerThisRoom ["LEFT", clientNick]]
|
|
111 |
else
|
|
112 |
[]
|
|
113 |
answerOthersQuit =
|
|
114 |
if not $ Prelude.null clientNick then
|
|
115 |
if not $ Prelude.null msg then
|
|
116 |
[AnswerAll ["LOBBY:LEFT", clientNick, msg]]
|
|
117 |
else
|
|
118 |
[AnswerAll ["LOBBY:LEFT", clientNick]]
|
|
119 |
else
|
|
120 |
[]
|
|
121 |
|
|
122 |
|
|
123 |
processAction (clID, serverInfo, clients, rooms) (ModifyClient func) = do
|
|
124 |
return (clID, serverInfo, adjust func clID clients, rooms)
|
|
125 |
|
|
126 |
|
|
127 |
processAction (clID, serverInfo, clients, rooms) (ModifyRoom func) = do
|
|
128 |
return (clID, serverInfo, clients, adjust func rID rooms)
|
|
129 |
where
|
|
130 |
rID = roomID $ clients ! clID
|
|
131 |
|
|
132 |
|
|
133 |
processAction (clID, serverInfo, clients, rooms) (RoomAddThisClient rID) = do
|
|
134 |
processAction (
|
|
135 |
clID,
|
|
136 |
serverInfo,
|
|
137 |
adjust (\cl -> cl{roomID = rID}) clID clients,
|
|
138 |
adjust (\r -> r{playersIDs = IntSet.insert clID (playersIDs r), playersIn = (playersIn r) + 1}) rID $
|
|
139 |
adjust (\r -> r{playersIDs = IntSet.delete clID (playersIDs r)}) 0 rooms
|
|
140 |
) joinMsg
|
|
141 |
where
|
|
142 |
client = clients ! clID
|
|
143 |
joinMsg = if rID == 0 then
|
|
144 |
AnswerAllOthers ["LOBBY:JOINED", nick client]
|
|
145 |
else
|
|
146 |
AnswerThisRoom ["JOINED", nick client]
|
|
147 |
|
|
148 |
|
|
149 |
processAction (clID, serverInfo, clients, rooms) (RoomRemoveThisClient) = do
|
|
150 |
when (rID /= 0) $ (processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["LEFT", nick client, "part"]) >> return ()
|
|
151 |
return (
|
|
152 |
clID,
|
|
153 |
serverInfo,
|
|
154 |
adjust (\cl -> cl{roomID = 0}) clID clients,
|
|
155 |
adjust (\r -> r{playersIDs = IntSet.delete clID (playersIDs r), playersIn = (playersIn r) - 1}) rID $
|
|
156 |
adjust (\r -> r{playersIDs = IntSet.insert clID (playersIDs r)}) 0 rooms
|
|
157 |
)
|
|
158 |
where
|
|
159 |
rID = roomID client
|
|
160 |
client = clients ! clID
|
|
161 |
|
|
162 |
|
|
163 |
processAction (clID, serverInfo, clients, rooms) (AddRoom roomName roomPassword) = do
|
|
164 |
let newServerInfo = serverInfo {nextRoomID = newID}
|
|
165 |
let room = newRoom{
|
|
166 |
roomUID = newID,
|
|
167 |
name = roomName,
|
|
168 |
password = roomPassword,
|
|
169 |
roomProto = (clientProto client)
|
|
170 |
}
|
|
171 |
|
|
172 |
processAction (clID, serverInfo, clients, rooms) $ AnswerLobby ["ROOM", "ADD", roomName]
|
|
173 |
|
|
174 |
processAction (
|
|
175 |
clID,
|
|
176 |
newServerInfo,
|
|
177 |
adjust (\cl -> cl{isMaster = True}) clID clients,
|
|
178 |
insert newID room rooms
|
|
179 |
) $ RoomAddThisClient newID
|
|
180 |
where
|
|
181 |
newID = (nextRoomID serverInfo) - 1
|
|
182 |
client = clients ! clID
|
|
183 |
|
|
184 |
|
|
185 |
processAction (clID, serverInfo, clients, rooms) (RemoveRoom) = do
|
1811
|
186 |
processAction (clID, serverInfo, clients, rooms) $ AnswerLobby ["ROOM", "DEL", name room]
|
|
187 |
processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["ROOMABANDONED", name room]
|
1804
|
188 |
return (clID,
|
|
189 |
serverInfo,
|
|
190 |
Data.IntMap.map (\cl -> if roomID cl == rID then cl{roomID = 0, isMaster = False} else cl) clients,
|
1811
|
191 |
delete rID $ adjust (\r -> r{playersIDs = IntSet.union (playersIDs room) (playersIDs r)}) 0 rooms
|
1804
|
192 |
)
|
|
193 |
where
|
1811
|
194 |
room = rooms ! rID
|
1804
|
195 |
rID = roomID client
|
|
196 |
client = clients ! clID
|
|
197 |
|
1813
|
198 |
|
1811
|
199 |
processAction (clID, serverInfo, clients, rooms) (UnreadyRoomClients) = do
|
|
200 |
processAction (clID, serverInfo, clients, rooms) $ AnswerThisRoom ("NOT_READY" : roomPlayers)
|
|
201 |
return (clID,
|
|
202 |
serverInfo,
|
|
203 |
Data.IntMap.map (\cl -> if roomID cl == rID then cl{isReady = False} else cl) clients,
|
|
204 |
rooms)
|
|
205 |
where
|
|
206 |
room = rooms ! rID
|
|
207 |
rID = roomID client
|
|
208 |
client = clients ! clID
|
|
209 |
roomPlayers = Prelude.map (nick . (clients !)) roomPlayersIDs
|
|
210 |
roomPlayersIDs = IntSet.elems $ playersIDs room
|
|
211 |
|
|
212 |
|
1813
|
213 |
processAction (clID, serverInfo, clients, rooms) (RemoveTeam teamName) = do
|
|
214 |
newRooms <- if not $ gameinprogress room then
|
|
215 |
do
|
|
216 |
processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["REMOVE_TEAM", teamName]
|
|
217 |
return $
|
|
218 |
adjust (\r -> r{teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r}) rID rooms
|
|
219 |
else
|
|
220 |
do
|
|
221 |
processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["GAMEMSG", rmTeamMsg]
|
|
222 |
return $
|
|
223 |
adjust (\r -> r{
|
|
224 |
teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r,
|
|
225 |
leftTeams = teamName : leftTeams r,
|
|
226 |
roundMsgs = roundMsgs r Seq.|> rmTeamMsg
|
|
227 |
}) rID rooms
|
|
228 |
return (clID, serverInfo, clients, newRooms)
|
|
229 |
where
|
|
230 |
room = rooms ! rID
|
|
231 |
rID = roomID client
|
|
232 |
client = clients ! clID
|
|
233 |
rmTeamMsg = toEngineMsg $ 'F' : teamName
|
|
234 |
|
|
235 |
|
1804
|
236 |
processAction (clID, serverInfo, clients, rooms) (Dump) = do
|
|
237 |
writeChan (sendChan $ clients ! clID) ["DUMP", show serverInfo, showTree clients, showTree rooms]
|
|
238 |
return (clID, serverInfo, clients, rooms)
|
|
239 |
|