From d70bd2d83d68f6d51606b2101c318f6f0ff89c85 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Tue, 18 Jun 2024 15:11:02 -0400 Subject: when poll gets delayed, shorten the time so it closes before midnight --- source.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/source.cpp b/source.cpp index dc5b39a..1894a71 100644 --- a/source.cpp +++ b/source.cpp @@ -245,6 +245,11 @@ void process_all_pending() { } } +int seconds_to_midnight() { + int seconds = (post_time_utc - time(0)) % 86400; + return seconds < 0 ? seconds + 86400 : seconds; +} + void post_poll(std::string e1, std::string e2, int poll_no) { nlohmann::json body = { { "poll", { @@ -252,7 +257,7 @@ void post_poll(std::string e1, std::string e2, int poll_no) { { "answers", { { { "poll_media", { { "text", e1 } } } }, { { "poll_media", { { "text", e2 } } } } } }, - { "duration", 23 } } } }; + { "duration", seconds_to_midnight() / 3600 } } } }; std::string msg_id = api("/channels/" + channel_id + "/messages", true, body.dump())["id"]; @@ -278,13 +283,6 @@ void send_message(std::string str) { api("/channels/" + channel_id + "/messages", true, body.dump()); } -void sleep_until_midnight() { - sleep(2); - long timestamp = time(0); - int time = (post_time_utc - timestamp) % 86400; - sleep(time < 0 ? time + 86400 : time); -} - int main() { prng.seed(time(0)); @@ -339,7 +337,9 @@ int main() { load_left_for_round(); while (true) { - sleep_until_midnight(); + sleep(seconds_to_midnight()); + sleep(2); + process_all_pending(); std::optional advanced = {}; -- cgit v1.2.3