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