LuminaWebsite/assets/js/raids.js
Maaxxs fea0cc3a91 Overhaul raid signup/management to match the Discord bot's full feature set
Signup is now a single "Anmelden" button that opens a class-icon
picker + optional group choice. A new raid_wish_is_sensible() mirrors
the bot's exact role-cap algorithm (Tank/Heal 1, DPS 3 per 5-player
group, ROLE_MAPPING copied from teamplaner.py) to decide server-side
whether the requested group is honored or the request falls back to
the bot's own auto-assignment - with a message telling the member to
ping the raid lead if they wanted something specific. Verified the
cap logic with 7 synthetic cases (own-signup exclusion, full group,
role caps, bench always allowed) before wiring it up.

Admins get a per-raid edit-mode toggle (pencil icon) that reveals:
inline raid editing (title/date/requirements/gearscore/groups/slots/
group locks) via PATCH, a premade-invite mini-form via the new
premades endpoint, and native HTML5 drag-and-drop to move a signup
between groups/bench via PATCH .../signup/{user_id}.

New pieces: includes/raids_api.php gained move_signup(), update_raid(),
invite_premade(), fetch_premades(), fetch_raid_by_id() and the wish-
sensibility check; four new JSON action endpoints (raid_signup_action,
raid_move_action, raid_edit_action, raid_premade_invite_action) reusing
the session CSRF token via a new verify_csrf_token()/read_json_body()/
json_response() trio in functions.php; assets/js/raids.js drives the
modal, edit-mode, and drag-and-drop with plain fetch() (no framework).

Read-heavy parts (page render, class icons, signup-wish logic) verified
against real data/live API. Edit, move and premade-invite are NOT
tested against the live API yet since they'd mutate the real raid or
send real Discord DMs to guild members - flagging this for a
supervised live check before considering it fully verified.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-18 18:36:04 +02:00

235 lines
9.1 KiB
JavaScript

