summaryrefslogtreecommitdiff
path: root/bot/src
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-04-16 20:49:33 +0500
committerilotterytea <iltsu@alright.party>2025-04-16 23:04:52 +0500
commit5ca6a1c1ccfff28ff28f992a6d2231c49f7cabf9 (patch)
treea4634a327162a555eb14604c3dc6bb70437fecb6 /bot/src
parent52a6af6a2e5d64561a6a8a15c7e079fe46ffaa85 (diff)
feat: more string functions
Diffstat (limited to 'bot/src')
-rw-r--r--bot/src/commands/lua.cpp61
-rw-r--r--bot/src/schemas/stream.cpp16
-rw-r--r--bot/src/schemas/stream.hpp1
3 files changed, 78 insertions, 0 deletions
diff --git a/bot/src/commands/lua.cpp b/bot/src/commands/lua.cpp
index 4723311..369937c 100644
--- a/bot/src/commands/lua.cpp
+++ b/bot/src/commands/lua.cpp
@@ -28,6 +28,7 @@
#include "cpr/multipart.h"
#include "cpr/response.h"
#include "schemas/channel.hpp"
+#include "schemas/stream.hpp"
#include "schemas/user.hpp"
#include "utils/chrono.hpp"
#include "utils/string.hpp"
@@ -390,6 +391,64 @@ namespace bot::command::lua {
return sol::make_object(*state, sol::lua_nil);
}
});
+
+ state->set_function("event_type_to_str", [](const int &v) {
+ return schemas::event_type_to_string(v);
+ });
+
+ state->set_function("str_to_event_type", [](const std::string &v) {
+ return (int)schemas::string_to_event_type(v);
+ });
+
+ state->set_function("str_to_event_flag", [state](const std::string &v) {
+ auto o = schemas::string_to_event_flag(v);
+ if (o.has_value()) {
+ return sol::make_object(*state, o.value());
+ } else {
+ return sol::make_object(*state, sol::lua_nil);
+ }
+ });
+
+ state->set_function("event_flag_to_str", [state](const int &v) {
+ auto o = schemas::event_flag_to_string(v);
+ if (o.has_value()) {
+ return sol::make_object(*state, o.value());
+ } else {
+ return sol::make_object(*state, sol::lua_nil);
+ }
+ });
+
+ state->set_function("str_make_parts", [state](
+ const std::string &base,
+ const sol::table &values,
+ const std::string &prefix,
+ const std::string &separator,
+ const long long &max_length) {
+ std::vector<std::string> lines = {""};
+ int index = 0;
+
+ for (auto &[_, v] : values) {
+ const std::string &m = lines.at(index);
+ std::string x = prefix + v.as<std::string>();
+
+ if (base.length() + m.length() + x.length() + separator.length() >=
+ max_length) {
+ index += 1;
+ }
+
+ if (index > lines.size() - 1) {
+ lines.push_back(x);
+ } else {
+ lines[index] = m + separator + x;
+ }
+ }
+
+ sol::table o = state->create_table();
+ std::for_each(lines.begin(), lines.end(),
+ [&o, &base](const std::string &x) { o.add(base + x); });
+
+ return o;
+ });
}
void add_db_library(std::shared_ptr<sol::state> state,
@@ -581,6 +640,8 @@ namespace bot::command::lua {
o.add(u);
});
+
+ return o;
});
state->set_function(
diff --git a/bot/src/schemas/stream.cpp b/bot/src/schemas/stream.cpp
index 34d170a..dbd5929 100644
--- a/bot/src/schemas/stream.cpp
+++ b/bot/src/schemas/stream.cpp
@@ -16,4 +16,20 @@ namespace bot::schemas {
return EventType::CUSTOM;
}
}
+
+ std::string event_type_to_string(const int &type) {
+ if (type == LIVE) {
+ return "live";
+ } else if (type == OFFLINE) {
+ return "offline";
+ } else if (type == TITLE) {
+ return "title";
+ } else if (type == GAME) {
+ return "game";
+ } else if (type == GITHUB) {
+ return "github";
+ } else {
+ return "custom";
+ }
+ }
}
diff --git a/bot/src/schemas/stream.hpp b/bot/src/schemas/stream.hpp
index 0811561..9763ba9 100644
--- a/bot/src/schemas/stream.hpp
+++ b/bot/src/schemas/stream.hpp
@@ -5,6 +5,7 @@
namespace bot::schemas {
enum EventType { LIVE, OFFLINE, TITLE, GAME, GITHUB = 10, CUSTOM = 99 };
EventType string_to_event_type(const std::string &type);
+ std::string event_type_to_string(const int &type);
enum EventFlag { MASSPING };