İçeriğe geç
Tüm kodlar

Hipodrom

Yorumla oynanan TikTok LIVE at yarışı

77 satır · 3,4 KBHamİndir
import { connectServer } from './net.js';

const $ = (id) => document.getElementById(id);
const NAMES = ['aysu', 'mert', 'zeynep', 'emre', 'elif', 'burak', 'selin', 'can', 'deniz', 'ece', 'kaan', 'irem'];
const fixedRandom = NAMES[Math.floor(Math.random() * NAMES.length)] + Math.floor(Math.random() * 100);

function user() {
  let id;
  if ($('randUser').checked) id = NAMES[Math.floor(Math.random() * NAMES.length)] + Math.floor(Math.random() * 1000);
  else id = $('simUser').value.trim().replace(/^@/, '') || fixedRandom;
  return { id, name: id };
}

function log(text) {
  const el = document.createElement('div');
  el.textContent = new Date().toLocaleTimeString('tr') + '  ' + text;
  $('log').prepend(el);
  while ($('log').children.length > 50) $('log').lastChild.remove();
}

const net = connectServer((msg) => {
  if (msg.kind === 'status') {
    const tt = msg.tiktok || {};
    $('ttState').textContent = tt.user ? `@${tt.user}: ${tt.state}` : tt.state;
    $('ttDot').className = 'dot ' + (tt.state === 'bağlı' ? 'ok' : tt.user ? 'wait' : '');
    $('ttErr').textContent = tt.error || '';
    if (tt.user && !$('ttUser').value) $('ttUser').value = tt.user;
    $('ytState').textContent = msg.youtubeUrl ? '▶ YouTube çalıyor' : '♪ Oyunun kendi müziği çalıyor';
    if (msg.youtubeUrl && !$('ytUrl').value) $('ytUrl').value = msg.youtubeUrl;
    for (const [k, v] of Object.entries(msg.volumes || {})) {
      const el = $('vol-' + k);
      if (el && document.activeElement !== el) {
        el.value = Math.round(v * 100);
        $('vol-' + k + '-txt').textContent = el.value + '%';
      }
    }
    $('simDot').className = 'dot ' + (msg.sim?.running ? 'ok' : '');
    $('simState').textContent = msg.sim?.running ? `çalışıyor (${msg.sim.rate} yorum/sn)` : 'kapalı';
  } else if (msg.kind === 'event' && msg.event.type === 'chat') {
    log(`@${msg.event.user.name}: ${msg.event.text}`);
  }
});

const action = (name) => net.send({ kind: 'action', name });

$('sendChat').onclick = () => {
  const t = $('simText').value.trim();
  if (!t) return;
  net.send({ kind: 'inject', event: { type: 'chat', user: user(), text: t } });
};
$('simText').addEventListener('keydown', (e) => e.key === 'Enter' && $('sendChat').click());

$('rate').oninput = () => ($('rateTxt').textContent = $('rate').value);
$('simStart').onclick = () => net.send({ kind: 'sim', on: true, rate: Number($('rate').value) });
$('simStop').onclick = () => net.send({ kind: 'sim', on: false });
$('ttConnect').onclick = () => net.send({ kind: 'tiktok-connect', username: $('ttUser').value });
$('ttDisconnect').onclick = () => net.send({ kind: 'tiktok-disconnect' });
$('start').onclick = () => action('start');
$('cancel').onclick = () => action('cancel');
$('clearChamps').onclick = () => action('clear-champions');
$('reset').onclick = () => net.send({ kind: 'reset' });
$('reload').onclick = () => net.send({ kind: 'reload' });
$('musicNext').onclick = () => net.send({ kind: 'music-next' });

$('ytPlay').onclick = () => net.send({ kind: 'youtube', url: $('ytUrl').value });
$('ytUrl').addEventListener('keydown', (e) => e.key === 'Enter' && $('ytPlay').click());
$('ytOff').onclick = () => {
  $('ytUrl').value = '';
  net.send({ kind: 'youtube', url: '' });
};
for (const k of ['youtube', 'music', 'sfx', 'sonses']) {
  const el = $('vol-' + k);
  el.oninput = () => {
    $('vol-' + k + '-txt').textContent = el.value + '%';
    net.send({ kind: 'volume', key: k, value: Number(el.value) / 100 });
  };
}

Lisans bilgisi projenin LICENSE dosyasında.