Skip to content

Commit

Permalink
Improves paging for ?trades
Browse files Browse the repository at this point in the history
Full listings will now be visible, even if a single store has many, many offers
  • Loading branch information
Spoffy committed Mar 31, 2019
1 parent 22c18df commit 1ccd80a
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions DiscordLink/DiscordDiscordCommands.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
Expand Down Expand Up @@ -182,7 +183,7 @@ await ctx.RespondAsync(
#region Trades

private static int EMBED_CONTENT_CHARACTER_LIMIT = 5000;
private static int EMBED_FIELD_CHARACTER_LIMIT = 1000;
private static int EMBED_FIELD_CHARACTER_LIMIT = 900;

private Dictionary<ulong, PagedEnumerator<Tuple<string, string>>> previousQueryEnumerator =
new Dictionary<ulong, PagedEnumerator<Tuple<string, string>>>();
Expand Down Expand Up @@ -287,20 +288,48 @@ private IEnumerable<Tuple<string, string>> OffersToFields<T>(T buyOffers, T sell
{
foreach(var group in sellOffers)
{
var body = TradeOffersFieldMessage(group,
var offer_descriptions = TradeOffersToDescriptions(group,
t => t.Item2.Price.ToString(),
t => context(t),
t => t.Item2.Stack.Quantity);
yield return Tuple.Create($"**Selling for {group.Key}**", body.Substring(0, body.Length > 1000? 1000 : body.Length));
var enumerator = new PagedEnumerable<string>(offer_descriptions, EMBED_FIELD_CHARACTER_LIMIT, str => str.Length).GetPagedEnumerator();
while (enumerator.HasMorePages)
{
var fieldBodyBuilder = new StringBuilder();
while (enumerator.MoveNext())
{
fieldBodyBuilder.Append(enumerator.Current);
fieldBodyBuilder.Append("\n");
}
yield return Tuple.Create($"**Selling for {group.Key}**", fieldBodyBuilder.ToString());
}
}
foreach(var group in buyOffers)
{
var offer_descriptions = TradeOffersToDescriptions(group,
t => t.Item2.Price.ToString(),
t => context(t),
t => t.Item2.Stack.Quantity);
var enumerator = new PagedEnumerable<string>(offer_descriptions, EMBED_FIELD_CHARACTER_LIMIT, str => str.Length).GetPagedEnumerator();
while (enumerator.HasMorePages)
{
var fieldBodyBuilder = new StringBuilder();
while (enumerator.MoveNext())
{
fieldBodyBuilder.Append(enumerator.Current);
fieldBodyBuilder.Append("\n");
}
yield return Tuple.Create($"**Buying for {group.Key}**", fieldBodyBuilder.ToString());
}
}
/* foreach(var group in buyOffers)
{
var body = TradeOffersFieldMessage(group,
t => t.Item2.Price.ToString(),
t => context(t),
t => t.Item2.ShouldLimit ? (int?) t.Item2.Stack.Quantity : null);
yield return Tuple.Create($"**Buying with {group.Key}**", body.Substring(0, body.Length > 1000? 1000 : body.Length));
}
}*/
}

private PagedEnumerator<Tuple<string, string>> TradeOffersBuySell(DiscordEmbedBuilder embed, Func<StoreComponent,TradeOffer, bool> filter, Func<Tuple<StoreComponent,TradeOffer>,string> context)
Expand All @@ -320,17 +349,17 @@ private PagedEnumerator<Tuple<string, string>> TradeOffersBuySell(DiscordEmbedBu
return pagedFieldEnumerator;
}

private string TradeOffersFieldMessage<T>(IEnumerable<T> offers, Func<T, string> getPrice, Func<T,string> getLabel, Func<T,int?> getQuantity)
private IEnumerable<string> TradeOffersToDescriptions<T>(IEnumerable<T> offers, Func<T, string> getPrice, Func<T,string> getLabel, Func<T,int?> getQuantity)
{
return String.Join("\n", offers.Select(t =>
return offers.Select(t =>
{
var price = getPrice(t);
var quantity = getQuantity(t);
var quantityString = quantity.HasValue ? $"{quantity.Value} - " : "";
var line = $"{quantityString}${price} {getLabel(t)}";
if (quantity == 0) line = $"~~{line}~~";
return line;
}));
});
}

#endregion Trades
Expand Down

0 comments on commit 1ccd80a

Please sign in to comment.