// 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 };