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