netserver/HWProto.hs
changeset 1411 df78c9571bc7
parent 1408 fab171a17968
child 1412 20746999bc4a
equal deleted inserted replaced
1410:eece43296890 1411:df78c9571bc7
    54 answerMap mapName = [(othersInRoom, ["MAP", mapName])]
    54 answerMap mapName = [(othersInRoom, ["MAP", mapName])]
    55 answerRunGame = [(sameRoom, ["RUN_GAME"])]
    55 answerRunGame = [(sameRoom, ["RUN_GAME"])]
    56 answerCannotCreateRoom = [(clientOnly, ["WARNING", "Cannot create more rooms"])]
    56 answerCannotCreateRoom = [(clientOnly, ["WARNING", "Cannot create more rooms"])]
    57 answerIsReady nick = [(sameRoom, ["READY", nick])]
    57 answerIsReady nick = [(sameRoom, ["READY", nick])]
    58 answerNotReady nick = [(sameRoom, ["NOT_READY", nick])]
    58 answerNotReady nick = [(sameRoom, ["NOT_READY", nick])]
    59 
    59 answerTooFewClans = [(clientOnly, ["ERROR", "Too few clans in game"])]
       
    60 answerRestricted = [(clientOnly, ["WARNING", "Room joining restricted"])]
    60 
    61 
    61 -- Main state-independent cmd handler
    62 -- Main state-independent cmd handler
    62 handleCmd :: CmdHandler
    63 handleCmd :: CmdHandler
    63 handleCmd client _ rooms ("QUIT":xs) =
    64 handleCmd client _ rooms ("QUIT":xs) =
    64 	if null (room client) then
    65 	if null (room client) then
   138 handleCmd_noRoom client clients rooms ["JOIN", roomName, roomPassword] =
   139 handleCmd_noRoom client clients rooms ["JOIN", roomName, roomPassword] =
   139 	if noSuchRoom then
   140 	if noSuchRoom then
   140 		(noChangeClients, noChangeRooms, answerNoRoom)
   141 		(noChangeClients, noChangeRooms, answerNoRoom)
   141 	else if roomPassword /= password clRoom then
   142 	else if roomPassword /= password clRoom then
   142 		(noChangeClients, noChangeRooms, answerWrongPassword)
   143 		(noChangeClients, noChangeRooms, answerWrongPassword)
       
   144 	else if isRestrictedJoins clRoom then
       
   145 		(noChangeClients, noChangeRooms, answerRestricted)
   143 	else
   146 	else
   144 		(modifyClient client{room = roomName}, modifyRoom clRoom{playersIn = 1 + playersIn clRoom}, answerNicks ++ answerReady ++ (answerJoined $ nick client) ++ (answerNotReady $ nick client) ++ answerFullConfig clRoom ++ answerAllTeams clRoom)
   147 		(modifyClient client{room = roomName}, modifyRoom clRoom{playersIn = 1 + playersIn clRoom}, answerNicks ++ answerReady ++ (answerJoined $ nick client) ++ (answerNotReady $ nick client) ++ answerFullConfig clRoom ++ answerAllTeams clRoom)
   145 	where
   148 	where
   146 		noSuchRoom = isNothing $ find (\room -> roomName == name room && roomProto room == protocol client) rooms
   149 		noSuchRoom = isNothing $ find (\room -> roomName == name room && roomProto room == protocol client) rooms
   147 		answerNicks = [(clientOnly, ["JOINED"] ++ (map nick $ sameRoomClients))]
   150 		answerNicks = [(clientOnly, ["JOINED"] ++ (map nick $ sameRoomClients))]
   176 	where
   179 	where
   177 		clRoom = roomByName (room client) rooms
   180 		clRoom = roomByName (room client) rooms
   178 
   181 
   179 handleCmd_inRoom client _ rooms ("ADD_TEAM" : name : color : grave : fort : difStr : hhsInfo)
   182 handleCmd_inRoom client _ rooms ("ADD_TEAM" : name : color : grave : fort : difStr : hhsInfo)
   180 	| length hhsInfo == 16 =
   183 	| length hhsInfo == 16 =
   181 	if length (teams clRoom) == 6 || canAddNumber <= 0 || isJust findTeam || gameinprogress clRoom then
   184 	if length (teams clRoom) == 6
       
   185 		|| canAddNumber <= 0
       
   186 		|| isJust findTeam
       
   187 		|| gameinprogress clRoom
       
   188 		|| isRestrictedTeams clRoom then
   182 		(noChangeClients, noChangeRooms, answerCantAdd)
   189 		(noChangeClients, noChangeRooms, answerCantAdd)
   183 	else
   190 	else
   184 		(noChangeClients, modifyRoom clRoom{teams = teams clRoom ++ [newTeam]}, answerTeamAccepted newTeam ++ answerAddTeam newTeam ++ answerTeamColor name color)
   191 		(noChangeClients, modifyRoom clRoom{teams = teams clRoom ++ [newTeam]}, answerTeamAccepted newTeam ++ answerAddTeam newTeam ++ answerTeamColor name color)
   185 	where
   192 	where
   186 		clRoom = roomByName (room client) rooms
   193 		clRoom = roomByName (room client) rooms
   233 		findTeam = find (\t -> teamName == teamname t) $ teams clRoom
   240 		findTeam = find (\t -> teamName == teamname t) $ teams clRoom
   234 		clRoom = roomByName (room client) rooms
   241 		clRoom = roomByName (room client) rooms
   235 
   242 
   236 handleCmd_inRoom client _ rooms ["TOGGLE_READY"] =
   243 handleCmd_inRoom client _ rooms ["TOGGLE_READY"] =
   237 	if isReady client then
   244 	if isReady client then
   238 		(modifyClient client{isReady = False}, modifyRoom clRoom{readyPlayers = newReadyPlayers}, (answerNotReady $ nick client))
   245 		(modifyClient client{isReady = False}, modifyRoom clRoom{readyPlayers = newReadyPlayers}, answerNotReady $ nick client)
   239 	else
   246 	else
   240 		if (playersIn clRoom) == newReadyPlayers then
   247 		(modifyClient client{isReady = True}, modifyRoom clRoom{readyPlayers = newReadyPlayers}, answerIsReady $ nick client)
   241 			(modifyClient client{isReady = True}, modifyRoom clRoom{gameinprogress = True, readyPlayers = newReadyPlayers}, (answerIsReady $ nick client) ++ answerRunGame)
       
   242 		else
       
   243 			(modifyClient client{isReady = True}, modifyRoom clRoom{readyPlayers = newReadyPlayers}, answerIsReady $ nick client)
       
   244 	where
   248 	where
   245 		clRoom = roomByName (room client) rooms
   249 		clRoom = roomByName (room client) rooms
   246 		newReadyPlayers = (readyPlayers clRoom) + if isReady client then -1 else 1
   250 		newReadyPlayers = (readyPlayers clRoom) + if isReady client then -1 else 1
       
   251 
       
   252 handleCmd_inRoom client _ rooms ["START_GAME"] =
       
   253 	if isMaster client && (playersIn clRoom == readyPlayers clRoom) && (not $ gameinprogress clRoom) then
       
   254 		if enoughClans then
       
   255 			(noChangeClients, modifyRoom clRoom{gameinprogress = True}, answerRunGame)
       
   256 		else
       
   257 			(noChangeClients, noChangeRooms, answerTooFewClans)
       
   258 	else
       
   259 		(noChangeClients, noChangeRooms, [])
       
   260 	where
       
   261 		clRoom = roomByName (room client) rooms
       
   262 		enoughClans = not $ null $ drop 1 $ group $ map teamcolor $ teams clRoom
       
   263 
       
   264 handleCmd_inRoom client _ rooms ["TOGGLE_RESTRICT_JOINS"] =
       
   265 	if isMaster client then
       
   266 		(noChangeClients, modifyRoom clRoom{isRestrictedJoins = newStatus}, [])
       
   267 	else
       
   268 		(noChangeClients, noChangeRooms, answerNotMaster)
       
   269 	where
       
   270 		clRoom = roomByName (room client) rooms
       
   271 		newStatus = not $ isRestrictedJoins clRoom
       
   272 
       
   273 handleCmd_inRoom client _ rooms ["TOGGLE_RESTRICT_TEAMS"] =
       
   274 	if isMaster client then
       
   275 		(noChangeClients, modifyRoom clRoom{isRestrictedTeams = newStatus}, [])
       
   276 	else
       
   277 		(noChangeClients, noChangeRooms, answerNotMaster)
       
   278 	where
       
   279 		clRoom = roomByName (room client) rooms
       
   280 		newStatus = not $ isRestrictedTeams clRoom
   247 
   281 
   248 handleCmd_inRoom client clients rooms ["ROUNDFINISHED"] =
   282 handleCmd_inRoom client clients rooms ["ROUNDFINISHED"] =
   249 	if isMaster client then
   283 	if isMaster client then
   250 		(modifyRoomClients clRoom (\cl -> cl{isReady = False}), modifyRoom clRoom{gameinprogress = False, readyPlayers = 0}, answerAllNotReady)
   284 		(modifyRoomClients clRoom (\cl -> cl{isReady = False}), modifyRoom clRoom{gameinprogress = False, readyPlayers = 0}, answerAllNotReady)
   251 	else
   285 	else