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