gameServer/HWProtoInRoomState.hs
author unc0rr
Mon, 23 Feb 2009 18:35:32 +0000
changeset 1827 3bb5e22b7f9a
parent 1818 4391c7037281
child 1831 025348f05b9f
permissions -rw-r--r--
Fix ready players number after a round
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     1
module HWProtoInRoomState where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     2
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     3
import qualified Data.IntMap as IntMap
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     4
import qualified Data.Map as Map
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     5
import Data.Sequence(Seq, (|>), (><), fromList, empty)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     6
import Data.List
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     7
import Maybe
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     8
--------------------------------------
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     9
import CoreTypes
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    10
import Actions
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    11
import Utils
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    12
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    13
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    14
handleCmd_inRoom :: CmdHandler
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    15
1815
3d62cf9c350e Save some more bytes in protocol
unc0rr
parents: 1814
diff changeset
    16
handleCmd_inRoom clID clients _ ["CHAT", msg] =
3d62cf9c350e Save some more bytes in protocol
unc0rr
parents: 1814
diff changeset
    17
	[AnswerOthersInRoom ["CHAT", clientNick, msg]]
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    18
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    19
		clientNick = nick $ clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    20
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
    21
1814
e5391d901cff - Remove client teams on exit
unc0rr
parents: 1813
diff changeset
    22
handleCmd_inRoom clID clients rooms ["PART"] =
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    23
	if isMaster client then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    24
		[RemoveRoom]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    25
	else
1818
4391c7037281 Fix team removing on PART command
unc0rr
parents: 1815
diff changeset
    26
		removeClientTeams ++
4391c7037281 Fix team removing on PART command
unc0rr
parents: 1815
diff changeset
    27
		[RoomRemoveThisClient]
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    28
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    29
		client = clients IntMap.! clID
1814
e5391d901cff - Remove client teams on exit
unc0rr
parents: 1813
diff changeset
    30
		room = rooms IntMap.! (roomID client)
e5391d901cff - Remove client teams on exit
unc0rr
parents: 1813
diff changeset
    31
		clientTeams = filter (\t -> teamowner t == nick client) $ teams room
