MediaWiki:NakamaNetwork/NakamaNetwork.js

/****************************** * Nakama Network Integration * *    - by User:RoboCafaz    * ******************************/

// --- Team Search --- // Usage:  // Template:

window.nakama = window.nakama || { errorMsg: ' ' + ''   + ' An error occurred trying to load Nakama Network teams... '   + ' ',    footerMsg: ' ' + '' + ' Visit Nakama Network for more teams! '   + ' ',    loadingTemplate: ' ' + ''   + ' Loading Nakama Network teams... '   + ' ',    getShipImage: function (shipId) { return 'BlankUnit.png'; },   getShipLink: function (shipId) { return ''; },   getUnitImage: function (unitId) { if (unitId < 0) { return 'BlankUnit.png'; }       unitId = '' + unitId; while (unitId.length < 4) { unitId = '0' + unitId; }       return 'F' + unitId + '.png'; },   getUnitLink: function (unitId) { if (unitId < 0) { return ''; }       return 'http://optc-db.github.io/characters/#/view/' + unitId; },   processUnit: function (unit, args) { var unitId = unit.unitId; switch (unit.position) { case 0: args.FriendImage = nakama.getUnitImage(unitId); args.FriendLink = nakama.getUnitLink(unitId); break; case 1: args.CapImage = nakama.getUnitImage(unitId); args.CapLink = nakama.getUnitLink(unitId); break; case 2: args.Sub1Image = nakama.getUnitImage(unitId); args.Sub1Link = nakama.getUnitLink(unitId); break; case 3: args.Sub2Image = nakama.getUnitImage(unitId); args.Sub2Link = nakama.getUnitLink(unitId); break; case 4: args.Sub3Image = nakama.getUnitImage(unitId); args.Sub3Link = nakama.getUnitLink(unitId); break; case 5: args.Sub5Image = nakama.getUnitImage(unitId); args.Sub5Link = nakama.getUnitLink(unitId); break; }       return args; },   buildArgs: function (team) { var args = {}; args.Title = team.name; for (var i = 0; i < 6; i++) { var unit = $.grep(team.teamUnits, function (teamUnit) {               return teamUnit.position == i;            }); if (unit.length > 0) { unit = unit[0]; } else { unit = { position: i, unitId: -1 }; }           args = nakama.processUnit(unit, args); }       args.ShipImage = nakama.getShipImage(team.shipId); args.ShipLink = nakama.getShipLink(team.shipId); args.Tips = 'See full guide on + team.id + '/details Nakama Network'; return args; },   buildTemplate: function (args) { var templateText = ''; return templateText; },   expandTemplates: function (templateText, container) { var url = '/api.php' + '?action=parse' + '&prop=text' + '&format=json' + '&text=' + templateText; $.getJSON(url).done(function (result) {           container.html(result.parse.text['*']);        }).fail(function (error) {            nakama.reportFailure(container);        }); },   reportNothing: function (container) { container.html(nakama.footerMsg); },   reportFailure: function (container) { container.html(nakama.errorMsg); },   loadTeams: function  { var container = $(this); container.html(nakama.loadingTemplate); var stageId = container.attr('data-stage-id'); if (stageId) { var url = 'https://www.nakama.network/api/team/search' + '?page=1&pageSize=5&stageId=' + stageId + '&sortBy=Score'; $.getJSON(url).done(function (data) {               var html = '';                var args = $.map(data.results, nakama.buildArgs);                var templates = $.map(args, nakama.buildTemplate);                var templateText = encodeURIComponent(templates.join('\n'));                nakama.expandTemplates(templateText, container);            }).fail(function (error) {                nakama.reportFailure(container);            }); } else { nakama.reportNothing(container); }   },    process: function ($content) { if ($content) { $content.find('.nakama-teams').each(nakama.loadTeams); }   } };

importArticle({   type: 'style',    article: 'MediaWiki:NakamaNetwork/NakamaNetwork.css' }); mw.hook('wikipage.content').add(nakama.process);