-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDurabilityLine.cs
More file actions
43 lines (39 loc) · 1.91 KB
/
Copy pathDurabilityLine.cs
File metadata and controls
43 lines (39 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System.Globalization;
using RustPlusBot.Features.ItemData.Data;
using RustPlusBot.Features.ItemData.Naming;
namespace RustPlusBot.Features.Commands.Formatting;
/// <summary>Formats the multi-line durability (raid-cost) reply for a target.</summary>
internal static class DurabilityLine
{
/// <summary>Lists each explosive cost for a target, cheapest by sulfur first.</summary>
/// <param name="target">The raid target (with at least one cost).</param>
/// <param name="names">Resolves tool item ids to names.</param>
public static string Format(RaidTarget target, IItemNameResolver names)
{
ArgumentNullException.ThrowIfNull(target);
ArgumentNullException.ThrowIfNull(names);
var lines = target.Costs
.OrderBy(c => c.Sulfur ?? int.MaxValue)
.ThenBy(c => c.Quantity)
.Select(c => FormatCost(c, names));
return string.Create(CultureInfo.InvariantCulture, $"{target.Name}:\n{string.Join("\n", lines)}");
}
private static string FormatCost(RaidCost cost, IItemNameResolver names)
{
var quantity = (int)Math.Ceiling(cost.Quantity);
var tool = names.Resolve(cost.ToolId);
var side = cost.Side is "soft" or "hard"
? string.Create(CultureInfo.InvariantCulture, $" ({cost.Side})")
: string.Empty;
var sulfur = cost.Sulfur is { } s
? string.Create(CultureInfo.InvariantCulture, $" — {s} sulfur")
: string.Empty;
var time = cost.TimeSeconds is { } t and > 0
? string.Create(CultureInfo.InvariantCulture, $" ({DurationFormat.Seconds(t)})")
: string.Empty;
var caption = string.IsNullOrEmpty(cost.Caption)
? string.Empty
: string.Create(CultureInfo.InvariantCulture, $" · {cost.Caption}");
return string.Create(CultureInfo.InvariantCulture, $"{tool} ×{quantity}{side}{sulfur}{time}{caption}");
}
}