From 37d24bb6569ca8e9b266c2b397fcb6f3978fc3ec Mon Sep 17 00:00:00 2001 From: Tobias Messner Date: Mon, 28 Nov 2022 14:23:17 +0100 Subject: [PATCH 1/2] Add match-wide preds and make them default --- server/src/actions/manage-prediction.js | 34 +++++++++++++++++++------ 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/server/src/actions/manage-prediction.js b/server/src/actions/manage-prediction.js index 8bed7180..7115c277 100644 --- a/server/src/actions/manage-prediction.js +++ b/server/src/actions/manage-prediction.js @@ -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 { @@ -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"], @@ -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 => { @@ -66,26 +77,28 @@ module.exports = { return map; })); - if (maps.length === 0) throw ("No maps associated with match"); + + const matchWinner = getMatchWinner(match, team1, team2); + + if (maps.length === 0 && predictionType === "map") 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 (!currentMap && predictionType === "map") 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")) { outcomes.push("Draw"); } @@ -109,9 +122,14 @@ 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) { + 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 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 { From 10f805c7fae5ccdd054b1e220bacd0e889764951 Mon Sep 17 00:00:00 2001 From: Solomon Cammack Date: Wed, 30 Nov 2022 22:16:28 +0000 Subject: [PATCH 2/2] Flip some statements and remove draws --- server/src/actions/manage-prediction.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/server/src/actions/manage-prediction.js b/server/src/actions/manage-prediction.js index 7115c277..ad4c5e1e 100644 --- a/server/src/actions/manage-prediction.js +++ b/server/src/actions/manage-prediction.js @@ -61,7 +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 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 => { @@ -80,14 +80,14 @@ module.exports = { const matchWinner = getMatchWinner(match, team1, team2); - if (maps.length === 0 && predictionType === "map") throw ("No maps associated with match"); + 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 && predictionType === "map") 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); @@ -98,7 +98,9 @@ module.exports = { let outcomes = [team1.name, team2.name]; - if (predictionType === "map" && !(currentMap && currentMap.map.type === "Control")) { + if (predictionType === "map" && + !(currentMap && currentMap.map.type === "Control") && + (broadcast.broadcast_settings || []).includes("Allow draw predictions")) { outcomes.push("Draw"); } @@ -129,12 +131,14 @@ module.exports = { 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 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); + 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)) {