e5391d901cff - Remove client teams on exit
unc0rr
parents: 1813
diff changeset
    32
		removeClientTeams = map (RemoveTeam . teamname) clientTeams
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    33
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
    34
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    35
handleCmd_inRoom clID clients rooms ("CFG" : paramName : paramStrs) =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    36
	if isMaster client then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    37
		[ModifyRoom (\r -> r{params = Map.insert paramName paramStrs (params r)})
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    38
		, AnswerOthersInRoom ("CFG" : paramName : paramStrs)]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    39
	else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    40
		[ProtocolError "Not room master"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    41
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    42
		client = clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    43
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    44
handleCmd_inRoom clID clients rooms ("ADD_TEAM" : name : color : grave : fort : voicepack : difStr : hhsInfo)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    45
	| length hhsInfo == 16 =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    46
	if length (teams room) == 6 then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    47
		[Warning "too many teams"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    48
	else if canAddNumber <= 0 then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    49
		[Warning "too many hedgehogs"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    50
	else if isJust findTeam then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    51
		[Warning "already have a team with same name"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    52
	else if gameinprogress room then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    53
		[Warning "round in progress"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    54
	else if isRestrictedTeams room then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    55
		[Warning "restricted"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    56
	else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    57
		[ModifyRoom (\r -> r{teams = teams r ++ [newTeam]}),
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    58
		AnswerThisClient ["TEAM_ACCEPTED", name],
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    59
		AnswerOthersInRoom $ teamToNet newTeam,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    60
		AnswerOthersInRoom ["TEAM_COLOR", name, color]
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
		client = clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    64
		room = rooms IntMap.! (roomID client)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    65
		canAddNumber = 48 - (sum . map hhnum $ teams room)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    66
		findTeam = find (\t -> name == teamname t) $ teams room
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    67
		newTeam = (TeamInfo (nick client) name color grave fort voicepack difficulty newTeamHHNum (hhsList hhsInfo))
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    68
		difficulty = fromMaybe 0 (maybeRead difStr :: Maybe Int)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    69
		hhsList [] = []
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    70
		hhsList (n:h:hhs) = HedgehogInfo n h : hhsList hhs
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    71
		newTeamHHNum = min 4 canAddNumber
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    72
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    73
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    74
handleCmd_inRoom clID clients rooms ["REMOVE_TEAM", teamName] =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    75
	if noSuchTeam then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    76
		[Warning "REMOVE_TEAM: no such team"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    77
	else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    78
		if not $ nick client == teamowner team then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    79
			[ProtocolError "Not team owner!"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    80
		else
1813
cfe1481e0247 Removeteam action
unc0rr
parents: 1811
diff changeset
    81
			[RemoveTeam teamName]
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    82
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    83
		client = clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    84
		room = rooms IntMap.! (roomID client)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    85
		noSuchTeam = isNothing findTeam
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    86
		team = fromJust findTeam
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    87
		findTeam = find (\t -> teamName == teamname t) $ teams room
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    88
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    89
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    90
handleCmd_inRoom clID clients rooms ["HH_NUM", teamName, numberStr] =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    91
	if not $ isMaster client then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    92
		[ProtocolError "Not room master"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    93
	else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    94
		if hhNumber < 1 || hhNumber > 8 || noSuchTeam || hhNumber > (canAddNumber + (hhnum team)) then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    95
			[]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    96
		else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    97
			[ModifyRoom $ modifyTeam team{hhnum = hhNumber},
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    98
			AnswerOthersInRoom ["HH_NUM", teamName, show hhNumber]]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    99
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   100
		client = clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   101
		room = rooms IntMap.! (roomID client)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   102
		hhNumber = fromMaybe 0 (maybeRead numberStr :: Maybe Int)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   103
		noSuchTeam = isNothing findTeam
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   104
		team = fromJust findTeam
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   105
		findTeam = find (\t -> teamName == teamname t) $ teams room
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   106
		canAddNumber = 48 - (sum . map hhnum $ teams room)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   107
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   108
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   109
handleCmd_inRoom clID clients rooms ["TEAM_COLOR", teamName, newColor] =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   110
	if not $ isMaster client then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   111
		[ProtocolError "Not room master"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   112
	else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   113
		if noSuchTeam then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   114
			[]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   115
		else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   116
			[ModifyRoom $ modifyTeam team{teamcolor = newColor},
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   117
			AnswerOthersInRoom ["TEAM_COLOR", teamName, newColor]]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   118
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   119
		noSuchTeam = isNothing findTeam
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   120
		team = fromJust findTeam
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   121
		findTeam = find (\t -> teamName == teamname t) $ teams room
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   122
		client = clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   123
		room = rooms IntMap.! (roomID client)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   124
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   125
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   126
handleCmd_inRoom clID clients rooms ["TOGGLE_READY"] =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   127
	[ModifyClient (\c -> c{isReady = not $ isReady client}),
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   128
	ModifyRoom (\r -> r{readyPlayers = readyPlayers r + (if isReady client then -1 else 1)}),
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   129
	AnswerThisRoom $ [if isReady client then "NOT_READY" else "READY", nick client]]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   130
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   131
		client = clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   132
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   133
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   134
handleCmd_inRoom clID clients rooms ["START_GAME"] =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   135
	if isMaster client && (playersIn room == readyPlayers room) && (not $ gameinprogress room) then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   136
		if enoughClans then
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   137
			[ModifyRoom
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   138
					(\r -> r{
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   139
						gameinprogress = True,
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   140
						roundMsgs = empty,
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   141
						leftTeams = [],
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   142
						teamsAtStart = teams r}
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   143
					),
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   144
			AnswerThisRoom ["RUN_GAME"]]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   145
		else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   146
			[Warning "Less than two clans!"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   147
	else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   148
		[]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   149
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   150
		client = clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   151
		room = rooms IntMap.! (roomID client)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   152
		enoughClans = not $ null $ drop 1 $ group $ map teamcolor $ teams room
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   153
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   154
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   155
handleCmd_inRoom _ _ rooms ["GAMEMSG", msg] =
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   156
	[ModifyRoom (\r -> r{roundMsgs = roundMsgs r |> msg}),
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   157
	AnswerOthersInRoom ["GAMEMSG", msg]]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   158
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   159
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   160
handleCmd_inRoom clID clients rooms ["ROUNDFINISHED"] =
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   161
	if isMaster client then
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   162
		[ModifyRoom
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   163
				(\r -> r{
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   164
					gameinprogress = False,
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   165
					readyPlayers = 0,
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   166
					roundMsgs = empty,
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   167
					leftTeams = [],
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   168
					teamsAtStart = []}
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   169
				),
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   170
		UnreadyRoomClients
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   171
		] ++ answerRemovedTeams
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   172
	else
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   173
		[]
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   174
	where
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   175
		client = clients IntMap.! clID
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   176
		room = rooms IntMap.! (roomID client)
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   177
		answerRemovedTeams = map (\t -> AnswerThisRoom ["REMOVE_TEAM", t]) $ leftTeams room
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   178
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   179
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   180
handleCmd_inRoom clID _ _ _ = [ProtocolError "Incorrect command (state: in room)"]