Set temporary variables in Twitch condition

This commit is contained in:
WarmUpTill
2023-11-04 12:35:51 +01:00
committed by WarmUpTill
parent 324d3fe7a5
commit 569b915c07
3 changed files with 768 additions and 3 deletions

View File

@@ -3,6 +3,7 @@
#include <log-helper.hpp>
#include <utility.hpp>
#include <nlohmann/json.hpp>
namespace advss {
@@ -222,6 +223,7 @@ const static std::map<MacroConditionTwitch::Condition, std::string>
void MacroConditionTwitch::SetCondition(const Condition &condition)
{
_condition = condition;
SetupTempVars();
ResetSubscription();
}
@@ -238,6 +240,38 @@ void MacroConditionTwitch::SetPointsReward(
ResetSubscription();
}
static void
setTempVarsHelper(const std::string &jsonStr,
std::function<void(const char *, const char *)> setVar)
{
try {
auto json = nlohmann::json::parse(jsonStr);
for (auto it = json.begin(); it != json.end(); ++it) {
if (it->is_string()) {
setVar(it.key().c_str(),
it->get<std::string>().c_str());
continue;
}
setVar(it.key().c_str(), it->dump().c_str());
}
} catch (const nlohmann::json::exception &e) {
vblog(LOG_INFO, "%s", jsonStr.c_str());
vblog(LOG_INFO, "%s", e.what());
}
}
static void
setTempVarsHelper(obs_data_t *data,
std::function<void(const char *, const char *)> setVar)
{
auto jsonStr = obs_data_get_json(data);
if (!jsonStr) {
return;
}
setTempVarsHelper(jsonStr, setVar);
}
bool MacroConditionTwitch::CheckChannelGenericEvents(TwitchToken &token)
{
auto eventSub = token.GetEventSub();
@@ -247,10 +281,16 @@ bool MacroConditionTwitch::CheckChannelGenericEvents(TwitchToken &token)
auto events = eventSub->Events();
for (const auto &event : events) {
if (_subscriptionID == event.id) {
SetVariableValue(event.ToString());
return true;
if (_subscriptionID != event.id) {
continue;
}
SetVariableValue(event.ToString());
setTempVarsHelper(
event.data,
std::bind(&MacroConditionTwitch::SetTempVarValue, this,
std::placeholders::_1,
std::placeholders::_2));
return true;
}
return false;
@@ -281,6 +321,11 @@ bool MacroConditionTwitch::CheckChannelLiveEvents(TwitchToken &token)
}
SetVariableValue(event.ToString());
setTempVarsHelper(
event.data,
std::bind(&MacroConditionTwitch::SetTempVarValue, this,
std::placeholders::_1,
std::placeholders::_2));
return true;
}
@@ -302,6 +347,60 @@ static bool titleMatches(const RegexConfig &conf, const std::string &title,
return match.hasMatch();
}
void MacroConditionTwitch::SetTempVarValues(const ChannelLiveInfo &info)
{
SetTempVarValue("broadcaster_user_id", info.user_id);
SetTempVarValue("broadcaster_user_login", info.user_login);
SetTempVarValue("broadcaster_user_name", info.user_name);
SetTempVarValue("id", info.id);
SetTempVarValue("game_id", info.game_id);
SetTempVarValue("game_name", info.game_name);
SetTempVarValue("title", info.title);
std::string tags;
for (const auto &tag : info.tags) {
tags += tag + " ";
}
if (!tags.empty()) {
tags.pop_back();
}
SetTempVarValue("tags", tags);
SetTempVarValue("viewer_count", std::to_string(info.viewer_count));
SetTempVarValue("started_at", info.started_at.toString().toStdString());
SetTempVarValue("language", info.language);
SetTempVarValue("thumbnail_url", info.thumbnail_url);
SetTempVarValue("is_mature", info.is_mature ? "true" : "false");
}
void MacroConditionTwitch::SetTempVarValues(const ChannelInfo &info)
{
SetTempVarValue("broadcaster_user_id", info.broadcaster_id);
SetTempVarValue("broadcaster_user_login", info.broadcaster_login);
SetTempVarValue("broadcaster_user_name", info.broadcaster_name);
SetTempVarValue("language", info.broadcaster_language);
SetTempVarValue("game_id", info.game_id);
SetTempVarValue("game_name", info.game_name);
SetTempVarValue("title", info.title);
SetTempVarValue("delay", std::to_string(info.delay));
std::string tags;
for (const auto &tag : info.tags) {
tags += tag + " ";
}
if (!tags.empty()) {
tags.pop_back();
}
SetTempVarValue("tags", tags);
std::string classificationLabels;
for (const auto &label : info.content_classification_labels) {
classificationLabels += label + " ";
}
if (!classificationLabels.empty()) {
classificationLabels.pop_back();
}
SetTempVarValue("content_classification_labels", tags);
SetTempVarValue("is_branded_content",
info.is_branded_content ? "true" : "false");
}
bool MacroConditionTwitch::CheckCondition()
{
SetVariableValue("");
@@ -377,6 +476,8 @@ bool MacroConditionTwitch::CheckCondition()
if (!info) {
return false;
}
SetTempVarValues(*info);
return info->IsLive();
}
case Condition::TITLE_POLLING: {
@@ -385,6 +486,7 @@ bool MacroConditionTwitch::CheckCondition()
return false;
}
SetVariableValue(info->title);
SetTempVarValues(*info);
return titleMatches(_regex, info->title, _streamTitle);
}
case Condition::CATEGORY_POLLING: {
@@ -393,6 +495,7 @@ bool MacroConditionTwitch::CheckCondition()
return false;
}
SetVariableValue(info->game_name);
SetTempVarValues(*info);
return info->game_id == std::to_string(_category.id);
}
default:
@@ -699,6 +802,331 @@ void MacroConditionTwitch::AddChannelGenericEventSubscription(
_subscriptionIDFuture = waitForSubscription(token, subscription);
}
static std::string tryTranslate(const std::string &testString)
{
auto translated = obs_module_text(testString.c_str());
if (testString != translated) {
return translated;
}
return "";
}
void MacroConditionTwitch::SetupTempVars()
{
MacroCondition::SetupTempVars();
auto setupTempVarHelper = [&](const std::string &id,
const std::string &extra = "") {
std::string name = tryTranslate(
"AdvSceneSwitcher.tempVar.twitch." + id + extra);
std::string description =
tryTranslate("AdvSceneSwitcher.tempVar.twitch." + id +
extra + ".description");
AddTempvar(id, name.empty() ? id : name, description);
};
setupTempVarHelper("broadcaster_user_id");
setupTempVarHelper("broadcaster_user_login");
setupTempVarHelper("broadcaster_user_name");
switch (_condition) {
case Condition::STREAM_ONLINE_LIVE_EVENT:
case Condition::STREAM_ONLINE_PLAYLIST_EVENT:
case Condition::STREAM_ONLINE_WATCHPARTY_EVENT:
case Condition::STREAM_ONLINE_PREMIERE_EVENT:
case Condition::STREAM_ONLINE_RERUN_EVENT:
case Condition::STREAM_ONLINE_EVENT:
setupTempVarHelper("id", ".stream.online");
setupTempVarHelper("type", ".stream.online");
setupTempVarHelper("started_at", ".stream.online");
break;
case Condition::STREAM_OFFLINE_EVENT:
break;
case Condition::SUBSCRIPTION_START_EVENT:
setupTempVarHelper("user_id", ".subscribe");
setupTempVarHelper("user_login", ".subscribe");
setupTempVarHelper("user_name", ".subscribe");
setupTempVarHelper("tier", ".subscribe");
setupTempVarHelper("is_gift", ".subscribe");
break;
case Condition::SUBSCRIPTION_END_EVENT:
setupTempVarHelper("user_id", ".subscribe.end");
setupTempVarHelper("user_login", ".subscribe.end");
setupTempVarHelper("user_name", ".subscribe.end");
setupTempVarHelper("tier", ".subscribe");
setupTempVarHelper("is_gift", ".subscribe.end");
break;
case Condition::SUBSCRIPTION_GIFT_EVENT:
setupTempVarHelper("user_id", ".subscribe.gift");
setupTempVarHelper("user_login", ".subscribe.gift");
setupTempVarHelper("user_name", ".subscribe.gift");
setupTempVarHelper("tier", ".subscribe");
setupTempVarHelper("total", ".subscribe");
setupTempVarHelper("cumulative_total", ".subscribe");
setupTempVarHelper("is_anonymous", ".subscribe");
break;
case Condition::SUBSCRIPTION_MESSAGE_EVENT:
setupTempVarHelper("user_id", ".subscribe.message");
setupTempVarHelper("user_login", ".subscribe.message");
setupTempVarHelper("user_name", ".subscribe.message");
setupTempVarHelper("tier", ".subscribe");
setupTempVarHelper("message", ".subscribe");
setupTempVarHelper("cumulative_months", ".subscribe");
setupTempVarHelper("streak_months", ".subscribe");
setupTempVarHelper("duration_months", ".subscribe");
break;
case Condition::CHEER_EVENT:
setupTempVarHelper("user_id", ".cheer");
setupTempVarHelper("user_login", ".cheer");
setupTempVarHelper("user_name", ".cheer");
setupTempVarHelper("is_anonymous", ".cheer");
setupTempVarHelper("message", ".cheer");
setupTempVarHelper("bits");
break;
case Condition::POLL_START_EVENT:
case Condition::POLL_PROGRESS_EVENT:
setupTempVarHelper("id", ".poll");
setupTempVarHelper("title", ".poll");
setupTempVarHelper("choices", ".poll");
setupTempVarHelper("channel_points_voting");
setupTempVarHelper("started_at", ".poll");
setupTempVarHelper("ends_at", ".poll");
break;
case Condition::POLL_END_EVENT:
setupTempVarHelper("id", ".poll");
setupTempVarHelper("title", ".poll");
setupTempVarHelper("choices", ".pollEnd");
setupTempVarHelper("channel_points_voting");
setupTempVarHelper("started_at", ".poll");
setupTempVarHelper("ends_at", ".poll");
setupTempVarHelper("status", ".poll");
break;
case Condition::PREDICTION_START_EVENT:
case Condition::PREDICTION_PROGRESS_EVENT:
case Condition::PREDICTION_LOCK_EVENT:
setupTempVarHelper("id", ".prediction");
setupTempVarHelper("title", ".prediction");
setupTempVarHelper("outcomes", ".prediction");
setupTempVarHelper("started_at", ".prediction");
setupTempVarHelper("locks_at", ".prediction");
break;
case Condition::PREDICTION_END_EVENT:
setupTempVarHelper("id", ".prediction");
setupTempVarHelper("title", ".prediction");
setupTempVarHelper("outcomes", ".prediction");
setupTempVarHelper("started_at", ".prediction");
setupTempVarHelper("locks_at", ".prediction");
setupTempVarHelper("ended_at", ".prediction");
setupTempVarHelper("status", ".prediction");
break;
case Condition::GOAL_START_EVENT:
case Condition::GOAL_PROGRESS_EVENT:
case Condition::GOAL_END_EVENT:
setupTempVarHelper("id", ".goal");
setupTempVarHelper("type", ".goal");
setupTempVarHelper("description", ".goal");
setupTempVarHelper("is_achieved", ".goal");
setupTempVarHelper("current_amount", ".goal");
setupTempVarHelper("target_amount", ".goal");
setupTempVarHelper("started_at", ".goal");
setupTempVarHelper("ended_at", ".goal");
break;
case Condition::HYPE_TRAIN_START_EVENT:
case Condition::HYPE_TRAIN_PROGRESS_EVENT:
setupTempVarHelper("id", ".hype");
setupTempVarHelper("total", ".hype");
setupTempVarHelper("progress", ".hype");
setupTempVarHelper("goal", ".hype");
setupTempVarHelper("top_contributions", ".hype");
setupTempVarHelper("last_contribution", ".hype");
setupTempVarHelper("level", ".hype");
setupTempVarHelper("started_at", ".hype");
setupTempVarHelper("expires_at", ".hype");
break;
case Condition::HYPE_TRAIN_END_EVENT:
setupTempVarHelper("id", ".hype");
setupTempVarHelper("total", ".hype");
setupTempVarHelper("progress", ".hype");
setupTempVarHelper("goal", ".hype");
setupTempVarHelper("top_contributions", ".hype");
setupTempVarHelper("last_contribution", ".hype");
setupTempVarHelper("level", ".hype");
setupTempVarHelper("started_at", ".hype");
setupTempVarHelper("ended_at", ".hype");
setupTempVarHelper("cooldown_ends_at", ".hype");
break;
case Condition::CHARITY_CAMPAIGN_START_EVENT:
setupTempVarHelper("id", ".charity");
setupTempVarHelper("charity_name");
setupTempVarHelper("charity_description");
setupTempVarHelper("charity_logo");
setupTempVarHelper("current_amount", ".charity");
setupTempVarHelper("target_amount", ".charity");
setupTempVarHelper("started_at", ".charity");
break;
case Condition::CHARITY_CAMPAIGN_PROGRESS_EVENT:
setupTempVarHelper("id", ".charity");
setupTempVarHelper("charity_name");
setupTempVarHelper("charity_description");
setupTempVarHelper("charity_logo");
setupTempVarHelper("current_amount", ".charity");
setupTempVarHelper("target_amount", ".charity");
break;
case Condition::CHARITY_CAMPAIGN_DONATION_EVENT:
setupTempVarHelper("charity_name");
setupTempVarHelper("charity_description");
setupTempVarHelper("charity_logo");
setupTempVarHelper("id", ".charity");
setupTempVarHelper("user_id", ".charity");
setupTempVarHelper("user_login", ".charity");
setupTempVarHelper("user_name", ".charity");
setupTempVarHelper("amount", ".charity");
break;
case Condition::CHARITY_CAMPAIGN_END_EVENT:
setupTempVarHelper("id", ".charity");
setupTempVarHelper("charity_name");
setupTempVarHelper("charity_description");
setupTempVarHelper("charity_logo");
setupTempVarHelper("current_amount", ".charity");
setupTempVarHelper("target_amount", ".charity");
setupTempVarHelper("stopped_at", ".charity");
break;
case Condition::POINTS_REWARD_ADDITION_EVENT:
case Condition::POINTS_REWARD_UPDATE_EVENT:
case Condition::POINTS_REWARD_DELETION_EVENT:
setupTempVarHelper("id", ".reward");
setupTempVarHelper("is_enabled", ".reward");
setupTempVarHelper("is_paused", ".reward");
setupTempVarHelper("is_in_stock", ".reward");
setupTempVarHelper("title", ".reward");
setupTempVarHelper("cost", ".reward");
setupTempVarHelper("prompt", ".reward");
setupTempVarHelper("is_user_input_required", ".reward");
setupTempVarHelper("should_redemptions_skip_request_queue",
".reward");
setupTempVarHelper("max_per_stream", ".reward");
setupTempVarHelper("max_per_user_per_stream", ".reward");
setupTempVarHelper("background_color", ".reward");
setupTempVarHelper("image", ".reward");
setupTempVarHelper("default_image", ".reward");
setupTempVarHelper("global_cooldown", ".reward");
setupTempVarHelper("cooldown_expires_at", ".reward");
setupTempVarHelper("redemptions_redeemed_current_stream",
".reward");
break;
case Condition::POINTS_REWARD_REDEMPTION_EVENT:
case Condition::POINTS_REWARD_REDEMPTION_UPDATE_EVENT:
setupTempVarHelper("id", ".reward");
setupTempVarHelper("user_id", ".reward");
setupTempVarHelper("user_login", ".reward");
setupTempVarHelper("user_name", ".reward");
setupTempVarHelper("user_input", ".reward");
setupTempVarHelper("status", ".reward");
setupTempVarHelper("reward", ".reward");
setupTempVarHelper("redeemed_at", ".reward");
break;
case Condition::USER_BAN_EVENT:
setupTempVarHelper("user_id", ".ban");
setupTempVarHelper("user_login", ".ban");
setupTempVarHelper("user_name", ".ban");
setupTempVarHelper("moderator_user_id", ".ban");
setupTempVarHelper("moderator_user_login", ".ban");
setupTempVarHelper("moderator_user_name", ".ban");
setupTempVarHelper("reason", ".ban");
setupTempVarHelper("banned_at", ".ban");
setupTempVarHelper("ends_at", ".ban");
setupTempVarHelper("is_permanent", ".ban");
break;
case Condition::USER_UNBAN_EVENT:
setupTempVarHelper("user_id", ".unban");
setupTempVarHelper("user_login", ".unban");
setupTempVarHelper("user_name", ".unban");
setupTempVarHelper("moderator_user_id", ".unban");
setupTempVarHelper("moderator_user_login", ".unban");
setupTempVarHelper("moderator_user_name", ".unban");
break;
case Condition::USER_MODERATOR_ADDITION_EVENT:
setupTempVarHelper("user_id", ".addMod");
setupTempVarHelper("user_login", ".addMod");
setupTempVarHelper("user_name", ".addMod");
break;
case Condition::USER_MODERATOR_DELETION_EVENT:
setupTempVarHelper("user_id", ".removeMod");
setupTempVarHelper("user_login", ".removeMod");
setupTempVarHelper("user_name", ".removeMod");
break;
case Condition::CHANNEL_INFO_UPDATE_EVENT:
setupTempVarHelper("title");
setupTempVarHelper("language");
setupTempVarHelper("category_id");
setupTempVarHelper("category_name");
setupTempVarHelper("content_classification_labels");
break;
case Condition::SHOUTOUT_OUTBOUND_EVENT:
setupTempVarHelper("to_broadcaster_user_id", ".shoutout");
setupTempVarHelper("to_broadcaster_user_login", ".shoutout");
setupTempVarHelper("to_broadcaster_user_name", ".shoutout");
setupTempVarHelper("moderator_user_id", ".shoutout");
setupTempVarHelper("moderator_user_login", ".shoutout");
setupTempVarHelper("moderator_user_name", ".shoutout");
setupTempVarHelper("viewer_count", ".shoutout");
setupTempVarHelper("started_at", ".shoutout");
setupTempVarHelper("cooldown_ends_at", ".shoutout");
setupTempVarHelper("target_cooldown_ends_at", ".shoutout");
break;
case Condition::SHOUTOUT_INBOUND_EVENT:
setupTempVarHelper("from_broadcaster_user_id",
".shoutoutReceived");
setupTempVarHelper("from_broadcaster_user_login",
".shoutoutReceived");
setupTempVarHelper("from_broadcaster_user_name",
".shoutoutReceived");
setupTempVarHelper("viewer_count", ".shoutoutReceived");
setupTempVarHelper("started_at", ".shoutoutReceived");
break;
case Condition::SHIELD_MODE_START_EVENT:
case Condition::SHIELD_MODE_END_EVENT:
setupTempVarHelper("moderator_user_id", ".shieldMode");
setupTempVarHelper("moderator_user_login", ".shieldMode");
setupTempVarHelper("moderator_user_name", ".shieldMode");
setupTempVarHelper("started_at", ".shieldMode");
setupTempVarHelper("ended_at", ".shieldMode");
break;
case Condition::FOLLOW_EVENT:
setupTempVarHelper("user_id", ".follow");
setupTempVarHelper("user_login", ".follow");
setupTempVarHelper("user_name", ".follow");
setupTempVarHelper("followed_at");
break;
case Condition::RAID_OUTBOUND_EVENT:
case Condition::LIVE_POLLING:
setupTempVarHelper("id", ".stream.online");
setupTempVarHelper("game_id");
setupTempVarHelper("game_name");
setupTempVarHelper("title");
setupTempVarHelper("tags");
setupTempVarHelper("viewer_count", ".live");
setupTempVarHelper("started_at", ".stream.online");
setupTempVarHelper("language");
setupTempVarHelper("thumbnail_url");
setupTempVarHelper("is_mature");
break;
case Condition::TITLE_POLLING:
case Condition::CATEGORY_POLLING:
setupTempVarHelper("language");
setupTempVarHelper("game_id");
setupTempVarHelper("game_name");
setupTempVarHelper("title");
setupTempVarHelper("delay");
setupTempVarHelper("tags");
setupTempVarHelper("content_classification_labels");
setupTempVarHelper("is_branded_content");
break;
default:
break;
}
}
static inline void populateConditionSelection(QComboBox *list)
{
for (const auto &[condition, name] : conditionTypes) {
@@ -747,6 +1175,8 @@ MacroConditionTwitchEdit::MacroConditionTwitchEdit(
QWidget::connect(_category,
SIGNAL(CategoreyChanged(const TwitchCategory &)), this,
SLOT(CategoreyChanged(const TwitchCategory &)));
QWidget::connect(this, SIGNAL(TempVarsChanged()), window(),
SIGNAL(SegmentTempVarsChanged()));
PlaceWidgets(obs_module_text("AdvSceneSwitcher.condition.twitch.entry"),
_layout,
@@ -794,6 +1224,7 @@ void MacroConditionTwitchEdit::ConditionChanged(int idx)
_entryData->SetCondition(static_cast<MacroConditionTwitch::Condition>(
_conditions->itemData(idx).toInt()));
SetupWidgetVisibility();
emit TempVarsChanged();
}
void MacroConditionTwitchEdit::TwitchTokenChanged(const QString &token)

View File

@@ -109,6 +109,9 @@ private:
const char *version, bool includeModeratorId = false,
const char *mainUserIdFieldName = "broadcaster_user_id",
obs_data_t *extraConditions = nullptr);
void SetupTempVars();
void SetTempVarValues(const ChannelLiveInfo &);
void SetTempVarValues(const ChannelInfo &);
Condition _condition = Condition::LIVE_POLLING;
std::future<std::string> _subscriptionIDFuture;
@@ -145,6 +148,7 @@ private slots:
signals:
void HeaderInfoChanged(const QString &);
void TempVarsChanged();
protected:
std::shared_ptr<MacroConditionTwitch> _entryData;