MediaWiki:OverwatchAPI.js

/* OverwatchAPI by Luqgreg */ $(function{   if ($("div#owapi-main").length) {        console.log("OW-Api: Initializing...");		var decMark = ".";		var decSpaces = 2;		var errorMsg = "Error: Unknown error occurred! ";        $.getJSON("/api.php?action=query&meta=allmessages&ammessages=Custom-OverwatchAPI/DecimalMark|Custom-OverwatchAPI/DecimalSpaces|Custom-OverwatchAPI/Error&format=json", function(data) { var _data = data.query.allmessages[0]; if(_data['*'] !== undefined) decMark = _data['*']; _data = data.query.allmessages[1]; if(_data['*'] !== undefined) { var _decSpaces = parseInt(_data['*'], 10); if(!isNaN(_decSpaces)) decSpaces = _decSpaces; }           _data = data.query.allmessages[2]; if(_data['*'] !== undefined) errorMsg = _data['*']; });       var nick = $("div#owapi-main").data("battletag");        nick = nick.substring(0, nick.length - 5);		var tag = $("div#owapi-main").data("battletag");		tag = tag.substring(tag.length - 4);        var server = $("div#owapi-main").data("server");        var platform = $("div#owapi-main").data("platform");        if(server !== null || (server !== "eu" && server !== "na" && server !== "kr" )) server = "eu";		if(platform !== null || (platform !== "pc" && platform !== "xbl" && platform !== "psn" )) platform = "pc";

console.log("OW-Api: Downloading player data..."); $.getJSON("https://owapi.net/api/v3/u/" + nick + '-' + tag + "/blob?platform=" + platform, function(data) {			console.log("OW-Api: Parsing player data...");			var serverdata;			switch(server)			{				case "na":					serverdata = data.na;					break;				case "kr":					serverdata = data.kr;					break;				default:				case "eu":					serverdata = data.eu;					break;			}			var level_border = "";			if(serverdata.stats.competitive.overall_stats.prestige === 0 && serverdata.stats.competitive.overall_stats.level < 11) level_border = "0";			else level_border = Math.floor((serverdata.stats.competitive.overall_stats.prestige * 100 + serverdata.stats.competitive.overall_stats.level - 1)/10).toString;			var ach_query = "";			var ach_count        = 0, ach_completed         = 0, ach_completion         = 0,				ach_general_count = 0, ach_general_completed = 0, ach_general_completion = 0,				ach_offense_count = 0, ach_offense_completed = 0, ach_offense_completion = 0, ach_defense_count = 0, ach_defense_completed = 0, ach_defense_completion = 0, ach_tank_count   = 0, ach_tank_completed    = 0, ach_tank_completion    = 0, ach_support_count = 0, ach_support_completed = 0, ach_support_completion = 0, ach_maps_count   = 0, ach_maps_completed    = 0, ach_maps_completion    = 0, ach_special_count = 0, ach_special_completed = 0, ach_special_completion = 0; $.each(serverdata.achievements.general, function (key, value) {				ach_query += '|' + key + '=' + (value ? 1 : 0);				if(value === true) { ach_general_completed++; ach_completed++ }				ach_general_count++;				ach_count++;			}); $.each(serverdata.achievements.offense, function (key, value) {				ach_query += '|' + key + '=' + (value ? 1 : 0);				if(value === true) { ach_offense_completed++; ach_completed++ }				ach_offense_count++;				ach_count++;			}); $.each(serverdata.achievements.defense, function (key, value) {				ach_query += '|' + key + '=' + (value ? 1 : 0);				if(value === true) { ach_defense_completed++; ach_completed++ }				ach_defense_count++;				ach_count++;			}); $.each(serverdata.achievements.tank, function (key, value) {				ach_query += '|' + key + '=' + (value ? 1 : 0);				if(value === true) { ach_tank_completed++; ach_completed++ }				ach_tank_count++;				ach_count++;			}); $.each(serverdata.achievements.support, function (key, value) {				ach_query += '|' + key + '=' + (value ? 1 : 0);				if(value === true) { ach_support_completed++; ach_completed++ }				ach_support_count++;				ach_count++;			}); $.each(serverdata.achievements.maps, function (key, value) {				ach_query += '|' + key + '=' + (value ? 1 : 0);				if(value === true) { ach_maps_completed++; ach_completed++ }				ach_maps_count++;				ach_count++;			}); $.each(serverdata.achievements.special, function (key, value) {				ach_query += '|' + key + '=' + (value ? 1 : 0);				if(value === true) ach_special_completed++;				ach_special_count++;				ach_count++;			}); ach_completion        = Math.floor((ach_completed         / ach_count        ) * 100); ach_general_completion = Math.floor((ach_general_completed / ach_general_count) * 100); ach_offense_completion = Math.floor((ach_offense_completed / ach_offense_count) * 100); ach_defense_completion = Math.floor((ach_defense_completed / ach_defense_count) * 100); ach_tank_completion   = Math.floor((ach_tank_completed    / ach_tank_count   ) * 100); ach_support_completion = Math.floor((ach_support_completed / ach_support_count) * 100); ach_maps_completion   = Math.floor((ach_maps_completed    / ach_maps_count   ) * 100); ach_special_completion = Math.floor((ach_special_completed / ach_special_count) * 100); var hero_query = ""; $.each(serverdata.heroes.playtime.competitive, function (key, value) {				var hero_stats_cp = serverdata.heroes.stats.competitive[key];				var hero_stats_qp = serverdata.heroes.stats.quickplay[key];				var time_cp = 0, eliminations_cp     = 0, damage_cp      = 0,					time_qp = 0, eliminations_qp      = 0, damage_qp      = 0,					games_won_cp = 0, games_won_qp = 0;				if(hero_stats_cp !== undefined) {					time_cp              = defaultValue(hero_stats_cp.general_stats.time_played, 0);					eliminations_cp      = defaultValue(hero_stats_cp.general_stats.eliminations, 0);					damage_cp            = defaultValue(hero_stats_cp.general_stats.damage_done , 0);					games_won_cp         = defaultValue(hero_stats_cp.general_stats.games_won   , 0);				}				if(hero_stats_qp !== undefined) {					time_qp              = defaultValue(hero_stats_qp.general_stats.time_played , 0);					eliminations_qp      = defaultValue(hero_stats_qp.general_stats.eliminations, 0); damage_qp           = defaultValue(hero_stats_qp.general_stats.damage_done, 0); games_won_qp        = defaultValue(hero_stats_qp.general_stats.games_won  , 0); }				hero_query += '|' + key + "_time_cp="             + Math.floor(time_cp) + '|' + key + "_time_qp="             + Math.floor(time_qp) + '|' + key + "_time="                + Math.floor(time_cp + time_qp) + '|' + key + "_elim_cp="     + eliminations_cp + '|' + key + "_elim_qp="     + eliminations_qp + '|' + key + "_elim="        + (eliminations_cp + eliminations_qp) + '|' + key + "_dmg_cp="           + damage_cp + '|' + key + "_dmg_qp="           + damage_qp + '|' + key + "_dmg="              + (damage_cp + damage_qp) + '|' + key + "_wins_cp="        + games_won_cp + '|' + key + "_wins_qp="        + games_won_qp + '|' + key + "_wins="           + (games_won_cp + games_won_qp); });			var games_played_cp = 0, games_won_cp = 0, games_tied_cp = 0, games_lost_cp = 0,				games_played_qp = 0, games_won_qp = 0, games_tied_qp = 0, games_lost_qp = 0;				games_played_cp = defaultValue(serverdata.stats.competitive.overall_stats.games, 0);				games_won_cp   = defaultValue(serverdata.stats.competitive.overall_stats.wins  , 0);				games_tied_cp   = defaultValue(serverdata.stats.competitive.overall_stats.ties  , 0);				games_lost_cp   = defaultValue(serverdata.stats.competitive.overall_stats.losses, 0);				games_played_qp = defaultValue(serverdata.stats.quickplay.overall_stats.games   , 0);				games_won_qp    = defaultValue(serverdata.stats.quickplay.overall_stats.wins    , 0);				games_tied_qp   = defaultValue(serverdata.stats.quickplay.overall_stats.ties    , 0);				games_lost_qp   = defaultValue(serverdata.stats.quickplay.overall_stats.losses  , 0);			var query = "/api.php?action=parse&text=&format=json"; console.log("OW-Api: Parsing user template..."); $.getJSON(query, function(data) { 				var code = data.parse.text['*'];				$("div#owapi-main").html(code); 			}); }).error(function{ console.log("OW-Api: Unknown Error"); $("div#owapi-main").html(errorMsg); });   } });

function defaultValue(variable, defaultVal) { if(variable !== undefined && variable !== null) return variable; return defaultVal; }

function roundFloat(float, decSpaces) { if(decSpaces === 0) return Math.floor(float); return (Math.floor(float * Math.pow(10, decSpaces)) / Math.pow(10, decSpaces)); }