Pārlūkot izejas kodu

Rewrote most of it, now takes data from opendota API

tags/v1.0
BinHong Lee pirms 7 gadiem
vecāks
revīzija
5bd9f0aa37
4 mainītis faili ar 77 papildinājumiem un 178 dzēšanām
  1. +1
    -5
      package.json
  2. +6
    -9
      speechAssets/IntentSchema.json
  3. +9
    -6
      speechAssets/Utterances.txt
  4. +61
    -158
      src/index.js

+ 1
- 5
package.json Parādīt failu

@@ -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"


+ 6
- 9
speechAssets/IntentSchema.json Parādīt failu

@@ -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"
}
]
}

+ 9
- 6
speechAssets/Utterances.txt Parādīt failu

@@ -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

+ 61
- 158
src/index.js Parādīt failu

@@ -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))
}

Notiek ielāde…
Atcelt
Saglabāt