1804
|
1 |
module HWProtoLobbyState where
|
|
2 |
|
|
3 |
import qualified Data.Map as Map
|
|
4 |
import qualified Data.IntMap as IntMap
|
|
5 |
import qualified Data.IntSet as IntSet
|
1813
|
6 |
import qualified Data.Foldable as Foldable
|
1804
|
7 |
import Maybe
|
|
8 |
import Data.List
|
|
9 |
--------------------------------------
|
|
10 |
import CoreTypes
|
|
11 |
import Actions
|
|
12 |
import Utils
|
|
13 |
|
|
14 |
answerAllTeams teams = concatMap toAnswer teams
|
|
15 |
where
|
|
16 |
toAnswer team =
|
|
17 |
[AnswerThisClient $ teamToNet team,
|
|
18 |
AnswerThisClient ["TEAM_COLOR", teamname team, teamcolor team],
|
|
19 |
AnswerThisClient ["HH_NUM", teamname team, show $ hhnum team]]
|
|
20 |
|
|
21 |
handleCmd_lobby :: CmdHandler
|
|
22 |
|
|
23 |
handleCmd_lobby clID clients rooms ["LIST"] =
|
|
24 |
[AnswerThisClient ("ROOMS" : roomsInfoList)]
|
|
25 |
where
|
|
26 |
roomsInfoList = concatMap roomInfo $ sameProtoRooms
|
|
27 |
sameProtoRooms = filter (\r -> (roomProto r == protocol) && (not $ isRestrictedJoins r)) roomsList
|
|
28 |
roomsList = IntMap.elems rooms
|
|
29 |
protocol = clientProto client
|
|
30 |
client = clients IntMap.! clID
|
|
31 |
roomInfo room = [
|
|
32 |
name room,
|
|
33 |
(show $ playersIn room) ++ "(" ++ (show $ length $ teams room) ++ ")",
|
|
34 |
show $ gameinprogress room
|
|
35 |
]
|
|
36 |
|
1862
|
37 |
|
1815
|
38 |
handleCmd_lobby clID clients _ ["CHAT", msg] =
|
|
39 |
[AnswerOthersInRoom ["CHAT", clientNick, msg]]
|
1804
|
40 |
where
|
|
41 |
clientNick = nick $ clients IntMap.! clID
|
|
42 |
|
1862
|
43 |
|
1804
|
44 |
handleCmd_lobby clID clients rooms ["CREATE", newRoom, roomPassword] =
|
|
45 |
if haveSameRoom then
|
|
46 |
[Warning "Room exists"]
|
|
47 |
else
|
|
48 |
[RoomRemoveThisClient, -- leave lobby
|
|
49 |
AddRoom newRoom roomPassword,
|
|
50 |
AnswerThisClient ["NOT_READY", clientNick]
|
|
51 |
]
|
|
52 |
where
|
|
53 |
clientNick = nick $ clients IntMap.! clID
|
|
54 |
haveSameRoom = isJust $ find (\room -> newRoom == name room) $ IntMap.elems rooms
|
|
55 |
|
1862
|
56 |
|
1804
|
57 |
handleCmd_lobby clID clients rooms ["CREATE", newRoom] =
|
|
58 |
handleCmd_lobby clID clients rooms ["CREATE", newRoom, ""]
|
|
59 |
|
1862
|
60 |
|
1804
|
61 |
handleCmd_lobby clID clients rooms ["JOIN", roomName, roomPassword] =
|
|
62 |
if noSuchRoom then
|
|
63 |
[Warning "No such room"]
|
|
64 |
else if isRestrictedJoins jRoom then
|
|
65 |
[Warning "Joining restricted"]
|
|
66 |
else if roomPassword /= password jRoom then
|
|
67 |
[Warning "Wrong password"]
|
|
68 |
else
|
|
69 |
[RoomRemoveThisClient, -- leave lobby
|
|
70 |
RoomAddThisClient rID] -- join room
|
|
71 |
++ answerNicks
|
|
72 |
++ answerReady
|
|
73 |
++ [AnswerThisRoom ["NOT_READY", nick client]]
|
1871
|
74 |
++ answerFullConfig
|
1804
|
75 |
++ answerTeams
|
1813
|
76 |
++ watchRound
|
1804
|
77 |
where
|
|
78 |
noSuchRoom = isNothing mbRoom
|
|
79 |
mbRoom = find (\r -> roomName == name r && roomProto r == clientProto client) $ IntMap.elems rooms
|
|
80 |
jRoom = fromJust mbRoom
|
|
81 |
rID = roomUID jRoom
|
|
82 |
client = clients IntMap.! clID
|
|
83 |
roomClientsIDs = IntSet.elems $ playersIDs jRoom
|
|
84 |
answerNicks = if playersIn jRoom /= 0 then
|
|
85 |
[AnswerThisClient $ ["JOINED"] ++ (map (\clID -> nick $ clients IntMap.! clID) $ roomClientsIDs)]
|
|
86 |
else
|
|
87 |
[]
|
|
88 |
answerReady =
|
|
89 |
map (\c -> AnswerThisClient [if isReady c then "READY" else "NOT_READY", nick c]) $
|
|
90 |
map (\clID -> clients IntMap.! clID) roomClientsIDs
|
|
91 |
|
|
92 |
toAnswer (paramName, paramStrs) = AnswerThisClient $ "CFG" : paramName : paramStrs
|
1871
|
93 |
|
|
94 |
answerFullConfig = map toAnswer (leftConfigPart ++ rightConfigPart)
|
|
95 |
(leftConfigPart, rightConfigPart) = partition (\(p, _) -> p /= "MAP") (Map.toList $ params jRoom)
|
1813
|
96 |
|
|
97 |
watchRound = if not $ gameinprogress jRoom then
|
1804
|
98 |
[]
|
|
99 |
else
|
1813
|
100 |
[AnswerThisClient ["RUN_GAME"],
|
1866
|
101 |
AnswerThisClient $ "EM" : toEngineMsg "e$spectate 1" : (Foldable.toList $ roundMsgs jRoom)]
|
1813
|
102 |
|
1804
|
103 |
answerTeams = if gameinprogress jRoom then
|
|
104 |
answerAllTeams (teamsAtStart jRoom)
|
|
105 |
else
|
|
106 |
answerAllTeams (teams jRoom)
|
|
107 |
|
|
108 |
|
1862
|
109 |
handleCmd_lobby clID clients rooms ["JOIN", roomName] =
|
|
110 |
handleCmd_lobby clID clients rooms ["JOIN", roomName, ""]
|
|
111 |
|
|
112 |
|
|
113 |
handleCmd_lobby clID clients rooms ["KICK", kickNick] =
|
|
114 |
if not $ isAdministrator client then
|
|
115 |
[]
|
|
116 |
else
|
|
117 |
if noSuchClient then
|
|
118 |
[]
|
|
119 |
else
|
|
120 |
if kickID == clID then
|
|
121 |
[]
|
|
122 |
else
|
|
123 |
[KickClient kickID]
|
|
124 |
where
|
|
125 |
client = clients IntMap.! clID
|
|
126 |
maybeClient = Foldable.find (\cl -> kickNick == nick cl) clients
|
|
127 |
noSuchClient = isNothing maybeClient
|
|
128 |
kickID = clientUID $ fromJust maybeClient
|
1866
|
129 |
|
|
130 |
|
|
131 |
handleCmd_lobby clID clients rooms ["BAN", banNick] =
|
|
132 |
if not $ isAdministrator client then
|
|
133 |
[]
|
|
134 |
else
|
|
135 |
BanClient banNick : handleCmd_lobby clID clients rooms ["KICK", banNick]
|
|
136 |
where
|
|
137 |
client = clients IntMap.! clID
|
1862
|
138 |
|
1804
|
139 |
|
|
140 |
handleCmd_lobby clID _ _ _ = [ProtocolError "Incorrect command (state: in lobby)"]
|