|
| 1 | +// Copyright 2020 Las Venturas Playground. All rights reserved. |
| 2 | +// Use of this source code is governed by the MIT license, a copy of which can |
| 3 | +// be found in the LICENSE file. |
| 4 | + |
| 5 | +import { Strategy } from 'features/reaction_tests/strategy.js'; |
| 6 | + |
| 7 | +import { random } from 'base/random.js'; |
| 8 | + |
| 9 | +// File that contains the word definitions for scrambled word games. |
| 10 | +const kWordDefinitionFile = 'data/scrambled_words.json'; |
| 11 | + |
| 12 | +// Utility function to determine whether the given |charCode| should be scrambled. |
| 13 | +function shouldScramble(charCode) { |
| 14 | + return (charCode >= 48 && charCode <= 57) || /* numbers */ |
| 15 | + (charCode >= 65 && charCode <= 90) || /* upper case */ |
| 16 | + (charCode >= 97 && charCode <= 122); /* lower case */ |
| 17 | +} |
| 18 | + |
| 19 | +// This strategy works by scrambling the characters in a list of predefined phrases, and requiring |
| 20 | +// players to guess the right word. The actual list is defined in |kWordDefinitionFile|. |
| 21 | +export class UnscrambleStrategy extends Strategy { |
| 22 | + // How many players should be on the server in order to consider this strategy? |
| 23 | + static kMinimumPlayerCount = 1; |
| 24 | + |
| 25 | + answer_ = null; |
| 26 | + scrambled_ = null; |
| 27 | + settings_ = null; |
| 28 | + words_ = new Set(); |
| 29 | + |
| 30 | + constructor(settings) { |
| 31 | + super(); |
| 32 | + |
| 33 | + this.settings_ = settings; |
| 34 | + } |
| 35 | + |
| 36 | + // Gets the answer to the current reaction test. May be NULL. |
| 37 | + get answer() { return this.answer_; } |
| 38 | + |
| 39 | + // Gets the scrambled answer, primarily for testing purposes. |
| 40 | + get scrambled() { return this.scrambled_; } |
| 41 | + |
| 42 | + // Initializes the list of words that players should unscramble. Will be called when a test is |
| 43 | + // starting, to avoid doing this too often while running tests. |
| 44 | + ensureWordListInitialized() { |
| 45 | + if (this.words_.size) |
| 46 | + return; |
| 47 | + |
| 48 | + const words = JSON.parse(readFile(kWordDefinitionFile)); |
| 49 | + for (const word of words) { |
| 50 | + if (word.startsWith('__')) |
| 51 | + continue; // considered as a note/comment, ignore it |
| 52 | + |
| 53 | + this.words_.add(word); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // Starts a new test provided by this strategy. The question must be determined, and it should |
| 58 | + // be announced to people in-game and available through Nuwani accordingly. |
| 59 | + start(announceFn, nuwani, prize) { |
| 60 | + this.ensureWordListInitialized(); |
| 61 | + |
| 62 | + // Pick a random phrase from the loaded word list. |
| 63 | + this.answer_ = ([ ...this.words_ ][ random(this.words_.size) ]).toUpperCase(); |
| 64 | + this.scrambled_ = this.scramble(this.answer_); |
| 65 | + |
| 66 | + // Announce the test to all in-game participants. |
| 67 | + announceFn(Message.REACTION_TEST_ANNOUNCE_UNSCRAMBLE, this.scrambled_, prize); |
| 68 | + |
| 69 | + // Announce the test to everyone reading along through Nuwani. |
| 70 | + nuwani().echo('reaction-unscramble', this.scrambled_, prize); |
| 71 | + } |
| 72 | + |
| 73 | + // Scrambles the given |phrase| and returns the result. We decide which characters are valid to |
| 74 | + // be scrambled, then scramble them, re-compose the word, and return it as a string. |
| 75 | + scramble(phrase) { |
| 76 | + if (phrase.trim() !== phrase) |
| 77 | + console.log(`[reaction test] warning: "${phrase}" has excess spacing.`); |
| 78 | + |
| 79 | + const kStaticPercentage = |
| 80 | + this.settings_().getValue('playground/reaction_test_unscramble_fixed'); |
| 81 | + |
| 82 | + const words = phrase.split(' '); |
| 83 | + const result = []; |
| 84 | + |
| 85 | + for (const word of words) { |
| 86 | + const characters = []; |
| 87 | + const fixed = new Set(); |
| 88 | + |
| 89 | + // (1) Decide which characters are supposed to be fixed. |
| 90 | + for (let index = 0; index < word.length; ++index) { |
| 91 | + if (random(100) < kStaticPercentage) |
| 92 | + fixed.add(index); |
| 93 | + } |
| 94 | + |
| 95 | + // (1) Collect all the characters in the |word|. |
| 96 | + for (let index = 0; index < word.length; ++index) { |
| 97 | + if (!shouldScramble(word.charCodeAt(index)) || fixed.has(index)) |
| 98 | + continue; |
| 99 | + |
| 100 | + characters.push(word.charAt(index)); |
| 101 | + } |
| 102 | + |
| 103 | + const shuffled = this.shuffle(characters); |
| 104 | + const composed = []; |
| 105 | + |
| 106 | + // (2) Re-compose the scrambled word. |
| 107 | + for (let index = 0; index < word.length; ++index) { |
| 108 | + if (shouldScramble(word.charCodeAt(index)) && !fixed.has(index)) |
| 109 | + composed.push(shuffled.shift()); |
| 110 | + else |
| 111 | + composed.push(word.charAt(index)); |
| 112 | + } |
| 113 | + |
| 114 | + result.push(composed.join('')); |
| 115 | + } |
| 116 | + |
| 117 | + // (3) Join the composed word together, then translate to upper case. |
| 118 | + return result.join(' '); |
| 119 | + } |
| 120 | + |
| 121 | + // Shuffles the given |array| with the Fisher-Yates algorithm: |
| 122 | + // https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle |
| 123 | + shuffle(array) { |
| 124 | + let counter = array.length; |
| 125 | + |
| 126 | + while (counter > 0) { |
| 127 | + const index = random(counter--); |
| 128 | + const temp = array[counter]; |
| 129 | + |
| 130 | + array[counter] = array[index]; |
| 131 | + array[index] = temp; |
| 132 | + } |
| 133 | + |
| 134 | + return array; |
| 135 | + } |
| 136 | + |
| 137 | + // Verifies whether the |message| is, or contains, the answer to this reaction test. |
| 138 | + verify(message) { |
| 139 | + return message.toUpperCase().startsWith(this.answer_); |
| 140 | + } |
| 141 | +} |
0 commit comments