rework alert system, still not really happy though

This commit is contained in:
2025-07-27 22:36:57 +02:00
parent a340b004a0
commit ecd5909acf
3 changed files with 58 additions and 30 deletions

View File

@@ -1,28 +1,49 @@
import { alert } from "web/alerts/types"; import { alert } from "web/alerts/types";
import alerts from "./index"; import alerts from "./index";
function generateRandomCSSIdentifier() {
const firstChar = String.fromCharCode(97 + Math.floor(Math.random() * 26));
const secondChar = String.fromCharCode(97 + Math.floor(Math.random() * 26));
const randomChar = String.fromCharCode(97 + Math.floor(Math.random() * 26));
return firstChar + secondChar + randomChar + Math.floor(Math.random() * 1000);
};
class AlertManager { class AlertManager {
#busy: boolean; private busy: boolean;
#alertQueue: alert[]; private alertQueue: alert[];
private currentZIndex: number;
constructor() { constructor() {
this.#busy = false; this.busy = false;
this.#alertQueue = []; this.alertQueue = [];
this.currentZIndex = 1000;
}; };
private async playAlert(alert: alert) { private async playAlert(alert: alert) {
this.#busy = true; this.busy = true;
await alerts[alert.name](alert); const data = await alerts[alert.name](alert);
const div = data.alertDiv;
const alertId = generateRandomCSSIdentifier();
div.classList.add(alertId);
const nextAlert = this.#alertQueue.shift(); const removalScript = document.createElement('script');
if (!nextAlert) { this.#busy = false; return; }; // if .shift has no results, we done and return removalScript.textContent = `setTimeout(() => document.querySelectorAll(".${alertId}").forEach(elem => elem.remove()), ${data.duration})`;
div.appendChild(removalScript);
div.style.zIndex = this.currentZIndex.toString();
document.body.appendChild(div);
if (data.blocking) await new Promise(async resolve => setTimeout(resolve, data.duration));
const nextAlert = this.alertQueue.shift();
if (!nextAlert) { this.busy = false; this.currentZIndex = 1000; return; }; // if .shift has no results, we done and return
this.currentZIndex -= 1;
this.playAlert(nextAlert); this.playAlert(nextAlert);
}; };
async queueAlert(alert: alert) { async queueAlert(alert: alert) {
if (this.#busy) { this.#alertQueue.push(alert); return; }; if (this.busy) { this.alertQueue.push(alert); return; };
await this.playAlert(alert); await this.playAlert(alert);
}; };
}; };

View File

@@ -1,14 +1,19 @@
export function delay(time: number) { import { alert } from "web/alerts/types";
return new Promise(function(resolve) { import userBlast from "./userBlast";
setTimeout(resolve, time)
}); export type AlertRunner = {
duration: number;
alertDiv: HTMLDivElement;
blocking: boolean;
}; };
import userBlast from "./userBlast"; type AlertMap = {
[key: string]: (alert: alert) => Promise<AlertRunner>;
};
export default { export default {
'userBlast': userBlast, 'userBlast': userBlast,
'userExecute': userBlast, 'userExecute': userBlast,
'grenadeExplosion': userBlast, 'grenadeExplosion': userBlast,
'tntExplosion': userBlast 'tntExplosion': userBlast,
} } as AlertMap;

View File

@@ -1,18 +1,20 @@
import { userBlastAlert } from "web/alerts/types"; import { userBlastAlert } from "web/alerts/types";
import { delay } from "./index"; import { AlertRunner } from "./index";
export default async function execute(alert: userBlastAlert) { const duration = 10000;
export default async function execute(alert: userBlastAlert): Promise<AlertRunner> {
const parentDiv = document.createElement('div'); const parentDiv = document.createElement('div');
const textElement = document.createElement('span'); parentDiv.className = 'userBlastAlert';
textElement.textContent = `${alert.user} just blasted ${alert.target} for 60 seconds! Rip bozo!`; parentDiv.innerHTML = `
parentDiv.appendChild(textElement); <span>${alert.user} just blasted ${alert.target} for 60 seconds! Rip bozo!</span>
Object.assign(textElement.style, { <style>
position: 'fixed', .userBlastAlert {
top: '20px', position: fixed;
left: '20px', top: 20px;
zIndex: 1000 left: 20px;
}); }
document.querySelector("#app").appendChild(parentDiv); </style>
await delay(10000); `;
parentDiv.remove(); return { blocking: false, duration, alertDiv: parentDiv };
}; };