Skip to content

Instantly share code, notes, and snippets.

@kleneway
Created July 9, 2021 18:50
Show Gist options
  • Save kleneway/a8d300b829c76665d9d1676dee45d276 to your computer and use it in GitHub Desktop.
Save kleneway/a8d300b829c76665d9d1676dee45d276 to your computer and use it in GitHub Desktop.
/*jshint esversion: 6 */
var axios = require("axios");
// FOR ASSISTS
const NAMES = [
"Devin Booker",
"Khris Middleton",
"Giannis Antetokounmpo",
"Jrue Holiday",
"Chris Paul",
"Jae Crowder",
"Deandre Ayton",
"Cameron Payne",
"Cameron Johnson",
"Mikal Bridges"
];
export default (_req, res) => {
const { username } = _req.query;
if (!username || !username.length) {
return res.status(500).json({ error: "Please enter a username" });
}
var data = JSON.stringify({
query: `query getUserProfileByUsername ($input: getUserProfileByUsernameInput!) {
getUserProfileByUsername (input: $input) {
publicInfo {
dapperID
}
}
}`,
variables: { input: { username } }
});
var config = {
method: "post",
url: "https://public-api.nbatopshot.com/graphql",
headers: {
"Content-Type": "application/json"
},
data
};
axios(config)
.then(function (response) {
const userID =
response.data.data.getUserProfileByUsername.publicInfo.dapperID;
if (!userID) {
return res.status(500).json({ error: "UserID not found." });
}
var axios = require("axios");
var data1 = JSON.stringify({
query: `query getUserShowcases ($input: GetUserShowcasesInput!) {
getUserShowcases (input: $input) {
showcases {
id
name
userID
moments {
set {
id
flowName
}
play {
id
stats {
primaryPosition
playCategory
playerName
}
}
}
}
}
}`,
variables: { input: { userID } }
});
var config1 = {
method: "post",
url: "https://public-api.nbatopshot.com/graphql",
headers: {
"Content-Type": "application/json"
},
data: data1
};
axios(config1)
.then(function (response) {
const showcases = response.data.data.getUserShowcases.showcases;
const checklist = {
foundShowcase: false,
isTitleCorrect: false,
numShowcases: 0,
correctMoments: [],
incorrectMoments: [],
isSuccess: false,
overlappingMoments: false,
missingPlayers: [...NAMES],
showcaseUrl: ""
};
if (!showcases) {
res.status(200).json(checklist);
}
for (const showcase of showcases) {
if (
showcase.name &&
showcase.name.toLowerCase().trim() === "finals quest game 2"
) {
checklist.foundShowcase = true;
let totalValidMoments = 0;
checklist.isTitleCorrect =
showcase.name === "Finals Quest Game 2";
checklist.numShowcases += 1;
// now check for each type of moment
for (const moment of showcase.moments) {
let isValid = false;
for (const name of NAMES) {
if (name === moment.play.stats.playerName) {
let isDup = false;
for (const existingMoments of checklist.correctMoments) {
if (existingMoments.play.stats.playerName === name) {
isDup = true;
checklist.overlappingMoments = true;
}
}
if (!isDup) {
checklist.correctMoments.push(moment);
checklist.missingPlayers =
checklist.missingPlayers.filter((p) => p !== name);
isValid = true;
}
}
}
if (!isValid) {
checklist.incorrectMoments.push(moment);
} else {
totalValidMoments++;
}
}
if (
totalValidMoments == 7 &&
checklist.incorrectMoments.length == 0 &&
checklist.numShowcases == 1 &&
checklist.isTitleCorrect
) {
checklist.isSuccess = true;
checklist.showcaseUrl =
"https://nbatopshot.com/showcases/" + showcase.id;
}
}
}
res.status(200).json(checklist);
})
.catch(function (error) {
console.log(error);
res.status(500).json({ error });
});
})
.catch(function (error) {
console.log(error);
res.status(500).json({ error });
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment