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