gameServer/HWProtoLobbyState.hs
author koda
Wed, 15 Jul 2009 00:35:59 +0000
changeset 2262 0dc94341bf42
parent 2155 d897222d3339
child 2352 7eaf82cf0890
permissions -rw-r--r--
sound for mortar
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"]
2150
45b695f3a7b9 Forbid room names and nicknames consisting only of space characters
unc0rr
parents: 2126
diff changeset
    47
	else if illegalName newRoom then
45b695f3a7b9 Forbid room names and nicknames consisting only of space characters
unc0rr
parents: 2126
diff changeset
    48
		[Warning "Illegal room name"]
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    49
	else
2126
cb249fa8e3da - Prevent server from producing zombies
unc0rr
parents: 1925
diff changeset
    50
		[RoomRemoveThisClient "", -- leave lobby
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    51
		AddRoom newRoom roomPassword,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    52
		AnswerThisClient ["NOT_READY", clientNick]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    53
		]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    54
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    55
		clientNick = nick $ clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    56
		haveSameRoom = isJust $ find (\room -> newRoom == name room) $ IntMap.elems rooms
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    57
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
    58
1905
b1ec8db513f2 - Use QCryptographicHash for md5
unc0rr
parents: 1871
diff changeset
    59
handleCmd_lobby clID clients rooms ["CREATE_ROOM", newRoom] =
b1ec8db513f2 - Use QCryptographicHash for md5
unc0rr
parents: 1871
diff changeset
    60
	handleCmd_lobby clID clients rooms ["CREATE_ROOM", newRoom, ""]
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    61
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
    62
1905
b1ec8db513f2 - Use QCryptographicHash for md5
unc0rr
parents: 1871
diff changeset
    63
handleCmd_lobby clID clients rooms ["JOIN_ROOM", roomName, roomPassword] =
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    64
	if noSuchRoom then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    65
		[Warning "No such room"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    66
	else if isRestrictedJoins jRoom then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    67
		[Warning "Joining restricted"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    68
	else if roomPassword /= password jRoom then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    69
		[Warning "Wrong password"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    70
	else
2126
cb249fa8e3da - Prevent server from producing zombies
unc0rr
parents: 1925
diff changeset
    71
		[RoomRemoveThisClient "", -- leave lobby
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    72
		RoomAddThisClient rID] -- join room
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    73
		++ answerNicks
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    74
		++ answerReady
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    75
		++ [AnswerThisRoom ["NOT_READY", nick client]]
1871
ce5854fbc631 Send MAP config param last
unc0rr
parents: 1866
diff changeset
    76
		++ answerFullConfig
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    77
		++ answerTeams
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
    78
		++ watchRound
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    79
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    80
		noSuchRoom = isNothing mbRoom
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    81
		mbRoom = find (\r -> roomName == name r && roomProto r == clientProto client) $ IntMap.elems rooms 
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    82
		jRoom = fromJust mbRoom
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    83
		rID = roomUID jRoom
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    84
		client = clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    85
		roomClientsIDs = IntSet.elems $ playersIDs jRoom
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    86
		answerNicks = if playersIn jRoom /= 0 then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    87
					[AnswerThisClient $ ["JOINED"] ++ (map (\clID -> nick $ clients IntMap.! clID) $ roomClientsIDs)]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    88
				else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    89
					[]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    90
		answerReady =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    91
			map (\c -> AnswerThisClient [if isReady c then "READY" else "NOT_READY", nick c]) $
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    92
			map (\clID -> clients IntMap.! clID) roomClientsIDs
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    93
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    94
		toAnswer (paramName, paramStrs) = AnswerThisClient $ "CFG" : paramName : paramStrs
1871
ce5854fbc631 Send MAP config param last
unc0rr
parents: 1866
diff changeset
    95
		
ce5854fbc631 Send MAP config param last
unc0rr
parents: 1866
diff changeset
    96
		answerFullConfig = map toAnswer (leftConfigPart ++ rightConfigPart)
ce5854fbc631 Send MAP config param last
unc0rr
parents: 1866
diff changeset
    97
		(leftConfigPart, rightConfigPart) = partition (\(p, _) -> p /= "MAP") (Map.toList $ params jRoom)
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
    98
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
    99
		watchRound = if not $ gameinprogress jRoom then
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   100
					[]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   101
				else
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   102
					[AnswerThisClient  ["RUN_GAME"],
1866
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   103
					AnswerThisClient $ "EM" : toEngineMsg "e$spectate 1" : (Foldable.toList $ roundMsgs jRoom)]
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   104
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   105
		answerTeams = if gameinprogress jRoom then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   106
				answerAllTeams (teamsAtStart jRoom)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   107
			else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   108
				answerAllTeams (teams jRoom)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   109
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   110
1905
b1ec8db513f2 - Use QCryptographicHash for md5
unc0rr
parents: 1871
diff changeset
   111
handleCmd_lobby clID clients rooms ["JOIN_ROOM", roomName] =
b1ec8db513f2 - Use QCryptographicHash for md5
unc0rr
parents: 1871
diff changeset
   112
	handleCmd_lobby clID clients rooms ["JOIN_ROOM", roomName, ""]
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   113
2155
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   114
	---------------------------
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   115
	-- Administrator's stuff --
1862
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
handleCmd_lobby clID clients rooms ["KICK", kickNick] =
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   118
	if not $ isAdministrator client 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
		if noSuchClient then
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   122
			[]
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   123
		else
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   124
			if kickID == clID then
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   125
				[]
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   126
			else
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   127
				[KickClient kickID]
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   128
	where
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   129
		client = clients IntMap.! clID
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   130
		maybeClient = Foldable.find (\cl -> kickNick == nick cl) clients
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   131
		noSuchClient = isNothing maybeClient
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   132
		kickID = clientUID $ fromJust maybeClient
1866
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
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   135
handleCmd_lobby clID clients rooms ["BAN", banNick] =
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   136
	if not $ isAdministrator client then
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   137
		[]
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   138
	else
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   139
		BanClient banNick : handleCmd_lobby clID clients rooms ["KICK", banNick]
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   140
	where
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   141
		client = clients IntMap.! clID
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   142
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   143
1925
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   144
handleCmd_lobby clID clients rooms ["SET_SERVER_MESSAGE", newMessage] =
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   145
	if not $ isAdministrator client then
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   146
		[]
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   147
	else
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   148
		[ModifyServerInfo (\si -> si{serverMessage = newMessage})]
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   149
	where
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   150
		client = clients IntMap.! clID
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   151
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   152
2155
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   153
handleCmd_lobby clID clients rooms ["CLEAR_ACCOUNTS_CACHE"] =
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   154
	if not $ isAdministrator client then
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   155
		[]
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   156
	else
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   157
		[ClearAccountsCache]
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   158
	where
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   159
		client = clients IntMap.! clID
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   160
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   161
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   162
handleCmd_lobby clID _ _ _ = [ProtocolError "Incorrect command (state: in lobby)"]