Skip to content

Commit

Permalink
Try to track the status of dynamic and manageable better
Browse files Browse the repository at this point in the history
  • Loading branch information
Rosuav committed Oct 25, 2023
1 parent 894f0d9 commit 99291d0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
17 changes: 11 additions & 6 deletions modules/http/chan_giveaway.pike
Original file line number Diff line number Diff line change
Expand Up @@ -334,16 +334,21 @@ continue mapping(string:mixed)|Concurrent.Future http_request(Protocols.HTTP.Ser
//multiple rewards, add a numeric disambiguator on conflict.
string deftitle = copyfrom->title || "Example Dynamic Reward";
mapping rwd = (["basecost": copyfrom->cost || 1000, "availability": "{online}", "formula": "PREV"]);
array have = filter((G->G->pointsrewards[chan]||({}))->title, has_prefix, deftitle);
array have = filter((G->G->pointsrewards[broadcaster_id]||({}))->title, has_prefix, deftitle);
copyfrom |= (["title": deftitle + " #" + (sizeof(have) + 1), "cost": rwd->basecost]);
string id = yield(twitch_api_request("https://api.twitch.tv/helix/channel_points/custom_rewards?broadcaster_id=" + broadcaster_id,
mapping info = yield(twitch_api_request("https://api.twitch.tv/helix/channel_points/custom_rewards?broadcaster_id=" + broadcaster_id,
(["Authorization": "Bearer " + token]),
(["method": "POST", "json": copyfrom]),
))->data[0]->id;
//write("Created new dynamic: %O\n", info->data[0]);
cfg->dynamic_rewards[id] = rwd;
))->data[0];
//write("Created new dynamic: %O\n", info);
//TODO: Update G->G->pointsrewards immediately, and push out the update
//This will speed up the response to user significantly. Do this once it's
//all dealt with by pointsmgr instead of here.
cfg->dynamic_rewards[info->id] = rwd;
if (!G->G->rewards_manageable[broadcaster_id]) G->G->rewards_manageable[broadcaster_id] = (<>);
G->G->rewards_manageable[broadcaster_id][info->id] = 1;
req->misc->channel->config_save();
return jsonify((["ok": 1, "reward": rwd | (["id": id])]));
return jsonify((["ok": 1, "reward": rwd | (["id": info->id])]));
}
if (string id = body->dynamic_id) { //TODO: Ditto, move to pointsrewards
if (!cfg->dynamic_rewards || !cfg->dynamic_rewards[id]) return (["error": 400]);
Expand Down
5 changes: 1 addition & 4 deletions modules/http/chan_pointsrewards.pike
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ mapping get_chan_state(object channel, string grp, string|void id, string|void t
foreach (rewards, mapping rew) {
mapping r = current[rew->id];
//Note that attributes set in dynamic_rewards override those seen in current status.
if (r) {
dynrewards += ({(["id": rew->id, "title": rew->title, "prompt": rew->prompt, "curcost": rew->cost]) | r});
rew->is_dynamic = 1; //hack
}
if (r) dynrewards += ({(["id": rew->id, "title": rew->title, "prompt": rew->prompt, "curcost": rew->cost]) | r});
rew->invocations = channel->redemption_commands[rew->id] || ({ });
if (rew->id == id) return type == "dynreward" ? r && dynrewards[-1] : rew; //Can't be bothered remapping to remove the search
}
Expand Down
13 changes: 10 additions & 3 deletions modules/pointsmgr.pike
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ inherit hook;
inherit annotated;

@retain: mapping pointsrewards = ([]);
@retain: mapping(int:multiset(string)) rewards_manageable = ([]); //rewards_manageable[broadcaster_id][reward_id] is 1 if we can edit that reward

@create_hook:
constant point_redemption = ({"string chan", "string rewardid", "int(0..1) refund", "mapping data"});
Expand Down Expand Up @@ -58,6 +59,9 @@ mapping remap_eventsub_message(mapping info) {
mapping el = elem ? info[elem] : info;
if (el && !undefinedp(el[from])) el[to] = m_delete(el, from);
}
if (rewards_manageable[(int)info->broadcaster_id][?info->id]) info->can_manage = 1;
mapping current = G->G->irc->id[(int)info->broadcaster_id]->?config->?dynamic_rewards;
if (current[?info->id]) info->is_dynamic = 1;
return info;
}

Expand Down Expand Up @@ -104,7 +108,7 @@ continue Concurrent.Future update_dynamic_reward(object channel, string rewardid
));
//TODO: Error check
//Note that the update doesn't need to be pushed through the cache, as the
//rewardupd hook above should do this for us.
//rewardupd hook above should do this for us. It would speed things up though.
}

multiset pending_update_alls = (<>);
Expand Down Expand Up @@ -136,8 +140,11 @@ continue Concurrent.Future populate_rewards_cache(string chan, string|int|void b
multiset unseen = (multiset)indices(current) - (multiset)rewards->id;
if (sizeof(unseen)) {m_delete(current, ((array)unseen)[*]); channel->config_save();}
}
multiset manageable = (multiset)yield(twitch_api_request(url + "&only_manageable_rewards=true", params))->data->id;
foreach (rewards, mapping r) r->can_manage = manageable[r->id];
multiset manageable = rewards_manageable[broadcaster_id] = (multiset)yield(twitch_api_request(url + "&only_manageable_rewards=true", params))->data->id;
foreach (rewards, mapping r) {
r->can_manage = manageable[r->id];
if (current[?r->id]) r->is_dynamic = 1;
}
pointsrewards[(int)broadcaster_id] = rewards;
broadcaster_id = (string)broadcaster_id;
rewardadd(broadcaster_id, (["broadcaster_user_id": broadcaster_id]));
Expand Down

0 comments on commit 99291d0

Please sign in to comment.