LuminaWebsite/assets/js/contributions.js
Maaxxs fe1561f4f7 Raids: attendance list, automatic points on archive, reminder callback
Each raid gets an attendance list for raids.manage (present, absent, excused)
with quick actions, members added by hand and a "checked" mark with who and
when. The contribution form can now select a raid's present members for
points or its absent members for a deduction.

Raids can carry points for present members and a deduction for absent ones
(raid_settings). When a raid is archived - on the website or via the button
in Discord, detected on the next raid or contribution page view - the site
awards them once: by attendance if a list exists, otherwise to everyone in a
group without deduction, and not at all if points were already given for
that raid. Settings, attendance and awards follow a raid_id change when the
API re-posts a deleted message.

bot_api.php lets the bot ask which signed-up members have raids.manage when a
raid starts (shared key in config.php bot_callback.key); the bot then sends
them a DM with a link to the list. The bot side lives in the separate bot
repository (cogs/attendance.py).

Also fixes contribution_awards.raid_id being too small for Discord IDs (it
is widened automatically) and raid IDs losing precision in the contribution
form's JavaScript, which kept the raid preselection from ever matching.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 09:52:34 +02:00

156 lines
6.7 KiB
JavaScript

(function () {
'use strict';
function matches(element, term) {
return term === '' || (element.dataset.name || '').indexOf(term) !== -1;
}
// ---- Rangliste durchsuchen ----
var rankingSearch = document.getElementById('contribution-search');
var ranking = document.getElementById('contribution-ranking');
if (rankingSearch && ranking) {
var noMatch = document.getElementById('contribution-no-match');
rankingSearch.addEventListener('input', function () {
var term = rankingSearch.value.trim().toLowerCase();
var visible = 0;
ranking.querySelectorAll('.contribution-card').forEach(function (card) {
card.hidden = !matches(card, term);
if (!card.hidden) visible++;
});
if (noMatch) noMatch.hidden = visible > 0;
});
}
// ---- Formular "Punkte vergeben" ----
var form = document.getElementById('contribution-form');
if (!form) return;
var picks = Array.prototype.slice.call(form.querySelectorAll('.contribution-pick'));
var boxes = picks.map(function (pick) { return pick.querySelector('input[type="checkbox"]'); });
var counter = document.getElementById('picker-count');
var pickerSearch = document.getElementById('picker-search');
var submit = document.getElementById('contribution-submit');
var pointsInput = document.getElementById('points');
function updateCount() {
var count = boxes.filter(function (box) { return box.checked; }).length;
counter.textContent = count === 0 ? '(keins ausgewählt)' : '(' + count + ' ausgewählt)';
picks.forEach(function (pick, i) { pick.classList.toggle('checked', boxes[i].checked); });
var points = parseInt(pointsInput.value, 10);
var label = 'Punkte vergeben';
if (count > 0 && points) {
label = points > 0
? points + ' Punkte an ' + count + (count === 1 ? ' Mitglied' : ' Mitglieder') + ' vergeben'
: Math.abs(points) + ' Punkte bei ' + count + (count === 1 ? ' Mitglied' : ' Mitgliedern') + ' abziehen';
}
submit.textContent = label;
}
boxes.forEach(function (box) { box.addEventListener('change', updateCount); });
pointsInput.addEventListener('input', updateCount);
pickerSearch.addEventListener('input', function () {
var term = pickerSearch.value.trim().toLowerCase();
picks.forEach(function (pick) { pick.hidden = !matches(pick, term); });
});
// "Alle" / "Keine" wirken nur auf die gerade sichtbaren (gefilterten) Mitglieder.
function setVisible(checked) {
picks.forEach(function (pick, i) {
if (!pick.hidden) boxes[i].checked = checked;
});
updateCount();
}
document.getElementById('picker-all').addEventListener('click', function () { setVisible(true); });
document.getElementById('picker-none').addEventListener('click', function () { setVisible(false); });
// ---- Anlass und Raid ----
var category = document.getElementById('category');
var raidField = document.getElementById('raid-field');
var raidSelect = document.getElementById('raid_id');
var raidInfo = document.getElementById('raid-info');
var titleInput = document.getElementById('title');
var timeInput = document.getElementById('awarded_at');
var raidsEl = document.getElementById('contribution-raids');
var raids = raidsEl ? JSON.parse(raidsEl.textContent) : [];
var autoTitle = '';
function toggleRaidField() {
raidField.hidden = category.value !== 'raid';
if (raidField.hidden && raidInfo) raidInfo.hidden = true;
}
category.addEventListener('change', toggleRaidField);
toggleRaidField();
var raidMode = document.getElementById('raid-mode');
var currentRaid = null;
function selectUsers(ids) {
boxes.forEach(function (box) {
box.checked = ids.indexOf(parseInt(box.value, 10)) !== -1;
});
}
// mode: present = Anwesende mit Punkten, absent = Abwesende mit Abzug,
// participants = alle in einer Gruppe Angemeldeten (ohne Anwesenheitsliste).
function applyRaid(raid, mode) {
var ids = mode === 'present' ? raid.present : (mode === 'absent' ? raid.absent : raid.participants);
selectUsers(ids);
// Grund nur ueberschreiben, wenn nichts Eigenes eingetragen wurde.
if (titleInput.value === '' || titleInput.value === autoTitle) {
autoTitle = (mode === 'absent' ? 'Abwesend: ' : '') + raid.title + (raid.date ? ' (' + raid.date + ')' : '');
titleInput.value = autoTitle;
}
if (raid.time) timeInput.value = raid.time;
// Abwesenden werden Punkte abgezogen, allen anderen gutgeschrieben.
var points = parseInt(pointsInput.value, 10);
if (points && ((mode === 'absent' && points > 0) || (mode !== 'absent' && points < 0))) {
pointsInput.value = -points;
}
var info;
if (mode === 'present') {
info = ids.length + ' Anwesende ausgewählt (laut Anwesenheitsliste).';
} else if (mode === 'absent') {
info = ids.length + ' Abwesende ausgewählt - die Punkte werden abgezogen.';
} else {
info = ids.length + ' Teilnehmer ausgewählt (laut Anmeldung).';
if (raid.bench.length) info += ' ' + raid.bench.length + ' auf der Ersatzbank (nicht ausgewählt).';
}
if (!raid.attendance) {
info += ' Für diesen Raid wurde noch keine Anwesenheitsliste geführt.';
}
if (raid.withoutAccount.length) {
info += ' Ohne Website-Konto, daher keine Punkte möglich: ' + raid.withoutAccount.join(', ') + '.';
}
raidInfo.textContent = info;
raidInfo.hidden = false;
raidMode.querySelectorAll('[data-raid-mode]').forEach(function (button) {
button.classList.toggle('active', button.getAttribute('data-raid-mode') === mode);
});
updateCount();
}
if (raidSelect) {
raidSelect.addEventListener('change', function () {
currentRaid = raids.filter(function (r) { return r.id === raidSelect.value; })[0] || null;
raidMode.hidden = !(currentRaid && currentRaid.attendance);
if (!currentRaid) {
raidInfo.hidden = true;
return;
}
applyRaid(currentRaid, currentRaid.attendance ? 'present' : 'participants');
});
raidMode.querySelectorAll('[data-raid-mode]').forEach(function (button) {
button.addEventListener('click', function () {
if (currentRaid) applyRaid(currentRaid, button.getAttribute('data-raid-mode'));
});
});
}
updateCount();
})();