Mercurial > irccd
comparison plugins/hangman/hangman.js @ 154:f554d1314208
Plugin hangman: fix formats and invalid number of tries
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 22 May 2016 23:18:26 +0200 |
parents | 2ce88e3a4759 |
children | 6635b9187d71 |
comparison
equal
deleted
inserted
replaced
153:70da13c626de | 154:f554d1314208 |
---|---|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 */ | 17 */ |
18 | 18 |
19 // Plugin information. | |
20 info = { | |
21 author: "David Demelier <markand@malikania.fr>", | |
22 license: "ISC", | |
23 summary: "A hangman game for IRC", | |
24 version: "@IRCCD_VERSION@" | |
25 }; | |
26 | |
19 // Modules. | 27 // Modules. |
20 var Logger = Irccd.Logger; | 28 var Logger = Irccd.Logger; |
21 var File = Irccd.File; | 29 var File = Irccd.File; |
22 var Plugin = Irccd.Plugin; | 30 var Plugin = Irccd.Plugin; |
23 var Server = Irccd.Server; | 31 var Server = Irccd.Server; |
24 var Unicode = Irccd.Unicode | 32 var Unicode = Irccd.Unicode |
25 var Util = Irccd.Util; | 33 var Util = Irccd.Util; |
26 | |
27 // Plugin information. | |
28 info = { | |
29 author: "David Demelier <markand@malikania.fr>", | |
30 license: "ISC", | |
31 summary: "A hangman game for IRC", | |
32 version: "@IRCCD_VERSION@" | |
33 }; | |
34 | 34 |
35 // Default options. | 35 // Default options. |
36 Plugin.config["collaborative"] = "true"; | 36 Plugin.config["collaborative"] = "true"; |
37 | 37 |
38 // Formats. | 38 // Formats. |
123 */ | 123 */ |
124 Hangman.loadWords = function () | 124 Hangman.loadWords = function () |
125 { | 125 { |
126 var path; | 126 var path; |
127 | 127 |
128 /* User specified file? */ | 128 // User specified file? |
129 if (Plugin.config["file"]) | 129 if (Plugin.config["file"]) |
130 path = Plugin.config["file"]; | 130 path = Plugin.config["file"]; |
131 else | 131 else |
132 path = Plugin.configPath + "words.conf"; | 132 path = Plugin.configPath + "words.conf"; |
133 | 133 |
153 /** | 153 /** |
154 * Load all formats. | 154 * Load all formats. |
155 */ | 155 */ |
156 Hangman.loadFormats = function () | 156 Hangman.loadFormats = function () |
157 { | 157 { |
158 // --- DEPRECATED ------------------------------------------ | 158 // --- DEPRECATED ------------------------------------------- |
159 // | 159 // |
160 // This code will be removed. | 160 // This code will be removed. |
161 // | 161 // |
162 // Since: 2.1.0 | 162 // Since: 2.1.0 |
163 // Until: 3.0.0 | 163 // Until: 3.0.0 |
180 /** | 180 /** |
181 * Select the next word for the game. | 181 * Select the next word for the game. |
182 */ | 182 */ |
183 Hangman.prototype.select = function () | 183 Hangman.prototype.select = function () |
184 { | 184 { |
185 /* Reload the words if empty */ | 185 // Reload the words if empty. |
186 if (!this.words || this.words.length === 0) | 186 if (!this.words || this.words.length === 0) |
187 this.words = Hangman.words.slice(0); | 187 this.words = Hangman.words.slice(0); |
188 | 188 |
189 var i = Math.floor(Math.random() * this.words.length); | 189 var i = Math.floor(Math.random() * this.words.length); |
190 | 190 |
191 this.word = this.words[i]; | 191 this.word = this.words[i]; |
192 this.words.splice(i, 1); | 192 this.words.splice(i, 1); |
193 | 193 |
194 /* Fill table */ | 194 // Fill table. |
195 this.table = {}; | 195 this.table = {}; |
196 | 196 |
197 for (var j = 0; j < this.word.length; ++j) | 197 for (var j = 0; j < this.word.length; ++j) |
198 this.table[this.word.charCodeAt(j)] = false; | 198 this.table[this.word.charCodeAt(j)] = false; |
199 } | 199 } |
231 */ | 231 */ |
232 Hangman.prototype.propose = function (ch, nickname) | 232 Hangman.prototype.propose = function (ch, nickname) |
233 { | 233 { |
234 var status = "found"; | 234 var status = "found"; |
235 | 235 |
236 /* Check for collaborative mode */ | 236 // Check for collaborative mode. |
237 if (Plugin.config["collaborative"] === "true") { | 237 if (Plugin.config["collaborative"] === "true") { |
238 if (this.last !== undefined && this.last === nickname) | 238 if (this.last !== undefined && this.last === nickname) |
239 return "wrong-player"; | 239 return "wrong-player"; |
240 | 240 |
241 this.last = nickname; | 241 this.last = nickname; |
258 status = "wrong-word"; | 258 status = "wrong-word"; |
259 } else | 259 } else |
260 status = "win"; | 260 status = "win"; |
261 } | 261 } |
262 | 262 |
263 /* Check if dead */ | 263 // Check if dead. |
264 if (this.tries < 0) | 264 if (this.tries <= 0) |
265 status = "dead"; | 265 status = "dead"; |
266 | 266 |
267 /* Check if win */ | 267 // Check if win. |
268 var win = true; | 268 var win = true; |
269 | 269 |
270 for (var i = 0; i < this.word.length; ++i) { | 270 for (var i = 0; i < this.word.length; ++i) { |
271 if (!this.table[this.word.charCodeAt(i)]) { | 271 if (!this.table[this.word.charCodeAt(i)]) { |
272 win = false; | 272 win = false; |
290 | 290 |
291 function propose(server, channel, origin, game, proposition) | 291 function propose(server, channel, origin, game, proposition) |
292 { | 292 { |
293 var kw = { | 293 var kw = { |
294 channel: channel, | 294 channel: channel, |
295 command: server.info().commandChar, | 295 command: server.info().commandChar + Plugin.info().name, |
296 nickname: Util.splituser(origin), | 296 nickname: Util.splituser(origin), |
297 origin: origin, | 297 origin: origin, |
298 plugin: Plugin.info().name, | 298 plugin: Plugin.info().name, |
299 server: server.toString() | 299 server: server.toString() |
300 }; | 300 }; |
307 server.message(channel, Util.format(Plugin.format["found"], kw)); | 307 server.message(channel, Util.format(Plugin.format["found"], kw)); |
308 break; | 308 break; |
309 case "wrong-letter": | 309 case "wrong-letter": |
310 case "wrong-player": | 310 case "wrong-player": |
311 case "wrong-word": | 311 case "wrong-word": |
312 kw.word = proposition; | |
312 case "asked": | 313 case "asked": |
313 kw.letter = String.fromCharCode(proposition); | 314 kw.letter = String.fromCharCode(proposition); |
314 server.message(channel, Util.format(Plugin.format[st], kw)); | 315 server.message(channel, Util.format(Plugin.format[st], kw)); |
315 break; | 316 break; |
316 case "dead": | 317 case "dead": |
329 function onCommand(server, origin, channel, message) | 330 function onCommand(server, origin, channel, message) |
330 { | 331 { |
331 var game = Hangman.find(server, channel); | 332 var game = Hangman.find(server, channel); |
332 var kw = { | 333 var kw = { |
333 channel: channel, | 334 channel: channel, |
334 command: server.info().commandChar, | 335 command: server.info().commandChar + Plugin.info().name, |
335 nickname: Util.splituser(origin), | 336 nickname: Util.splituser(origin), |
336 origin: origin, | 337 origin: origin, |
337 plugin: Plugin.info().name, | 338 plugin: Plugin.info().name, |
338 server: server | 339 server: server.toString() |
339 }; | 340 }; |
340 | 341 |
341 if (game) { | 342 if (game) { |
342 var list = message.split(" \t"); | 343 var list = message.split(" \t"); |
343 | 344 |