gameServer/HWProtoLobbyState.hs
author unc0rr
Sat, 07 Mar 2009 17:43:25 +0000
changeset 1866 36aa0ca6e8af
parent 1862 7f303aa066da
child 1871 ce5854fbc631
permissions -rw-r--r--
Cut the length of most used net packet
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
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
    37
1815
3d62cf9c350e Save some more bytes in protocol
unc0rr
parents: 1813
diff changeset
    38
handleCmd_lobby clID clients _ ["CHAT", msg] =
3d62cf9c350e Save some more bytes in protocol
unc0rr
parents: 1813
diff changeset
    39
	[AnswerOthersInRoom ["CHAT", clientNick, msg]]
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    40
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    41
		clientNick = nick $ clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    42
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
    43
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    44
handleCmd_lobby clID clients rooms ["CREATE", newRoom, roomPassword] =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    45
	if haveSameRoom then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    46
		[Warning "Room exists"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    47
	else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    48
		[RoomRemoveThisClient, -- leave lobby
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    49
		AddRoom newRoom roomPassword,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    50
		AnswerThisClient ["NOT_READY", clientNick]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    51
		]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    52
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    53
		clientNick = nick $ clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    54
		haveSameRoom = isJust $ find (\room -> newRoom == name room) $ IntMap.elems rooms
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    55
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
    56
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    57
handleCmd_lobby clID clients rooms ["CREATE", newRoom] =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    58
	handleCmd_lobby clID clients rooms ["CREATE", newRoom, ""]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    59
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
    60
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    61
handleCmd_lobby clID clients rooms ["JOIN", roomName, roomPassword] =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    62
	if noSuchRoom then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    63
		[Warning "No such room"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    64
	else if isRestrictedJoins jRoom then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    65
		[Warning "Joining restricted"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    66
	else if roomPassword /= password jRoom then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    67
		[Warning "Wrong password"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    68
	else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    69
		[RoomRemoveThisClient, -- leave lobby
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    70
		RoomAddThisClient rID] -- join room
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    71
		++ answerNicks
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    72
		++ answerReady
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    73
		++ [AnswerThisRoom ["NOT_READY", nick client]]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    74
		++ answerFullConfig jRoom
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    75
		++ answerTeams
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
    76
		++ watchRound
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    77
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    78
		noSuchRoom = isNothing mbRoom
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    79
		mbRoom = find (\r -> roomName == name r && roomProto r == clientProto client) $ IntMap.elems rooms 
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    80
		jRoom = fromJust mbRoom
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    81
		rID = roomUID jRoom
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    82
		client = clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    83
		roomClientsIDs = IntSet.elems $ playersIDs jRoom
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    84
		answerNicks = if playersIn jRoom /= 0 then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    85
					[AnswerThisClient $ ["JOINED"] ++ (map (\clID -> nick $ clients IntMap.! clID) $ roomClientsIDs)]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    86
				else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    87
					[]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    88
		answerReady =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    89
			map (\c -> AnswerThisClient [if isReady c then "READY" else "NOT_READY", nick c]) $
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    90
			map (\clID -> clients IntMap.! clID) roomClientsIDs
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    91
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    92
		toAnswer (paramName, paramStrs) = AnswerThisClient $ "CFG" : paramName : paramStrs
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    93
		answerFullConfig room = map toAnswer (Map.toList $ params room)
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
    94
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
    95
		watchRound = if not $ gameinprogress jRoom then
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    96
					[]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    97
				else
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
    98
					[AnswerThisClient  ["RUN_GAME"],
1866
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
    99
					AnswerThisClient $ "EM" : toEngineMsg "e$spectate 1" : (Foldable.toList $ roundMsgs jRoom)]
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   100
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   101
		answerTeams = if gameinprogress jRoom then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   102
				answerAllTeams (teamsAtStart jRoom)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   103
			else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   104
				answerAllTeams (teams jRoom)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   105
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   106
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   107
handleCmd_lobby clID clients rooms ["JOIN", roomName] =
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   108
	handleCmd_lobby clID clients rooms ["JOIN", roomName, ""]
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   109
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   110
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   111
handleCmd_lobby clID clients rooms ["KICK", kickNick] =
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   112
	if not $ isAdministrator client then
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   113
		[]
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   114
	else
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   115
		if noSuchClient then
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   116
			[]
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   117
		else
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   118
			if kickID == clID then
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   119
				[]
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   120
			else
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   121
				[KickClient kickID]
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   122
	where
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   123
		client = clients IntMap.! clID
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   124
		maybeClient = Foldable.find (\cl -> kickNick == nick cl) clients
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   125
		noSuchClient = isNothing maybeClient
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   126
		kickID = clientUID $ fromJust maybeClient
1866
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   127
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   128
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   129
handleCmd_lobby clID clients rooms ["BAN", banNick] =
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   130
	if not $ isAdministrator client then
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   131
		[]
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   132
	else
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   133
		BanClient banNick : handleCmd_lobby clID clients rooms ["KICK", banNick]
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   134
	where
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   135
		client = clients IntMap.! clID
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   136
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   137
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   138
handleCmd_lobby clID _ _ _ = [ProtocolError "Incorrect command (state: in lobby)"]