From 77936081b5a8eaf187878fc9d4ed331885de016f Mon Sep 17 00:00:00 2001 From: Green Sky Date: Wed, 10 Jan 2024 18:22:50 +0100 Subject: [PATCH] add quick uptime --- src/main.cpp | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 93aaffb..60c2ec2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include #include @@ -19,6 +21,7 @@ #include "./config_commands.hpp" #include "./tox_commands.hpp" #include "./fun_commands.hpp" +#include // TODO: move uptime //#include //#include @@ -121,6 +124,7 @@ int main(int argc, char** argv) { signal(SIGINT, sigint_handler); #endif + const auto started_at = std::chrono::steady_clock::now(); auto last_time = std::chrono::steady_clock::now(); std::string config_path {"config.json"}; @@ -250,6 +254,42 @@ int main(int argc, char** argv) { registerToxCommands(mcd, conf, cr, rmm, tc, tpi); registerFunCommands(mcd, conf, cr, rmm); + mcd.registerCommand( + "totato", "", + "uptime", + [&](std::string_view params, Message3Handle m) -> bool { + const auto contact_from = m.get().c; + + const auto uptime = (std::chrono::steady_clock::now() - started_at); + + const auto days = std::chrono::duration_cast>>(uptime); + const auto hours = std::chrono::duration_cast(uptime) - std::chrono::duration_cast(days); + const auto minutes = (std::chrono::duration_cast(uptime) - std::chrono::duration_cast(days)) - std::chrono::duration_cast(hours); + const auto seconds = ((std::chrono::duration_cast(uptime) - std::chrono::duration_cast(days)) - std::chrono::duration_cast(hours)) - std::chrono::duration_cast(minutes); + + std::string reply_text; + reply_text += "totato uptime: "; + reply_text += std::to_string(days.count()); + reply_text += "d "; + reply_text += std::to_string(hours.count()); + reply_text += "h "; + reply_text += std::to_string(minutes.count()); + reply_text += "min "; + reply_text += std::to_string(seconds.count()); + reply_text += "s ("; + reply_text += std::to_string(std::chrono::duration_cast(uptime).count()); + reply_text += "s)"; + + rmm.sendText( + contact_from, + reply_text + ); + return true; + }, + "get current uptime.", + MessageCommandDispatcher::Perms::EVERYONE // mod? + ); + conf.dump(); std::this_thread::sleep_for(std::chrono::milliseconds(10)); // at startup, just to be safe @@ -263,7 +303,7 @@ int main(int argc, char** argv) { mts.iterate(); - pm.tick(/*time_delta*/0.02f); + const float pm_interval = pm.tick(/*time_delta*/0.02f); mc.iterate(0.02f);