From 5bd9f0aa376830e6e49d8e0c48133df1c0d9b7f0 Mon Sep 17 00:00:00 2001 From: BinHong Lee Date: Fri, 14 Jul 2017 17:26:35 -0700 Subject: [PATCH] Rewrote most of it, now takes data from opendota API --- package.json | 6 +- speechAssets/IntentSchema.json | 15 +-- speechAssets/Utterances.txt | 15 ++- src/index.js | 219 +++++++++------------------------ 4 files changed, 77 insertions(+), 178 deletions(-) diff --git a/package.json b/package.json index e0997c1..46d5c4e 100644 --- a/package.json +++ b/package.json @@ -4,14 +4,10 @@ "description": "Provides a random hero", "main": "index.js", "dependencies": { - "alexa-sdk": "^1.0.6" + "alexa-sdk": "^1.0.7" }, "author": "binhonglee", "license": "MIT", - "devDependencies": {}, - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, "repository": { "type": "git", "url": "git+ssh://git@github.com/binhonglee/dota2-alexa-skill.git" diff --git a/speechAssets/IntentSchema.json b/speechAssets/IntentSchema.json index f416083..da875f6 100644 --- a/speechAssets/IntentSchema.json +++ b/speechAssets/IntentSchema.json @@ -1,19 +1,16 @@ { "intents": [ { - "intent": "RequestIntent" + "intent": "AnyRequest" }, { - "intent": "RequestSpecificIntent", - "slots": [ - { - "name": "AttackType", - "type": "AMAZON.US_STATE" - } - ] + "intent": "RequestMelee" }, { - "intent": "StopIntent" + "intent": "RequestRanged" + }, + { + "intent": "Stop" } ] } diff --git a/speechAssets/Utterances.txt b/speechAssets/Utterances.txt index 6a9e68d..6e43b9b 100644 --- a/speechAssets/Utterances.txt +++ b/speechAssets/Utterances.txt @@ -1,6 +1,9 @@ -RequestIntent random any hero -RequestIntent random any other hero -RequestSpecificIntent random {AttackType} -RequestSpecificIntent random another {AttackType} -RequestSpecificIntent random {AttackType} again -StopIntent stop +AnyRequest random any hero +AnyRequest random any other hero +RequestMelee random melee +RequestMelee random another melee +RequestMelee random melee again +RequestRanged random ranged +RequestRanged random another ranged +RequestRanged random ranged again +Stop stop diff --git a/src/index.js b/src/index.js index c9f662d..734e617 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,4 @@ -'use strict' - -const Alexa = require('alexa-sdk') +var Alexa = require('alexa-sdk') exports.handler = function (event, context, callback) { var alexa = Alexa.handler(event, context) @@ -8,177 +6,82 @@ exports.handler = function (event, context, callback) { alexa.execute() } -// Handler for all input var handlers = { - // LaunchRequest handler 'LaunchRequest': function () { - // Providing users with example of how this is to be used - const say = 'Welcome to Dota2 random skill! ' - // Emit the message - this.emit(':ask', say) + this.emit('RequestMelee') }, - // RequestIntent handler - 'RequestIntent': function () { - var speechOutput = random(0) - - // Emit the message - this.emit(':ask', speechOutput) + 'AnyRequest': function () { + getHero(0, (name) => { + this.emit(':ask', name) + }) }, - 'RequestSpecificIntent': function () { - var attackType = this.event.request.intent.slots.AttackType - let speechOutput = '' - - if (attackType.value === 'melee') { - speechOutput = random(1) - } else if (attackType.value === 'ranged') { - speechOutput = random(2) - } else { - speechOutput = random(0) - } + 'RequestMelee': function () { + getHero(1, (name) => { + this.emit(':ask', name) + }) + }, - this.emit(':ask', speechOutput) + 'RequestRanged': function () { + getHero(2, (name) => { + this.emit(':ask', name) + }) }, - // StopIntent handler - 'StopIntent': function () { - // Tell user "Goodbye" + 'Stop': function () { this.emit(':tell', 'Enjoy the game!') } } -function random (num) { - if (num === 1) { - return melee[Math.floor(Math.random() * melee.length)] - } else if (num === 2) { - return ranged[Math.floor(Math.random() * ranged.length)] - } else { - var result = Math.floor(Math.random() * (melee.length + ranged.length)) +var https = require('https') - if (result < melee.length) { - return melee[result] +function getHero (type, callback) { + httpsGet((heroes) => { + var position = -1 + var toCompare = '' + + if (type === 1) { + toCompare = 'Melee' + } else if (type === 2) { + toCompare = 'Ranged' } else { - return ranged[result - melee.length] + callback(heroes[random(heroes.length)].localized_name) + return } + + do { + position = random(heroes.length) + } while (heroes[position].attack_type.localeCompare(toCompare) !== 0) + + callback(heroes[position].localized_name) + } + ) +} + +function httpsGet (callback) { + var options = { + host: 'api.opendota.com', + path: '/api/heroes', + method: 'GET' } + + var req = https.request(options, res => { + res.setEncoding('utf8') + var returnData = '' + + res.on('data', chunk => { + returnData = returnData + chunk + }) + + res.on('end', () => { + var pop = JSON.parse(returnData) + callback(pop) + }) + }) + req.end() } -var melee = [ - 'Anti-Mage', - 'Axe', - 'Bloodseeker', - 'Earthshaker', - 'Juggernaut', - 'Phantom Lancer', - 'Pudge', - 'Sand King', - 'Sven', - 'Tiny', - 'Kunkka', - 'Slardar', - 'Tidehunter', - 'Riki', - 'Beastmaster', - 'Faceless Void', - 'Wraith King', - 'Phantom Assassin', - 'Dragon Knight', - 'Clockwerk', - 'Lifestealer', - 'Dark Seer', - 'Omniknight', - 'Night Stalker', - 'Broodmother', - 'Bounty Hunter', - 'Spectre', - 'Doom', - 'Ursa', - 'Spirit Breaker', - 'Alchemist', - 'Lycan', - 'Brewmaster', - 'Chaos Knight', - 'Meepo', - 'Treant Protector', - 'Ogre Magi', - 'Undying', - 'Nyx Assassin', - 'Naga Siren', - 'Slark', - 'Centaur Warrunner', - 'Magnus', - 'Timbersaw', - 'Bristleback', - 'Tusk', - 'Abaddon', - 'Elder Titan', - 'Legion Commander', - 'Ember Spirit', - 'Earth Spirit', - 'Underlord', - 'Terrorblade', - 'Monkey King' -] - -var ranged = [ - 'Bane', - 'Crystal Maiden', - 'Drow Ranger', - 'Mirana', - 'Morphling', - 'Shadow Fiend', - 'Puck', - 'Razor', - 'Storm Spirit', - 'Vengeful Spirit', - 'Windranger', - 'Zeus', - 'Lina', - 'Lion', - 'Shadow Shaman', - 'Witch Doctor', - 'Lich', - 'Enigma', - 'Tinker', - 'Sniper', - 'Necrophos', - 'Warlock', - 'Queen of Pain', - 'Venomancer', - 'Death Prophet', - 'Pugna', - 'Templar Assassin', - 'Viper', - 'Luna', - 'Dazzle', - 'Leshrac', - 'Nature\'s Prophet', - 'Clinkz', - 'Enchantress', - 'Huskar', - 'Weaver', - 'Jakiro', - 'Batrider', - 'Chen', - 'Ancient Apparition', - 'Gyrocopter', - 'Invoker', - 'Silencer', - 'Outworld Devourer', - 'Shadow Demon', - 'Lone Druid', - 'Rubick', - 'Disruptor', - 'Keeper of the Light', - 'Io', - 'Visage', - 'Medusa', - 'Troll Warlord', - 'Skywrath Mage', - 'Techies', - 'Phoenix', - 'Oracle', - 'Winter Wyvern', - 'Arc Warden' -] +function random (size) { + return Math.floor(Math.random() * (size)) +}