'use strict';
if ($('#player-table').length === 1) {
$(document).ready(initPlayersTable);
}
function initPlayersTable() {
const specificUid = getQueryString('uid');
const query = specificUid ? `?uid=${specificUid}` : '';
$('#player-table').DataTable({
ajax: url(`admin/player-data${query}`),
scrollY: ($('.content-wrapper').height() - $('.content-header').outerHeight()) * 0.7,
fnDrawCallback: () => $('[data-toggle="tooltip"]').tooltip(),
columnDefs: playersTableColumnDefs
}).on('xhr.dt', handleDataTablesAjaxError);
}
const playersTableColumnDefs = [
{
targets: 0,
data: 'pid',
width: '1%'
},
{
targets: 1,
data: 'uid',
render: (data, type, row) => `${data}`
},
{
targets: 2,
data: 'player_name'
},
{
targets: 3,
data: 'preference',
render: data => {
return `
`;
}
},
{
targets: 4,
searchable: false,
orderable: false,
render: (data, type, row) => ['steve', 'alex', 'cape'].reduce((html, type) => {
const currentTypeTid = row[`tid_${type}`];
const imageId = `${row.pid}-${currentTypeTid}`;
if (currentTypeTid === 0) {
return html + `
`;
} else {
return html + `
`;
}
}, '')
},
{
targets: 5,
data: 'last_modified'
},
{
targets: 6,
searchable: false,
orderable: false,
render: (data, type, row) => `
${trans('admin.deletePlayer')}`
}
];
async function changePreference() {
try {
const { errno, msg } = await fetch({
type: 'POST',
url: url('admin/players?action=preference'),
dataType: 'json',
data: {
pid: $(this).parent().parent().attr('id'),
preference: $(this).val()
}
});
errno === 0 ? toastr.success(msg) : toastr.warning(msg);
} catch (error) {
showAjaxError(error);
}
}
function changeTexture(pid, playerName) {
const dom = `
`;
showModal(dom, trans('admin.changePlayerTexture', { 'player': playerName }), 'default', {
callback: `ajaxChangeTexture(${pid})`
});
return;
}
async function ajaxChangeTexture(pid) {
// Remove interference of modal which is hide
$('.modal').each(function () {
if ($(this).css('display') === 'none') $(this).remove();
});
const model = $('#model').val();
const tid = $('#tid').val();
try {
const { errno, msg } = await fetch({
type: 'POST',
url: url('admin/players?action=texture'),
dataType: 'json',
data: { pid: pid, model: model, tid: tid }
});
if (errno === 0) {
$(`#${pid}-${model}`).attr('src', url(`preview/64/${tid}.png`));
$('.modal').modal('hide');
toastr.success(msg);
} else {
toastr.warning(msg);
}
} catch (error) {
showAjaxError(error);
}
}
async function changePlayerName(pid, oldName) {
const dom = $(`tr#${pid} > td:nth-child(3)`);
let newPlayerName;
try {
newPlayerName = await swal({
text: trans('admin.changePlayerNameNotice'),
input: 'text',
inputValue: oldName,
inputValidator: name => (new Promise((resolve, reject) => {
name ? resolve() : reject(trans('admin.emptyPlayerName'));
}))
});
} catch (error) {
return;
}
try {
const { errno, msg } = await fetch({
type: 'POST',
url: url('admin/players?action=name'),
dataType: 'json',
data: { pid: pid, name: newPlayerName }
});
if (errno === 0) {
dom.text(newPlayerName);
toastr.success(msg);
} else {
toastr.warning(msg);
}
} catch (error) {
showAjaxError(error);
}
}
function changeOwner(pid) {
const dom = $(`#${pid} > td:nth-child(2)`);
let owner = 0;
swal({
html: `${trans('admin.changePlayerOwner')}
`,
input: 'number',
inputValue: dom.text(),
showCancelButton: true
}).then(async uid => {
owner = uid;
try {
const { errno, msg } = await fetch({
type: 'POST',
url: url('admin/players?action=owner'),
dataType: 'json',
data: { pid, uid }
});
if (errno === 0) {
dom.text(owner);
toastr.success(msg);
} else {
toastr.warning(msg);
}
} catch (error) {
showAjaxError(error);
}
});
$('.swal2-input').on('input', debounce(showNicknameInSwal, 350));
}
async function showNicknameInSwal() {
const uid = $('.swal2-input').val();
if (isNaN(uid) || uid <= 0)
return;
try {
const { user } = await fetch({
type: 'GET',
url: url(`admin/user/${uid}`),
dataType: 'json'
});
$('.swal2-content').html(
trans('admin.changePlayerOwner') +
'' +
trans('admin.targetUser', { nickname: user.nickname }) +
''
);
} catch (error) {
$('.swal2-content').html(`
${trans('admin.changePlayerOwner')}
${trans('admin.noSuchUser')}
`);
}
}
async function deletePlayer(pid) {
try {
await swal({
text: trans('admin.deletePlayerNotice'),
type: 'warning',
showCancelButton: true
});
} catch (error) {
return;
}
try {
const { errno, msg } = await fetch({
type: 'POST',
url: url('admin/players?action=delete'),
dataType: 'json',
data: { pid: pid }
});
if (errno === 0) {
$(`tr#${pid}`).remove();
toastr.success(msg);
} else {
toastr.warning(msg);
}
} catch (error) {
showAjaxError(error);
}
}
if (process.env.NODE_ENV === 'test') {
module.exports = {
initPlayersTable,
changeOwner,
showNicknameInSwal,
deletePlayer,
changeTexture,
changePlayerName,
changePreference,
ajaxChangeTexture,
};
}