javascript/features/playground/playground_commands.js
// communication throughout Las Venturas Playground.
communication throughout Las Venturas Playground.
communication throughout Las Venturas Playground .
async handleSubstitutions(player) {
        const words = this.communication_().getBlockedWords();
        const substitutions = this.communication_().getReplacements();
        const menu = new Menu('Substitutions', ['Phrase', 'Substitution', 'Added by']);
        menu.addItem('Add a new substitution', '-', '-', async () => {
            const before = await Question.ask(player, {
                question: 'Substitutions',
                message: 'Which word do you want to substitute?',
                constraints: {
                    validation: /^.{3,24}$/i,
                    explanation: 'The word must be between 3 and 24 characters in length.',
                    abort: 'Sorry, you did not enter a valid word to replace.',
                }
            });
            if (!before)
                return;  
            const after = await Question.ask(player, {
                question: 'Substitutions',
                message: 'Which word should it be substituted with?',
                constraints: {
                    validation: /^.{1,24}$/i,
                    explanation: 'The substitution must be between 1 and 24 characters in length.',
                    abort: 'Sorry, you did not enter a valid word to substitute.',
                }
            });
            if (!after)
                return;  
            const lowerCaseBefore = before.trim().toLowerCase();
            const lowerCaseAfter = after.trim().toLowerCase();
            for (const existingSubstitution of substitutions) {
                if (existingSubstitution.before !== lowerCaseBefore)
                    continue;
                return alert(player, {
                    title: 'Substitutions',
                    message: `The word "${lowerCaseBefore}" is already being substituted.`
                });
            }
            for (const existingWord of words) {
                if (existingWord.word !== lowerCaseBefore)
                    continue;
                return alert(player, {
                    title: 'Substitutions',
                    message: `The word "${lowerCaseBefore}" already exists as a blocked word.`
                });
            }
            this.communication_().addReplacement(player, lowerCaseBefore, lowerCaseAfter);
            this.announce_().announceToAdministrators(
                Message.LVP_ANNOUNCE_SUBSTITUTION_ADDED, player.name, player.id, lowerCaseBefore,
                lowerCaseAfter);
            return alert(player, {
                title: 'Substitutions',
                message: `The word "${lowerCaseBefore}" will now be substituted with "` +
                         `${lowerCaseAfter}" on the server.`
            });
        });
        menu.addItem('-----', '-----', '-----');
        for (const { before, after, nickname } of substitutions.sort()) {
            menu.addItem(before, after, nickname, async() => {
                const confirmation = await confirm(player, {
                    title: 'Substitutions',
                    message: `Are you sure that you want remove the substitution for "${before}"?`,
                });
                if (!confirmation)
                    return;  
                this.communication_().removeReplacement(player, before);
                this.announce_().announceToAdministrators(
                    Message.LVP_ANNOUNCE_SUBSTITUTION_REMOVED, player.name, player.id, before,
                    after);
                return alert(player, {
                    title: 'Substitutions',
                    message: `The word "${before}" will no longer be substituted.`
                });
            });
        }
        await menu.displayForPlayer(player);
    }
async handleSubstitutions ( player ) { const words = this . communication_ ( ) . getBlockedWords ( ) ; const substitutions = this . communication_ ( ) . getReplacements ( ) ; const menu = new Menu ( 'Substitutions' , [ 'Phrase' , 'Substitution' , 'Added by' ] ) ; menu . addItem ( 'Add a new substitution' , '-' , '-' , async ( ) => { const before = await Question . ask ( player , { question : 'Substitutions' , message : 'Which word do you want to substitute?' , constraints : { validation : / ^.{3,24}$ / i , explanation : 'The word must be between 3 and 24 characters in length.' , abort : 'Sorry, you did not enter a valid word to replace.' , } } ) ; if ( ! before ) return ; const after = await Question . ask ( player , { question : 'Substitutions' , message : 'Which word should it be substituted with?' , constraints : { validation : / ^.{1,24}$ / i , explanation : 'The substitution must be between 1 and 24 characters in length.' , abort : 'Sorry, you did not enter a valid word to substitute.' , } } ) ; if ( ! after ) return ; const lowerCaseBefore = before . trim ( ) . toLowerCase ( ) ; const lowerCaseAfter = after . trim ( ) . toLowerCase ( ) ; for ( const existingSubstitution of substitutions ) { if ( existingSubstitution . before !== lowerCaseBefore ) continue ; return alert ( player , { title : 'Substitutions' , message : ` ${ lowerCaseBefore } ` } ) ; } for ( const existingWord of words ) { if ( existingWord . word !== lowerCaseBefore ) continue ; return alert ( player , { title : 'Substitutions' , message : ` ${ lowerCaseBefore } ` } ) ; } this . communication_ ( ) . addReplacement ( player , lowerCaseBefore , lowerCaseAfter ) ; this . announce_ ( ) . announceToAdministrators ( Message . LVP_ANNOUNCE_SUBSTITUTION_ADDED , player . name , player . id , lowerCaseBefore , lowerCaseAfter ) ; return alert ( player , { title : 'Substitutions' , message : ` ${ lowerCaseBefore } ` + ` ${ lowerCaseAfter } ` } ) ; } ) ; menu . addItem ( '-----' , '-----' , '-----' ) ; for ( const { before , after , nickname } of substitutions . sort ( ) ) { menu . addItem ( before , after , nickname , async ( ) => { const confirmation = await confirm ( player , { title : 'Substitutions' , message : ` ${ before } ` , } ) ; if ( ! confirmation ) return ; this . communication_ ( ) . removeReplacement ( player , before ) ; this . announce_ ( ) . announceToAdministrators ( Message . LVP_ANNOUNCE_SUBSTITUTION_REMOVED , player . name , player . id , before , after ) ; return alert ( player , { title : 'Substitutions' , message : ` ${ before } ` } ) ; } ) ; } await menu . displayForPlayer ( player ) ; }
communication throughout Las Venturas Playground.
communication throughout Las Venturas Playground .
// the |player| abandoned the flow // the |player| abandoned the flow // Verify that the |lowerCaseBefore| is not already being substituted to something else. // Verify that the |lowerCaseBefore| is not already known as a blocked word. // Actually block the |lowerCaseWordToBlock| now. // Inform administrators of the newly blocked word. // Confirm the action to the |player|. // Add a delimiter before listing all the existing substitutions. // the |player| abandoned the flow // Actually remove the substitution for the |before|. // Inform administrators of the substitution which has been removed.