Skip to content

Commit

Permalink
Improve trade embed
Browse files Browse the repository at this point in the history
  • Loading branch information
reflectronic committed May 11, 2024
1 parent 708fa51 commit 52ab0ed
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 41 deletions.
2 changes: 2 additions & 0 deletions DiscordMonies/Game/PrettyColors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public static class PrettyColors

public static readonly Color Gold = Color.FromArgb(0xF1C40F);

public static readonly Color Purple = Color.FromArgb(0x9B59B6);

public static readonly ImmutableArray<NamedColor> ColorList =
[
new("red", Color.FromArgb(255, 70, 70)),
Expand Down
6 changes: 3 additions & 3 deletions DiscordMonies/Game/Trade/TradeCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class TradeCommands(GameState gameState, IInteractionCommandContext ctx,
[Command("open")]
[RequiresPlayer(PlayerType.Player)]
[Description("Set up a new trade with a player")]
[Ephemeral]
public async Task<Result> Open([Description("The player to trade with")] GamePlayer otherPlayer)
{
var player = gameState.GetById(ctx.GetUserId());
Expand All @@ -35,8 +34,9 @@ public async Task<Result> Open([Description("The player to trade with")] GamePla
To = otherPlayer,
CopyOf = null
};

await feedback.SendContextualAsync($"Opened a new trade table. Add items to the trade table, and then offer it to {Mention.User(otherPlayer)} when you're ready.");

await feedback.SendContextualAsync($"{player} is creating a trade...", options: MessageOptions.SuppressNotifications);
await feedback.SendContextualAsync($"Opened a new trade table. Add items to the trade table, and then offer it to {Mention.User(otherPlayer)} when you're ready.", options: MessageOptions.Ephemeral);
return Result.Success;
}

Expand Down
43 changes: 14 additions & 29 deletions DiscordMonies/Game/Trade/TradeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public async Task Toggle(IOwnable ownable, bool giving)
}

entry.Add(ownable);
await feedback.SendContextualAsync($"Added {ownable} to the trade table.", embeds: new([tradeTable.PrintTradeTable(true)]));
await feedback.SendContextualAsync($"Added {ownable} to the trade table.", embeds: new([tradeTable.PrintTradeTable()]));
}

public async Task SetMoney(int money, bool giving)
Expand Down Expand Up @@ -93,7 +93,7 @@ public async Task SetMoney(int money, bool giving)
{
tradeTable.Money = -money;
}
await feedback.SendContextualAsync($"Added {money.MoneyString()} to the trade table.", embeds: new([tradeTable.PrintTradeTable(true)]));
await feedback.SendContextualAsync($"Added {money.MoneyString()} to the trade table.", embeds: new([tradeTable.PrintTradeTable()]));
}

public async Task Offer()
Expand All @@ -105,15 +105,15 @@ public async Task Offer()
{
await feedback.SendContextualAsync(
$"Unable to offer this trade table as it is empty. Please add items to the trade table.",
embeds: new([tradeTable.PrintTradeTable(true)]));
embeds: new([tradeTable.PrintTradeTable()]));
return;
}

if (!await EnsureTradeTableValid(tradeTable))
{
await feedback.SendContextualAsync(
$"Unable to offer this trade table as it is now invalid. Please check the trade table and ensure that each player has the required assets in order to trade.",
embeds: new([tradeTable.PrintTradeTable(true)]));
embeds: new([tradeTable.PrintTradeTable()]));
return;
}

