comparison plugins/hangman/hangman.js @ 462:eaec3bff1db8

Merge from stable-2
author David Demelier <markand@malikania.fr>
date Fri, 28 Jul 2017 11:41:41 +0200
parents f3c27790d0d1 c81b38ec7bd2
children 0b156b82b8c1
comparison
equal deleted inserted replaced
457:2958f36cb3be 462:eaec3bff1db8
60 * Map of games. 60 * Map of games.
61 */ 61 */
62 Hangman.map = {}; 62 Hangman.map = {};
63 63
64 /** 64 /**
65 * List of words 65 * List of words.
66 */ 66 */
67 Hangman.words = []; 67 Hangman.words = {
68 all: [], //!< All words,
69 registry: {} //!< Words list per server/channel.
70 };
68 71
69 /** 72 /**
70 * Search for an existing game. 73 * Search for an existing game.
71 * 74 *
72 * @param server the server object 75 * @param server the server object
137 var file = new File(path, "r"); 140 var file = new File(path, "r");
138 var line; 141 var line;
139 142
140 while ((line = file.readline()) !== undefined) 143 while ((line = file.readline()) !== undefined)
141 if (Hangman.isWord(line)) 144 if (Hangman.isWord(line))
142 Hangman.words.push(line); 145 Hangman.words.all.push(line);
143 } catch (e) { 146 } catch (e) {
144 throw new Error("could not open '" + path + "'"); 147 throw new Error("could not open '" + path + "'");
145 } 148 }
146 149
147 if (Hangman.words.length === 0) 150 if (Hangman.words.all.length === 0)
148 throw new Error("empty word database"); 151 throw new Error("empty word database");
149 152
150 Logger.info("number of words in database: " + Hangman.words.length); 153 Logger.info("number of words in database: " + Hangman.words.all.length);
151 } 154 }
152 155
153 /** 156 /**
154 * Load all formats. 157 * Load all formats.
155 */ 158 */
180 /** 183 /**
181 * Select the next word for the game. 184 * Select the next word for the game.
182 */ 185 */
183 Hangman.prototype.select = function () 186 Hangman.prototype.select = function ()
184 { 187 {
188 var id = this.server.toString() + "@" + this.channel;
189
185 // Reload the words if empty. 190 // Reload the words if empty.
186 if (!this.words || this.words.length === 0) 191 if (!Hangman.words.registry[id] || Hangman.words.registry[id].length === 0)
187 this.words = Hangman.words.slice(0); 192 Hangman.words.registry[id] = Hangman.words.all.slice(0);
188 193
189 var i = Math.floor(Math.random() * this.words.length); 194 var i = Math.floor(Math.random() * Hangman.words.registry[id].length);
190 195
191 this.word = this.words[i]; 196 this.word = Hangman.words.registry[id][i];
192 this.words.splice(i, 1); 197
198 // Erase words from the registry.
199 Hangman.words.registry[id].splice(i, 1);
193 200
194 // Fill table. 201 // Fill table.
195 this.table = {}; 202 this.table = {};
196 203
197 for (var j = 0; j < this.word.length; ++j) 204 for (var j = 0; j < this.word.length; ++j)