gameServer/HWProtoLobbyState.hs
author unc0rr
Wed, 27 May 2009 15:29:30 +0000
changeset 2126 cb249fa8e3da
parent 1925 ec923e56c444
child 2150 45b695f3a7b9
permissions -rw-r--r--
- Prevent server from producing zombies - Some tweaks
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
1905
b1ec8db513f2 - Use QCryptographicHash for md5
unc0rr
parents: 1871
diff changeset
    44
handleCmd_lobby clID clients rooms ["CREATE_ROOM", newRoom, roomPassword] =
1804
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
2126
cb249fa8e3da - Prevent server from producing zombies
unc0rr
parents: 1925
diff changeset
    48
		[RoomRemoveThisClient "", -- leave lobby
1804
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
1905
b1ec8db513f2 - Use QCryptographicHash for md5
unc0rr
parents: 1871
diff changeset
    57
handleCmd_lobby clID clients rooms ["CREATE_ROOM", newRoom] =
b1ec8db513f2 - Use QCryptographicHash for md5
unc0rr
parents: 1871
diff changeset
    58
	handleCmd_lobby clID clients rooms ["CREATE_ROOM", newRoom, ""]
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    59
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
    60
1905
b1ec8db513f2 - Use QCryptographicHash for md5
unc0rr
parents: 1871
diff changeset
    61
handleCmd_lobby clID clients rooms ["JOIN_ROOM", roomName, roomPassword] =
1804
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
2126
cb249fa8e3da - Prevent server from producing zombies
unc0rr
parents: 1925
diff changeset
    69
		[RoomRemoveThisClient "", -- leave lobby
1804
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]]
1871
ce5854fbc631 Send MAP config param last
unc0rr
parents: 1866
diff changeset
    74
		++ answerFullConfig
1804
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
1871
ce5854fbc631 Send MAP config param last
unc0rr
parents: 1866
diff changeset
    93
		
ce5854fbc631 Send MAP config param last
unc0rr
parents: 1866
diff changeset
    94
		answerFullConfig = map toAnswer (leftConfigPart ++ rightConfigPart)
ce5854fbc631 Send MAP config param last
unc0rr
parents: 1866
diff changeset
    95
		(leftConfigPart, rightConfigPart) = partition (\(p, _) -> p /= "MAP") (Map.toList $ params jRoom)
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
    96
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
    97
		watchRound = if not $ gameinprogress jRoom then
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    98
					[]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    99
				else
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   100
					[AnswerThisClient  ["RUN_GAME"],
1866
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   101
					AnswerThisClient $ "EM" : toEngineMsg "e$spectate 1" : (Foldable.toList $ roundMsgs jRoom)]
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   102
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   103
		answerTeams = if gameinprogress jRoom then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   104
				answerAllTeams (teamsAtStart jRoom)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   105
			else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   106
				answerAllTeams (teams jRoom)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   107
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   108
1905
b1ec8db513f2 - Use QCryptographicHash for md5
unc0rr
parents: 1871
diff changeset
   109
handleCmd_lobby clID clients rooms ["JOIN_ROOM", roomName] =
b1ec8db513f2 - Use QCryptographicHash for md5
unc0rr
parents: 1871
diff changeset
   110
	handleCmd_lobby clID clients rooms ["JOIN_ROOM", roomName, ""]
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   111
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   112
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   113
handleCmd_lobby clID clients rooms ["KICK", kickNick] =
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   114
	if not $ isAdministrator client then
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   115
		[]
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   116
	else
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   117
		if noSuchClient then
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   118
			[]
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   119
		else
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   120
			if kickID == clID then
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   121
				[]
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   122
			else
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   123
				[KickClient kickID]
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   124
	where
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   125
		client = clients IntMap.! clID
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   126
		maybeClient = Foldable.find (\cl -> kickNick == nick cl) clients
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   127
		noSuchClient = isNothing maybeClient
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   128
		kickID = clientUID $ fromJust maybeClient
1866
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   129
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   130
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   131
handleCmd_lobby clID clients rooms ["BAN", banNick] =
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   132
	if not $ isAdministrator client then
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   133
		[]
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   134
	else
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   135
		BanClient banNick : handleCmd_lobby clID clients rooms ["KICK", banNick]
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   136
	where
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   137
		client = clients IntMap.! clID
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   138
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   139
1925
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   140
handleCmd_lobby clID clients rooms ["SET_SERVER_MESSAGE", newMessage] =
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   141
	if not $ isAdministrator client then
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   142
		[]
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   143
	else
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   144
		[ModifyServerInfo (\si -> si{serverMessage = newMessage})]
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   145
	where
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   146
		client = clients IntMap.! clID
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   147
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   148
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   149
handleCmd_lobby clID _ _ _ = [ProtocolError "Incorrect command (state: in lobby)"]