AIsOfCatan.GameController.CompleteTrade C# (CSharp) Method

CompleteTrade() public method

public CompleteTrade ( Player player, int playerid ) : GameState
player Player
playerid int
return GameState
        public GameState CompleteTrade(Player player, int playerid)
        {
            if (!proposedTrades.ContainsKey(player.Id))
                throw new IllegalActionException("Tried to complete a trade, but no trade proposed");
            if (!proposedTrades[player.Id].ContainsKey(playerid) || playerid < 0 || playerid >= players.Length)
                throw new IllegalActionException("Tried to complete a trade with an illegal player Id");

            var trade = proposedTrades[player.Id][playerid]; //remember that the trade is as seen from the opponents pov
            var opponent = players[playerid];
            if (trade.Status == TradeStatus.Declined)
                throw new IllegalActionException("Tried to complete a declined trade");

            //Validate trade

            if (trade.Give.Count > 1 || trade.Take.Count > 1)
            {
                throw new IllegalActionException("Player " + player.Id + "(" +  player.Agent.GetName() + ") tried to complete an invalid trade with Player " + opponent.Id + "(" +  opponent.Agent.GetName() + ")");
            }
            //Validate that players have enough resources (maybe do this earlier?)

            foreach (Resource resource in Enum.GetValues(typeof(Resource)))
            {
                //Give - other must have
                if (trade.Give[0].Count(r => r == resource) > opponent.Resources.Count(r => r == resource))
                    throw new InsufficientResourcesException("Player " + opponent.Id + "(" +  opponent.Agent.GetName() + ") does not have enough resource to complete trade");
                //Take - this must have
                if (trade.Take[0].Count(r => r == resource) > player.Resources.Count(r => r == resource))
                    throw new InsufficientResourcesException("Player " + player.Id + "(" + player.Agent.GetName() + ") does not have enough resource to complete trade");
            }

            //Complete trade
            foreach (var res in trade.Give[0])
            {
                opponent.Resources.Remove(res);
                player.Resources.Add(res);
            }
            foreach (var res in trade.Take[0])
            {
                player.Resources.Remove(res);
                opponent.Resources.Add(res);
            }

            Log(new AcceptTradeLogEvent(player.Id, playerid, trade.Give[0], trade.Take[0]));

            return CurrentGamestate();
        }

Usage Example

Example #1
0
 public GameState Trade(int otherPlayer)
 {
     if (!valid)
     {
         throw new IllegalActionException("Tried to perform an action on an invalid GameAction");
     }
     if (!isAfterDieRoll)
     {
         throw new IllegalActionException("Tried to trade before the die roll");
     }
     return(controller.CompleteTrade(player, otherPlayer));
 }