gameServer/HWProtoInRoomState.hs
changeset 2352 7eaf82cf0890
parent 2337 723f1cbe2ef3
child 2381 959da8402cac
equal deleted inserted replaced
2351:a4a17b8df591 2352:7eaf82cf0890
    25 	where
    25 	where
    26 		clientNick = nick $ clients IntMap.! clID
    26 		clientNick = nick $ clients IntMap.! clID
    27 
    27 
    28 
    28 
    29 handleCmd_inRoom clID clients rooms ["PART"] =
    29 handleCmd_inRoom clID clients rooms ["PART"] =
    30 		[RoomRemoveThisClient "part"]
    30 	[RoomRemoveThisClient "part"]
    31 	where
    31 	where
    32 		client = clients IntMap.! clID
    32 		client = clients IntMap.! clID
    33 
    33 
    34 
    34 
    35 handleCmd_inRoom clID clients rooms ("CFG" : paramName : paramStrs) =
    35 handleCmd_inRoom clID clients rooms ("CFG" : paramName : paramStrs)
    36 	if isMaster client then
    36 	| isMaster client =
    37 		[ModifyRoom (\r -> r{params = Map.insert paramName paramStrs (params r)})
    37 		[ModifyRoom (\r -> r{params = Map.insert paramName paramStrs (params r)}),
    38 		, AnswerOthersInRoom ("CFG" : paramName : paramStrs)]
    38 		AnswerOthersInRoom ("CFG" : paramName : paramStrs)]
    39 	else
    39 	| otherwise = [ProtocolError "Not room master"]
    40 		[ProtocolError "Not room master"]
       
    41 	where
    40 	where
    42 		client = clients IntMap.! clID
    41 		client = clients IntMap.! clID
    43 
    42 
    44 handleCmd_inRoom clID clients rooms ("ADD_TEAM" : name : color : grave : fort : voicepack : difStr : hhsInfo)
    43 handleCmd_inRoom clID clients rooms ("ADD_TEAM" : name : color : grave : fort : voicepack : difStr : hhsInfo)
    45 	| length hhsInfo == 16 =
    44 	| length hhsInfo /= 16 = []
    46 	if length (teams room) == 6 then
    45 	| length (teams room) == 6 = [Warning "too many teams"]
    47 		[Warning "too many teams"]
    46 	| canAddNumber <= 0 = [Warning "too many hedgehogs"]
    48 	else if canAddNumber <= 0 then
    47 	| isJust findTeam = [Warning "There's already a team with same name in the list"]
    49 		[Warning "too many hedgehogs"]
    48 	| gameinprogress room = [Warning "round in progress"]
    50 	else if isJust findTeam then
    49 	| isRestrictedTeams room = [Warning "restricted"]
    51 		[Warning "already have a team with same name"]
    50 	| otherwise =
    52 	else if gameinprogress room then
       
    53 		[Warning "round in progress"]
       
    54 	else if isRestrictedTeams room then
       
    55 		[Warning "restricted"]
       
    56 	else
       
    57 		[ModifyRoom (\r -> r{teams = teams r ++ [newTeam]}),
    51 		[ModifyRoom (\r -> r{teams = teams r ++ [newTeam]}),
    58 		ModifyClient (\c -> c{teamsInGame = teamsInGame c + 1}),
    52 		ModifyClient (\c -> c{teamsInGame = teamsInGame c + 1}),
    59 		AnswerThisClient ["TEAM_ACCEPTED", name],
    53 		AnswerThisClient ["TEAM_ACCEPTED", name],
    60 		AnswerOthersInRoom $ teamToNet newTeam,
    54 		AnswerOthersInRoom $ teamToNet newTeam,
    61 		AnswerOthersInRoom ["TEAM_COLOR", name, color]
    55 		AnswerOthersInRoom ["TEAM_COLOR", name, color]
    70 		hhsList [] = []
    64 		hhsList [] = []
    71 		hhsList (n:h:hhs) = HedgehogInfo n h : hhsList hhs
    65 		hhsList (n:h:hhs) = HedgehogInfo n h : hhsList hhs
    72 		newTeamHHNum = min 4 canAddNumber
    66 		newTeamHHNum = min 4 canAddNumber
    73 
    67 
    74 
    68 
    75 handleCmd_inRoom clID clients rooms ["REMOVE_TEAM", teamName] =
    69 handleCmd_inRoom clID clients rooms ["REMOVE_TEAM", teamName]
    76 	if noSuchTeam then
    70 	| noSuchTeam = [Warning "REMOVE_TEAM: no such team"]
    77 		[Warning "REMOVE_TEAM: no such team"]
    71 	| nick client /= teamowner team = [ProtocolError "Not team owner!"]
    78 	else
    72 	| otherwise =
    79 		if not $ nick client == teamowner team then
       
    80 			[ProtocolError "Not team owner!"]
       
    81 		else
       
    82 			[RemoveTeam teamName,
    73 			[RemoveTeam teamName,
    83 			ModifyClient (\c -> c{teamsInGame = teamsInGame c - 1})	
    74 			ModifyClient (\c -> c{teamsInGame = teamsInGame c - 1})
    84 			]
    75 			]
    85 	where
    76 	where
    86 		client = clients IntMap.! clID
    77 		client = clients IntMap.! clID
    87 		room = rooms IntMap.! (roomID client)
    78 		room = rooms IntMap.! (roomID client)
    88 		noSuchTeam = isNothing findTeam
    79 		noSuchTeam = isNothing findTeam
    89 		team = fromJust findTeam
    80 		team = fromJust findTeam
    90 		findTeam = find (\t -> teamName == teamname t) $ teams room
    81 		findTeam = find (\t -> teamName == teamname t) $ teams room
    91 
    82 
    92 
    83 
    93 handleCmd_inRoom clID clients rooms ["HH_NUM", teamName, numberStr] =
    84 handleCmd_inRoom clID clients rooms ["HH_NUM", teamName, numberStr]
    94 	if not $ isMaster client then
    85 	| not $ isMaster client = [ProtocolError "Not room master"]
    95 		[ProtocolError "Not room master"]
    86 	| hhNumber < 1 || hhNumber > 8 || noSuchTeam || hhNumber > (canAddNumber + (hhnum team)) = []
    96 	else
    87 	| otherwise =
    97 		if hhNumber < 1 || hhNumber > 8 || noSuchTeam || hhNumber > (canAddNumber + (hhnum team)) then
    88 		[ModifyRoom $ modifyTeam team{hhnum = hhNumber},
    98 			[]
    89 		AnswerOthersInRoom ["HH_NUM", teamName, show hhNumber]]
    99 		else
       
   100 			[ModifyRoom $ modifyTeam team{hhnum = hhNumber},
       
   101 			AnswerOthersInRoom ["HH_NUM", teamName, show hhNumber]]
       
   102 	where
    90 	where
   103 		client = clients IntMap.! clID
    91 		client = clients IntMap.! clID
   104 		room = rooms IntMap.! (roomID client)
    92 		room = rooms IntMap.! (roomID client)
   105 		hhNumber = fromMaybe 0 (maybeRead numberStr :: Maybe Int)
    93 		hhNumber = fromMaybe 0 (maybeRead numberStr :: Maybe Int)
   106 		noSuchTeam = isNothing findTeam
    94 		noSuchTeam = isNothing findTeam
   107 		team = fromJust findTeam
    95 		team = fromJust findTeam
   108 		findTeam = find (\t -> teamName == teamname t) $ teams room
    96 		findTeam = find (\t -> teamName == teamname t) $ teams room
   109 		canAddNumber = 48 - (sum . map hhnum $ teams room)
    97 		canAddNumber = 48 - (sum . map hhnum $ teams room)
   110 
    98 
   111 
    99 
   112 handleCmd_inRoom clID clients rooms ["TEAM_COLOR", teamName, newColor] =
   100 handleCmd_inRoom clID clients rooms ["TEAM_COLOR", teamName, newColor]
   113 	if not $ isMaster client then
   101 	| not $ isMaster client = [ProtocolError "Not room master"]
   114 		[ProtocolError "Not room master"]
   102 	| noSuchTeam = []
   115 	else
   103 	| otherwise = [ModifyRoom $ modifyTeam team{teamcolor = newColor},
   116 		if noSuchTeam then
       
   117 			[]
       
   118 		else
       
   119 			[ModifyRoom $ modifyTeam team{teamcolor = newColor},
       
   120 			AnswerOthersInRoom ["TEAM_COLOR", teamName, newColor]]
   104 			AnswerOthersInRoom ["TEAM_COLOR", teamName, newColor]]
   121 	where
   105 	where
   122 		noSuchTeam = isNothing findTeam
   106 		noSuchTeam = isNothing findTeam
   123 		team = fromJust findTeam
   107 		team = fromJust findTeam
   124 		findTeam = find (\t -> teamName == teamname t) $ teams room
   108 		findTeam = find (\t -> teamName == teamname t) $ teams room
   127 
   111 
   128 
   112 
   129 handleCmd_inRoom clID clients rooms ["TOGGLE_READY"] =
   113 handleCmd_inRoom clID clients rooms ["TOGGLE_READY"] =
   130 	[ModifyClient (\c -> c{isReady = not $ isReady client}),
   114 	[ModifyClient (\c -> c{isReady = not $ isReady client}),
   131 	ModifyRoom (\r -> r{readyPlayers = readyPlayers r + (if isReady client then -1 else 1)}),
   115 	ModifyRoom (\r -> r{readyPlayers = readyPlayers r + (if isReady client then -1 else 1)}),
   132 	AnswerThisRoom $ [if isReady client then "NOT_READY" else "READY", nick client]]
   116 	AnswerThisRoom [if isReady client then "NOT_READY" else "READY", nick client]]
   133 	where
   117 	where
   134 		client = clients IntMap.! clID
   118 		client = clients IntMap.! clID
   135 
   119 
   136 
   120 
   137 handleCmd_inRoom clID clients rooms ["START_GAME"] =
   121 handleCmd_inRoom clID clients rooms ["START_GAME"] =
   138 	if isMaster client && (playersIn room == readyPlayers room) && (not $ gameinprogress room) then
   122 	if isMaster client && (playersIn room == readyPlayers room) && (not . gameinprogress) room then
   139 		if enoughClans then
   123 		if enoughClans then
   140 			[ModifyRoom
   124 			[ModifyRoom
   141 					(\r -> r{
   125 					(\r -> r{
   142 						gameinprogress = True,
   126 						gameinprogress = True,
   143 						roundMsgs = empty,
   127 						roundMsgs = empty,
   182 		client = clients IntMap.! clID
   166 		client = clients IntMap.! clID
   183 		room = rooms IntMap.! (roomID client)
   167 		room = rooms IntMap.! (roomID client)
   184 		answerRemovedTeams = map (\t -> AnswerThisRoom ["REMOVE_TEAM", t]) $ leftTeams room
   168 		answerRemovedTeams = map (\t -> AnswerThisRoom ["REMOVE_TEAM", t]) $ leftTeams room
   185 
   169 
   186 
   170 
   187 handleCmd_inRoom clID clients _ ["TOGGLE_RESTRICT_JOINS"] =
   171 handleCmd_inRoom clID clients _ ["TOGGLE_RESTRICT_JOINS"]
   188 	if isMaster client then
   172 	| isMaster client = [ModifyRoom (\r -> r{isRestrictedJoins = not $ isRestrictedJoins r})]
   189 		[ModifyRoom (\r -> r{isRestrictedJoins = not $ isRestrictedJoins r})]
   173 	| otherwise = [ProtocolError "Not room master"]
   190 	else
       
   191 		[ProtocolError "Not room master"]
       
   192 	where
   174 	where
   193 		client = clients IntMap.! clID
   175 		client = clients IntMap.! clID
   194 
   176 
   195 
   177 
   196 handleCmd_inRoom clID clients _ ["TOGGLE_RESTRICT_TEAMS"] =
   178 handleCmd_inRoom clID clients _ ["TOGGLE_RESTRICT_TEAMS"]
   197 	if isMaster client then
   179 	| isMaster client = [ModifyRoom (\r -> r{isRestrictedTeams = not $ isRestrictedTeams r})]
   198 		[ModifyRoom (\r -> r{isRestrictedTeams = not $ isRestrictedTeams r})]
   180 	| otherwise = [ProtocolError "Not room master"]
   199 	else
       
   200 		[ProtocolError "Not room master"]
       
   201 	where
   181 	where
   202 		client = clients IntMap.! clID
   182 		client = clients IntMap.! clID
   203 
   183 
   204 handleCmd_inRoom clID clients rooms ["KICK", kickNick] =
   184 handleCmd_inRoom clID clients rooms ["KICK", kickNick] =
   205 	if not $ isMaster client then
   185 	[KickRoomClient kickID | isMaster client && not noSuchClient && (kickID /= clID) && (roomID client == roomID kickClient)]
   206 		[]
       
   207 	else
       
   208 		if noSuchClient then
       
   209 			[]
       
   210 		else
       
   211 			if (kickID == clID) || (roomID client /= roomID kickClient) then
       
   212 				[]
       
   213 			else
       
   214 				[KickRoomClient kickID]
       
   215 	where
   186 	where
   216 		client = clients IntMap.! clID
   187 		client = clients IntMap.! clID
   217 		maybeClient = Foldable.find (\cl -> kickNick == nick cl) clients
   188 		maybeClient = Foldable.find (\cl -> kickNick == nick cl) clients
   218 		noSuchClient = isNothing maybeClient
   189 		noSuchClient = isNothing maybeClient
   219 		kickClient = fromJust maybeClient
   190 		kickClient = fromJust maybeClient