gameServer/HWProtoInRoomState.hs
author unc0rr
Fri, 20 Feb 2009 11:58:58 +0000
changeset 1811 1b9e33623b7e
parent 1804 4e78ad846fb6
child 1813 cfe1481e0247
permissions -rw-r--r--
Implement 'roundfinished' cmd on net server
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
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    16
handleCmd_inRoom clID clients _ ["CHAT_STRING", msg] =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    17
	[AnswerOthersInRoom ["CHAT_STRING", clientNick, msg]]
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
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    22
handleCmd_inRoom clID clients _ ["PART"] =
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
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    26
		[RoomRemoveThisClient]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    27
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    28
		client = clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    29
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
    30
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    31
handleCmd_inRoom clID clients rooms ("CFG" : paramName : paramStrs) =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    32
	if isMaster client then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    33
		[ModifyRoom (\r -> r{params = Map.insert paramName paramStrs (params r)})
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    34
		, AnswerOthersInRoom ("CFG" : paramName : paramStrs)]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    35
	else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    36
		[ProtocolError "Not room master"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    37
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    38
		client = clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    39
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    40
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    41
handleCmd_inRoom clID clients rooms ("ADD_TEAM" : name : color : grave : fort : voicepack : difStr : hhsInfo)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    42
	| length hhsInfo == 16 =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    43
	if length (teams room) == 6 then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    44
		[Warning "too many teams"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    45
	else if canAddNumber <= 0 then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    46
		[Warning "too many hedgehogs"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    47
	else if isJust findTeam then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    48
		[Warning "already have a team with same name"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    49
	else if gameinprogress room then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    50
		[Warning "round in progress"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    51
	else if isRestrictedTeams room then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    52
		[Warning "restricted"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    53
	else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    54
		[ModifyRoom (\r -> r{teams = teams r ++ [newTeam]}),
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    55
		AnswerThisClient ["TEAM_ACCEPTED", name],
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    56
		AnswerOthersInRoom $ teamToNet newTeam,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    57
		AnswerOthersInRoom ["TEAM_COLOR", name, color]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    58
		]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    59
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    60
		client = clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    61
		room = rooms IntMap.! (roomID client)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    62
		canAddNumber = 48 - (sum . map hhnum $ teams room)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    63
		findTeam = find (\t -> name == teamname t) $ teams room
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    64
		newTeam = (TeamInfo (nick client) name color grave fort voicepack difficulty newTeamHHNum (hhsList hhsInfo))
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    65
		difficulty = fromMaybe 0 (maybeRead difStr :: Maybe Int)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    66
		hhsList [] = []
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    67
		hhsList (n:h:hhs) = HedgehogInfo n h : hhsList hhs
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    68
		newTeamHHNum = min 4 canAddNumber
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    69
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    70
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    71
handleCmd_inRoom clID clients rooms ["REMOVE_TEAM", teamName] =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    72
	if noSuchTeam then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    73
		[Warning "REMOVE_TEAM: no such team"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    74
	else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    75
		if not $ nick client == teamowner team then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    76
			[ProtocolError "Not team owner!"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    77
		else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    78
			if not $ gameinprogress room then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    79
				[ModifyRoom (\r -> r{teams = filter (\t -> teamName /= teamname t) $ teams r}),
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    80
				AnswerOthersInRoom ["REMOVE_TEAM", teamName]]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    81
			else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    82
				[]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    83
{-			else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    84
				(noChangeClients,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    85
				modifyRoom clRoom{
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    86
					teams = filter (\t -> teamName /= teamname t) $ teams clRoom,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    87
					leftTeams = teamName : leftTeams clRoom,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    88
					roundMsgs = roundMsgs clRoom |> rmTeamMsg
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    89
					},
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    90
				answerOthersRoom ["GAMEMSG", rmTeamMsg]) -}
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    91
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    92
		client = clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    93
		room = rooms IntMap.! (roomID client)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    94
		noSuchTeam = isNothing findTeam
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    95
		team = fromJust findTeam
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    96
		findTeam = find (\t -> teamName == teamname t) $ teams room
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    97
		rmTeamMsg = toEngineMsg $ 'F' : teamName
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    98
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    99
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   100
handleCmd_inRoom clID clients rooms ["HH_NUM", teamName, numberStr] =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   101
	if not $ isMaster client then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   102
		[ProtocolError "Not room master"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   103
	else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   104
		if hhNumber < 1 || hhNumber > 8 || noSuchTeam || hhNumber > (canAddNumber + (hhnum team)) then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   105
			[]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   106
		else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   107
			[ModifyRoom $ modifyTeam team{hhnum = hhNumber},
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   108
			AnswerOthersInRoom ["HH_NUM", teamName, show hhNumber]]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   109
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   110
		client = clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   111
		room = rooms IntMap.! (roomID client)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   112
		hhNumber = fromMaybe 0 (maybeRead numberStr :: Maybe Int)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   113
		noSuchTeam = isNothing findTeam
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   114
		team = fromJust findTeam
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   115
		findTeam = find (\t -> teamName == teamname t) $ teams room
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   116
		canAddNumber = 48 - (sum . map hhnum $ teams room)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   117
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   118
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   119
handleCmd_inRoom clID clients rooms ["TEAM_COLOR", teamName, newColor] =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   120
	if not $ isMaster client then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   121
		[ProtocolError "Not room master"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   122
	else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   123
		if noSuchTeam then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   124
			[]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   125
		else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   126
			[ModifyRoom $ modifyTeam team{teamcolor = newColor},
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   127
			AnswerOthersInRoom ["TEAM_COLOR", teamName, newColor]]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   128
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   129
		noSuchTeam = isNothing findTeam
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   130
		team = fromJust findTeam
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   131
		findTeam = find (\t -> teamName == teamname t) $ teams room
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   132
		client = clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   133
		room = rooms IntMap.! (roomID client)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   134
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   135
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   136
handleCmd_inRoom clID clients rooms ["TOGGLE_READY"] =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   137
	[ModifyClient (\c -> c{isReady = not $ isReady client}),
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   138
	ModifyRoom (\r -> r{readyPlayers = readyPlayers r + (if isReady client then -1 else 1)}),
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   139
	AnswerThisRoom $ [if isReady client then "NOT_READY" else "READY", nick client]]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   140
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   141
		client = clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   142
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   143
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   144
handleCmd_inRoom clID clients rooms ["START_GAME"] =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   145
	if isMaster client && (playersIn room == readyPlayers room) && (not $ gameinprogress room) then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   146
		if enoughClans then
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   147
			[ModifyRoom
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   148
					(\r -> r{
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   149
						gameinprogress = True,
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   150
						roundMsgs = empty,
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   151
						leftTeams = [],
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   152
						teamsAtStart = teams r}
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   153
					),
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   154
			AnswerThisRoom ["RUN_GAME"]]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   155
		else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   156
			[Warning "Less than two clans!"]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   157
	else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   158
		[]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   159
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   160
		client = clients IntMap.! clID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   161
		room = rooms IntMap.! (roomID client)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   162
		enoughClans = not $ null $ drop 1 $ group $ map teamcolor $ teams room
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   163
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   164
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   165
handleCmd_inRoom _ _ rooms ["GAMEMSG", msg] =
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   166
	[ModifyRoom (\r -> r{roundMsgs = roundMsgs r |> msg}),
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   167
	AnswerOthersInRoom ["GAMEMSG", msg]]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   168
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   169
1811
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   170
handleCmd_inRoom clID clients rooms ["ROUNDFINISHED"] =
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   171
	if isMaster client then
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   172
		[ModifyRoom
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   173
				(\r -> r{
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   174
					gameinprogress = False,
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   175
					readyPlayers = 0,
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   176
					roundMsgs = empty,
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   177
					leftTeams = [],
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   178
					teamsAtStart = []}
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   179
				),
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   180
		UnreadyRoomClients
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   181
		] ++ answerRemovedTeams
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   182
	else
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   183
		[]
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   184
	where
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   185
		client = clients IntMap.! clID
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   186
		room = rooms IntMap.! (roomID client)
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   187
		answerRemovedTeams = map (\t -> AnswerThisRoom ["REMOVE_TEAM", t]) $ leftTeams room
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   188
1b9e33623b7e Implement 'roundfinished' cmd on net server
unc0rr
parents: 1804
diff changeset
   189
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
   190
handleCmd_inRoom clID _ _ _ = [ProtocolError "Incorrect command (state: in room)"]