User:Monster Domosed/common.js: Difference between revisions

No edit summary
No edit summary
Line 6: Line 6:
var R = '}' + '}';
var R = '}' + '}';


// ---- CONFIG ---------------------------------------------------------
var GAME_MODE_SECTIONS = [ 'Street Brawl' ];
var GAME_MODE_SECTIONS = [ 'Street Brawl' ];


Line 58: Line 59:


} else if ( current !== null ) {
} else if ( current !== null ) {
current += ' ' + stripped;
current += ' ' + stripped;
}
}
Line 67: Line 69:


return entries.map( function ( e ) {
return entries.map( function ( e ) {
return e.replace( /\s+/g, ' ' ).trim();
return e.replace( /\s+/g, ' ' ).trim();
} ).filter( Boolean );
} ).filter( Boolean );
}
}
Line 102: Line 106:
}
}


function formatBody( text, heroes, abilities, items, usedHeroes, usedAbilities, usedItems ) {
function formatBody( text, heroes, abilities, items ) {


var protectedTokens = [];
var protectedTokens = [];
Line 112: Line 116:
}
}


function wrap( terms, template, usedSet ) {
function wrap( terms, template ) {


terms.slice()
terms.slice()
Line 120: Line 124:
.forEach( function ( term ) {
.forEach( function ( term ) {


var idx = 0;
var pattern = new RegExp(
var found = false;
'(?<!\\w)' + escapeRegExp( term ) + '(?!\\w)',
 
'g'
while ( ( idx = text.indexOf( term, idx ) ) !== -1 ) {
);
 
var before = text[ idx - 1 ];
var after = text[ idx + term.length ];
 
var validBefore = !before || /\s|:|\.|\(|\[/ .test( before );
var validAfter = !after || /\s|\.|,|\)|\]/.test( after );
 
if ( validBefore && validAfter ) {
 
found = true;
 
var replacement = protect(
template.replace( '%s', term )
);
 
text =
text.slice( 0, idx ) +
replacement +
text.slice( idx + term.length );
 
idx += replacement.length;
 
} else {
idx += 1;
}
}


if ( found ) {
text = text.replace( pattern, function () {
usedSet.add( term );
return protect(
}
template.replace( '%s', term )
);
} );
} );
} );
}
}


wrap( abilities, L + 'AbilityIcon|%s' + R, usedAbilities );
wrap( abilities, L + 'AbilityIcon|%s' + R );
wrap( items, L + 'ItemIcon|%s' + R, usedItems );
wrap( items, L + 'ItemIcon|%s' + R );
wrap( heroes, L + 'HeroIcon|%s' + R, usedHeroes );
wrap( heroes, L + 'HeroIcon|%s' + R );


WIKI_LINKS.forEach( function ( pair ) {
WIKI_LINKS.forEach( function ( pair ) {
Line 181: Line 161:
var itemSections = {}, itemOrder = [];
var itemSections = {}, itemOrder = [];
var heroSections = {}, heroOrder = [];
var heroSections = {}, heroOrder = [];
var usedHeroes = new Set();
var usedItems = new Set();
var usedAbilities = new Set();


entries.forEach( function ( entry ) {
entries.forEach( function ( entry ) {


var c = classify( entry, heroes, items, gameModes );
var c = classify( entry, heroes, items, gameModes );
var kind = c[ 0 ];
var name = c[ 1 ];
var body = formatBody( c[ 2 ], heroes, abilities, items );


var body = formatBody(
if ( kind === 'general' ) {
c[ 2 ],
heroes,
abilities,
items,
usedHeroes,
usedAbilities,
usedItems
);


if ( c[ 0 ] === 'general' ) {
general.push( body );
general.push( body );
} else if ( c[ 0 ] === 'mode' ) {


if ( !modes[ c[ 1 ] ] ) {
} else if ( kind === 'mode' ) {
modes[ c[ 1 ] ] = [];
 
modeOrder.push( c[ 1 ] );
if ( !modes[ name ] ) {
modes[ name ] = [];
modeOrder.push( name );
}
}


modes[ c[ 1 ] ].push( body );
modes[ name ].push( body );


} else if ( c[ 0 ] === 'item' ) {
} else if ( kind === 'item' ) {


if ( !itemSections[ c[ 1 ] ] ) {
if ( !itemSections[ name ] ) {
itemSections[ c[ 1 ] ] = [];
itemSections[ name ] = [];
itemOrder.push( c[ 1 ] );
itemOrder.push( name );
}
}


itemSections[ c[ 1 ] ].push( body );
itemSections[ name ].push( body );


} else if ( c[ 0 ] === 'hero' ) {
} else if ( kind === 'hero' ) {


if ( !heroSections[ c[ 1 ] ] ) {
if ( !heroSections[ name ] ) {
heroSections[ c[ 1 ] ] = [];
heroSections[ name ] = [];
heroOrder.push( c[ 1 ] );
heroOrder.push( name );
}
}


heroSections[ c[ 1 ] ].push( body );
heroSections[ name ].push( body );
}
}
} );
} );
Line 233: Line 204:
var out = [];
var out = [];


// General
general.forEach( function ( entry ) {
general.forEach( function ( entry ) {
out.push( '* ' + entry );
out.push( '* ' + entry );
} );
} );


// Modes
modeOrder.forEach( function ( mode ) {
modeOrder.forEach( function ( mode ) {


Line 247: Line 220:
} );
} );


// Items
if ( itemOrder.length ) {
if ( itemOrder.length ) {


Line 263: Line 237:
}
}


// Heroes
if ( heroOrder.length ) {
if ( heroOrder.length ) {


Line 279: Line 254:
}
}


return {
return out.join( '\n' ).trim() + '\n';
text: out.join( '\n' ).trim() + '\n',
stats: {
heroes: usedHeroes.size,
abilities: usedAbilities.size,
items: usedItems.size
}
};
}
}


Line 293: Line 261:


var app = document.getElementById( 'patchformatter' );
var app = document.getElementById( 'patchformatter' );
if ( !app ) return;
 
if ( !app ) {
return;
}


var inputWidget = new OO.ui.MultilineTextInputWidget( {
var inputWidget = new OO.ui.MultilineTextInputWidget( {
Line 350: Line 321:
);
);


outputWidget.setValue( result.text );
outputWidget.setValue( result );


status.setLabel(
} ).catch( function ( e ) {
'Done: ' + entries.length +
' entries; ' +
result.stats.heroes + ' heroes; ' +
result.stats.abilities + ' abilities; ' +
result.stats.items + ' items.'
);


} ).catch( function () {
status.setLabel( 'Error' );
status.setLabel( 'Error' );
} );
} );
} );
} );


copyButton.on( 'click', function () {
copyButton.on( 'click', function () {
navigator.clipboard.writeText( outputWidget.getValue() );
 
navigator.clipboard.writeText(
outputWidget.getValue()
);
 
status.setLabel( 'Copied' );
status.setLabel( 'Copied' );
} );
} );
}
}


// ---- BOOT -----------------------------------------------------------
mw.loader.using( [
mw.loader.using( [
'oojs-ui-core',
'oojs-ui-core',
Line 382: Line 350:


}() );
}() );