MediaWiki:Common.js: Difference between revisions

From Blue Archive Wiki
Jump to navigation Jump to search
Content added Content deleted
(See if this keeps character table from breaking)
(Disabled Minerva-specific styles workaround)
 
(48 intermediate revisions by 2 users not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */

$(function() {
$(function() {
if (window.location.host === "bluearchive.miraheze.org") {
if (window.location.host === "bluearchive.miraheze.org") {
window.location.replace("https://bluearchive.wiki" + window.location.pathname);
window.location.replace("https://bluearchive.wiki" + window.location.pathname);
}
}
// Common styles are currently loaded through an @import statement in MediaWiki:Mobile.css. I do not know what calls mw.loader.using('mobile.site.styles') currently, but it appears to happen. This code is preserved in case something breaks after caches expire.
if (mw.config.get("skin") === "minerva") {
// if (mw.config.get("skin") === "minerva") {
// This hack is required to get custom css to load in mobile view. See https://phabricator.wikimedia.org/T270845
// // This hack is required to get custom css to load in mobile view. See https://phabricator.wikimedia.org/T270845
loadMobileStylesheet();
// loadMobileStylesheet();
}
// }
function initCountdown() {
function initCountdown() {
var reset = new Date();
var reset = new Date();
Line 53: Line 55:
initBirthdays();
initBirthdays();
}
}
if (mw.config.get("wgPageName") === "Characters") {
if (mw.config.get("wgPageName") === "Characters") {
initCharacterTable();
initCharacterTable();
}
}
if (mw.config.get("wgPageName") === "Characters" || mw.config.get("wgPageName") === "Characters_StatChart") {
}
mw.loader.load( '/w/index.php?title=MediaWiki:CharacterTable.js&action=raw&ctype=text/javascript' );
});
}
if (mw.config.get("wgPageName") === "An_Unconcealed_Heart/Mitcher") {
mw.loader.load( '/w/index.php?title=MediaWiki:Mitcher.js&action=raw&ctype=text/javascript' );
}
if (mw.config.get("wgPageName") === "A_Game_Before_the_New_Year's_Feast_~_One-and-Done_Match_~/Junby") {
mw.loader.load( '/w/index.php?title=MediaWiki:Junby.js&action=raw&ctype=text/javascript' );
}


const title_split = mw.config.get('wgTitle').toLowerCase().split("/");
if (mw.config.get('wgNamespaceNumber') === 0 && title_split.length === 2 && title_split[1] === 'momotalk') {
mw.loader.load( '/w/index.php?title=MediaWiki:MomotalkChoice.js&action=raw&ctype=text/javascript' );
}


}
/* Character stat calc - start */
const level_cap = 80;
const fix_ingame_stats = true; //attempt to recalculate raw numbers if provided data looks like ingame stats

const rarity_bonus = {
"attack" : [0, 0, 1000, 2200, 3600, 5300 ],
"defense" : [0, 0, 0, 0, 0, 0 ],
"healing" : [0, 0, 750, 1750, 2950, 4450 ],
"hp" : [0, 0, 500, 1200, 2100, 3500 ]
};
var stats = {};
var tableCounter = 0;

$( document ).ready(function() {
initStatCalc();
$(".stattable-controls input").on("change mouseup keyup click", function(){levelChange($(this).closest("table"));});
$(".stattable-rarity-selector").children("img").on("click", function(){rarityChange($(this).closest("table"),$(this).attr('data-rarity'));})
});
});


