eorzea-votes/client/EorzeaVotes/Model/FullQuestion.cs

81 lines
2.2 KiB
C#

using EorzeaVotes.Utilities;
using ImGuiNET;
namespace EorzeaVotes.Model;
[Serializable]
internal class FullQuestion : IQuestion {
/// <inheritdoc/>
public required Guid Id { get; init; }
/// <inheritdoc/>
public required string Date { get; init; }
/// <inheritdoc/>
public required bool Active { get; init; }
/// <inheritdoc/>
public required string Text { get; init; }
/// <inheritdoc/>
public required string[] Answers { get; init; }
/// <inheritdoc/>
public required string? Suggester { get; init; }
/// <summary>
/// A map of country to a list of responses to the question.
/// <br/><br/>
/// The array is the number of respondents who chose the answer at that
/// index.
/// </summary>
public required ulong[] Responses { get; init; }
/// <summary>
/// The response given by the user, if any.
/// </summary>
public required ushort? Response { get; init; }
internal void DrawResponses() {
this.DrawTable(this.Responses);
}
internal void DrawTable(ulong[] responses, string? label = null) {
label ??= $"##{this.Text}-total-responses";
if (!ImGui.BeginTable(label, 3)) {
return;
}
using var end2 = new OnDispose(ImGui.EndTable);
var sum = responses.Aggregate(0ul, (agg, val) => agg + val);
ImGui.TableSetupColumn("Response");
ImGui.TableSetupColumn("Pct");
ImGui.TableSetupColumn("Total");
ImGui.TableHeadersRow();
for (var i = 0; i < responses.Length && i < this.Answers.Length; i++) {
ImGui.TableNextRow();
ImGui.TableNextColumn();
var answer = this.Answers[i];
if (this.Response == i) {
answer += " \u2713";
}
ImGui.TextUnformatted(answer);
ImGui.TableNextColumn();
// ReSharper disable once PossiblyMistakenUseOfInterpolatedStringInsert
ImGui.TextUnformatted(
sum == 0
? $"{0:N2}%"
: $"{(float) responses[i] / sum * 100:N2}%"
);
ImGui.TableNextColumn();
ImGui.TextUnformatted($"{responses[i]:N0}");
}
}
}