author | unc0rr |
Tue, 08 Feb 2011 22:24:30 +0300 | |
changeset 4936 | d65d438acd23 |
parent 4932 | f11d80bac7ed |
child 4941 | 90572c338e60 |
permissions | -rw-r--r-- |
4295
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
1 |
{-# LANGUAGE OverloadedStrings #-} |
1804 | 2 |
module HWProtoLobbyState where |
3 |
||
4 |
import qualified Data.Map as Map |
|
1813 | 5 |
import qualified Data.Foldable as Foldable |
4295
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
6 |
import Data.Maybe |
1804 | 7 |
import Data.List |
4295
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
8 |
import Control.Monad.Reader |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
9 |
import qualified Data.ByteString.Char8 as B |
1804 | 10 |
-------------------------------------- |
11 |
import CoreTypes |
|
12 |
import Actions |
|
13 |
import Utils |
|
4295
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
14 |
import HandlerUtils |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
15 |
import RoomsAndClients |
1804 | 16 |
|
4932 | 17 |
|
18 |
answerAllTeams :: ClientInfo -> [TeamInfo] -> [Action] |
|
4591 | 19 |
answerAllTeams cl = concatMap toAnswer |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
20 |
where |
4591 | 21 |
clChan = sendChan cl |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
22 |
toAnswer team = |
4591 | 23 |
[AnswerClients [clChan] $ teamToNet team, |
24 |
AnswerClients [clChan] ["TEAM_COLOR", teamname team, teamcolor team], |
|
25 |
AnswerClients [clChan] ["HH_NUM", teamname team, B.pack . show $ hhnum team]] |
|
4568 | 26 |
|
1804 | 27 |
handleCmd_lobby :: CmdHandler |
28 |
||
4295
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
29 |
|
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
30 |
handleCmd_lobby ["LIST"] = do |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
31 |
(ci, irnc) <- ask |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
32 |
let cl = irnc `client` ci |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
33 |
rooms <- allRoomInfos |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
34 |
let roomsInfoList = concatMap (roomInfo irnc) . filter (\r -> (roomProto r == clientProto cl) && not (isRestrictedJoins r)) |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
35 |
return [AnswerClients [sendChan cl] ("ROOMS" : roomsInfoList rooms)] |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
36 |
where |
4932 | 37 |
roomInfo irnc r = [ |
38 |
showB $ gameinprogress r, |
|
39 |
name r, |
|
40 |
showB $ playersIn r, |
|
41 |
showB $ length $ teams r, |
|
42 |
nick $ irnc `client` masterID r, |
|
43 |
head (Map.findWithDefault ["+gen+"] "MAP" (params r)), |
|
44 |
head (Map.findWithDefault ["Default"] "SCHEME" (params r)), |
|
4936
d65d438acd23
Merge MAP, MAPGEN and SEED params into one on room join, so engine isn't spawned three times for a preview. Not tested as I'm unable to see my rooms (why?)
unc0rr
parents:
4932
diff
changeset
|
45 |
head (Map.findWithDefault ["Default"] "AMMO" (params r)), |
d65d438acd23
Merge MAP, MAPGEN and SEED params into one on room join, so engine isn't spawned three times for a preview. Not tested as I'm unable to see my rooms (why?)
unc0rr
parents:
4932
diff
changeset
|
46 |
head (Map.findWithDefault ["Default"] "SCHEME" (params r)), |
d65d438acd23
Merge MAP, MAPGEN and SEED params into one on room join, so engine isn't spawned three times for a preview. Not tested as I'm unable to see my rooms (why?)
unc0rr
parents:
4932
diff
changeset
|
47 |
head (Map.findWithDefault ["0"] "MAPGEN" (params r)), |
d65d438acd23
Merge MAP, MAPGEN and SEED params into one on room join, so engine isn't spawned three times for a preview. Not tested as I'm unable to see my rooms (why?)
unc0rr
parents:
4932
diff
changeset
|
48 |
head (Map.findWithDefault ["seed"] "SEED" (params r)) |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
49 |
] |
3501 | 50 |
|
4295
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
51 |
|
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
52 |
handleCmd_lobby ["CHAT", msg] = do |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
53 |
n <- clientNick |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
54 |
s <- roomOthersChans |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
55 |
return [AnswerClients s ["CHAT", n, msg]] |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
56 |
|
4932 | 57 |
handleCmd_lobby ["CREATE_ROOM", rName, roomPassword] |
58 |
| illegalName rName = return [Warning "Illegal room name"] |
|
4295
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
59 |
| otherwise = do |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
60 |
rs <- allRoomInfos |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
61 |
cl <- thisClient |
4932 | 62 |
return $ if isJust $ find (\r -> rName == name r) rs then |
4295
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
63 |
[Warning "Room exists"] |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
64 |
else |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
65 |
[ |
4932 | 66 |
AddRoom rName roomPassword, |
4917
8ff92bdc9f98
Convert READY and NOT_READY messages to CLIENT_FLAGS message
unc0rr
parents:
4914
diff
changeset
|
67 |
AnswerClients [sendChan cl] ["CLIENT_FLAGS", "-r", nick cl] |
4295
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
68 |
] |
1804 | 69 |
|
3536 | 70 |
|
4932 | 71 |
handleCmd_lobby ["CREATE_ROOM", rName] = |
72 |
handleCmd_lobby ["CREATE_ROOM", rName, ""] |
|
3536 | 73 |
|
1862 | 74 |
|
4295
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
75 |
handleCmd_lobby ["JOIN_ROOM", roomName, roomPassword] = do |
4932 | 76 |
(_, irnc) <- ask |
4295
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
77 |
let ris = allRooms irnc |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
78 |
cl <- thisClient |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
79 |
let maybeRI = find (\ri -> roomName == name (irnc `room` ri)) ris |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
80 |
let jRI = fromJust maybeRI |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
81 |
let jRoom = irnc `room` jRI |
4597
31e042ab870c
Finally a solution for excess lazyness when working with unsafeThaw'ed arrays
unc0rr
parents:
4595
diff
changeset
|
82 |
let jRoomClients = map (client irnc) $ roomClients irnc jRI |
31e042ab870c
Finally a solution for excess lazyness when working with unsafeThaw'ed arrays
unc0rr
parents:
4595
diff
changeset
|
83 |
let nicks = map nick jRoomClients |
31e042ab870c
Finally a solution for excess lazyness when working with unsafeThaw'ed arrays
unc0rr
parents:
4595
diff
changeset
|
84 |
let chans = map sendChan (cl : jRoomClients) |
4295
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
85 |
return $ |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
86 |
if isNothing maybeRI then |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
87 |
[Warning "No such rooms"] |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
88 |
else if isRestrictedJoins jRoom then |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
89 |
[Warning "Joining restricted"] |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
90 |
else if roomPassword /= password jRoom then |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
91 |
[Warning "Wrong password"] |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
92 |
else |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
93 |
[ |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
94 |
MoveToRoom jRI, |
4597
31e042ab870c
Finally a solution for excess lazyness when working with unsafeThaw'ed arrays
unc0rr
parents:
4595
diff
changeset
|
95 |
AnswerClients [sendChan cl] $ "JOINED" : nicks, |
4917
8ff92bdc9f98
Convert READY and NOT_READY messages to CLIENT_FLAGS message
unc0rr
parents:
4914
diff
changeset
|
96 |
AnswerClients chans ["CLIENT_FLAGS", "-r", nick cl] |
4295
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
97 |
] |
4932 | 98 |
++ map (readynessMessage cl) jRoomClients |
99 |
++ answerFullConfig cl (params jRoom) |
|
100 |
++ answerTeams cl jRoom |
|
101 |
++ watchRound cl jRoom |
|
1804 | 102 |
|
4587 | 103 |
where |
4917
8ff92bdc9f98
Convert READY and NOT_READY messages to CLIENT_FLAGS message
unc0rr
parents:
4914
diff
changeset
|
104 |
readynessMessage cl c = AnswerClients [sendChan cl] ["CLIENT_FLAGS", if isReady c then "+r" else "-r", nick c] |
3536 | 105 |
|
4587 | 106 |
toAnswer cl (paramName, paramStrs) = AnswerClients [sendChan cl] $ "CFG" : paramName : paramStrs |
1813 | 107 |
|
4936
d65d438acd23
Merge MAP, MAPGEN and SEED params into one on room join, so engine isn't spawned three times for a preview. Not tested as I'm unable to see my rooms (why?)
unc0rr
parents:
4932
diff
changeset
|
108 |
answerFullConfig cl pr = map (toAnswer cl) $ |
d65d438acd23
Merge MAP, MAPGEN and SEED params into one on room join, so engine isn't spawned three times for a preview. Not tested as I'm unable to see my rooms (why?)
unc0rr
parents:
4932
diff
changeset
|
109 |
("FULLMAPCONFIG", concatMap ((Map.!) pr) ["MAP", "MAPGEN", "SEED"]) |
d65d438acd23
Merge MAP, MAPGEN and SEED params into one on room join, so engine isn't spawned three times for a preview. Not tested as I'm unable to see my rooms (why?)
unc0rr
parents:
4932
diff
changeset
|
110 |
: ("SCHEME", pr Map.! "SCHEME") |
d65d438acd23
Merge MAP, MAPGEN and SEED params into one on room join, so engine isn't spawned three times for a preview. Not tested as I'm unable to see my rooms (why?)
unc0rr
parents:
4932
diff
changeset
|
111 |
: (filter (\(p, _) -> p /= "SCHEME" && p /= "MAP" && p /= "MAPGEN" && p /= "SEED") $ Map.toList pr) |
4587 | 112 |
|
4591 | 113 |
answerTeams cl jRoom = let f = if gameinprogress jRoom then teamsAtStart else teams in answerAllTeams cl $ f jRoom |
114 |
||
4595 | 115 |
watchRound cl jRoom = if not $ gameinprogress jRoom then |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
116 |
[] |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
117 |
else |
4595 | 118 |
[AnswerClients [sendChan cl] ["RUN_GAME"], |
119 |
AnswerClients [sendChan cl] $ "EM" : toEngineMsg "e$spectate 1" : Foldable.toList (roundMsgs jRoom)] |
|
1813 | 120 |
|
3536 | 121 |
|
4295
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
122 |
handleCmd_lobby ["JOIN_ROOM", roomName] = |
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
123 |
handleCmd_lobby ["JOIN_ROOM", roomName, ""] |
4568 | 124 |
|
1804 | 125 |
|
4616 | 126 |
handleCmd_lobby ["FOLLOW", asknick] = do |
127 |
(_, rnc) <- ask |
|
128 |
ci <- clientByNick asknick |
|
129 |
let ri = clientRoom rnc $ fromJust ci |
|
130 |
let clRoom = room rnc ri |
|
131 |
if isNothing ci || ri == lobbyId then |
|
132 |
return [] |
|
133 |
else |
|
134 |
handleCmd_lobby ["JOIN_ROOM", name clRoom] |
|
1862 | 135 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
136 |
--------------------------- |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2408
diff
changeset
|
137 |
-- Administrator's stuff -- |
1862 | 138 |
|
4618 | 139 |
handleCmd_lobby ["KICK", kickNick] = do |
140 |
(ci, _) <- ask |
|
141 |
cl <- thisClient |
|
142 |
kickId <- clientByNick kickNick |
|
143 |
return [KickClient $ fromJust kickId | isAdministrator cl && isJust kickId && fromJust kickId /= ci] |
|
1866 | 144 |
|
4909 | 145 |
|
146 |
handleCmd_lobby ["BAN", banNick, reason] = do |
|
147 |
(ci, _) <- ask |
|
148 |
cl <- thisClient |
|
149 |
banId <- clientByNick banNick |
|
150 |
return [BanClient 60 reason (fromJust banId) | isAdministrator cl && isJust banId && fromJust banId /= ci] |
|
1804 | 151 |
|
3283 | 152 |
|
4620 | 153 |
handleCmd_lobby ["SET_SERVER_VAR", "MOTD_NEW", newMessage] = do |
154 |
cl <- thisClient |
|
155 |
return [ModifyServerInfo (\si -> si{serverMessage = newMessage}) | isAdministrator cl] |
|
1925 | 156 |
|
4620 | 157 |
handleCmd_lobby ["SET_SERVER_VAR", "MOTD_OLD", newMessage] = do |
158 |
cl <- thisClient |
|
159 |
return [ModifyServerInfo (\si -> si{serverMessageForOldVersions = newMessage}) | isAdministrator cl] |
|
3260
b44b88908758
Allow to set motd for old client versions (not used yet, as server needs some refactoring)
unc0rr
parents:
2961
diff
changeset
|
160 |
|
4620 | 161 |
handleCmd_lobby ["SET_SERVER_VAR", "LATEST_PROTO", protoNum] = do |
162 |
cl <- thisClient |
|
163 |
return [ModifyServerInfo (\si -> si{latestReleaseVersion = readNum}) | isAdministrator cl && readNum > 0] |
|
3260
b44b88908758
Allow to set motd for old client versions (not used yet, as server needs some refactoring)
unc0rr
parents:
2961
diff
changeset
|
164 |
where |
4620 | 165 |
readNum = case B.readInt protoNum of |
166 |
Just (i, t) | B.null t -> fromIntegral i |
|
4932 | 167 |
_ -> 0 |
1925 | 168 |
|
4620 | 169 |
handleCmd_lobby ["GET_SERVER_VAR"] = do |
170 |
cl <- thisClient |
|
171 |
return [SendServerVars | isAdministrator cl] |
|
3283 | 172 |
|
4620 | 173 |
handleCmd_lobby ["CLEAR_ACCOUNTS_CACHE"] = do |
174 |
cl <- thisClient |
|
175 |
return [ClearAccountsCache | isAdministrator cl] |
|
3283 | 176 |
|
4914 | 177 |
handleCmd_lobby ["RESTART_SERVER", restartType] = do |
178 |
cl <- thisClient |
|
179 |
return [RestartServer f | let f = restartType == "FORCE", isAdministrator cl] |
|
180 |
||
3283 | 181 |
|
4295
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
182 |
handleCmd_lobby _ = return [ProtocolError "Incorrect command (state: in lobby)"] |