author | unc0rr |
Thu, 26 Mar 2009 19:28:56 +0000 | |
changeset 1923 | 956b6b3529bc |
parent 1921 | 2a09f7f786a0 |
child 1925 | ec923e56c444 |
permissions | -rw-r--r-- |
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 |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
8 |
import System.Log.Logger |
1804 | 9 |
import Monad |
10 |
----------------------------- |
|
11 |
import CoreTypes |
|
1813 | 12 |
import Utils |
1804 | 13 |
|
14 |
data Action = |
|
15 |
AnswerThisClient [String] |
|
16 |
| AnswerAll [String] |
|
17 |
| AnswerAllOthers [String] |
|
18 |
| AnswerThisRoom [String] |
|
19 |
| AnswerOthersInRoom [String] |
|
20 |
| AnswerLobby [String] |
|
1923 | 21 |
| SendServerMessage |
1804 | 22 |
| RoomAddThisClient Int -- roomID |
23 |
| RoomRemoveThisClient |
|
1813 | 24 |
| RemoveTeam String |
1804 | 25 |
| RemoveRoom |
1811 | 26 |
| UnreadyRoomClients |
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
27 |
| MoveToLobby |
1804 | 28 |
| ProtocolError String |
29 |
| Warning String |
|
30 |
| ByeClient String |
|
1862 | 31 |
| KickClient Int -- clID |
1879 | 32 |
| KickRoomClient Int -- clID |
1921 | 33 |
| BanClient String -- nick |
1879 | 34 |
| RemoveClientTeams Int -- clID |
1804 | 35 |
| ModifyClient (ClientInfo -> ClientInfo) |
36 |
| ModifyRoom (RoomInfo -> RoomInfo) |
|
37 |
| AddRoom String String |
|
1834 | 38 |
| CheckRegistered |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
39 |
| ProcessAccountInfo AccountInfo |
1804 | 40 |
| Dump |
41 |
||
42 |
type CmdHandler = Int -> Clients -> Rooms -> [String] -> [Action] |
|
43 |
||
1862 | 44 |
replaceID a (b, c, d, e) = (a, c, d, e) |
1804 | 45 |
|
46 |
processAction :: (Int, ServerInfo, Clients, Rooms) -> Action -> IO (Int, ServerInfo, Clients, Rooms) |
|
47 |
||
48 |
||
49 |
processAction (clID, serverInfo, clients, rooms) (AnswerThisClient msg) = do |
|
50 |
writeChan (sendChan $ clients ! clID) msg |
|
51 |
return (clID, serverInfo, clients, rooms) |
|
52 |
||
53 |
||
54 |
processAction (clID, serverInfo, clients, rooms) (AnswerAll msg) = do |
|
55 |
mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) (keys clients) |
|
56 |
return (clID, serverInfo, clients, rooms) |
|
57 |
||
58 |
||
59 |
processAction (clID, serverInfo, clients, rooms) (AnswerAllOthers msg) = do |
|
1879 | 60 |
mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) $ |
61 |
Prelude.filter (\id' -> (id' /= clID) && (logonPassed $ clients ! id')) (keys clients) |
|
1804 | 62 |
return (clID, serverInfo, clients, rooms) |
63 |
||
64 |
||
65 |
processAction (clID, serverInfo, clients, rooms) (AnswerThisRoom msg) = do |
|
66 |
mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) 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) (AnswerOthersInRoom msg) = do |
|
76 |
mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) $ Prelude.filter (/= clID) roomClients |
|
77 |
return (clID, serverInfo, clients, rooms) |
|
78 |
where |
|
79 |
roomClients = IntSet.elems $ playersIDs room |
|
80 |
room = rooms ! rID |
|
81 |
rID = roomID client |
|
82 |
client = clients ! clID |
|
83 |
||
84 |
||
85 |
processAction (clID, serverInfo, clients, rooms) (AnswerLobby msg) = do |
|
86 |
mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) roomClients |
|
87 |
return (clID, serverInfo, clients, rooms) |
|
88 |
where |
|
89 |
roomClients = IntSet.elems $ playersIDs room |
|
90 |
room = rooms ! 0 |
|
91 |
||
92 |
||
1923 | 93 |
processAction (clID, serverInfo, clients, rooms) SendServerMessage = do |
94 |
writeChan (sendChan $ clients ! clID) $ ["SERVER_MESSAGE", serverMessage serverInfo] |
|
95 |
return (clID, serverInfo, clients, rooms) |
|
96 |
||
97 |
||
1804 | 98 |
processAction (clID, serverInfo, clients, rooms) (ProtocolError msg) = do |
99 |
writeChan (sendChan $ clients ! clID) ["ERROR", msg] |
|
100 |
return (clID, serverInfo, clients, rooms) |
|
101 |
||
102 |
||
103 |
processAction (clID, serverInfo, clients, rooms) (Warning msg) = do |
|
104 |
writeChan (sendChan $ clients ! clID) ["WARNING", msg] |
|
105 |
return (clID, serverInfo, clients, rooms) |
|
106 |
||
107 |
||
108 |
processAction (clID, serverInfo, clients, rooms) (ByeClient msg) = do |
|
109 |
mapM_ (processAction (clID, serverInfo, clients, rooms)) $ answerOthersQuit ++ answerInformRoom |
|
110 |
writeChan (sendChan $ clients ! clID) ["BYE"] |
|
111 |
return ( |
|
112 |
0, |
|
113 |
serverInfo, |
|
114 |
delete clID clients, |
|
1823 | 115 |
adjust (\r -> r{ |
116 |
playersIDs = IntSet.delete clID (playersIDs r), |
|
117 |
playersIn = (playersIn r) - 1, |
|
118 |
readyPlayers = if isReady client then readyPlayers r - 1 else readyPlayers r |
|
119 |
}) rID rooms |
|
1804 | 120 |
) |
121 |
where |
|
122 |
client = clients ! clID |
|
123 |
rID = roomID client |
|
124 |
clientNick = nick client |
|
125 |
answerInformRoom = |
|
126 |
if roomID client /= 0 then |
|
127 |
if not $ Prelude.null msg then |
|
128 |
[AnswerThisRoom ["LEFT", clientNick, msg]] |
|
129 |
else |
|
130 |
[AnswerThisRoom ["LEFT", clientNick]] |
|
131 |
else |
|
132 |
[] |
|
133 |
answerOthersQuit = |
|
1846 | 134 |
if logonPassed client then |
1804 | 135 |
if not $ Prelude.null msg then |
136 |
[AnswerAll ["LOBBY:LEFT", clientNick, msg]] |
|
137 |
else |
|
138 |
[AnswerAll ["LOBBY:LEFT", clientNick]] |
|
139 |
else |
|
140 |
[] |
|
141 |
||
142 |
||
143 |
processAction (clID, serverInfo, clients, rooms) (ModifyClient func) = do |
|
144 |
return (clID, serverInfo, adjust func clID clients, rooms) |
|
145 |
||
146 |
||
147 |
processAction (clID, serverInfo, clients, rooms) (ModifyRoom func) = do |
|
148 |
return (clID, serverInfo, clients, adjust func rID rooms) |
|
149 |
where |
|
150 |
rID = roomID $ clients ! clID |
|
151 |
||
152 |
||
153 |
processAction (clID, serverInfo, clients, rooms) (RoomAddThisClient rID) = do |
|
154 |
processAction ( |
|
155 |
clID, |
|
156 |
serverInfo, |
|
157 |
adjust (\cl -> cl{roomID = rID}) clID clients, |
|
158 |
adjust (\r -> r{playersIDs = IntSet.insert clID (playersIDs r), playersIn = (playersIn r) + 1}) rID $ |
|
159 |
adjust (\r -> r{playersIDs = IntSet.delete clID (playersIDs r)}) 0 rooms |
|
160 |
) joinMsg |
|
161 |
where |
|
162 |
client = clients ! clID |
|
163 |
joinMsg = if rID == 0 then |
|
164 |
AnswerAllOthers ["LOBBY:JOINED", nick client] |
|
165 |
else |
|
166 |
AnswerThisRoom ["JOINED", nick client] |
|
167 |
||
168 |
||
169 |
processAction (clID, serverInfo, clients, rooms) (RoomRemoveThisClient) = do |
|
170 |
when (rID /= 0) $ (processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["LEFT", nick client, "part"]) >> return () |
|
171 |
return ( |
|
172 |
clID, |
|
173 |
serverInfo, |
|
174 |
adjust (\cl -> cl{roomID = 0}) clID clients, |
|
1823 | 175 |
adjust (\r -> r{ |
176 |
playersIDs = IntSet.delete clID (playersIDs r), |
|
177 |
playersIn = (playersIn r) - 1, |
|
1827 | 178 |
readyPlayers = if isReady client then (readyPlayers r) - 1 else readyPlayers r |
1823 | 179 |
}) rID $ |
1804 | 180 |
adjust (\r -> r{playersIDs = IntSet.insert clID (playersIDs r)}) 0 rooms |
181 |
) |
|
182 |
where |
|
183 |
rID = roomID client |
|
184 |
client = clients ! clID |
|
185 |
||
186 |
||
187 |
processAction (clID, serverInfo, clients, rooms) (AddRoom roomName roomPassword) = do |
|
188 |
let newServerInfo = serverInfo {nextRoomID = newID} |
|
189 |
let room = newRoom{ |
|
190 |
roomUID = newID, |
|
191 |
name = roomName, |
|
192 |
password = roomPassword, |
|
193 |
roomProto = (clientProto client) |
|
194 |
} |
|
195 |
||
196 |
processAction (clID, serverInfo, clients, rooms) $ AnswerLobby ["ROOM", "ADD", roomName] |
|
197 |
||
198 |
processAction ( |
|
199 |
clID, |
|
200 |
newServerInfo, |
|
201 |
adjust (\cl -> cl{isMaster = True}) clID clients, |
|
202 |
insert newID room rooms |
|
203 |
) $ RoomAddThisClient newID |
|
204 |
where |
|
205 |
newID = (nextRoomID serverInfo) - 1 |
|
206 |
client = clients ! clID |
|
207 |
||
208 |
||
209 |
processAction (clID, serverInfo, clients, rooms) (RemoveRoom) = do |
|
1811 | 210 |
processAction (clID, serverInfo, clients, rooms) $ AnswerLobby ["ROOM", "DEL", name room] |
211 |
processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["ROOMABANDONED", name room] |
|
1804 | 212 |
return (clID, |
213 |
serverInfo, |
|
1827 | 214 |
Data.IntMap.map (\cl -> if roomID cl == rID then cl{roomID = 0, isMaster = False, isReady = False} else cl) clients, |
1811 | 215 |
delete rID $ adjust (\r -> r{playersIDs = IntSet.union (playersIDs room) (playersIDs r)}) 0 rooms |
1804 | 216 |
) |
217 |
where |
|
1811 | 218 |
room = rooms ! rID |
1804 | 219 |
rID = roomID client |
220 |
client = clients ! clID |
|
221 |
||
1813 | 222 |
|
1811 | 223 |
processAction (clID, serverInfo, clients, rooms) (UnreadyRoomClients) = do |
224 |
processAction (clID, serverInfo, clients, rooms) $ AnswerThisRoom ("NOT_READY" : roomPlayers) |
|
225 |
return (clID, |
|
226 |
serverInfo, |
|
227 |
Data.IntMap.map (\cl -> if roomID cl == rID then cl{isReady = False} else cl) clients, |
|
1827 | 228 |
adjust (\r -> r{readyPlayers = 0}) rID rooms) |
1811 | 229 |
where |
230 |
room = rooms ! rID |
|
231 |
rID = roomID client |
|
232 |
client = clients ! clID |
|
233 |
roomPlayers = Prelude.map (nick . (clients !)) roomPlayersIDs |
|
234 |
roomPlayersIDs = IntSet.elems $ playersIDs room |
|
235 |
||
236 |
||
1813 | 237 |
processAction (clID, serverInfo, clients, rooms) (RemoveTeam teamName) = do |
238 |
newRooms <- if not $ gameinprogress room then |
|
239 |
do |
|
240 |
processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["REMOVE_TEAM", teamName] |
|
241 |
return $ |
|
242 |
adjust (\r -> r{teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r}) rID rooms |
|
243 |
else |
|
244 |
do |
|
1866 | 245 |
processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["EM", rmTeamMsg] |
1813 | 246 |
return $ |
247 |
adjust (\r -> r{ |
|
248 |
teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r, |
|
249 |
leftTeams = teamName : leftTeams r, |
|
250 |
roundMsgs = roundMsgs r Seq.|> rmTeamMsg |
|
251 |
}) rID rooms |
|
252 |
return (clID, serverInfo, clients, newRooms) |
|
253 |
where |
|
254 |
room = rooms ! rID |
|
255 |
rID = roomID client |
|
256 |
client = clients ! clID |
|
257 |
rmTeamMsg = toEngineMsg $ 'F' : teamName |
|
258 |
||
259 |
||
1834 | 260 |
processAction (clID, serverInfo, clients, rooms) (CheckRegistered) = do |
1921 | 261 |
writeChan (dbQueries serverInfo) $ CheckAccount client |
1834 | 262 |
return (clID, serverInfo, clients, rooms) |
263 |
where |
|
264 |
client = clients ! clID |
|
265 |
||
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
266 |
|
1804 | 267 |
processAction (clID, serverInfo, clients, rooms) (Dump) = do |
268 |
writeChan (sendChan $ clients ! clID) ["DUMP", show serverInfo, showTree clients, showTree rooms] |
|
269 |
return (clID, serverInfo, clients, rooms) |
|
270 |
||
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
271 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
272 |
processAction (clID, serverInfo, clients, rooms) (ProcessAccountInfo info) = do |
1923 | 273 |
processAction (clID, serverInfo, clients, rooms) SendServerMessage |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
274 |
case info of |
1847
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1846
diff
changeset
|
275 |
HasAccount passwd isAdmin -> do |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
276 |
infoM "Clients" $ show clID ++ " has account" |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
277 |
writeChan (sendChan $ clients ! clID) ["ASKPASSWORD"] |
1847
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1846
diff
changeset
|
278 |
return (clID, serverInfo, adjust (\cl -> cl{webPassword = passwd, isAdministrator = isAdmin}) clID clients, rooms) |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
279 |
Guest -> do |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
280 |
infoM "Clients" $ show clID ++ " is guest" |
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
281 |
processAction (clID, serverInfo, adjust (\cl -> cl{logonPassed = True}) clID clients, rooms) MoveToLobby |
1921 | 282 |
Admin -> do |
283 |
infoM "Clients" $ show clID ++ " is admin" |
|
284 |
foldM processAction (clID, serverInfo, adjust (\cl -> cl{logonPassed = True, isAdministrator = True}) clID clients, rooms) [MoveToLobby, AnswerThisClient ["ADMIN_ACCESS"]] |
|
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
285 |
|
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
286 |
|
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
287 |
processAction (clID, serverInfo, clients, rooms) (MoveToLobby) = do |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
288 |
foldM processAction (clID, serverInfo, clients, rooms) $ |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
289 |
(RoomAddThisClient 0) |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
290 |
: answerLobbyNicks |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
291 |
-- ++ (answerServerMessage client clients) |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
292 |
where |
1846 | 293 |
lobbyNicks = Prelude.map nick $ Prelude.filter logonPassed $ elems clients |
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
294 |
answerLobbyNicks = if not $ Prelude.null lobbyNicks then |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
295 |
[AnswerThisClient (["LOBBY:JOINED"] ++ lobbyNicks)] |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
296 |
else |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
297 |
[] |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
298 |
|
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
299 |
|
1862 | 300 |
processAction (clID, serverInfo, clients, rooms) (KickClient kickID) = do |
1879 | 301 |
liftM2 replaceID (return clID) (processAction (kickID, serverInfo, clients, rooms) $ ByeClient "Kicked") |
302 |
||
1921 | 303 |
processAction (clID, serverInfo, clients, rooms) (BanClient banNick) = do |
304 |
return (clID, serverInfo, clients, rooms) |
|
305 |
||
1879 | 306 |
|
307 |
processAction (clID, serverInfo, clients, rooms) (KickRoomClient kickID) = do |
|
308 |
writeChan (sendChan $ clients ! kickID) ["KICKED"] |
|
309 |
liftM2 replaceID (return clID) (processAction (kickID, serverInfo, clients, rooms) $ RoomRemoveThisClient) |
|
310 |
||
311 |
||
312 |
processAction (clID, serverInfo, clients, rooms) (RemoveClientTeams teamsClID) = do |
|
313 |
liftM2 replaceID (return clID) $ |
|
314 |
foldM processAction (teamsClID, serverInfo, clients, rooms) $ removeTeamsActions |
|
315 |
where |
|
316 |
client = clients ! teamsClID |
|
317 |
room = rooms ! (roomID client) |
|
318 |
teamsToRemove = Prelude.filter (\t -> teamowner t == nick client) $ teams room |
|
319 |
removeTeamsActions = Prelude.map (RemoveTeam . teamname) teamsToRemove |