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