public bool EquipBestItems(string userId) { return(playerManager.EquipBestItems(AssertGetSessionToken(), userId)); }
public ItemBuyResult BuyItem( SessionToken token, string userId, Guid itemId, long amount, decimal maxPricePerItem) { var boughtPricePerItem = new List <decimal>(); var boughtItemCount = new List <long>(); var boughtTotalCost = 0m; var boughtTotalAmount = 0L; if (amount <= 0 || maxPricePerItem <= 0) { return(new ItemBuyResult(ItemTradeState.RequestToLow, new long[0], new decimal[0], 0, 0)); } var character = GetCharacterAsync(token, userId); if (character == null) { return(new ItemBuyResult(ItemTradeState.Failed, new long[0], new decimal[0], 0, 0)); } if (!playerManager.AcquiredUserLock(token, character)) { return(new ItemBuyResult(ItemTradeState.Failed, new long[0], new decimal[0], 0, 0)); } var possibleMarketItems = gameData.GetMarketItems(itemId); var requestAmount = amount; var resources = gameData.GetResources(character.ResourcesId); var coins = resources.Coins; //var coins = character.Resources.Coins; var insufficientCoins = false; foreach (var marketItem in possibleMarketItems) { if (requestAmount <= 0) { break; } if (marketItem.PricePerItem > maxPricePerItem) { boughtItemCount.Add(0); boughtPricePerItem.Add(marketItem.PricePerItem); continue; } var buyAmount = 0L; if (marketItem.Amount < requestAmount) { buyAmount = marketItem.Amount; } else { buyAmount = requestAmount; } if (buyAmount <= 0 || marketItem.Amount <= 0) { if (marketItem.Amount <= 0) { gameData.Remove(marketItem); } else { boughtItemCount.Add(0); boughtPricePerItem.Add(marketItem.PricePerItem); } break; } var cost = marketItem.PricePerItem * buyAmount; if (cost > coins) { buyAmount = (long)(coins / marketItem.PricePerItem); // round the cost to what we can afford cost = buyAmount * marketItem.PricePerItem; if (buyAmount <= 0) { insufficientCoins = true; } } if (buyAmount <= 0) { boughtItemCount.Add(0); boughtPricePerItem.Add(marketItem.PricePerItem); continue; } boughtPricePerItem.Add(marketItem.PricePerItem); boughtItemCount.Add(buyAmount); boughtTotalCost += cost; boughtTotalAmount += buyAmount; requestAmount -= BuyMarketItemAsync(token, itemId, character, marketItem, buyAmount, cost); } if (boughtTotalAmount <= 0 || boughtTotalCost <= 0) { return(new ItemBuyResult( boughtItemCount.Count > 0 ? boughtItemCount.All(x => x == 0) || insufficientCoins ? ItemTradeState.InsufficientCoins : ItemTradeState.RequestToLow : ItemTradeState.DoesNotExist, boughtItemCount.ToArray(), boughtPricePerItem.ToArray(), 0, 0)); } character = GetCharacterAsync(token, userId); playerManager.EquipBestItems(character); return(new ItemBuyResult( ItemTradeState.Success, boughtItemCount.ToArray(), boughtPricePerItem.ToArray(), boughtTotalAmount, boughtTotalCost)); }