MediaWiki:Common.js: Difference between revisions
MediaWiki interface page
More actions
Player infobox |
Player infobox |
||
| Line 267: | Line 267: | ||
function formatRole(role) { | function formatRole(role) { | ||
if (role == null) { | |||
if ( | |||
return ''; | return ''; | ||
} | } | ||
role = String(role).trim(); | |||
if (!role) { | |||
return ''; | |||
} | |||
var lower = role.toLowerCase(); | |||
var names = { | var names = { | ||
owner: 'Owner', | owner: 'Owner', | ||
moderator: 'Moderator', | moderator: 'Moderator', | ||
admin: 'Admin' | admin: 'Admin', | ||
scout: 'Scout' | |||
}; | }; | ||
if (names[ | if (names[lower]) { | ||
return names[ | return names[lower]; | ||
} | } | ||
return | return lower.replace(/[_-]+/g, ' ').replace(/\b\w/g, function (ch) { | ||
return ch.toUpperCase(); | |||
}); | |||
} | } | ||
Revision as of 22:03, 27 August 2026
function abCompactCount(n) {
var num = Number(n) || 0;
if (num >= 1e9) {
return (num / 1e9).toFixed(1).replace(/\.0$/, '') + 'B';
}
if (num >= 1e6) {
return (num / 1e6).toFixed(1).replace(/\.0$/, '') + 'M';
}
if (num >= 1e3) {
return (num / 1e3).toFixed(1).replace(/\.0$/, '') + 'K';
}
return String(Math.floor(num));
}
$(function () {
var levelId = mw.config.get('abLevelId');
var $box = $('.ab-level-infobox');
if (!levelId || !$box.length) {
return;
}
var defaultThumbs = ['82374078680299', '105249369413891', '104825146904235'];
function thumbAssetId(level) {
if (!level || level.locked) {
return defaultThumbs[0];
}
var raw = String(level.thumbnail || '').replace(/^rbxassetid:\/\//i, '').replace(/[^0-9]/g, '');
if (raw && raw !== '0') {
return raw;
}
return defaultThumbs[Math.abs(Number(level.id) || Number(levelId) || 0) % defaultThumbs.length];
}
function setThumb(assetId) {
var $thumb = $box.find('.ab-level-thumb');
if (!$thumb.length || !assetId) {
return;
}
fetch(
'https://adventure-builder.net/api/roblox-proxy?service=thumbnails&assetIds=' +
encodeURIComponent(assetId) +
'&size=' +
encodeURIComponent('420x420')
)
.then(function (res) {
return res.ok ? res.json() : null;
})
.then(function (data) {
var item = data && data.data && data.data[0];
if (!item || item.state !== 'Completed' || !item.imageUrl) {
return;
}
$thumb.attr('src', item.imageUrl).removeClass('ab-level-thumb--placeholder');
})
.catch(function () {});
}
function atUsername(name) {
name = String(name || '')
.replace(/^@+/, '')
.trim();
return name ? '@' + name : '';
}
function setCreator(level) {
var $el = $box.find('.ab-level-creator');
if (!$el.length || !level) {
return;
}
var creatorId = String(level.creator || '').replace(/[^0-9]/g, '');
function apply(name) {
var label = atUsername(name);
if (!label) {
return;
}
var $link = $el.find('a');
if ($link.length) {
$link.text(label);
return;
}
if (!creatorId) {
$el.text(label);
return;
}
$('<a>')
.attr('href', 'https://adventure-builder.net/players/' + encodeURIComponent(creatorId))
.attr('target', '_blank')
.attr('rel', 'noopener noreferrer')
.text(label)
.appendTo($el.empty());
}
var seeded = level.creator_name || level.creatorName;
if (seeded) {
apply(seeded);
return;
}
if (!creatorId) {
return;
}
fetch(
'https://adventure-builder.net/api/roblox-proxy?service=username&userId=' +
encodeURIComponent(creatorId)
)
.then(function (res) {
return res.ok ? res.json() : null;
})
.then(function (data) {
apply(data && (data.name || data.displayName));
})
.catch(function () {});
}
fetch('https://adventure-builder.net/api/v1/levels/' + encodeURIComponent(levelId))
.then(function (res) {
return res.ok ? res.json() : null;
})
.then(function (data) {
var level = data && (data.level || data);
if (!level || level.locked || !level.id) {
setThumb(defaultThumbs[0]);
return;
}
if (level.title) {
$box.find('.pi-title').text(level.title);
}
setCreator(level);
[
['plays', level.plays],
['clears', level.clears],
['favorites', level.favorites]
].forEach(function (pair) {
$box.find('.ab-level-stat[data-stat="' + pair[0] + '"]').text(
abCompactCount(pair[1])
);
});
$box.find('.ab-level-stat[data-stat="featured"]').text(
level.featured ? 'Yes' : 'No'
);
setThumb(thumbAssetId(level));
})
.catch(function () {
if ($box.find('.ab-level-thumb--placeholder').length) {
setThumb(defaultThumbs[Math.abs(Number(levelId) || 0) % defaultThumbs.length]);
}
});
});
$(function () {
var userId = mw.config.get('abRobloxUserId');
if (!userId) {
return;
}
var $img = $('.profile-avatar-image');
if ($img.length) {
var fallback = $img.attr('src');
$img.on('error', function () {
$img.attr('src', fallback);
});
$img.attr(
'src',
'https://adventure-builder.net/api/roblox-proxy?service=profile-picture&userId=' +
encodeURIComponent(userId)
);
}
var $stats = $('.profile-header-statistics');
if (!$stats.length) {
return;
}
fetch(
'https://adventure-builder.net/api/v1/profiles/' + encodeURIComponent(userId)
)
.then(function (res) {
return res.ok ? res.json() : null;
})
.then(function (profile) {
if (!profile) {
return;
}
[
[profile.levels_count, 'Levels'],
[profile.clears, 'Clears'],
[profile.followers, 'Followers']
].forEach(function (pair) {
$('<li>')
.addClass('ab-central-stat')
.text(abCompactCount(pair[0]) + ' ' + pair[1])
.appendTo($stats);
});
})
.catch(function () {});
});
$(function () {
var playerId = mw.config.get('abPlayerId');
var $box = $('.ab-player-infobox');
if (!playerId || !$box.length) {
return;
}
function atUsername(name) {
name = String(name || '')
.replace(/^@+/, '')
.trim();
return name ? '@' + name : '';
}
function setPfp() {
var $thumb = $box.find('.ab-player-thumb');
if (!$thumb.length) {
return;
}
$thumb
.on('error', function () {
$thumb.css('visibility', 'hidden');
})
.attr(
'src',
'https://adventure-builder.net/api/roblox-proxy?service=profile-picture&userId=' +
encodeURIComponent(playerId)
)
.removeClass('ab-player-thumb--placeholder');
}
fetch(
'https://adventure-builder.net/api/roblox-proxy?service=username&userId=' +
encodeURIComponent(playerId)
)
.then(function (res) {
return res.ok ? res.json() : null;
})
.then(function (data) {
var label = atUsername(data && (data.name || data.displayName));
if (label) {
$box.find('.ab-player-title').text(label);
$box.find('.ab-player-thumb').attr('alt', label);
}
})
.catch(function () {});
fetch('https://adventure-builder.net/api/v1/profiles/' + encodeURIComponent(playerId))
.then(function (res) {
return res.ok ? res.json() : null;
})
.then(function (data) {
var profile = data && (data.profile || data);
if (!profile) {
return;
}
[
['levels', profile.levels_count],
['clears', profile.clears],
['followers', profile.followers]
].forEach(function (pair) {
$box.find('.ab-level-stat[data-stat="' + pair[0] + '"]').text(
abCompactCount(pair[1])
);
});
setRole(profile.role);
})
.catch(function () {});
function formatRole(role) {
if (role == null) {
return '';
}
role = String(role).trim();
if (!role) {
return '';
}
var lower = role.toLowerCase();
var names = {
owner: 'Owner',
moderator: 'Moderator',
admin: 'Admin',
scout: 'Scout'
};
if (names[lower]) {
return names[lower];
}
return lower.replace(/[_-]+/g, ' ').replace(/\b\w/g, function (ch) {
return ch.toUpperCase();
});
}
function setRole(role) {
var label = formatRole(role);
var $row = $box.find('.ab-level-stat[data-stat="role"]').closest('.pi-data');
if (!label) {
$row.remove();
return;
}
if ($row.length) {
$box.find('.ab-level-stat[data-stat="role"]').text(label);
return;
}
var $nav = $box.find('.pi-navigation');
var $newRow = $(
'<div class="pi-item pi-data pi-item-spacing pi-border-color">' +
'<div class="pi-data-label">Role</div>' +
'<div class="pi-data-value ab-level-stat" data-stat="role"></div>' +
'</div>'
);
$newRow.find('[data-stat="role"]').text(label);
if ($nav.length) {
$nav.before($newRow);
} else {
$box.append($newRow);
}
}
setPfp();
});