function initStatCalc(){
$(".character-stattable").each(function(){
var id = 'statTable-'+(++tableCounter);
$(this).attr('id',id);
//console.log('StatCalc - init table id ' + id);
var attack_data = $(this).find(".stat-attack").html().split('/');
var defense_data = $(this).find(".stat-defense").html().split('/');
var hp_data = $(this).find(".stat-hp").html().split('/');
var healing_data = $(this).find(".stat-healing").html().split('/');
stats[id] = {};
stats[id].rarity = !isNaN(parseInt($(document).find(".character-rarity").attr('data-value'))) ? parseInt($(document).find(".character-rarity").attr('data-value')) : 3 ;
stats[id].level = level_cap;
stats[id].attack_min = parseInt(attack_data[0]) > 0 ? parseInt(attack_data[0]) : null;
stats[id].attack_max = parseInt(attack_data[1]) > 0 ? parseInt(attack_data[1]) : null;
stats[id].defense_min = parseInt(defense_data[0]) > 0 ? parseInt(defense_data[0]) : null;
stats[id].defense_max = parseInt(defense_data[1]) > 0 ? parseInt(defense_data[1]) : null;
stats[id].hp_min = parseInt(hp_data[0]) > 0 ? parseInt(hp_data[0]) : null;
stats[id].hp_max = parseInt(hp_data[1]) > 0 ? parseInt(hp_data[1]) : null;
stats[id].healing_min = parseInt(healing_data[0]) > 0 ? parseInt(healing_data[0]) : null;
stats[id].healing_max = parseInt(healing_data[1]) > 0 ? parseInt(healing_data[1]) : null;
//stats[$(this).attr('id')] = JSON.parse($(this).attr('stat-data'));
if (!hasNull(stats[id]))
{
// Data sanity check to make sure actual raw numbers were provided
if (fix_ingame_stats && (stats[id].attack_max/stats[id].attack_min < 9))
{
console.log('StatCalc - Attack data provided is probably NOT a raw number, consider updating data');
fixedStats = calcReverseStat(level_cap,stats[id].rarity,'attack',stats[id].attack_min,stats[id].attack_max);
stats[id].attack_min = fixedStats[0];
stats[id].attack_max = fixedStats[1];
}
if (fix_ingame_stats && (stats[id].defense_max/stats[id].defense_min < 5.5))
{
console.log('StatCalc - Defense data provided is probably NOT a raw number, consider updating data');
fixedStats = calcReverseStat(level_cap,stats[id].rarity,'defense',stats[id].defense_min,stats[id].defense_max);
stats[id].defense_min = fixedStats[0];
stats[id].defense_max = fixedStats[1];
}
if (fix_ingame_stats && (stats[id].hp_max/stats[id].hp_min < 7.4))
{
console.log('StatCalc - HP data provided is probably NOT a raw number, consider updating data');
fixedStats = calcReverseStat(level_cap,stats[id].rarity,'hp',stats[id].hp_min,stats[id].hp_max);
stats[id].hp_min = fixedStats[0];
stats[id].hp_max = fixedStats[1];
}
if (fix_ingame_stats && (stats[id].healing_max/stats[id].healing_min < 2.7))
{
console.log('StatCalc - Healing data provided is probably NOT a raw number, consider updating data');
fixedStats = calcReverseStat(level_cap,stats[id].rarity,'healing',stats[id].healing_min,stats[id].healing_max);
stats[id].healing_min = fixedStats[0];
stats[id].healing_max = fixedStats[1];
}
raritySelector = $(this).find(".stattable-rarity-selector");
var star_html = raritySelector.html();
raritySelector.html(star_html+star_html+star_html+star_html+star_html);
raritySelector.children().each(function(index){$(this).attr('data-rarity',index+1)})
$(this).find(".stattable-controls td").append('<span class="stattable-level-selector">Level: <input class="stattable-level" type="number" value="'+level_cap+'" step="1" min="1" max="100" /></span>');
$(this).find(".stattable-controls").css( "display", "" );
rarityChange($(this), stats[id].rarity);
//statTableRecalc(id);
}
else
{ console.log('StatCalc - init cancelled due to incomplete data'); }


/* Character stat calc & affection table */
});
mw.loader.load( '/w/index.php?title=MediaWiki:StatCalc.js&action=raw&ctype=text/javascript' );
}
function levelChange (statTable){
//console.log('changing LEVEL in table '+statTable.attr('id'));
var level = !isNaN(parseInt(statTable.find(".stattable-level").val())) ? parseInt(statTable.find(".stattable-level").val()) : 1 ;
if (level < 1) { statTable.find(".stattable-level").val(1); level = 1; }
if (level > 100) { statTable.find(".stattable-level").val(100); level = 100; }
stats[statTable.attr('id')].level = level;
statTableRecalc(statTable.attr('id'));
}

function rarityChange (statTable, rarity){
//console.log('changing RARITY in table '+statTable.attr('id')+' to '+rarity);
stats[statTable.attr('id')].rarity = rarity;
statTable.find(".stattable-rarity-selector").children().each(function(index){
($(this).attr('data-rarity') <= rarity) ? $(this).addClass('active').removeClass('inactive') : $(this).addClass('inactive').removeClass('active');
});
statTableRecalc(statTable.attr('id'));
}

