gameServer/HWProtoLobbyState.hs
author unc0rr
Sun, 27 Sep 2009 14:33:59 +0000
changeset 2397 2ca4ca6b4bab
parent 2352 7eaf82cf0890
child 2408 41ebdb5f1e6e
permissions -rw-r--r--
- Patch for "Syncronizing" message by Smaxx + some tweaks by me - Tweak chat colours a bit
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
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    31
		roomInfo room = [
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    32
				name room,
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    33
				show (playersIn room) ++ "(" ++ show (length $ teams room) ++ ")",
1804
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
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    44
handleCmd_lobby clID clients rooms ["CREATE_ROOM", newRoom, roomPassword]
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    45
	| haveSameRoom = [Warning "Room exists"]
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    46
	| illegalName newRoom = [Warning "Illegal room name"]
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    47
	| otherwise =
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
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    61
handleCmd_lobby clID clients rooms ["JOIN_ROOM", roomName, roomPassword]
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    62
	| noSuchRoom = [Warning "No such room"]
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    63
	| isRestrictedJoins jRoom = [Warning "Joining restricted"]
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    64
	| roomPassword /= password jRoom = [Warning "Wrong password"]
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    65
	| otherwise =
2126
cb249fa8e3da - Prevent server from producing zombies
unc0rr
parents: 1925
diff changeset
    66
		[RoomRemoveThisClient "", -- leave lobby
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    67
		RoomAddThisClient rID] -- join room
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    68
		++ answerNicks
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    69
		++ answerReady
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    70
		++ [AnswerThisRoom ["NOT_READY", nick client]]
1871
ce5854fbc631 Send MAP config param last
unc0rr
parents: 1866
diff changeset
    71
		++ answerFullConfig
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    72
		++ answerTeams
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
    73
		++ watchRound
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    74
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    75
		noSuchRoom = isNothing mbRoom
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    76
		mbRoom = find (\r -> roomName == name r && roomProto r == clientProto client) $ IntMap.elems rooms
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    77
		jRoom = fromJust mbRoom
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    78
		rID = roomUID jRoom
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    79
		client = clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    80
		roomClientsIDs = IntSet.elems $ playersIDs jRoom
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    81
		answerNicks =
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    82
			[AnswerThisClient $ "JOINED" :
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    83
			map (\clID -> nick $ clients IntMap.! clID) roomClientsIDs | playersIn jRoom /= 0]
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    84
		answerReady = map
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    85
			((\ c ->
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    86
				AnswerThisClient
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    87
				[if isReady c then "READY" else "NOT_READY", nick c])
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    88
			. (\ clID -> clients IntMap.! clID))
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
    89
			roomClientsIDs
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    90
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    91
		toAnswer (paramName, paramStrs) = AnswerThisClient $ "CFG" : paramName : paramStrs
1871
ce5854fbc631 Send MAP config param last
unc0rr
parents: 1866
diff changeset
    92
		
ce5854fbc631 Send MAP config param last
unc0rr
parents: 1866
diff changeset
    93
		answerFullConfig = map toAnswer (leftConfigPart ++ rightConfigPart)
ce5854fbc631 Send MAP config param last
unc0rr
parents: 1866
diff changeset
    94
		(leftConfigPart, rightConfigPart) = partition (\(p, _) -> p /= "MAP") (Map.toList $ params jRoom)
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
    95
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
    96
		watchRound = if not $ gameinprogress jRoom then
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    97
					[]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    98
				else
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
    99
					[AnswerThisClient  ["RUN_GAME"],
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
   100
					AnswerThisClient $ "EM" : toEngineMsg "e$spectate 1" : Foldable.toList (roundMsgs jRoom)]
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
   101
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   102
		answerTeams = if gameinprogress jRoom then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   103
				answerAllTeams (teamsAtStart jRoom)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   104
			else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   105
				answerAllTeams (teams jRoom)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   106
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   107
1905
b1ec8db513f2 - Use QCryptographicHash for md5
unc0rr
parents: 1871
diff changeset
   108
handleCmd_lobby clID clients rooms ["JOIN_ROOM", roomName] =
b1ec8db513f2 - Use QCryptographicHash for md5
unc0rr
parents: 1871
diff changeset
   109
	handleCmd_lobby clID clients rooms ["JOIN_ROOM", roomName, ""]
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   110
2155
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   111
	---------------------------
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   112
	-- Administrator's stuff --
1862
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
handleCmd_lobby clID clients rooms ["KICK", kickNick] =
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
   115
		[KickClient kickID | isAdministrator client && (not noSuchClient) && kickID /= clID]
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   116
	where
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   117
		client = clients IntMap.! clID
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   118
		maybeClient = Foldable.find (\cl -> kickNick == nick cl) clients
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   119
		noSuchClient = isNothing maybeClient
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   120
		kickID = clientUID $ fromJust maybeClient
1866
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   121
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   122
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   123
handleCmd_lobby clID clients rooms ["BAN", banNick] =
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   124
	if not $ isAdministrator client then
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   125
		[]
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   126
	else
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   127
		BanClient banNick : handleCmd_lobby clID clients rooms ["KICK", banNick]
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   128
	where
36aa0ca6e8af Cut the length of most used net packet
unc0rr
parents: 1862
diff changeset
   129
		client = clients IntMap.! clID
1862
7f303aa066da Implement kick from server by administrator
unc0rr
parents: 1815
diff changeset
   130
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   131
1925
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   132
handleCmd_lobby clID clients rooms ["SET_SERVER_MESSAGE", newMessage] =
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
   133
		[ModifyServerInfo (\si -> si{serverMessage = newMessage}) | isAdministrator client]
1925
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   134
	where
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   135
		client = clients IntMap.! clID
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   136
ec923e56c444 Allow admin to set server's motd
unc0rr
parents: 1905
diff changeset
   137
2155
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   138
handleCmd_lobby clID clients rooms ["CLEAR_ACCOUNTS_CACHE"] =
2352
7eaf82cf0890 Fixes suggested by hlint tool
unc0rr
parents: 2155
diff changeset
   139
		[ClearAccountsCache | isAdministrator client]
2155
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   140
	where
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   141
		client = clients IntMap.! clID
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   142
d897222d3339 Implement ability for server admin to clear accounts cache
unc0rr
parents: 2150
diff changeset
   143
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   144
handleCmd_lobby clID _ _ _ = [ProtocolError "Incorrect command (state: in lobby)"]