Skip to content

Commit

Permalink
Merge pull request #157 from ZusorCode/match-wide-preds
Browse files Browse the repository at this point in the history
Add match-wide preds and make them default
  • Loading branch information
slmnio authored Nov 30, 2022
2 parents 7abf465 + 10f805c commit 9fedbca
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions server/src/actions/manage-prediction.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ const { getTwitchChannel,

const automaticPredictionTitleStartCharacter = "⬥";

function generatePredictionTitle(map) {
function generatePredictionTitle(map, predictionType) {
let title;
if (map.number) {
if (predictionType === "match") {
title = "Who will win the match?";
} else if (map.number) {
if (map.name) {
title = `Who will win map ${map.number} - ${map.name}?`;
} else {
Expand All @@ -30,6 +32,14 @@ function getTargetPrediction(predictions, teams) {
);
}


function getMatchWinner(match, team1, team2) {
if (match.score1 >= match.first_to) return team1;
if (match.score2 >= match.first_to) return team2;
return null;
}


module.exports = {
key: "manage-prediction",
auth: ["client"],
Expand All @@ -51,6 +61,7 @@ module.exports = {
const { broadcast, channel } = getTwitchChannel(client, ["channel:manage:predictions", "channel:read:predictions"]);
// console.log(channel);
const api = await getTwitchAPIClient(channel);
const predictionType = (broadcast.broadcast_settings || []).includes("Predict every map") ? "map" : "match";
const { match, team1, team2 } = await getMatchData(broadcast, true);

const maps = await Promise.all((match.maps || []).map(async m => {
Expand All @@ -66,26 +77,30 @@ module.exports = {

return map;
}));
if (maps.length === 0) throw ("No maps associated with match");

const matchWinner = getMatchWinner(match, team1, team2);

if (predictionType === "map" && maps.length === 0) throw ("No maps associated with match");

const { data: predictions } = await api.predictions.getPredictions(channel.channel_id);


if (["create", "lock"].includes(predictionAction)) {
const currentMap = maps.filter(m => !m.dummy && !m.winner && !m.draw && !m.banner)[0];
if (!currentMap) throw ("No valid map to start a prediction for");

if (predictionType === "map" && !currentMap) throw ("No valid map to start a prediction for");

const targetPrediction = getTargetPrediction(predictions, [team1, team2]);
console.log(targetPrediction);

if (predictionAction === "create") {
if (targetPrediction) throw ("Prediction already exists");
const predictionTitle = generatePredictionTitle(currentMap);
const predictionTitle = generatePredictionTitle(currentMap, predictionType);

let outcomes = [team1.name, team2.name];

if (!(currentMap && currentMap.map.type === "Control")) {
if (predictionType === "map" &&
!(currentMap && currentMap.map.type === "Control") &&
(broadcast.broadcast_settings || []).includes("Allow draw predictions")) {
outcomes.push("Draw");
}

Expand All @@ -109,14 +124,21 @@ module.exports = {
} else if (["resolve"].includes(predictionAction)) {
const lastMap = maps.filter(m => !m.dummy && !m.banner && (m.winner || m.draw)).pop();
const targetPrediction = getTargetPrediction(predictions, [team1, team2]);
if (!targetPrediction) throw ("Prediction does not exist");
console.log(targetPrediction);

if (lastMap.draw) {
const responsePrediction = await api.predictions.resolvePrediction(channel.channel_id, targetPrediction.id, targetPrediction.outcomes.find(o => o.title === "Draw").id);
if (predictionType === "match") {
if (!matchWinner) throw ("Match has not been won yet");
const responsePrediction = await api.predictions.resolvePrediction(channel.channel_id, targetPrediction.id, targetPrediction.outcomes.find(o => o.title === matchWinner.name).id);
console.log(responsePrediction);
} else {
const responsePrediction = await api.predictions.resolvePrediction(channel.channel_id, targetPrediction.id, targetPrediction.outcomes.find(o => o.title === lastMap.winner.name).id);
console.log(responsePrediction);
if (lastMap.draw) {
const responsePrediction = await api.predictions.resolvePrediction(channel.channel_id, targetPrediction.id, targetPrediction.outcomes.find(o => o.title === "Draw").id);
console.log(responsePrediction);
} else {
const responsePrediction = await api.predictions.resolvePrediction(channel.channel_id, targetPrediction.id, targetPrediction.outcomes.find(o => o.title === lastMap.winner.name).id);
console.log(responsePrediction);
}
}

} else if (["cancel"].includes(predictionAction)) {
Expand Down

0 comments on commit 9fedbca

Please sign in to comment.