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