示例#1
0
        public NegotiationResult AddResource(string resourceName, int quantity)
        {
            var usedPayload     = _resources.Sum(r => r.Value);
            var proposedPayload = usedPayload + quantity;

            if (proposedPayload > Payload)
            {
                return(new InsufficientPayloadNegotiationResult(proposedPayload - Payload));
            }

            var resource = new Dictionary <string, int>
            {
                { resourceName, quantity }
            };

            var originResult = OriginDepot.NegotiateConsumer(resource);

            if (originResult is FailedNegotiationResult)
            {
                return(originResult);
            }

            var destinationResult = DestinationDepot.NegotiateProvider(resource);

            if (!_resources.ContainsKey(resourceName))
            {
                _resources.Add(resourceName, quantity);
            }
            else
            {
                _resources[resourceName] += quantity;
            }
            return(destinationResult);
        }
示例#2
0
        public NegotiationResult RemoveResource(string resourceName, int quantity)
        {
            if (quantity > 0)
            {
                quantity = quantity * -1;
            }

            var resource = new Dictionary <string, int>
            {
                { resourceName, quantity }
            };

            var destinationResult = DestinationDepot.NegotiateProvider(resource);

            if (destinationResult is BrokenNegotiationResult)
            {
                return(destinationResult);
            }

            var originResult = OriginDepot.NegotiateConsumer(resource);

            _resources[resourceName] += quantity;  // this will actually deduct the resources since quantity will be negative
            if (_resources[resourceName] < 1)
            {
                _resources.Remove(resourceName);
            }

            return(originResult);
        }