Expand All @@ -127,7 +127,7 @@ await feedback.SendContextualAsync(

await feedback.SendAsync(gameState.ThreadId,
tradeTable.CopyOf is not null ? $"{Mention.User(tradeTable.To)}, **{tradeTable.From}** offers you a counter trade." : $"{Mention.User(tradeTable.To)}, **{tradeTable.From}** wants to trade with you.",
embeds: new([tradeTable.PrintTradeTable(false)]), options: new()
embeds: new([tradeTable.PrintTradeTable()]), options: new()
{
MessageComponents = new([
new ActionRowComponent([
Expand Down Expand Up @@ -184,28 +184,22 @@ public async Task AcceptTradeTable(TradeTable tradeTable)
{
if (tradeTable.State != TradeTable.TradeTableState.Offered)
{
await feedback.SendContextualAsync("You can't accept this trade table because it's not currently open.", options: new()
{
MessageFlags = new(MessageFlags.Ephemeral)
});
await feedback.SendContextualAsync("You can't accept this trade table because it's not currently open.", options: MessageOptions.Ephemeral);
return;
}

var player = gameState.GetById(ctx.GetUserId());
if (tradeTable.To != player)
{
await feedback.SendContextualAsync("You can't accept this trade table because it's not addressed to you.", options: new()
{
MessageFlags = new(MessageFlags.Ephemeral)
});
await feedback.SendContextualAsync("You can't accept this trade table because it's not addressed to you.", options: MessageOptions.Ephemeral);
return;
}

if (!await EnsureTradeTableValid(tradeTable))
{
await feedback.SendContextualAsync(
"Unable to accept this trade table as it is now invalid. Please check the trade table and ensure that each player has the required assets in order to trade. You can counter the trade table in order to edit it.",
embeds: new([tradeTable.PrintTradeTable(true)]));
embeds: new([tradeTable.PrintTradeTable()]));
return;
}

Expand All @@ -218,36 +212,30 @@ await feedback.SendContextualAsync(
await gameControllerService.TryPay(tradeTable.From, tradeTable.To, tradeTable.Money);

await feedback.SendAsync(gameState.ThreadId,
$"{tradeTable.To} has completed a trade with **{tradeTable.From}**.",
embeds: new([tradeTable.PrintTradeTable(true)]));
$"{tradeTable.To} has accepted {tradeTable.From}'s trade.",
embeds: new([tradeTable.PrintTradeTable()]));
}

public async Task CounterTradeTable(TradeTable tradeTable)
{
if (tradeTable.State != TradeTable.TradeTableState.Offered)
{
await feedback.SendContextualAsync("You can't counter this trade table because it's not currently open.", options: new()
{
MessageFlags = new(MessageFlags.Ephemeral)
});
await feedback.SendContextualAsync("You can't counter this trade table because it's not currently open.", options: MessageOptions.Ephemeral);
return;
}

var player = gameState.GetById(ctx.GetUserId());
if (tradeTable.To != player)
{
await feedback.SendContextualAsync("You can't counter this trade table because it's not addressed to you.", options: new()
{
MessageFlags = new(MessageFlags.Ephemeral)
});
await feedback.SendContextualAsync("You can't counter this trade table because it's not addressed to you.", options: MessageOptions.Ephemeral);
return;
}

tradeTable.State = TradeTable.TradeTableState.Closed;
player.ConfiguringTradeTable = tradeTable.Copy();

await feedback.SendContextualAsync("You can now edit the trade. Once you're done, offer the trade with /trade offer again.",
embeds: new([tradeTable.PrintTradeTable(true)]), options: new()
embeds: new([tradeTable.PrintTradeTable()]), options: new()
{
MessageFlags = new(MessageFlags.Ephemeral)
});
Expand All @@ -257,10 +245,7 @@ public async Task RejectTradeTable(TradeTable tradeTable)
{
if (tradeTable.State != TradeTable.TradeTableState.Offered)
{
await feedback.SendContextualAsync("You can't reject this trade table because it's not currently open.", options: new()
{
MessageFlags = new(MessageFlags.Ephemeral)
});
await feedback.SendContextualAsync("You can't reject this trade table because it's not currently open.", options: MessageOptions.Ephemeral);
return;
}

Expand Down
19 changes: 10 additions & 9 deletions DiscordMonies/Game/Trade/TradeTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,18 @@ public TradeTable Copy()
};
}

public Embed PrintTradeTable(bool asGiving)
public Embed PrintTradeTable()
{
static string OwnableEntry(IOwnable ownable)
{
return ownable is Space { IsMortgaged: true } ? $"- {Emoji.CircledM} {ownable}" : $"- {ownable}";
}

var builder = new EmbedBuilder()
.WithTitle("Trade")
.AddField($"{From} gives", string.Join("\n", Giving.Select(OwnableEntry).Append(Money > 0 ? Money.MoneyString() : string.Empty))).Get()
.AddField($"{To} gives", string.Join("\n", Receiving.Select(OwnableEntry).Append(Money < 0 ? (-Money).MoneyString() : string.Empty))).Get();
.WithTitle($"Trade {Emoji.TwistedArrows}")
.WithColour(PrettyColors.Purple)
.AddField($"{From.DisplayName} gives:", string.Join("\n", Giving.Select(OwnableEntry).Append(Money > 0 ? $"- {Money.MoneyString()}" : string.Empty)), true).Get()
.AddField($"{To.DisplayName} gives:", string.Join("\n", Receiving.Select(OwnableEntry).Append(Money < 0 ? $"- {(-Money).MoneyString()}" : string.Empty)), true).Get();

if (Giving.Concat(Receiving).Any(x => x is Space { IsMortgaged: true }))
{
Expand All @@ -58,9 +64,4 @@ public Embed PrintTradeTable(bool asGiving)

return builder.Build().Get();
}

private static string OwnableEntry(IOwnable ownable)
{
return ownable is Space { IsMortgaged: true } ? $"{Emoji.CircledM} {ownable.ToString()}" : ownable.ToString();
}
}

0 comments on commit 52ab0ed

Please sign in to comment.