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