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