gameServer/HWProtoLobbyState.hs
author koda
Mon, 18 Jan 2010 05:36:08 +0000
changeset 2699 249adefa9c1c
parent 2408 41ebdb5f1e6e
child 2867 9be6693c78cb
permissions -rw-r--r--
replace initialization/finalization statements with custom init functions
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
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    26
		roomsInfoList = concatMap roomInfo sameProtoRooms
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    27
		sameProtoRooms = filter (\r -> (roomProto r == protocol) && not (isRestrictedJoins r)) roomsList
1804
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
2408
41ebdb5f1e6e Server provides more info in rooms list
unc0rr
parents: 2352
diff changeset
    31
		roomInfo room
41ebdb5f1e6e Server provides more info in rooms list
unc0rr
parents: 2352
diff changeset
    32
			| clientProto client < 28 = [
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    33
				name room,
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    34
				show (playersIn room) ++ "(" ++ show (length $ teams room) ++ ")",
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    35
				show $ gameinprogress room
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    36
				]
2408
41ebdb5f1e6e Server provides more info in rooms list
unc0rr
parents: 2352
diff changeset
    37
			| otherwise = [
41ebdb5f1e6e Server provides more info in rooms list
unc0rr
parents: 2352
diff changeset
    38
				show $ gameinprogress room,
41ebdb5f1e6e Server provides more info in rooms list
unc0rr
parents: 2352
diff changeset
    39
				name room,
41ebdb5f1e6e Server provides more info in rooms list
unc0rr
parents: 2352
diff changeset
    40
				show $ playersIn room,
41ebdb5f1e6e Server provides more info in rooms list
unc0rr
parents: 2352
diff changeset
    41
				show $ length $ teams room,
41ebdb5f1e6e Server provides more info in rooms list
unc0rr
parents: 2352
diff changeset
    42
				nick $ clients IntMap.! (masterID room),
41ebdb5f1e6e Server provides more info in rooms list
unc0rr
parents: 2352
diff changeset
    43
				head (Map.findWithDefault ["+gen+"] "MAP" (params room)),
41ebdb5f1e6e Server provides more info in rooms list
unc0rr
parents: 2352
diff changeset
    44
				head (Map.findWithDefault ["Default"] "SCHEME" (params room)),
41ebdb5f1e6e Server provides more info in rooms list
unc0rr
parents: 2352
diff changeset
    45
				head (Map.findWithDefault ["Default"] "AMMO" (params room))
41ebdb5f1e6e Server provides more info in rooms list
unc0rr
parents: 2352
diff changeset
    46
				]
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
    47
1815
3d62cf9c350e Save some more bytes in protocol
unc0rr
parents: 1813
diff changeset
    48
handleCmd_lobby clID clients _ ["CHAT", msg] =
3d62cf9c350e Save some more bytes in protocol
unc0rr
parents: 1813
diff changeset
    49
	[AnswerOthersInRoom ["CHAT", clientNick, msg]]
1804
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
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
    53
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    54
handleCmd_lobby clID clients rooms ["CREATE_ROOM", newRoom, roomPassword]
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    55
	| haveSameRoom = [Warning "Room exists"]
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    56
	| illegalName newRoom = [Warning "Illegal room name"]
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    57
	| otherwise =
2126
cb249fa8e3da - Prevent server from producing zombies
unc0rr
parents: 1925
diff changeset
    58
		[RoomRemoveThisClient "", -- leave lobby
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    59
		AddRoom newRoom roomPassword,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    60
		AnswerThisClient ["NOT_READY", clientNick]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    61
		]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    62
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    63
		clientNick = nick $ clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    64
		haveSameRoom = isJust $ find (\room -> newRoom == name room) $ IntMap.elems rooms
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    65
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
    66
1905
b1ec8db513f2 - Use QCryptographicHash for md5
unc0rr
parents: 1871
diff changeset
    67
handleCmd_lobby clID clients rooms ["CREATE_ROOM", newRoom] =
b1ec8db513f2 - Use QCryptographicHash for md5
unc0rr
parents: 1871
diff changeset
    68
	handleCmd_lobby clID clients rooms ["CREATE_ROOM", newRoom, ""]
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    69
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
    70
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    71
handleCmd_lobby clID clients rooms ["JOIN_ROOM", roomName, roomPassword]
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    72
	| noSuchRoom = [Warning "No such room"]
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    73
	| isRestrictedJoins jRoom = [Warning "Joining restricted"]
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    74
	| roomPassword /= password jRoom = [Warning "Wrong password"]
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    75
	| otherwise =
