feat: handle new response format from server

This commit is contained in:
Anna 2023-11-12 18:59:12 -05:00
parent ffe97dd525
commit 2565994737
Signed by: anna
GPG Key ID: D0943384CD9F87D1
2 changed files with 8 additions and 12 deletions

View File

@ -23,5 +23,5 @@ internal class FullQuestion : IQuestion {
/// The array is the number of respondents who chose the answer at that
/// index.
/// </summary>
public required Dictionary<string, uint[]> Responses { get; init; }
public required ulong[] Responses { get; init; }
}

View File

@ -149,7 +149,10 @@ internal class PluginUi : IDisposable {
var answer = i;
Task.Run(async () => {
try {
// first vote
await this.Plugin.Manager.Vote(active.Id, (ushort) answer);
// then get updated info
await this.Plugin.Manager.Check();
} finally {
this._voting = false;
}
@ -177,14 +180,7 @@ internal class PluginUi : IDisposable {
}
private void DrawResponses(FullQuestion question) {
var totalResponses = new uint[question.Answers.Length];
foreach (var responses in question.Responses.Values) {
for (var i = 0; i < totalResponses.Length && i < responses.Length; i++) {
totalResponses[i] += responses[i];
}
}
var sum = totalResponses.Aggregate(0u, (agg, val) => agg + val);
var sum = question.Responses.Aggregate(0ul, (agg, val) => agg + val);
if (!ImGui.BeginTable($"##{question.Text}-total-responses", 3)) {
return;
@ -192,7 +188,7 @@ internal class PluginUi : IDisposable {
using var end2 = new OnDispose(ImGui.EndTable);
for (var i = 0; i < totalResponses.Length && i < question.Answers.Length; i++) {
for (var i = 0; i < question.Responses.Length && i < question.Answers.Length; i++) {
ImGui.TableNextRow();
ImGui.TableNextColumn();
ImGui.TextUnformatted(question.Answers[i]);
@ -202,11 +198,11 @@ internal class PluginUi : IDisposable {
ImGui.TextUnformatted(
sum == 0
? $"{0:N2}%"
: $"{(float) totalResponses[i] / sum * 100:N2}%"
: $"{(float) question.Responses[i] / sum * 100:N2}%"
);
ImGui.TableNextColumn();
ImGui.TextUnformatted($"{totalResponses[i]:N0}");
ImGui.TextUnformatted($"{question.Responses[i]:N0}");
}
}
}