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