function statTableRecalc(id){
//console.log(id+' recalc called');
var statTable = $('#'+id);
statTable.find(".stat-attack").html(calcStat( stats[id].level, stats[id].rarity, 'attack', stats[id].attack_min, stats[id].attack_max));
statTable.find(".stat-defense").html(calcStat( stats[id].level, stats[id].rarity, 'defense', stats[id].defense_min, stats[id].defense_max));
statTable.find(".stat-hp").html(calcStat( stats[id].level, stats[id].rarity, 'hp', stats[id].hp_min, stats[id].hp_max));
statTable.find(".stat-healing").html(calcStat( stats[id].level, stats[id].rarity, 'healing', stats[id].healing_min, stats[id].healing_max));
}
function calcStat(level,rarity,statName,val1,val100){
return Math.ceil( Math.round(val1 + (val100 - val1) * (Math.round((level - 1) / 99 * 10000) / 10000)) * (10000 + rarity_bonus[statName][rarity]) / 10000 );
}
function calcReverseStat(startingLevel,startingRarity,statName,val1,val100){
var rawVal1 = Math.floor(val1 / (10000 + rarity_bonus[statName][startingRarity]) * 10000);
var rawVal100 = Math.floor((val100 / (10000 + rarity_bonus[statName][startingRarity]) * 10000 - rawVal1) / (startingLevel - 1) * 99 + rawVal1);
return [rawVal1, rawVal100];
}
function hasNull(target) {
for (var member in target) {
if (target[member] == null)
return true;
}
return false;
}
/* Character stat calc - end */




/* Character birthdays - start */
/* Character birthdays - start */
function initBirthdays(){
function initBirthdays(){
const separator = ' <span></span> ';
const months = [
const months = [
'January',
'January',
Line 259: Line 119:


data_out.forEach(
data_out.forEach(
function (character) { html_out += '<a href="/wiki/'+character[0]+'">'+character[2]+'&nbsp;('+character[1].replace(' ', '&nbsp;')+')</a>'+separator; }
function (character) { html_out += '<a href="/wiki/'+character[0]+'">'+character[2]+'&nbsp;('+character[1].replace(' ', '&nbsp;')+')</a>'; }
);
);


$('#character-birthdays').append(html_out.substring(0,html_out.length-separator.length)).css( "display", "" );
$('#character-birthdays').append(html_out).css( "display", "" );
}
}
/* Character birthdays - end */
/* Character birthdays - end */


/* Character affection table - start */
const affection_start = 50;
const affection_cap = 50;
var affection_data = {};
var affectionTableCounter = 0;

$( document ).ready(function() {
initAffectionTable();
$(".affection-level input").on("change mouseup keyup click", function(){affectionChange($(this).closest("table"), $(this).val());});
$(".affection-data").children("div").on("click", function(){affectionChange($(this).closest("table"),$(this).attr('data-level'));});
});

function initAffectionTable(){
$(".character-affectiontable").each(function(){
var id = 'affectionTable-'+(++affectionTableCounter);
$(this).attr('id',id);
var data = {};

$(this).find(".affection-data > div").each(function(){
var level = $(this).attr('data-level');
data[level] = {};
var bonus = $(this).attr('data-stats').split(' ');

$.each( bonus, function( index ) {
bonus[index] = bonus[index].split('+');
data[level][bonus[index][0]] = parseInt(bonus[index][1]);
});
});
affection_data[id] = data;

$(this).find(".affection-level").html('<input type="number" value="'+affection_start+'" step="1" min="1" max="'+affection_cap+'" />');
affectionChange($(this), affection_start);
});
}

function affectionChange (affectionTable, level){
var effective_bonus = {};
var html_out = '';

for (var index = 2; index <= level; index++) {
$.each( affection_data[affectionTable.attr('id')][index], function(stat_name, stat_value){
if (typeof effective_bonus[stat_name] == 'undefined') effective_bonus[stat_name] = 0;
effective_bonus[stat_name] += stat_value;
});
}
$.each( effective_bonus, function(stat_name, stat_value){
html_out += '<b>' + stat_name + '</b>' + ' +' + stat_value + ', ';
});

if (affectionTable.find(".affection-level input").val() !== level) affectionTable.find(".affection-level input").val(level);
affectionTable.find(".affection-total").html(html_out.substring(0,html_out.length-2));
}
/* Character affection table - end */




Line 336: Line 135:
var voice = $(".character td.character-voice");
var voice = $(".character td.character-voice");
if (voice.length && voice.attr('data-voice').length) {
if (voice.length && voice.attr('data-voice').length) {
voice.addClass('character-voice-preview').on("click", function(){playCharacterVoice();});
voice.wrapInner('<span>');
voice.wrapInner('<span>');
if (voice.find("span").width() > voice.width()-36 ) voice.css('padding-right', '16px');
if (voice.find("span").width() > voice.width()-36 ) voice.css('padding-right', '16px');
voice.find("span").children().unwrap();
voice.find("span").children().unwrap();
}
voice.append('<audio class="voice-clip" src="'+voice.attr('data-voice')+'"></audio>').addClass('character-voice-preview');
}

function playCharacterVoice(){
var voice = $(".character td.character-voice");
if (voice.find('audio').length == 0) {
voice.append('<audio class="voice-clip" src="'+voice.attr('data-voice')+'"></audio>');
voice.find('audio')[0].volume=0.6;
voice.find('audio')[0].volume=0.6;
$(".character td.character-voice-preview").on("click", function(){ voice.find('audio')[0].play(); })
}
}
voice.find('audio')[0].play();
}
}
/* Character voice preview - end */
/* Character voice preview - end */


