Last active
January 2, 2022 04:04
-
-
Save misterhay/3537dde6aeee325975459b246fab52b2 to your computer and use it in GitHub Desktop.
Javascript for creating a Halo Infinite statistics endpoint on autocode.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Return Halo Infinite stats for a gamertag as a json string | |
// instructions for creating your own endpoint at https://docs.autocode.com/getting-started/creating-your-first-project/creating-an-endpoint/ | |
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN}); | |
module.exports = async (context) => { | |
const gamertag = context.params.gamertag; | |
// if no gamertag is passed return an error | |
if (!gamertag) { | |
return { | |
headers: {'Content-Type': 'text/html'}, | |
statusCode: 400, | |
body: 'gamertag required', | |
}; | |
} | |
var stats = context.params.stats; | |
// if no stats parameter supplied, assume multiplayer service record | |
if (!stats) { | |
var stats = 'service-record.multiplayer'; | |
} | |
const stats_array = [ | |
'appearance', | |
'csrs', | |
'matches.list', | |
'matches.retrieve', | |
'service-record.multiplayer', | |
'service-record.campaign', | |
]; | |
// if it's not an allowed stats parameter | |
if (!stats_array.includes(stats)) { | |
return { | |
headers: {'Content-Type': 'text/html'}, | |
statusCode: 400, | |
body: 'stats must be one of: ' + stats_array, | |
}; | |
} | |
try { | |
switch (stats) { | |
case 'appearance': | |
var result = await lib.halo.infinite['@0.3.3'].appearance({ | |
gamertag: gamertag, | |
}); | |
break; | |
case 'csrs': | |
if (context.params.season) { | |
var result = await lib.halo.infinite['@0.3.3'].stats.csrs({ | |
gamertag: gamertag, | |
season: context.params.season, | |
}); | |
} else { | |
var result = await lib.halo.infinite['@0.3.3'].stats.csrs({ | |
gamertag: gamertag, | |
}); | |
} | |
break; | |
case 'matches.list': | |
if (context.params.mode) { | |
var result = await lib.halo.infinite['@0.3.3'].stats.matches.list({ | |
gamertag: gamertag, | |
mode: context.params.mode, | |
}); | |
} else { | |
var result = await lib.halo.infinite['@0.3.3'].stats.matches.list({ | |
gamertag: gamertag, | |
}); | |
} | |
break; | |
case 'matches.retrieve': | |
if (context.params.matchid) { | |
var result = await lib.halo.infinite['@0.3.3'].stats.matches.retrieve( | |
context.params.id | |
); | |
} else { | |
var result = {error: 'specify id of match'}; | |
} | |
break; | |
case 'service-record.campaign': | |
var result = await lib.halo.infinite['@0.3.3'].stats[ | |
'service-record' | |
].campaign({gamertag: gamertag}); | |
break; | |
case 'service-record.multiplayer': | |
if (context.params.filter) { | |
var result = await lib.halo.infinite['@0.3.3'].stats[ | |
'service-record' | |
].multiplayer({gamertag: gamertag, filter: context.params.filter}); | |
} else { | |
var result = await lib.halo.infinite['@0.3.3'].stats[ | |
'service-record' | |
].multiplayer({gamertag: gamertag}); | |
} | |
break; | |
} | |
return { | |
headers: {'Content-Type': 'application/json'}, | |
statusCode: 200, | |
body: JSON.stringify(result), | |
}; | |
} catch (e) { | |
return { | |
headers: {'Content-Type': 'text/html'}, | |
statusCode: 500, | |
body: `Unable to access Halo statistics: ${e.message}`, | |
}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment