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