mirror of
https://github.com/db9979/tidefront.git
synced 2026-12-06 04:44:09 +01:00
- apps/web: React, Vite, Tailwind, TanStack Query, PixiJS-Hexkarte; Basis, Karte, Truppen, Mission planen, Kampfbericht, Börse; untere Navigation mit fünf Zielen; WebSocket-Aktualisierung; Countdowns mit Server-Zeitversatz; Manifest und Service Worker - Dockerfile: ein Image für api, worker, migrate, Oberfläche im api-Container - docker-compose.yml: api, worker, migrate, postgres, valkey, backup mit Healthchecks, ein Port (8080) - scripts/backup.sh: tägliche Sicherung, Wiederherstellung und Probe-Wiederherstellung - README mit Startanleitung, Bauauftrag mit Stand 2
44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
import { useEffect, useState } from "react";
|
||
|
||
let offsetMs = 0;
|
||
|
||
/** Offset = serverNow − Date.now() beim Empfang. */
|
||
export function noteServerTime(serverNow: number): void {
|
||
offsetMs = serverNow - Date.now();
|
||
}
|
||
|
||
export function serverNow(): number {
|
||
return Date.now() + offsetMs;
|
||
}
|
||
|
||
/** Tick einmal je Sekunde; liefert die aktuelle Serverzeit. */
|
||
export function useNow(intervalMs = 1000): number {
|
||
const [now, setNow] = useState(serverNow());
|
||
useEffect(() => {
|
||
const t = setInterval(() => setNow(serverNow()), intervalMs);
|
||
return () => clearInterval(t);
|
||
}, [intervalMs]);
|
||
return now;
|
||
}
|
||
|
||
export function fmtDuration(ms: number): string {
|
||
if (ms <= 0) return "gleich";
|
||
const s = Math.ceil(ms / 1000);
|
||
const d = Math.floor(s / 86400);
|
||
const h = Math.floor((s % 86400) / 3600);
|
||
const m = Math.floor((s % 3600) / 60);
|
||
const sec = s % 60;
|
||
if (d > 0) return `${d} T ${h} h`;
|
||
if (h > 0) return `${h} h ${String(m).padStart(2, "0")} min`;
|
||
if (m > 0) return `${m}:${String(sec).padStart(2, "0")} min`;
|
||
return `${sec} s`;
|
||
}
|
||
|
||
export function fmtSeconds(sec: number): string {
|
||
return fmtDuration(sec * 1000);
|
||
}
|
||
|
||
export function fmtTime(ms: number): string {
|
||
return new Date(ms - offsetMs).toLocaleString("de-DE", { day: "2-digit", month: "2-digit", hour: "2-digit", minute: "2-digit" });
|
||
}
|