(function () {
'use strict';
var csrfToken = (document.querySelector('meta[name="csrf-token"]') || {}).content || '';
function postJson(url, data) {
data.csrf_token = csrfToken;
return fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
}).then(function (response) {
return response.json();
});
}
// ---- Anmelde-Popup ----
var modal = document.getElementById('raid-signup-modal');
var groupSelect = document.getElementById('raid-signup-group');
var feedbackEl = document.getElementById('raid-signup-feedback');
var confirmBtn = document.getElementById('raid-signup-confirm');
var currentRaidId = null;
var selectedClass = null;
function openSignupModal(card) {
if (!modal) return;
currentRaidId = card.getAttribute('data-raid-id');
selectedClass = card.getAttribute('data-my-class') || null;
var groups = [];
try {
groups = JSON.parse(card.getAttribute('data-groups') || '[]');
} catch (e) {
groups = [];
}
var myGroup = card.getAttribute('data-my-group') || '';
groupSelect.innerHTML = '<option value="">Automatisch zuteilen</option>';
groups.forEach(function (name, index) {
var opt = document.createElement('option');
opt.value = String(index + 1);
opt.textContent = name;
if (String(index + 1) === myGroup) opt.selected = true;
groupSelect.appendChild(opt);
});
var benchOpt = document.createElement('option');
benchOpt.value = '0';
benchOpt.textContent = 'Ersatzbank';
if (myGroup === '0') benchOpt.selected = true;
groupSelect.appendChild(benchOpt);
modal.querySelectorAll('.class-picker-option').forEach(function (btn) {
btn.classList.toggle('selected', btn.getAttribute('data-class') === selectedClass);
});
feedbackEl.textContent = '';
feedbackEl.className = 'raid-modal-feedback';
modal.hidden = false;
}
function closeSignupModal() {
if (modal) modal.hidden = true;
}
document.querySelectorAll('.raid-open-signup').forEach(function (btn) {
btn.addEventListener('click', function () {
var card = btn.closest('.raid-card');
if (card) openSignupModal(card);
});
});
if (modal) {
modal.querySelector('.modal-close').addEventListener('click', closeSignupModal);
modal.addEventListener('click', function (e) {
if (e.target === modal) closeSignupModal();
});
modal.querySelectorAll('.class-picker-option').forEach(function (btn) {
btn.addEventListener('click', function () {
selectedClass = btn.getAttribute('data-class');
modal.querySelectorAll('.class-picker-option').forEach(function (b) {
b.classList.toggle('selected', b === btn);
});
});
});
confirmBtn.addEventListener('click', function () {
if (!selectedClass) {
feedbackEl.textContent = 'Bitte zuerst eine Klasse auswählen.';
feedbackEl.className = 'raid-modal-feedback error';
return;
}
confirmBtn.disabled = true;
feedbackEl.textContent = 'Wird gesendet …';
feedbackEl.className = 'raid-modal-feedback';
postJson('/raid_signup_action.php', {
raid_id: currentRaidId,
class: selectedClass,
group: groupSelect.value === '' ? null : parseInt(groupSelect.value, 10)
}).then(function (result) {
feedbackEl.textContent = result.message || '';
feedbackEl.className = 'raid-modal-feedback ' + (result.status === 'success' ? 'success' : 'error');
if (result.status === 'success') {
setTimeout(function () { window.location.reload(); }, 1400);
} else {
confirmBtn.disabled = false;
}
}).catch(function () {
feedbackEl.textContent = 'Verbindung fehlgeschlagen. Bitte erneut versuchen.';
feedbackEl.className = 'raid-modal-feedback error';
confirmBtn.disabled = false;
});
});
}
// ---- Editor-Modus ----
document.querySelectorAll('.raid-edit-toggle').forEach(function (btn) {
btn.addEventListener('click', function () {
var card = btn.closest('.raid-card');
if (card) card.classList.toggle('editing');
});
});
// ---- Raid bearbeiten (Formular) ----
document.querySelectorAll('.raid-edit-form').forEach(function (form) {
form.addEventListener('submit', function (e) {
e.preventDefault();
var feedback = form.querySelector('.raid-edit-feedback');
var lockedGroups = Array.prototype.slice.call(form.querySelectorAll('input[name="locked_groups[]"]:checked'))
.map(function (cb) { return parseInt(cb.value, 10); });
feedback.textContent = 'Wird gespeichert …';
postJson('/raid_edit_action.php', {
raid_id: form.getAttribute('data-raid-id'),
title: form.title.value,
date_raw: form.date_raw.value,
requirements: form.requirements.value,
gearscore: form.gearscore.value,
group_names: form.group_names.value,
max_slots: form.max_slots.value,
locked_groups: lockedGroups
}).then(function (result) {
feedback.textContent = result.message || '';
if (result.status === 'success') {
setTimeout(function () { window.location.reload(); }, 900);
}
}).catch(function () {
feedback.textContent = 'Verbindung fehlgeschlagen.';
});
});
});
// ---- Premade einladen ----
document.querySelectorAll('.raid-premade-form').forEach(function (form) {
form.addEventListener('submit', function (e) {
e.preventDefault();
var feedback = form.querySelector('.raid-premade-feedback');
feedback.textContent = 'Wird eingeladen …';
postJson('/raid_premade_invite_action.php', {
raid_id: form.getAttribute('data-raid-id'),
premade_id: form.premade_id.value,
group: form.group.value
}).then(function (result) {
feedback.textContent = result.message || '';
if (result.status === 'success') {
setTimeout(function () { window.location.reload(); }, 1200);
}
}).catch(function () {
feedback.textContent = 'Verbindung fehlgeschlagen.';
});
});
});
// ---- Drag & Drop (nur im Editor-Modus) ----
document.querySelectorAll('.raid-signup-item').forEach(function (item) {
item.addEventListener('dragstart', function (e) {
var card = item.closest('.raid-card');
if (!card || !card.classList.contains('editing')) {
e.preventDefault();
return;
}
e.dataTransfer.setData('text/plain', JSON.stringify({
raidId: card.getAttribute('data-raid-id'),
userId: item.getAttribute('data-user-id')
}));
item.classList.add('dragging');
});
item.addEventListener('dragend', function () {
item.classList.remove('dragging');
});
});
document.querySelectorAll('.raid-drop-zone').forEach(function (zone) {
zone.addEventListener('dragover', function (e) {
var card = zone.closest('.raid-card');
if (!card || !card.classList.contains('editing')) return;
e.preventDefault();
zone.classList.add('drag-over');
});
zone.addEventListener('dragleave', function () {
zone.classList.remove('drag-over');
});
zone.addEventListener('drop', function (e) {
e.preventDefault();
zone.classList.remove('drag-over');
var card = zone.closest('.raid-card');
if (!card || !card.classList.contains('editing')) return;
var raw = e.dataTransfer.getData('text/plain');
if (!raw) return;
var payload;
try {
payload = JSON.parse(raw);
} catch (err) {
return;
}
postJson('/raid_move_action.php', {
raid_id: payload.raidId,
user_id: payload.userId,
group: zone.getAttribute('data-group')
}).then(function (result) {
if (result.status === 'success') {
window.location.reload();
} else {
alert(result.message || 'Verschieben fehlgeschlagen.');
}
}).catch(function () {
alert('Verbindung fehlgeschlagen.');
});
});
});
})();