2013-12-24 01:18:43 +01:00
|
|
|
/*************************************************************************
|
2013-12-28 23:49:13 +01:00
|
|
|
## NumberGuess mini-game:
|
2013-12-24 01:18:43 +01:00
|
|
|
|
2013-12-28 23:49:13 +01:00
|
|
|
### Description
|
2013-12-26 16:38:24 +01:00
|
|
|
This is a very simple number guessing game. Minecraft will ask you to
|
|
|
|
guess a number between 1 and 10 and you will tell you if you're too
|
|
|
|
hight or too low when you guess wrong. The purpose of this mini-game
|
|
|
|
code is to demonstrate use of Bukkit's Conversation API.
|
|
|
|
|
2013-12-28 23:49:13 +01:00
|
|
|
### Example
|
2013-10-13 22:08:04 +02:00
|
|
|
|
2013-12-30 22:33:12 +01:00
|
|
|
/js Game_NumberGuess.start(self)
|
2013-10-13 22:08:04 +02:00
|
|
|
|
2013-12-26 16:38:24 +01:00
|
|
|
Once the game begins, guess a number by typing the `/` character
|
|
|
|
followed by a number between 1 and 10.
|
|
|
|
|
2013-12-24 01:18:43 +01:00
|
|
|
***/
|
|
|
|
exports.Game_NumberGuess = {
|
2013-12-30 22:33:12 +01:00
|
|
|
start: function(sender) {
|
2013-12-24 01:18:43 +01:00
|
|
|
importPackage(org.bukkit.conversations);
|
|
|
|
|
2013-10-13 22:08:04 +02:00
|
|
|
var number = Math.ceil(Math.random() * 10);
|
2013-12-24 01:18:43 +01:00
|
|
|
|
2013-10-13 22:08:04 +02:00
|
|
|
var prompt = new Prompt()
|
|
|
|
{
|
|
|
|
getPromptText: function(ctx){
|
|
|
|
var hint = "";
|
|
|
|
var h = ctx.getSessionData("hint");
|
|
|
|
if (h){
|
|
|
|
hint = h;
|
|
|
|
}
|
|
|
|
return hint + "Think of a number between 1 and 10";
|
|
|
|
},
|
|
|
|
acceptInput: function(ctx, s)
|
|
|
|
{
|
|
|
|
s = s.replace(/^[^0-9]+/,""); // strip leading non-numeric characters (e.g. '/' )
|
|
|
|
s = parseInt(s);
|
|
|
|
if (s == number){
|
|
|
|
setTimeout(function(){
|
|
|
|
ctx.forWhom.sendRawMessage("You guessed Correct!");
|
|
|
|
},100);
|
|
|
|
return null;
|
|
|
|
}else{
|
|
|
|
if (s < number)
|
|
|
|
ctx.setSessionData("hint","too low\n");
|
|
|
|
if (s > number)
|
|
|
|
ctx.setSessionData("hint","too high\n");
|
|
|
|
return prompt;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
blocksForInput: function(ctx){ return true; }
|
|
|
|
};
|
|
|
|
var cf = new ConversationFactory(__plugin);
|
|
|
|
var conv = cf.withModality(true)
|
|
|
|
.withFirstPrompt(prompt)
|
|
|
|
.withPrefix(new ConversationPrefix(){ getPrefix: function(ctx){ return "[1-10] ";} })
|
2013-12-30 22:33:12 +01:00
|
|
|
.buildConversation(sender);
|
2013-10-13 22:08:04 +02:00
|
|
|
conv.begin();
|
2013-12-24 01:18:43 +01:00
|
|
|
}
|
|
|
|
};
|