From 2a49844a95593ac98e919c18651320e62f276fa7 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sun, 6 Apr 2025 17:28:47 +0400 Subject: feat: implementing lua coding --- bot/src/schemas/channel.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'bot/src/schemas/channel.cpp') diff --git a/bot/src/schemas/channel.cpp b/bot/src/schemas/channel.cpp index ae4ea53..540b7c0 100644 --- a/bot/src/schemas/channel.cpp +++ b/bot/src/schemas/channel.cpp @@ -1,5 +1,7 @@ #include "channel.hpp" +#include + namespace bot::schemas { std::optional string_to_channel_feature( const std::string &value) { @@ -11,4 +13,62 @@ namespace bot::schemas { return std::nullopt; } } + + std::optional channelfeature_to_string( + const ChannelFeature &value) { + switch (value) { + case MARKOV_RESPONSES: + return "markov_responses"; + case RANDOM_MARKOV_RESPONSES: + return "random_markov_responses"; + default: + std::nullopt; + } + } + + sol::table Channel::as_lua_table(std::shared_ptr luaState) const { + sol::table o = luaState->create_table(); + + o["id"] = this->id; + o["alias_id"] = this->alias_id; + o["alias_name"] = this->alias_name; + + o["joined_at"] = + static_cast(std::chrono::duration_cast( + this->joined_at.time_since_epoch()) + .count()); + if (this->opted_out_at.has_value()) { + o["opted_out_at"] = + static_cast(std::chrono::duration_cast( + this->opted_out_at->time_since_epoch()) + .count()); + } else { + o["opted_out_at"] = sol::lua_nil; + } + + return o; + } + + sol::table ChannelPreferences::as_lua_table( + std::shared_ptr luaState) const { + sol::table o = luaState->create_table(); + + o["id"] = this->channel_id; // TODO: remove it later too. + o["channel_id"] = this->channel_id; + o["prefix"] = this->prefix; + o["language"] = this->locale; + + sol::table f = luaState->create_table(); + + for (const ChannelFeature &feature : this->features) { + std::optional ff = channelfeature_to_string(feature); + if (ff.has_value()) { + f.add(ff.value()); + } + } + + o["features"] = f; + + return o; + } } \ No newline at end of file -- cgit v1.2.3