public bool CanPay(IReadOnlyTokenCollection price) { uint usableGold = TokenCount(TokenColor.Yellow); foreach (TokenColor tokenColor in TokenUtils.AllTokens) { uint discountedPrice = price.GetCount(tokenColor) - Discount(tokenColor); if (discountedPrice < 1) { continue; } uint have = TokenCount(tokenColor); if (discountedPrice > usableGold + have) { return(false); } var needGold = discountedPrice - have < 0; if (needGold) { usableGold -= (discountedPrice - have); } } return(true); }
public bool TryPay(IReadOnlyTokenCollection price) { if (!CanPay(price)) { return(false); } foreach (TokenColor key in TokenUtils.AllTokens) { var discountedPrice = price.GetCount(key) - Discount(key); if (discountedPrice < 1) { continue; } uint goldDiff = (uint)(discountedPrice - TokenCount(key)); if (goldDiff > 0) { _tokensInternal.TryTake(TokenColor.Yellow, goldDiff); _tokensInternal.AddTokens(key, goldDiff); } else { return(false); } } _tokensInternal.TryTake(price); return(true); }
public bool TryTake(IReadOnlyTokenCollection tokensToTake) { foreach (TokenColor key in TokenUtils.AllTokens) { if (GetCount(key) < tokensToTake.GetCount(key)) { return(false); } } foreach (TokenColor key in TokenUtils.AllTokens) { _tokensInternal[key] -= tokensToTake.GetCount(key); } return(true); }