Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Last active July 24, 2017 14:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save primaryobjects/5dcec2ab4b508f69b3c7bae5240fb2be to your computer and use it in GitHub Desktop.
Save primaryobjects/5dcec2ab4b508f69b3c7bae5240fb2be to your computer and use it in GitHub Desktop.
Dragon Hunt: An example game for Amazon Alexa running on chatskills.
var chatskills = require('chatskills');
var readlineSync = require('readline-sync');
// Define an alexa-app
var app = chatskills.app('dragonhunt');
var dragonTypes = [ 'fire', 'ice', 'undead', 'skeleton', 'golden' ];
app.launch(function(req,res) {
// Generate a random dragon.
var randomType = Math.floor((Math.random() * 5)); // 0 - 5
var hp = Math.floor((Math.random() * 11) + 5); // 5 - 15
// Store the dragon in session.
var dragon = { type: dragonTypes[randomType], hp: hp };
res.session('dragon', dragon);
// Welcome the user.
res.say('A large ' + dragon.type + ' dragon approaches! What do you want to do?');
res.shouldEndSession(false);
});
app.intent('look', {
'slots': {},
'utterances': ['{look|search|find} {a|} {weapon|sword|}']
},function(req,res) {
// The user picked-up a weapon! Set a session variable.
res.session('hasWeapon', true);
res.say('You search the cave and find a lance on the ground!');
res.shouldEndSession(false);
}
);
app.intent('attack', {
'slots': { 'DragonType': 'DRAGONTYPE' },
'utterances': ['{attack|fight|hit|use} {lance|weapon} on {-|DragonType} dragon']
}, function(req,res) {
if (!req.session('hasWeapon')) {
// No weapon yet.
res.say("You don't have a weapon! Try looking around.");
res.shouldEndSession(false);
}
else {
// The player has a weapon, it's time to attack.
var dragon = req.session('dragon');
if (dragon.type.toLowerCase() != req.slot('DragonType').toLowerCase()) {
// The player didn't speak the correct dragon type!
res.say("I don't see a " + req.slot('DragonType') + ' dragon anywhere! Just a ' + dragon.type + ' one.');
res.shouldEndSession(false);
}
else {
// The correct dragon type was spoken, let's attack!
dragon.hp -= 2;
res.session('dragon', dragon);
if (dragon.hp <= 0) {
res.say('You have slain the mighty ' + dragon.type + ' dragon! Congratulations!');
res.shouldEndSession(true);
}
else {
res.say("You attack the " + dragon.type + " dragon with your lance! It has " + dragon.hp + ' HP left!');
res.shouldEndSession(false);
}
}
}
}
);
chatskills.launch(app);
// Console client.
var text = ' ';
while (text.length > 0 && text != 'quit') {
text = readlineSync.question('> ');
// Respond to input.
chatskills.respond(text, function(response) {
console.log(response);
});
}
{
"name": "dragonhunt",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"chatskills": "*",
"readline-sync": "*"
}
}
A large golden dragon approaches! What do you want to do?
> use lance on golden dragon
You don't have a weapon! Try looking around.
> find a weapon
You search the cave and find a lance on the ground!
> use lance on golden dragon
You attack the golden dragon with your lance! It has 6 HP left!
> attack lance on red dragon!
I don't see a red dragon anywhere! Just a golden one.
> attack lance on golden dragon.
You attack the golden dragon with your lance! It has 4 HP left!
> hit lance on golden dragon.
You attack the golden dragon with your lance! It has 2 HP left!
> use lance on golden dragon.
You have slain the mighty golden dragon! Congratulations!
@rmxsantiago
Copy link

Hello Kory,
I am trying to run this example, but the 'look' intent is always been called. I mean the 'attack' never is called, so it looks like something only the first declared intent is always called. Could you check if this code still working for you?

@primaryobjects
Copy link
Author

@rmxsantiago Fixed in chatskills 0.0.19.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment