Repository navigation
Expand file tree
/
Copy pathshared.js
More file actions
105 lines (90 loc) · 3.67 KB
/
Copy pathshared.js
File metadata and controls
105 lines (90 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Shared across site.js and overlay.js
const GOON_IMAGES = [
'images/knight.png',
'images/bigpipe.png',
'images/birdseye.png',
];
const POLL_INTERVAL_MS = 360_000; // 6 minutes — tarkov.dev caches data for 5 min
// API uses 'pve' or 'regular' (PvP). 'pvp' is a legacy alias.
const _resolveMode = raw => {
if (raw === 'pve') return 'pve';
if (raw === 'regular' || raw === 'pvp') return 'regular';
return 'pve';
};
// Guaranteed no consecutive repeat
let lastImage = null;
function pickRandomImage() {
const available = GOON_IMAGES.filter(img => img !== lastImage);
lastImage = available[Math.floor(Math.random() * available.length)];
return lastImage;
}
// API returns timestamp in milliseconds
function timeAgo(timestamp) {
const diffMs = Date.now() - parseInt(timestamp, 10);
const diffMin = Math.floor(diffMs / 60_000);
if (diffMin < 1) return 'Just now';
if (diffMin < 60) return `${diffMin} min ago`;
const diffHr = Math.floor(diffMin / 60);
if (diffHr < 24) return `${diffHr} hr ago`;
const diffDay = Math.floor(diffHr / 24);
return `${diffDay} day${diffDay > 1 ? 's' : ''} ago`;
}
function stalenessClass(timestamp) {
const diffMin = (Date.now() - parseInt(timestamp, 10)) / 60_000;
if (diffMin < 30) return 'fresh';
if (diffMin < 120) return 'warning';
return 'stale';
}
// Tarkov clock runs at 7× real speed. Two server pools 12 hrs apart.
// Using full Unix timestamp × 7 approach (matches tarkov.dev).
function getTarkovTimes() {
const nowMs = Date.now();
const MOSCOW_OFFSET_MS = 3 * 3_600_000; // UTC+3 — game epoch anchored to Moscow
const leftMs = (nowMs * 7 + MOSCOW_OFFSET_MS) % 86_400_000;
const rightMs = (nowMs * 7 + MOSCOW_OFFSET_MS + 43_200_000) % 86_400_000;
function fmt(ms) {
const h = Math.floor(ms / 3_600_000);
const m = Math.floor((ms % 3_600_000) / 60_000);
const s = Math.floor((ms % 60_000) / 1_000);
const msec = Math.floor(ms % 1_000);
return (
`${String(h).padStart(2, '0')}:${String(m).padStart(2, '0')}:` +
`${String(s).padStart(2, '0')}.${String(msec).padStart(3, '0')}`
);
}
return { left: fmt(leftMs), right: fmt(rightMs) };
}
function updateTarkovClock() {
const { left, right } = getTarkovTimes();
document.getElementById('tarkov-left').textContent = left;
document.getElementById('tarkov-right').textContent = right;
}
// tarkov.dev retired the GraphQL API — /maps is now the only source for
// goon reports, no dedicated endpoint exists (see json.tarkov.dev/endpoints).
// map.name is a translation key, not a display string — resolve it against
// the endpoint's translation dictionary (fetched from the same path with
// _en appended, per tarkov.dev's documented translation convention).
async function fetchGoonReports(mode) {
const [mapsRes, translationsRes] = await Promise.all([
fetch(`https://json.tarkov.dev/${mode}/maps`),
fetch(`https://json.tarkov.dev/${mode}/maps_en`),
]);
if (!mapsRes.ok) throw new Error(`HTTP ${mapsRes.status}`);
if (!translationsRes.ok) throw new Error(`HTTP ${translationsRes.status}`);
const { data: mapsData } = await mapsRes.json();
const { data: translations } = await translationsRes.json();
const { goonReports, maps } = mapsData ?? {};
if (!goonReports || !maps || !translations) throw new Error('Malformed response');
return goonReports
.map(({ map: mapId, timestamp }) => {
const map = maps[mapId];
const mapName = map && (translations[map.name] || map.normalizedName) || 'Unknown';
return {
timestamp,
mapName,
wiki: map?.wiki ?? null,
raidDuration: map?.raidDuration ?? null,
};
})
.sort((a, b) => Number(b.timestamp) - Number(a.timestamp));
}