Skip to content

Commit ba51ed0

Browse files
authored
New SlackerBot parser was added, called Fruit Riddle (#453)
* Create FruitRiddle.js * Update FruitRiddle.js * Update FruitRiddle.js * Update FruitRiddle.js
1 parent 9b05f4b commit ba51ed0

File tree

1 file changed

+195
-0
lines changed

1 file changed

+195
-0
lines changed

Parsers/FruitRiddle.js

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/*
2+
activation_example:!fruit riddle start
3+
regex:^!fruit riddle.*$
4+
flags:gmi
5+
*/
6+
7+
8+
/**
9+
* Main variable: current, which is a record from the x_snc_slackerbot_chat table
10+
* Important attributes:
11+
* - Text (text)
12+
* - User (user)
13+
* - Channel (channel)
14+
*/
15+
16+
var FruitRiddle = Class.create();
17+
FruitRiddle.prototype = {
18+
19+
initialize: function(currentChatContext) {
20+
21+
// Available commands
22+
this.CMD_START = "start";
23+
this.CMD_EXIT = "exit";
24+
this.CMD_SOLUTION = "solution";
25+
26+
this.user = currentChatContext.getDisplayValue("user");
27+
this.channel = currentChatContext.getValue("channel");
28+
this.text = currentChatContext.getValue("text");
29+
this.threadTs = currentChatContext.getValue("thread_ts");
30+
this.isThread = !gs.nil(this.threadTs); // The current message is in thread or not
31+
32+
this.fruits = ["apple", "banana", "orange", "mango", "strawberry", "blueberry", "raspberry", "pineapple", "watermelon", "papaya", "kiwi", "peach", "pear", "grape", "cherry", "pomegranate", "lemon", "lime", "coconut", "avocado", "fig", "plum", "nectarine", "apricot", "dragonfruit", "grapefruit", "persimmon", "guava", "jackfruit", "lychee"];
33+
},
34+
35+
/**
36+
* The main logic is handled by this function
37+
*/
38+
parse: function() {
39+
40+
// Need to check the text at first
41+
// Only the following commands are allowed
42+
var textRegex = /^(!fruit riddle|!fruit riddle start|!fruit riddle exit|!fruit riddle solution [a-zA-Z]*|!fruit riddle [a-zA-Z]{4})$/g;
43+
44+
if (this.text.match(textRegex)) {
45+
46+
if (this.text == "!fruit riddle" && !this.isThread) {
47+
return {
48+
thread: this.isThread,
49+
message: `This is a simple game, where I think of a fruit, and you have to guess it. You can start the game with the command:
50+
51+
!fruit riddle start
52+
53+
To submit a guess, use the following command:
54+
55+
!fruit riddle abcd
56+
57+
If you know the answer, type:
58+
59+
!fruit riddle solution fruit
60+
61+
If you want to give up, use the message:
62+
63+
!fruit riddle exit
64+
65+
Have fun :smile:`
66+
};
67+
}
68+
else if (this.text == "!fruit riddle" && this.isThread) {
69+
return {
70+
thread: this.isThread,
71+
message: "Please use one of the input commands, such as solve, exit or guess letter"
72+
};
73+
}
74+
75+
// Need to get the 3rd word from the command
76+
var command = this.text.split(" ")[2];
77+
78+
if (this.isThread) {
79+
// The message is in a thread
80+
if (command === this.CMD_START) {
81+
return {
82+
thread: true,
83+
message: "The fruit riddle start command cannot be used in thread."
84+
};
85+
}
86+
else if (command === this.CMD_EXIT) {
87+
return {
88+
thread: true,
89+
message: `😮
90+
${this.user}!! So you give up...I wouldn't have thought that about you.`
91+
};
92+
}
93+
else if (command === this.CMD_SOLUTION) {
94+
95+
var solution = this.text.split(" ")[3];
96+
var selectedFruit = this._getRandomFruit();
97+
98+
if (solution === selectedFruit) {
99+
return {
100+
thread: true,
101+
message: "Exactly! Congratulations! 🥳"
102+
};
103+
}
104+
105+
return {
106+
thread: true,
107+
message: "Well, you missed it. Try again."
108+
};
109+
}
110+
else {
111+
// The letters
112+
113+
var letters = command.split('');
114+
var result = this._revealLetters(this._getRandomFruit(), letters);
115+
116+
return {
117+
thread: true,
118+
message: `Here we go: ${result}`
119+
};
120+
}
121+
}
122+
else {
123+
// The messasge is not part of a thread
124+
if (command === this.CMD_START) {
125+
return {
126+
thread: true,
127+
message: `Hey *${this.user}!* Do you want to play?
128+
129+
I think of a fruit, and you have to guess what it is. You can provide four letters at a time in the following format:
130+
!fruit riddle abcd
131+
132+
If you know the answer, use this command:
133+
!fruit riddle solution orange
134+
135+
If you give up, type:
136+
!fruit riddle exit
137+
138+
Let's start! 💪`
139+
};
140+
}
141+
else if (command === this.CMD_EXIT || command === this.CMD_SOLUTION) {
142+
return {
143+
thread: false,
144+
message: `The ${command} command cannot be used outside of the thread.`
145+
};
146+
}
147+
else {
148+
// The letters
149+
return {
150+
thread: false,
151+
message: "This game cannot be plaed outside of the thread."
152+
};
153+
}
154+
}
155+
}
156+
157+
return {
158+
thread: this.isThread,
159+
message: "I can't interpret this command."
160+
};
161+
},
162+
163+
/**
164+
* This function is responsible for get a random fruit from the array. The random number is based on the thread time.
165+
* @return - one element from the fruit array
166+
*/
167+
_getRandomFruit: function() {
168+
var _threadTs = (this.threadTs + "").split(".")[0];
169+
var randomNumber = _threadTs % 30;
170+
return this.fruits[randomNumber];
171+
},
172+
173+
/**
174+
* This function is responsible to check if the given letters are part of the fruit or not
175+
* @param {string} fruit - The current fruit
176+
* @param {string} letters - The four letters which were added by the uyer
177+
* @return - If there are letters witch match it will be given back in correct position. Other ones will be masked with the '_' character.
178+
*/
179+
_revealLetters: function(fruit, letters) {
180+
return fruit.split('').map(char => {
181+
// Check if the character is in the letters array
182+
return letters.includes(char) ? char : '_';
183+
}).join('');
184+
},
185+
186+
type: 'FruitRiddle'
187+
};
188+
189+
var gameResultObj = new x_snc_slackerbot.FruitRiddle(current).parse();
190+
try {
191+
new x_snc_slackerbot.Slacker().send_chat(current, gameResultObj.message, gameResultObj.thread);
192+
}
193+
catch( e ) {
194+
gs.error( "An error occured when SlackerBot tried to send a response back to Slack.\nError: " + e.name + ": " + e.message );
195+
}

0 commit comments

Comments
 (0)