netserver/HWProto.hs
changeset 1403 b8c921ed0f13
parent 1402 c164f215f7d2
child 1404 2b6b6809c2e4
equal deleted inserted replaced
1402:c164f215f7d2 1403:b8c921ed0f13
    52 			(clientOnly, ["TEAM_COLOR", teamname team, teamcolor team]),
    52 			(clientOnly, ["TEAM_COLOR", teamname team, teamcolor team]),
    53 			(clientOnly, ["HH_NUM", teamname team, show $ hhnum team])]
    53 			(clientOnly, ["HH_NUM", teamname team, show $ hhnum team])]
    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 answerReady nick = [(sameRoom, ["READY", nick])]
       
    58 answerNotReady nick = [(sameRoom, ["NOT_READY", nick])]
       
    59 
       
    60 
    57 -- Main state-independent cmd handler
    61 -- Main state-independent cmd handler
    58 handleCmd :: CmdHandler
    62 handleCmd :: CmdHandler
    59 handleCmd client _ rooms ("QUIT":xs) =
    63 handleCmd client _ rooms ("QUIT":xs) =
    60 	if null (room client) then
    64 	if null (room client) then
    61 		(noChangeClients, noChangeRooms, answerQuit)
    65 		(noChangeClients, noChangeRooms, answerQuit)
    62 	else if isMaster client then
    66 	else if isMaster client then
    63 		(noChangeClients, removeRoom (room client), answerQuit ++ answerAbandoned) -- core disconnects clients on ROOMABANDONED answer
    67 		(noChangeClients, removeRoom (room client), answerQuit ++ answerAbandoned) -- core disconnects clients on ROOMABANDONED answer
    64 	else
    68 	else
    65 		(noChangeClients, modifyRoom clRoom{teams = othersTeams, playersIn = (playersIn clRoom) - 1}, answerQuit ++ (answerQuitInform $ nick client) ++ answerRemoveClientTeams)
    69 		(noChangeClients, modifyRoom clRoom{teams = othersTeams, playersIn = (playersIn clRoom) - 1, readyPlayers = newReadyPlayers}, answerQuit ++ (answerQuitInform $ nick client) ++ answerRemoveClientTeams)
    66 	where
    70 	where
    67 		clRoom = roomByName (room client) rooms
    71 		clRoom = roomByName (room client) rooms
    68 		answerRemoveClientTeams = map (\tn -> (othersInRoom, ["REMOVE_TEAM", teamname tn])) clientTeams
    72 		answerRemoveClientTeams = map (\tn -> (othersInRoom, ["REMOVE_TEAM", teamname tn])) clientTeams
    69 		(clientTeams, othersTeams) = partition (\t -> teamowner t == nick client) $ teams clRoom
    73 		(clientTeams, othersTeams) = partition (\t -> teamowner t == nick client) $ teams clRoom
       
    74 		newReadyPlayers = if isReady client then (readyPlayers clRoom) - 1 else readyPlayers clRoom
    70 
    75 
    71 
    76 
    72 -- check state and call state-dependent commmand handlers
    77 -- check state and call state-dependent commmand handlers
    73 handleCmd client clients rooms cmd =
    78 handleCmd client clients rooms cmd =
    74 	if null (nick client) || protocol client == 0 then
    79 	if null (nick client) || protocol client == 0 then
   224 		noSuchTeam = isNothing findTeam
   229 		noSuchTeam = isNothing findTeam
   225 		team = fromJust findTeam
   230 		team = fromJust findTeam
   226 		findTeam = find (\t -> teamName == teamname t) $ teams clRoom
   231 		findTeam = find (\t -> teamName == teamname t) $ teams clRoom
   227 		clRoom = roomByName (room client) rooms
   232 		clRoom = roomByName (room client) rooms
   228 
   233 
   229 handleCmd_inRoom client _ rooms ["READY"] =
   234 handleCmd_inRoom client _ rooms ["TOGGLE_READY"] =
   230 	if not $ isMaster client then
   235 	if isReady client then
   231 		(noChangeClients, noChangeRooms, answerNotMaster)
   236 		(modifyClient client{isReady = False}, modifyRoom clRoom{readyPlayers = newReadyPlayers}, (answerNotReady $ nick client))
   232 	else
   237 	else
   233 		(noChangeClients, modifyRoom clRoom{gameinprogress = True}, answerRunGame)
   238 		if (playersIn clRoom) == newReadyPlayers then
   234 	where
   239 			(modifyClient client{isReady = True}, modifyRoom clRoom{gameinprogress = True, readyPlayers = newReadyPlayers}, (answerReady $ nick client) ++ answerRunGame)
   235 		clRoom = roomByName (room client) rooms
   240 		else
       
   241 			(modifyClient client{isReady = True}, modifyRoom clRoom{readyPlayers = newReadyPlayers}, answerReady $ nick client)
       
   242 	where
       
   243 		clRoom = roomByName (room client) rooms
       
   244 		newReadyPlayers = (readyPlayers clRoom) + if isReady client then 1 else -1
   236 
   245 
   237 handleCmd_inRoom client _ rooms ["ROUNDFINISHED"] =
   246 handleCmd_inRoom client _ rooms ["ROUNDFINISHED"] =
   238 	if isMaster client then
   247 	if isMaster client then
   239 		(noChangeClients, modifyRoom clRoom{gameinprogress = False}, [])
   248 		(noChangeClients, modifyRoom clRoom{gameinprogress = False}, [])
   240 	else
   249 	else