Statistics - Per day and per stream statistics (src/periodstats.js, one generic stat_day table), backfilled once from the older tables; Stream / Week / Month / Year / All time views, Community and Streams tabs. - Viewer count every 5 minutes and title / category history per stream (EventSub channel.update): viewer graph, games played (donut), games in the streams list. - Twitch sync (src/twitchsync.js): followers with follow dates, current subs, mods, VIPs, all-time bits, channel emotes, past streams from VODs. Twitch analytics CSV import (src/twitchcsv.js). - Public stats page on its own host (Settings -> Stats and data): no message text, no bots, !hideme / !showme opt-out. Viewer page: roles, follow age, sub status, bits, a menu for rarely used actions. - StreamElements watch-time importer removed (already imported minutes stay as a baseline). Dashboard - Settings and Actions with a left sidebar; each settings page saves only its own fields. Connections (Twitch, StreamElements token now set here, HUDFX), OBS links (hidden behind an eye toggle) with a link builder, Chat (sound picked from the media library), TTS, Stats and data, Backup (export / import the whole setup), Advanced. Time zone setting for the stats pages. - Actions -> Alerts: alert style, pictures, sounds, bits tiers and card colours, stored in the database (src/theme.js); an old file-based theme is imported once. Alert preview page (/actions/alerts/preview, tools/preview/export.js for a static copy). - Old /actions page removed; Actions = Redeems / Alerts / TTS voices / Media. Alerts - New plain Card style for everyone (public/alerts/card.js); Polaroid, Cassette and the TTS letter stay as optional looks. Redeem variations can be Card, Cassette, Sound only or Video. Repo - No channel data in git: the channel's art, stickers and sounds stay on disk (gitignored) and are picked in the dashboard; examples for the server config (apache.example.sh, .env.example). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
81 lines
4.6 KiB
JavaScript
81 lines
4.6 KiB
JavaScript
// Points (point system, values in docs/PLANS.md), calculated from the statistics
|
|
// tables, so changing a value here recalculates everyone. Also used by the Stats page; later by !points.
|
|
const db = require('./db');
|
|
|
|
const VALUE = {
|
|
watchTick: 5, // per 5-minute watch tick
|
|
chat: 10, // per points-eligible message (max 1 per 5 min, counted in chatlog.js)
|
|
checkin: 25, // first message of a stream
|
|
intro: 50, // first message ever on the channel
|
|
follow: 50, // once
|
|
bit: 1, // cheers, Power-ups, Custom Power-ups
|
|
per100ChannelPoints: 1,
|
|
subMonth: { 'Tier 1': 500, Prime: 500, 'Tier 2': 1000, 'Tier 3': 2500 },
|
|
tipPerUnit: 100, // per 1 EUR (or whatever currency the tip was in)
|
|
raid: 100, // flat, no reward for raid size
|
|
streakPer: 5, streakMax: 100,
|
|
};
|
|
|
|
// Levels by watch time (our ticks + the StreamElements baseline), not points: bits/subs do not buy a level.
|
|
const LEVELS = [
|
|
{ minHours: 0, name: 'Viewer' }, { minHours: 50, name: 'Regular' }, { minHours: 200, name: 'Loyal' }, { minHours: 1000, name: 'Veteran' },
|
|
];
|
|
const levelOf = watchMinutes => [...LEVELS].reverse().find(l => watchMinutes >= l.minHours * 60);
|
|
|
|
const has = t => !!db.raw().prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name = ?").get(t);
|
|
const all = (sql, ...a) => db.raw().prepare(sql).all(...a);
|
|
|
|
// -> Map key -> viewer { key, user_id, login, name, parts: {watch, chat, ...}, total, level }
|
|
// key = Twitch user id; viewers known only by name (tips, watch streaks) are merged by login when possible
|
|
function everyone() {
|
|
const v = new Map(), byLogin = new Map();
|
|
const get = (id, login, name) => {
|
|
let key = id ? String(id) : null;
|
|
if (!key && login && byLogin.has(login)) key = byLogin.get(login);
|
|
if (!key) key = `name:${login}`;
|
|
if (!v.has(key)) v.set(key, { key, user_id: id || null, login, name: name || login, parts: {}, total: 0 });
|
|
const x = v.get(key);
|
|
if (login) { byLogin.set(login, key); x.login = x.login || login; }
|
|
if (name) x.name = name;
|
|
return x;
|
|
};
|
|
const add = (x, part, pts) => { if (!pts) return; x.parts[part] = (x.parts[part] || 0) + pts; x.total += pts; };
|
|
|
|
if (has('chat_user')) for (const r of all('SELECT user_id, user_login, user_name, scored, intro FROM chat_user')) {
|
|
const x = get(r.user_id, r.user_login, r.user_name);
|
|
add(x, 'chat', (r.scored || 0) * VALUE.chat); add(x, 'first message', r.intro ? VALUE.intro : 0);
|
|
}
|
|
if (has('watch_user')) for (const r of all('SELECT user_id, user_login, user_name, ticks FROM watch_user')) {
|
|
const x = get(r.user_id, r.user_login, r.user_name);
|
|
x.watchMinutes = (x.watchMinutes || 0) + (r.ticks || 0) * 5;
|
|
add(x, 'watching', (r.ticks || 0) * VALUE.watchTick);
|
|
}
|
|
// StreamElements watch time from before our tracking (imported once, see watchtime.js): counted like our ticks
|
|
if (has('watch_import')) for (const r of all('SELECT user_login, minutes FROM watch_import')) {
|
|
const x = get(null, r.user_login, null);
|
|
x.importedMinutes = (x.importedMinutes || 0) + r.minutes;
|
|
x.watchMinutes = (x.watchMinutes || 0) + r.minutes;
|
|
add(x, 'watching (StreamElements)', Math.floor(r.minutes / 5) * VALUE.watchTick);
|
|
}
|
|
if (has('stream_checkin')) for (const r of all('SELECT user_id, COUNT(*) n FROM stream_checkin GROUP BY user_id')) {
|
|
add(get(r.user_id, null, null), 'check-ins', r.n * VALUE.checkin);
|
|
}
|
|
if (has('chat_action')) for (const r of all('SELECT * FROM chat_action ORDER BY user_key LIKE \'name:%\'')) {
|
|
const id = r.user_key.startsWith('name:') ? null : r.user_key;
|
|
const x = get(id, r.user_login, r.user_name);
|
|
switch (r.action) {
|
|
case 'follow': add(x, 'follow', VALUE.follow); break;
|
|
case 'bits': case 'power_up': case 'custom_power_up': add(x, 'bits', Math.round(r.amount * VALUE.bit)); break;
|
|
case 'channel_points': case 'auto_reward': add(x, 'channel points', Math.floor(r.amount / 100) * VALUE.per100ChannelPoints); break;
|
|
case 'sub': add(x, 'subs', Math.round(r.amount * (VALUE.subMonth[r.item] || VALUE.subMonth['Tier 1']))); break;
|
|
case 'gift_sub': add(x, 'gift subs', Math.round(r.amount * (VALUE.subMonth[r.item] || VALUE.subMonth['Tier 1']))); break;
|
|
case 'tip': add(x, 'tips', Math.round(r.amount * VALUE.tipPerUnit)); break;
|
|
case 'raid': add(x, 'raids', r.uses * VALUE.raid); break;
|
|
case 'watch_streak': add(x, 'watch streak', Math.min(VALUE.streakMax, r.amount * VALUE.streakPer)); break;
|
|
}
|
|
}
|
|
for (const x of v.values()) x.level = levelOf(x.watchMinutes || 0);
|
|
return v;
|
|
}
|
|
|
|
module.exports = { VALUE, LEVELS, levelOf, everyone };
|