Skip to content

Instantly share code, notes, and snippets.

@sahat
Last active January 11, 2017 06:07
Show Gist options
  • Save sahat/6cdaeefc37fd20b10c7bef8658635d51 to your computer and use it in GitHub Desktop.
Save sahat/6cdaeefc37fd20b10c7bef8658635d51 to your computer and use it in GitHub Desktop.
/* eslint-disable func-names */
/* eslint quote-props: ["error", "consistent"]*/
/**
* This sample demonstrates a simple skill built with the Amazon Alexa Skills
* nodejs skill development kit.
* This sample supports multiple lauguages. (en-US, en-GB, de-DE).
* The Intent Schema, Custom Slots and Sample Utterances for this skill, as well
* as testing instructions are located at https://github.com/alexa/skill-sample-nodejs-fact
**/
'use strict';
const https = require('https');
const qs = require('querystring');
const fs = require('fs');
const _ = require('lodash');
// const Alexa = require('alexa-sdk');
const { achievements } = require('./achievements.json');
const APP_ID = 'amzn1.ask.skill.0bdad118-58b2-4106-ac8e-09503eef2d68';
const languageStrings = {
'en-GB': {
translation: {
FACTS: [
'A year on Mercury is just 88 days long.',
'Despite 111 being farther from the Sun, Venus experiences higher temperatures than Mercury.',
'Venus rotates anti-clockwise, possibly because of a collision in the past with an asteroid.',
'On Mars, the Sun appears about half the size as it does on Earth.',
'Earth is the only planet not named after a god.',
'Jupiter has the shortest day of all the planets.',
'The Milky Way galaxy will collide with the Andromeda Galaxy in about 5 billion years.',
'The Sun contains 99.86% of the mass in the Solar System.',
'The Sun is an almost perfect sphere.',
'A total solar eclipse can happen once every 1 to 2 years. This makes them a rare event.',
'Saturn radiates two and a half times more energy into space than it receives from the sun.',
'The temperature inside the Sun can reach 15 million degrees Celsius.',
'The Moon is moving approximately 3.8 cm away from our planet every year.',
],
SKILL_NAME: 'British Space Facts',
GET_FACT_MESSAGE: "Here's your fact: ",
HELP_MESSAGE: 'You can say tell me a space fact, or, you can say exit... What can I help you with?',
HELP_REPROMPT: 'What can I help you with?',
STOP_MESSAGE: 'Goodbye!',
},
},
'en-US': {
translation: {
FACTS: [
'A year on Mercury is just 88 days long.',
'Despite being farther from the Sun, Venus experiences higher temperatures than Mercury.',
'Venus rotates counter-clockwise, possibly because of a collision in the past with an asteroid.',
'On Mars, the Sun appears about half the size as it does on Earth.',
'Earth is the only planet not named after a god.',
'Jupiter has the shortest day of all the planets.',
'The Milky Way galaxy will collide with the Andromeda Galaxy in about 5 billion years.',
'The Sun contains 99.86% of the mass in the Solar System.',
'The Sun is an almost perfect sphere.',
'A total solar eclipse can happen once every 1 to 2 years. This makes them a rare event.',
'Saturn radiates two and a half times more energy into space than it receives from the sun.',
'The temperature inside the Sun can reach 15 million degrees Celsius.',
'The Moon is moving approximately 3.8 cm away from our planet every year.',
],
SKILL_NAME: 'American Space Facts',
GET_FACT_MESSAGE: "Here's your fact: ",
HELP_MESSAGE: 'You can say tell me a space fact, or, you can say exit... What can I help you with?',
HELP_REPROMPT: 'What can I help you with?',
STOP_MESSAGE: 'Goodbye!',
},
},
'de-DE': {
translation: {
FACTS: [
'Ein Jahr dauert auf dem Merkur nur 88 Tage.',
'Die Venus ist zwar weiter von der Sonne entfernt, hat aber höhere Temperaturen als Merkur.',
'Venus dreht sich entgegen dem Uhrzeigersinn, möglicherweise aufgrund eines früheren Zusammenstoßes mit einem Asteroiden.',
'Auf dem Mars erscheint die Sonne nur halb so groß wie auf der Erde.',
'Die Erde ist der einzige Planet, der nicht nach einem Gott benannt ist.',
'Jupiter hat den kürzesten Tag aller Planeten.',
'Die Milchstraßengalaxis wird in etwa 5 Milliarden Jahren mit der Andromeda-Galaxis zusammenstoßen.',
'Die Sonne macht rund 99,86 % der Masse im Sonnensystem aus.',
'Die Sonne ist eine fast perfekte Kugel.',
'Eine Sonnenfinsternis kann alle ein bis zwei Jahre eintreten. Sie ist daher ein seltenes Ereignis.',
'Der Saturn strahlt zweieinhalb mal mehr Energie in den Weltraum aus als er von der Sonne erhält.',
'Die Temperatur in der Sonne kann 15 Millionen Grad Celsius erreichen.',
'Der Mond entfernt sich von unserem Planeten etwa 3,8 cm pro Jahr.',
],
SKILL_NAME: 'Weltraumwissen auf Deutsch',
GET_FACT_MESSAGE: 'Hier sind deine Fakten: ',
HELP_MESSAGE: 'Du kannst sagen, „Nenne mir einen Fakt über den Weltraum“, oder du kannst „Beenden“ sagen... Wie kann ich dir helfen?',
HELP_REPROMPT: 'Wie kann ich dir helfen?',
STOP_MESSAGE: 'Auf Wiedersehen!',
},
},
};
const handlers = {
'LaunchRequest': function () {
this.emit('GetFact');
},
'GetNewFactIntent': function () {
this.emit('GetFact');
},
'GetFact': function () {
// Get a random space fact from the space facts list
// Use this.t() to get corresponding language data
const factArr = this.t('FACTS');
const factIndex = Math.floor(Math.random() * factArr.length);
const randomFact = factArr[factIndex];
// Create speech output
const speechOutput = this.t('GET_FACT_MESSAGE') + randomFact;
this.emit(':tellWithCard', speechOutput, this.t('SKILL_NAME'), randomFact);
},
'AMAZON.HelpIntent': function () {
const speechOutput = this.t('HELP_MESSAGE');
const reprompt = this.t('HELP_MESSAGE');
this.emit(':ask', speechOutput, reprompt);
},
'AMAZON.CancelIntent': function () {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
'AMAZON.StopIntent': function () {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
'SessionEndedRequest': function () {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
};
const API_KEY = 'x9vg6h7mrutyr7yt6xphez474ufvt62j';
const realm = 'emerald-dream';
const character = 'Clawenforcer';
exports.handler = (event, context) => {
//const alexa = Alexa.handler(event, context);
//alexa.APP_ID = APP_ID;
// To enable string internationalization (i18n) features, set a resources object.
//alexa.resources = languageStrings;
//alexa.registerHandlers(handlers);
//alexa.execute();
};
const achievementsData = {};
const criteriaData = {};
achievements.forEach(achievementCategory => {
achievementCategory.achievements.forEach(achievement => {
achievementsData[achievement.id] = achievement;
achievement.criteria.forEach(criterion => {
criteriaData[criterion.id] = criterion;
criteriaData[criterion.id].achievementId = achievement.id;
});
});
});
getCharacterAchievements().then(data => {
const { criteria, criteriaQuantity, criteriaTimestamp, achievementsCompleted } = data.achievements;
let achievemenstList = [];
const todo = [];
console.log(criteria.length);
for (let i = 0; i < criteria.length; i++) {
// for (let i = 0; i < 1000; i++) {
const inProgressCriterionId = criteria[i];
const inProgressCriterionQuanity = criteriaQuantity[i];
const criterion = criteriaData[inProgressCriterionId.toString()];
if (criterion) {
if (inProgressCriterionQuanity < criterion.max && criterion.max !== 1) { // skip 0 of 1 achievements
if (achievementsCompleted.includes(criterion.achievementId)) {
} else {
if (!achievementsData[criterion.achievementId].description.includes('First') &&
!achievementsData[criterion.achievementId].description.includes('on the realm')) {
// console.log(achievementsData[criterion.achievementId].description);
// console.log(inProgressCriterionQuanity + ' of ' + criterion.max);
// console.log('----------------------------------');
// console.log();
todo.push({
achDescription: achievementsData[criterion.achievementId].description,
criDescription: criterion.description,
max: criterion.max,
quantity: inProgressCriterionQuanity
});
}
}
// if (achievementsCompleted.includes(criterion.achievementId)) {
// achievemenstList.push(criterion.achievementId);
// }
}
}
}
const sorted = _.sortBy(todo, (item) => {
const numerator = Math.abs(item.max - item.quantity);
const denominator = (item.max + item.quantity) / 2;
return (numerator / denominator) * 100;
});
console.log(sorted);
// achievemenstList = _.uniq(achievemenstList);
// achievemenstList.forEach(ach => {
// })
});
function getCharacterProfile() {
return new Promise((resolve, reject) => {
https.get(`https://us.api.battle.net/wow/character/${realm}/${character}?locale=en_US&apikey=${API_KEY}`, (res) => {
let rawData = '';
res.on('data', (chunk) => rawData += chunk);
res.on('end', () => {
try {
resolve(JSON.parse(rawData));
} catch (e) {
reject(e);
}
});
}).on('error', (e) => reject(e));
})
}
function getCharacterAchievements() {
return new Promise((resolve, reject) => {
https.get(`https://us.api.battle.net/wow/character/${realm}/${character}?fields=achievements&locale=en_US&apikey=${API_KEY}`, (res) => {
let rawData = '';
res.on('data', (chunk) => rawData += chunk);
res.on('end', () => {
try {
resolve(JSON.parse(rawData));
} catch (e) {
reject(e);
}
});
}).on('error', (e) => reject(e));
})
}
function getAchievements() {
return new Promise((resolve, reject) => {
https.get(`https://us.api.battle.net/wow/data/character/achievements?locale=en_US&apikey=${API_KEY}`, (res) => {
let rawData = '';
res.on('data', (chunk) => rawData += chunk);
res.on('end', () => {
try {
const parsedData = JSON.parse(rawData);
fs.writeFileSync(__dirname + '/achievements.json', rawData);
console.log('Saved!')
} catch (e) {
reject(e);
}
});
}).on('error', (e) => reject(e));
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment