gameServer/HWProtoInRoomState.hs
changeset 2352 7eaf82cf0890
parent 2337 723f1cbe2ef3
child 2381 959da8402cac
--- a/gameServer/HWProtoInRoomState.hs	Fri Sep 04 16:39:51 2009 +0000
+++ b/gameServer/HWProtoInRoomState.hs	Fri Sep 04 16:50:52 2009 +0000
@@ -27,33 +27,27 @@
 
 
 handleCmd_inRoom clID clients rooms ["PART"] =
-		[RoomRemoveThisClient "part"]
+	[RoomRemoveThisClient "part"]
 	where
 		client = clients IntMap.! clID
 
 
-handleCmd_inRoom clID clients rooms ("CFG" : paramName : paramStrs) =
-	if isMaster client then
-		[ModifyRoom (\r -> r{params = Map.insert paramName paramStrs (params r)})
-		, AnswerOthersInRoom ("CFG" : paramName : paramStrs)]
-	else
-		[ProtocolError "Not room master"]
+handleCmd_inRoom clID clients rooms ("CFG" : paramName : paramStrs)
+	| isMaster client =
+		[ModifyRoom (\r -> r{params = Map.insert paramName paramStrs (params r)}),
+		AnswerOthersInRoom ("CFG" : paramName : paramStrs)]
+	| otherwise = [ProtocolError "Not room master"]
 	where
 		client = clients IntMap.! clID
 
 handleCmd_inRoom clID clients rooms ("ADD_TEAM" : name : color : grave : fort : voicepack : difStr : hhsInfo)
-	| length hhsInfo == 16 =
-	if length (teams room) == 6 then
-		[Warning "too many teams"]
-	else if canAddNumber <= 0 then
-		[Warning "too many hedgehogs"]
-	else if isJust findTeam then
-		[Warning "already have a team with same name"]
-	else if gameinprogress room then
-		[Warning "round in progress"]
-	else if isRestrictedTeams room then
-		[Warning "restricted"]
-	else
+	| length hhsInfo /= 16 = []
+	| length (teams room) == 6 = [Warning "too many teams"]
+	| canAddNumber <= 0 = [Warning "too many hedgehogs"]
+	| isJust findTeam = [Warning "There's already a team with same name in the list"]
+	| gameinprogress room = [Warning "round in progress"]
+	| isRestrictedTeams room = [Warning "restricted"]
+	| otherwise =
 		[ModifyRoom (\r -> r{teams = teams r ++ [newTeam]}),
 		ModifyClient (\c -> c{teamsInGame = teamsInGame c + 1}),
 		AnswerThisClient ["TEAM_ACCEPTED", name],
@@ -72,15 +66,12 @@
 		newTeamHHNum = min 4 canAddNumber
 
 
-handleCmd_inRoom clID clients rooms ["REMOVE_TEAM", teamName] =
-	if noSuchTeam then
-		[Warning "REMOVE_TEAM: no such team"]
-	else
-		if not $ nick client == teamowner team then
-			[ProtocolError "Not team owner!"]
-		else
+handleCmd_inRoom clID clients rooms ["REMOVE_TEAM", teamName]
+	| noSuchTeam = [Warning "REMOVE_TEAM: no such team"]
+	| nick client /= teamowner team = [ProtocolError "Not team owner!"]
+	| otherwise =
 			[RemoveTeam teamName,
-			ModifyClient (\c -> c{teamsInGame = teamsInGame c - 1})	
+			ModifyClient (\c -> c{teamsInGame = teamsInGame c - 1})
 			]
 	where
 		client = clients IntMap.! clID
@@ -90,15 +81,12 @@
 		findTeam = find (\t -> teamName == teamname t) $ teams room
 
 
-handleCmd_inRoom clID clients rooms ["HH_NUM", teamName, numberStr] =
-	if not $ isMaster client then
-		[ProtocolError "Not room master"]
-	else
-		if hhNumber < 1 || hhNumber > 8 || noSuchTeam || hhNumber > (canAddNumber + (hhnum team)) then
-			[]
-		else
-			[ModifyRoom $ modifyTeam team{hhnum = hhNumber},
-			AnswerOthersInRoom ["HH_NUM", teamName, show hhNumber]]
+handleCmd_inRoom clID clients rooms ["HH_NUM", teamName, numberStr]
+	| not $ isMaster client = [ProtocolError "Not room master"]
+	| hhNumber < 1 || hhNumber > 8 || noSuchTeam || hhNumber > (canAddNumber + (hhnum team)) = []
+	| otherwise =
+		[ModifyRoom $ modifyTeam team{hhnum = hhNumber},
+		AnswerOthersInRoom ["HH_NUM", teamName, show hhNumber]]
 	where
 		client = clients IntMap.! clID
 		room = rooms IntMap.! (roomID client)
@@ -109,14 +97,10 @@
 		canAddNumber = 48 - (sum . map hhnum $ teams room)
 
 
-handleCmd_inRoom clID clients rooms ["TEAM_COLOR", teamName, newColor] =
-	if not $ isMaster client then
-		[ProtocolError "Not room master"]
-	else
-		if noSuchTeam then
-			[]
-		else
-			[ModifyRoom $ modifyTeam team{teamcolor = newColor},
+handleCmd_inRoom clID clients rooms ["TEAM_COLOR", teamName, newColor]
+	| not $ isMaster client = [ProtocolError "Not room master"]
+	| noSuchTeam = []
+	| otherwise = [ModifyRoom $ modifyTeam team{teamcolor = newColor},
 			AnswerOthersInRoom ["TEAM_COLOR", teamName, newColor]]
 	where
 		noSuchTeam = isNothing findTeam
@@ -129,13 +113,13 @@
 handleCmd_inRoom clID clients rooms ["TOGGLE_READY"] =
 	[ModifyClient (\c -> c{isReady = not $ isReady client}),
 	ModifyRoom (\r -> r{readyPlayers = readyPlayers r + (if isReady client then -1 else 1)}),
-	AnswerThisRoom $ [if isReady client then "NOT_READY" else "READY", nick client]]
+	AnswerThisRoom [if isReady client then "NOT_READY" else "READY", nick client]]
 	where
 		client = clients IntMap.! clID
 
 
 handleCmd_inRoom clID clients rooms ["START_GAME"] =
-	if isMaster client && (playersIn room == readyPlayers room) && (not $ gameinprogress room) then
+	if isMaster client && (playersIn room == readyPlayers room) && (not . gameinprogress) room then
 		if enoughClans then
 			[ModifyRoom
 					(\r -> r{
@@ -184,34 +168,21 @@
 		answerRemovedTeams = map (\t -> AnswerThisRoom ["REMOVE_TEAM", t]) $ leftTeams room
 
 
-handleCmd_inRoom clID clients _ ["TOGGLE_RESTRICT_JOINS"] =
-	if isMaster client then
-		[ModifyRoom (\r -> r{isRestrictedJoins = not $ isRestrictedJoins r})]
-	else
-		[ProtocolError "Not room master"]
+handleCmd_inRoom clID clients _ ["TOGGLE_RESTRICT_JOINS"]
+	| isMaster client = [ModifyRoom (\r -> r{isRestrictedJoins = not $ isRestrictedJoins r})]
+	| otherwise = [ProtocolError "Not room master"]
 	where
 		client = clients IntMap.! clID
 
 
-handleCmd_inRoom clID clients _ ["TOGGLE_RESTRICT_TEAMS"] =
-	if isMaster client then
-		[ModifyRoom (\r -> r{isRestrictedTeams = not $ isRestrictedTeams r})]
-	else
-		[ProtocolError "Not room master"]
+handleCmd_inRoom clID clients _ ["TOGGLE_RESTRICT_TEAMS"]
+	| isMaster client = [ModifyRoom (\r -> r{isRestrictedTeams = not $ isRestrictedTeams r})]
+	| otherwise = [ProtocolError "Not room master"]
 	where
 		client = clients IntMap.! clID
 
 handleCmd_inRoom clID clients rooms ["KICK", kickNick] =
-	if not $ isMaster client then
-		[]
-	else
-		if noSuchClient then
-			[]
-		else
-			if (kickID == clID) || (roomID client /= roomID kickClient) then
-				[]
-			else
-				[KickRoomClient kickID]
+	[KickRoomClient kickID | isMaster client && not noSuchClient && (kickID /= clID) && (roomID client == roomID kickClient)]
 	where
 		client = clients IntMap.! clID
 		maybeClient = Foldable.find (\cl -> kickNick == nick cl) clients