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
|
|
6 |
import Maybe
|
|
7 |
import Data.List
|
|
8 |
--------------------------------------
|
|
9 |
import CoreTypes
|
|
10 |
import Actions
|
|
11 |
import Utils
|
|
12 |
|
|
13 |
answerAllTeams teams = concatMap toAnswer teams
|
|
14 |
where
|
|
15 |
toAnswer team =
|
|
16 |
[AnswerThisClient $ teamToNet team,
|
|
17 |
AnswerThisClient ["TEAM_COLOR", teamname team, teamcolor team],
|
|
18 |
AnswerThisClient ["HH_NUM", teamname team, show $ hhnum team]]
|
|
19 |
|
|
20 |
handleCmd_lobby :: CmdHandler
|
|
21 |
|
|
22 |
handleCmd_lobby clID clients rooms ["LIST"] =
|
|
23 |
[AnswerThisClient ("ROOMS" : roomsInfoList)]
|
|
24 |
where
|
|
25 |
roomsInfoList = concatMap roomInfo $ sameProtoRooms
|
|
26 |
sameProtoRooms = filter (\r -> (roomProto r == protocol) && (not $ isRestrictedJoins r)) roomsList
|
|
27 |
roomsList = IntMap.elems rooms
|
|
28 |
protocol = clientProto client
|
|
29 |
client = clients IntMap.! clID
|
|
30 |
roomInfo room = [
|
|
31 |
name room,
|
|
32 |
(show $ playersIn room) ++ "(" ++ (show $ length $ teams room) ++ ")",
|
|
33 |
show $ gameinprogress room
|
|
34 |
]
|
|
35 |
|
|
36 |
handleCmd_lobby clID clients _ ["CHAT_STRING", msg] =
|
|
37 |
[AnswerOthersInRoom ["CHAT_STRING", clientNick, msg]]
|
|
38 |
where
|
|
39 |
clientNick = nick $ clients IntMap.! clID
|
|
40 |
|
|
41 |
handleCmd_lobby clID clients rooms ["CREATE", newRoom, roomPassword] =
|
|
42 |
if haveSameRoom then
|
|
43 |
[Warning "Room exists"]
|
|
44 |
else
|
|
45 |
[RoomRemoveThisClient, -- leave lobby
|
|
46 |
AddRoom newRoom roomPassword,
|
|
47 |
AnswerThisClient ["NOT_READY", clientNick]
|
|
48 |
]
|
|
49 |
where
|
|
50 |
clientNick = nick $ clients IntMap.! clID
|
|
51 |
haveSameRoom = isJust $ find (\room -> newRoom == name room) $ IntMap.elems rooms
|
|
52 |
|
|
53 |
handleCmd_lobby clID clients rooms ["CREATE", newRoom] =
|
|
54 |
handleCmd_lobby clID clients rooms ["CREATE", newRoom, ""]
|
|
55 |
|
|
56 |
handleCmd_lobby clID clients rooms ["JOIN", roomName, roomPassword] =
|
|
57 |
if noSuchRoom then
|
|
58 |
[Warning "No such room"]
|
|
59 |
else if isRestrictedJoins jRoom then
|
|
60 |
[Warning "Joining restricted"]
|
|
61 |
else if roomPassword /= password jRoom then
|
|
62 |
[Warning "Wrong password"]
|
|
63 |
else
|
|
64 |
[RoomRemoveThisClient, -- leave lobby
|
|
65 |
RoomAddThisClient rID] -- join room
|
|
66 |
++ answerNicks
|
|
67 |
++ answerReady
|
|
68 |
++ [AnswerThisRoom ["NOT_READY", nick client]]
|
|
69 |
++ answerFullConfig jRoom
|
|
70 |
++ answerTeams
|
|
71 |
-- ++ watchRound)
|
|
72 |
where
|
|
73 |
noSuchRoom = isNothing mbRoom
|
|
74 |
mbRoom = find (\r -> roomName == name r && roomProto r == clientProto client) $ IntMap.elems rooms
|
|
75 |
jRoom = fromJust mbRoom
|
|
76 |
rID = roomUID jRoom
|
|
77 |
client = clients IntMap.! clID
|
|
78 |
roomClientsIDs = IntSet.elems $ playersIDs jRoom
|
|
79 |
answerNicks = if playersIn jRoom /= 0 then
|
|
80 |
[AnswerThisClient $ ["JOINED"] ++ (map (\clID -> nick $ clients IntMap.! clID) $ roomClientsIDs)]
|
|
81 |
else
|
|
82 |
[]
|
|
83 |
answerReady =
|
|
84 |
map (\c -> AnswerThisClient [if isReady c then "READY" else "NOT_READY", nick c]) $
|
|
85 |
map (\clID -> clients IntMap.! clID) roomClientsIDs
|
|
86 |
|
|
87 |
toAnswer (paramName, paramStrs) = AnswerThisClient $ "CFG" : paramName : paramStrs
|
|
88 |
answerFullConfig room = map toAnswer (Map.toList $ params room)
|
|
89 |
{-
|
|
90 |
watchRound = if (roomProto clRoom < 20) || (not $ gameinprogress clRoom) then
|
|
91 |
[]
|
|
92 |
else
|
|
93 |
(answerClientOnly ["RUN_GAME"]) ++
|
|
94 |
answerClientOnly ("GAMEMSG" : toEngineMsg "e$spectate 1" : (toList $ roundMsgs clRoom)) -}
|
|
95 |
answerTeams = if gameinprogress jRoom then
|
|
96 |
answerAllTeams (teamsAtStart jRoom)
|
|
97 |
else
|
|
98 |
answerAllTeams (teams jRoom)
|
|
99 |
|
|
100 |
|
|
101 |
handleCmd_lobby client clients rooms ["JOIN", roomName] =
|
|
102 |
handleCmd_lobby client clients rooms ["JOIN", roomName, ""]
|
|
103 |
|
|
104 |
handleCmd_lobby clID _ _ _ = [ProtocolError "Incorrect command (state: in lobby)"]
|