2126
cb249fa8e3da - Prevent server from producing zombies
unc0rr
parents: 1925
diff changeset
    76
		[RoomRemoveThisClient "", -- leave lobby
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    77
		RoomAddThisClient rID] -- join room
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    78
		++ answerNicks
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    79
		++ answerReady
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    80
		++ [AnswerThisRoom ["NOT_READY", nick client]]
1871
ce5854fbc631 Send MAP config param last
unc0rr
parents: 1866
diff changeset
    81
		++ answerFullConfig
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    82
		++ answerTeams
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
    83
		++ watchRound
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    84
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    85
		noSuchRoom = isNothing mbRoom
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    86
		mbRoom = find (\r -> roomName == name r && roomProto r == clientProto client) $ IntMap.elems rooms
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    87
		jRoom = fromJust mbRoom
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    88
		rID = roomUID jRoom
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    89
		client = clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    90
		roomClientsIDs = IntSet.elems $ playersIDs jRoom
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    91
		answerNicks =
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    92
			[AnswerThisClient $ "JOINED" :
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    93
			map (\clID -> nick $ clients IntMap.! clID) roomClientsIDs | playersIn jRoom /= 0]
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    94
		answerReady = map
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    95
			((\ c ->
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    96
				AnswerThisClient
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    97
				[if isReady c then "READY" else "NOT_READY", nick c])
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    98
			. (\ clID -> clients IntMap.! clID))
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    99
			roomClientsIDs
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   100
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   101
		toAnswer (paramName, paramStrs) = AnswerThisClient $ "CFG" : paramName : paramStrs
1871
ce5854fbc631 Send MAP config param last
unc0rr
parents: 1866
diff changeset
   102
		
ce5854fbc631 Send MAP config param last
unc0rr
parents: 1866
diff changeset
   103
		answerFullConfig = map toAnswer (leftConfigPart ++ rightConfigPart)
ce5854fbc631 Send MAP config param last
unc0rr
parents: 1866
diff changeset
   104
		(leftConfigPart, rightConfigPart) = partition (\(p, _) -> p /= "MAP") (Map.toList $ params jRoom)
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   105
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   106
		watchRound = if not $ gameinprogress jRoom then
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   107
					[]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   108
				else
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   109
					[AnswerThisClient  ["RUN_GAME"],
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
   110
					AnswerThisClient $ "EM" : toEngineMsg "e$spectate 1" : Foldable.toList (roundMsgs jRoom)]
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   111
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   112
		answerTeams = if gameinprogress jRoom then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   113
				answerAllTeams (teamsAtStart jRoom)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   114
			else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   115
				answerAllTeams (teams jRoom)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   116
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   117
1905
b1ec8db513f2 - Use QCryptographicHash for md5
unc0rr
parents: 1871
diff changeset
   118
handleCmd_lobby clID clients rooms ["JOIN_ROOM", roomName] =
b1ec8db513f2 - Use QCryptographicHash for md5
unc0rr
parents: 1871
diff changeset
   119
	handleCmd_lobby clID clients rooms ["JOIN_ROOM", roomName, ""]
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   120
2155
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   121
	---------------------------
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   122
	-- Administrator's stuff --
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   123
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   124
handleCmd_lobby clID clients rooms ["KICK", kickNick] =
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
   125
		[KickClient kickID | isAdministrator client && (not noSuchClient) && kickID /= clID]
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   126
	where
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   127
		client = clients IntMap.! clID
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   128
		maybeClient = Foldable.find (\cl -> kickNick == nick cl) clients
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   129
		noSuchClient = isNothing maybeClient
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   130
		kickID = clientUID $ fromJust maybeClient
1866
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
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   133
handleCmd_lobby clID clients rooms ["BAN", banNick] =
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   134
	if not $ isAdministrator client then
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   135
		[]
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   136
	else
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   137
		BanClient banNick : handleCmd_lobby clID clients rooms ["KICK", banNick]
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   138
	where
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   139
		client = clients IntMap.! clID
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   140
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   141
1925
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   142
handleCmd_lobby clID clients rooms ["SET_SERVER_MESSAGE", newMessage] =
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
   143
		[ModifyServerInfo (\si -> si{serverMessage = newMessage}) | isAdministrator client]
1925
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   144
	where
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   145
		client = clients IntMap.! clID
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
2155
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   148
handleCmd_lobby clID clients rooms ["CLEAR_ACCOUNTS_CACHE"] =
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
   149
		[ClearAccountsCache | isAdministrator client]
2155
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   150
	where
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   151
		client = clients IntMap.! clID
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   152
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   153
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   154
handleCmd_lobby clID _ _ _ = [ProtocolError "Incorrect command (state: in lobby)"]