/* Character video preview - start */
$( document ).ready(function() {
//if video tab is open on inital page display
if ($(".character-images [data-title='Chibi']").attr('aria-hidden') == 'false' || window.location.hash == '#Chibi-0' || window.location.hash == '#Other-0' ) initCharacterVideo($('.character-images'));

//Tabber initializes late so events are bound at first click on parent div
$(".character-images div.tabber").on("click", function(){
if ($(".character-images [data-title='Chibi']").attr('aria-hidden') == 'false') initCharacterVideo($('.character-images'));
$(".character-images [data-title='Chibi']").on("click", function(){initCharacterVideo($('.character-images'));});
});
})

function initCharacterVideo(element) {
var container = element.find('div.video');
if (!container.attr('data-video-initialized')) {
var video_src = container.attr('data-videosrc');
container.html('<video autoplay loop playsinline><source src="'+video_src+'" type="video/webm" /></video>');
container.on("click", function(){playToggle(container)});
container.attr('data-video-initialized', true);
}
}

function playToggle(element) {
var video = element.find('video').get(0);
if (video.paused) video.play();
else video.pause();
}
/* Character video preview - end */


/* XP tables */
mw.loader.load( '/w/index.php?title=MediaWiki:XPtable.js&action=raw&ctype=text/javascript' );

Latest revision as of 04:21, 22 April 2024

/* Any JavaScript here will be loaded for all users on every page load. */

$(function() {
	if (window.location.host === "bluearchive.miraheze.org") {
		window.location.replace("https://bluearchive.wiki" + window.location.pathname);
	}
    // Common styles are currently loaded through an @import statement in MediaWiki:Mobile.css. I do not know what calls mw.loader.using('mobile.site.styles') currently, but it appears to happen. This code is preserved in case something breaks after caches expire.
	// if (mw.config.get("skin") === "minerva") {
    //     // This hack is required to get custom css to load in mobile view. See https://phabricator.wikimedia.org/T270845
    //     loadMobileStylesheet();
    // }
    function initCountdown() {
        var reset = new Date();
        reset.setUTCHours(19, 0, 0, 0);
        
        function pad(n) {
            return ("0" + (n | 0)).slice(-2);
        }

        function tick() {
            var now = new Date();
            if (now > reset) {
            reset.setUTCDate(reset.getUTCDate() + 1);
            }
            var remaining = ((reset - now) /  1000);
            var hours = (remaining / 3600) % 60;
            var minutes = (remaining / 60) % 60;
            var seconds = remaining % 60;
            document.getElementById("resetCountdown").innerHTML = pad(hours) + ":" + pad(minutes) + ":" + pad(seconds);
            setTimeout(tick, 1000);
        }

        tick();
    }
    function initCharacterTable() {
        mw.loader.using("jquery.tablesorter", function() {
            $("table.sortable").tablesorter({sortList: [{2: "desc"}, {1: "asc"}]});
        });
    }
    function loadMobileStylesheet() {
    	var head = document.getElementsByTagName("head")[0];
        var common = document.createElement("link");
		var mobile = document.createElement("link");
        common.rel = mobile.rel = "stylesheet";
        common.type = mobile.type = "text/css";
        common.href = "https://bluearchive.miraheze.org/w/load.php?lang=en&modules=site.styles&only=styles&skin=minerva";
		mobile.href = "https://bluearchive.miraheze.org/wiki/MediaWiki:Mobile.css?action=raw&ctype=text/css";
        head.appendChild(common);
        head.appendChild(mobile);
    }
    if (mw.config.get("wgIsArticle")) {
    	// These won't run in Edit mode
	    if (mw.config.get("wgPageName") === "Main_Page") {
	        initCountdown();
	        initBirthdays();
	    }
		if (mw.config.get("wgPageName") === "Characters") {
	        initCharacterTable();
	    }
	    if (mw.config.get("wgPageName") === "Characters" || mw.config.get("wgPageName") === "Characters_StatChart") {
			mw.loader.load( '/w/index.php?title=MediaWiki:CharacterTable.js&action=raw&ctype=text/javascript' );
	    }
		if (mw.config.get("wgPageName") === "An_Unconcealed_Heart/Mitcher") {
	        mw.loader.load( '/w/index.php?title=MediaWiki:Mitcher.js&action=raw&ctype=text/javascript' );
	    }
        if (mw.config.get("wgPageName") === "A_Game_Before_the_New_Year's_Feast_~_One-and-Done_Match_~/Junby") {
	        mw.loader.load( '/w/index.php?title=MediaWiki:Junby.js&action=raw&ctype=text/javascript' );
	    }

        const title_split = mw.config.get('wgTitle').toLowerCase().split("/");
        if (mw.config.get('wgNamespaceNumber') === 0 && title_split.length === 2 && title_split[1] === 'momotalk') {
            mw.loader.load( '/w/index.php?title=MediaWiki:MomotalkChoice.js&action=raw&ctype=text/javascript' ); 
        } 

    }
});


/* Character stat calc & affection table */
mw.loader.load( '/w/index.php?title=MediaWiki:StatCalc.js&action=raw&ctype=text/javascript' );


/* Character birthdays - start */
function initBirthdays(){
	const months = [
		'January',
		'February',
		'March',
		'April',
		'May',
		'June',
		'July',
		'August',
		'September',
		'October',
		'November',
		'December'
	];

	var birthdays = [];
	var data_out = [];
	var html_out = '';

	var raw_data = $('#character-birthdays').attr('data-birthdays').split('&');
	
	raw_data.forEach(function (character) {
		var char_data = character.split(' |');
		char_data.push(char_data[0].split('(')[0].trim()); //normalized name
		if (!birthdays.some(function (data) {return data[2] == char_data[2];})) birthdays.push(char_data);
	});

	for (var index = -1; index <= 30; index++) {
		var date = new Date();
		date.setDate(date.getDate() + index);
		data_out = data_out.concat(birthdays.filter(function (data) { return data[1] == months[date.getMonth()]+' '+date.getDate();}));

		if (data_out.length >= 5) { break; }
	}

	data_out.forEach(
		function (character) { html_out += '<a href="/wiki/'+character[0]+'">'+character[2]+'&nbsp;('+character[1].replace(' ', '&nbsp;')+')</a>'; }
	);

	$('#character-birthdays').append(html_out).css( "display", "" );
}
/* Character birthdays - end */


/* Character voice preview - start */
$( document ).ready(function() {
	initCharacterVoice();
});

function initCharacterVoice(){
    var voice = $(".character td.character-voice");
    if (voice.length && voice.attr('data-voice').length) {
        voice.addClass('character-voice-preview').on("click", function(){playCharacterVoice();});
        voice.wrapInner('<span>');
        if (voice.find("span").width() > voice.width()-36 ) voice.css('padding-right', '16px');
        voice.find("span").children().unwrap();
    }
}

function playCharacterVoice(){
    var voice = $(".character td.character-voice");
    if (voice.find('audio').length == 0) {
        voice.append('<audio class="voice-clip" src="'+voice.attr('data-voice')+'"></audio>');
        voice.find('audio')[0].volume=0.6;
    }
    voice.find('audio')[0].play();
}
/* Character voice preview - end */


/* Character video preview - start */
$( document ).ready(function() {
    //if video tab is open on inital page display
    if ($(".character-images [data-title='Chibi']").attr('aria-hidden') == 'false' || window.location.hash == '#Chibi-0' || window.location.hash == '#Other-0' ) initCharacterVideo($('.character-images'));

    //Tabber initializes late so events are bound at first click on parent div
    $(".character-images div.tabber").on("click", function(){ 
        if ($(".character-images [data-title='Chibi']").attr('aria-hidden') == 'false') initCharacterVideo($('.character-images'));
        $(".character-images [data-title='Chibi']").on("click", function(){initCharacterVideo($('.character-images'));}); 
    });
})

function initCharacterVideo(element) {
    var container = element.find('div.video');
    if (!container.attr('data-video-initialized')) {
        var video_src = container.attr('data-videosrc');
        container.html('<video autoplay loop playsinline><source src="'+video_src+'" type="video/webm" /></video>');
        container.on("click", function(){playToggle(container)});
        container.attr('data-video-initialized', true);
    }
}

function playToggle(element) {
    var video = element.find('video').get(0);
    if (video.paused) video.play();
    else video.pause();
}
/* Character video preview - end */


/* XP tables */
mw.loader.load( '/w/index.php?title=MediaWiki:XPtable.js&action=raw&ctype=text/javascript' );