void PreparePollData(Poll poll) { if (poll.Name == response?.Name) { return; } PollDiagramData diagramdata = new PollDiagramData(context.GetModule <PollModule>().GetWeightedVotes(poll.Name)); diagramdata.AddOptions(context.GetModule <PollModule>().GetOptions(poll.Name)); response = new PollHttpResponse { Name = poll.Name, Description = poll.Description, Items = diagramdata.GetItems().ToArray() }; }
void ServePollData(HttpRequest request, HttpClient client) { if (response == null) { if (request.GetParameter <bool>("init")) { Poll poll = context.GetModule <PollModule>().GetPolls().RandomItem(RNG.XORShift64); if (poll != null) { PreparePollData(poll); } } if (response == null) { client.WriteStatus(200, "OK"); client.WriteHeader("Content-Length", "0"); client.EndHeader(); return; } } int count = request.GetParameter <int>("items"); if (count == 0) { count = 5; } using (MemoryStream ms = new MemoryStream()) { PollHttpResponse clientresponse = new PollHttpResponse { Name = response.Name, Description = response.Description, Items = response.Items.Take(count).ToArray() }; JSON.Write(clientresponse, ms); client.ServeData(ms.ToArray(), ".json"); } response = null; }