-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
911 lines (751 loc) · 31.6 KB
/
script.js
File metadata and controls
911 lines (751 loc) · 31.6 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
// script.js
// Основной скрипт EmoteWall
(() => {
// Получаем конфиг
const cfg = window.EmoteWallConfig;
// Префикс для логов
const LOG_PREFIX = "[EmoteWall]";
// Утилиты для логирования
const log = (...args) => {
console.log(LOG_PREFIX, ...args);
if (cfg.debugLog) addDebugLog(...args);
};
const warn = (...args) => {
console.warn(LOG_PREFIX, ...args);
if (cfg.debugLog) addDebugLog('⚠️', ...args);
};
const error = (...args) => {
console.error(LOG_PREFIX, ...args);
if (cfg.debugLog) addDebugLog('❌', ...args);
};
const info = (...args) => {
console.info(LOG_PREFIX, ...args);
if (cfg.debugLog) addDebugLog('ℹ️', ...args);
};
// Тестовые эмодзи для каждой платформы
const TEST_EMOTES = {
'7tv': ['peepoHappy', 'EZ', 'PartyParrot'],
'bttv': ['FeelsBadMan', 'bttvNice', ':tf:'],
'ffz': ['ZreknarF', 'LaterSooner', 'BeanieHipster'],
'twitch': ['4Head', 'Kappa', 'SMOrc']
};
// Глобальные переменные
const emoteWall = document.getElementById('emote-wall');
const statsPanel = document.getElementById('stats-panel');
const testPanel = document.getElementById('test-panel');
const loadingIndicator = document.getElementById('loading');
const loadingStatus = document.getElementById('loading-status');
const debugLogContainer = document.getElementById('debug-log-container');
let activeEmotes = new Map(); // Map для активных эмодзи (id -> элемент)
let lastSpawnTime = 0; // Время последнего появления
let lastEmoteName = null; // Последнее показанное эмодзи
let emoteCombo = 0; // Счетчик комбо для текущего эмодзи
let emotesLoaded = 0; // Загруженные эмодзи
// Коллекции для разных платформ
let chatEmotes = new Map(); // 7TV канальные
let globalEmotes = new Map(); // 7TV глобальные
let bttvEmotes = new Map(); // BTTV эмодзи
let ffzEmotes = new Map(); // FFZ эмодзи
// Для спам-фильтра
let lastEmoteTimes = new Map(); // Время последнего показа каждого эмодзи
// Для статистики FPS
let lastFrameTime = 0;
let frameCount = 0;
let fps = 0;
// Для физики
let physicsEmotes = new Map(); // Эмодзи с физикой (id -> {element, vx, vy, animationType, rotation})
// Для тестового режима
let testInterval = null;
let testEmotesPool = []; // Пул эмодзи для тестового режима
let collectedTestEmotes = new Set(); // Уже собранные эмодзи для тестового режима
// === Функция для нормализации весов анимаций ===
function normalizeAnimationWeights() {
const weights = cfg.animationWeights || { float: 0, physics: 0, rain: 0 };
// Убедимся, что все типы анимаций имеют значения
const normalized = {
float: weights.float || 0,
physics: weights.physics || 0,
rain: weights.rain || 0
};
// Проверяем, есть ли хотя бы одна анимация с ненулевым весом
const total = normalized.float + normalized.physics + normalized.rain;
if (total === 0) {
warn("Все веса анимаций равны 0, используется анимация по умолчанию (float)");
normalized.float = 1;
}
return normalized;
}
// === Функция выбора типа анимации на основе весов ===
function selectAnimationType() {
const weights = normalizeAnimationWeights();
// Считаем суммарный вес
const totalWeight = weights.float + weights.physics + weights.rain;
// Если сумма весов 0, возвращаем null (без анимации)
if (totalWeight === 0) return null;
// Генерируем случайное число от 0 до totalWeight
const random = Math.random() * totalWeight;
// Выбираем анимацию на основе весов
let accumulated = 0;
// Проверяем float
accumulated += weights.float;
if (random <= accumulated) return 'float';
// Проверяем physics
accumulated += weights.physics;
if (random <= accumulated) return 'physics';
// Иначе rain
return 'rain';
}
// === Функция для показа дебаг-логов списком ===
function addDebugLog(...args) {
if (!cfg.debugLog || !debugLogContainer) return;
// Создаем элемент лога
const logElement = document.createElement('div');
logElement.className = 'debug-log';
// Форматируем текст
const logText = args.map(arg =>
typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
).join(' ');
// Добавляем временную метку
const timestamp = new Date().toLocaleTimeString();
logElement.textContent = `${timestamp}: ${logText}`;
// Добавляем в контейнер (в начало)
debugLogContainer.insertBefore(logElement, debugLogContainer.firstChild);
// Ограничиваем количество логов
const maxLogs = 15;
while (debugLogContainer.children.length > maxLogs) {
debugLogContainer.removeChild(debugLogContainer.lastChild);
}
// Удаляем после анимации (10 секунд)
setTimeout(() => {
if (logElement.parentNode === debugLogContainer) {
logElement.style.opacity = '0';
logElement.style.transform = 'translateY(-10px)';
setTimeout(() => {
if (logElement.parentNode === debugLogContainer) {
debugLogContainer.removeChild(logElement);
}
}, 300);
}
}, 10000);
}
// === Функция обновления статуса загрузки ===
function updateLoadingStatus(text) {
if (loadingStatus) {
loadingStatus.textContent = text;
}
}
// === Функция скрытия индикатора загрузки ===
function hideLoadingIndicator() {
if (loadingIndicator) {
loadingIndicator.style.opacity = '0';
setTimeout(() => {
if (loadingIndicator.parentNode) {
loadingIndicator.parentNode.removeChild(loadingIndicator);
}
}, 300);
}
}
// === Обновление статистики ===
function updateStats() {
if (cfg.debug) {
document.getElementById('emote-count').textContent = activeEmotes.size;
document.getElementById('fps').textContent = fps;
document.getElementById('test-pool').textContent = testEmotesPool.length;
statsPanel.classList.add('show');
// Показываем панель тестового режима только если debug включен
if (cfg.testMode) {
document.getElementById('test-interval').textContent = cfg.testInterval;
testPanel.classList.add('show');
} else {
testPanel.classList.remove('show');
}
} else {
// Если debug выключен, скрываем обе панели
statsPanel.classList.remove('show');
testPanel.classList.remove('show');
}
}
// === Расчет FPS ===
function updateFPS(currentTime) {
frameCount++;
if (currentTime - lastFrameTime >= 1000) {
fps = Math.round(frameCount);
frameCount = 0;
lastFrameTime = currentTime;
}
}
// === Получение Twitch User ID ===
async function getTwitchUserId(username) {
try {
const res = await fetch(`https://api.ivr.fi/v2/twitch/user?login=${encodeURIComponent(username)}`);
if (!res.ok) throw new Error("HTTP " + res.status);
const data = await res.json();
return Array.isArray(data) && data[0]?.id ? data[0].id : null;
} catch (e) {
error("Не удалось получить Twitch User ID:", e.message);
return null;
}
}
// === Загрузка эмодзи с разных платформ ===
async function loadEmotes(twitchUserId, channelName) {
emotesLoaded = 0;
const promises = [];
updateLoadingStatus("Загрузка эмодзи...");
// Загрузка 7TV эмодзи
if (cfg.enable7tv) {
promises.push(load7TVEmotes(twitchUserId));
}
// Загрузка BTTV эмодзи
if (cfg.enableBTTV) {
promises.push(loadBTTVEmotes(channelName));
}
// Загрузка FFZ эмодзи
if (cfg.enableFFZ) {
promises.push(loadFFZEmotes(channelName));
}
// Ожидаем завершения всех загрузок
await Promise.all(promises);
info(`✅ Загружено ${emotesLoaded} эмодзи`);
// Инициализируем тестовый пул
initTestEmotesPool();
}
async function load7TVEmotes(twitchUserId) {
try {
updateLoadingStatus("Загрузка 7TV эмодзи...");
// Глобальные 7TV эмодзи
const globalRes = await fetch('https://7tv.io/v3/emote-sets/global');
if (globalRes.ok) {
const data = await globalRes.json();
for (const emote of data.emotes || []) {
const url = build7TVUrl(emote);
if (url) {
globalEmotes.set(emote.name, url);
emotesLoaded++;
}
}
log(`✅ Загружено ${data.emotes?.length || 0} глобальных 7TV эмодзи`);
}
// Канальные 7TV эмодзи
if (twitchUserId) {
const channelRes = await fetch(`https://7tv.io/v3/users/twitch/${twitchUserId}`);
if (channelRes.ok) {
const data = await channelRes.json();
const emotes = data?.emote_set?.emotes || [];
for (const emote of emotes) {
const url = build7TVUrl(emote.data);
if (url) {
chatEmotes.set(emote.name, url);
emotesLoaded++;
}
}
log(`✅ Загружено ${emotes.length} канальных 7TV эмодзи`);
}
}
} catch (e) {
error("Ошибка загрузки 7TV:", e.message);
}
}
function build7TVUrl(emoteData) {
if (!emoteData?.host?.files?.length) return null;
const webpFiles = emoteData.host.files.filter(f => f.format === 'WEBP');
if (webpFiles.length === 0) return null;
webpFiles.sort((a, b) => a.width - b.width);
const best = webpFiles[webpFiles.length - 1];
const baseUrl = Array.isArray(emoteData.host.url)
? emoteData.host.url[0]
: emoteData.host.url;
return `https:${baseUrl}/${best.name}`;
}
async function loadBTTVEmotes(channelName) {
try {
updateLoadingStatus("Загрузка BTTV эмодзи...");
// Глобальные BTTV
const globalRes = await fetch('https://api.betterttv.net/3/cached/emotes/global');
if (globalRes.ok) {
const data = await globalRes.json();
for (const emote of data || []) {
bttvEmotes.set(emote.code, `https://cdn.betterttv.net/emote/${emote.id}/3x`);
emotesLoaded++;
}
log(`✅ Загружено ${data?.length || 0} глобальных BTTV эмодзи`);
}
// Канальные BTTV
const userId = await getTwitchUserId(channelName);
if (userId) {
const channelRes = await fetch(`https://api.betterttv.net/3/cached/users/twitch/${userId}`);
if (channelRes.ok) {
const data = await channelRes.json();
const emotes = [...(data.channelEmotes || []), ...(data.sharedEmotes || [])];
for (const emote of emotes) {
bttvEmotes.set(emote.code, `https://cdn.betterttv.net/emote/${emote.id}/3x`);
emotesLoaded++;
}
log(`✅ Загружено ${emotes.length} канальных BTTV эмодзи`);
}
}
} catch (e) {
error("Ошибка загрузки BTTV:", e.message);
}
}
async function loadFFZEmotes(channelName) {
try {
updateLoadingStatus("Загрузка FFZ эмодзи...");
// Канальные FFZ
const channelRes = await fetch(`https://api.frankerfacez.com/v1/room/${channelName}`);
if (channelRes.ok) {
const data = await channelRes.json();
const sets = data.sets || {};
for (const [setId, set] of Object.entries(sets)) {
for (const emote of set.emoticons || []) {
const url = emote.urls['4'] || emote.urls['2'] || emote.urls['1'];
if (url) {
const fullUrl = url.startsWith('http') ? url : `https:${url}`;
ffzEmotes.set(emote.name, fullUrl);
emotesLoaded++;
}
}
}
log(`✅ Загружено FFZ эмодзи канала ${channelName}`);
}
} catch (e) {
error("Ошибка загрузки FFZ:", e.message);
}
}
// === Инициализация тестового пула эмодзи ===
function initTestEmotesPool() {
testEmotesPool = [];
collectedTestEmotes.clear();
// Добавляем стандартные тестовые эмодзи
Object.keys(TEST_EMOTES).forEach(platform => {
TEST_EMOTES[platform].forEach(emoteName => {
// Проверяем, доступно ли эмодзи
const url = findEmoteUrl(emoteName);
if (url) {
testEmotesPool.push({ name: emoteName, url: url, source: 'standard' });
collectedTestEmotes.add(emoteName);
}
});
});
if (cfg.debug) {
log(`🧪 Инициализирован тестовый пул: ${testEmotesPool.length} эмодзи`);
}
}
// === Добавление эмодзи в тестовый пул ===
function addEmoteToTestPool(name, url) {
// Проверяем, не добавлено ли уже это эмодзи
if (collectedTestEmotes.has(name)) {
return false;
}
// Проверяем, что URL существует
if (!url) {
const foundUrl = findEmoteUrl(name);
if (!foundUrl) {
return false;
}
url = foundUrl;
}
// Добавляем в пул
testEmotesPool.push({ name: name, url: url, source: 'collected' });
collectedTestEmotes.add(name);
log(`🧪 Добавлено в тестовый пул: ${name} (собрано из чата)`);
updateStats();
return true;
}
// === Поиск URL эмодзи по имени ===
function findEmoteUrl(name) {
// Проверяем в порядке приоритета
if (chatEmotes.has(name)) return chatEmotes.get(name);
if (globalEmotes.has(name)) return globalEmotes.get(name);
if (bttvEmotes.has(name)) return bttvEmotes.get(name);
if (ffzEmotes.has(name)) return ffzEmotes.get(name);
return null;
}
// === Создание элемента эмодзи ===
function createEmoteElement(name, url) {
const emoteId = `emote-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
// Создаем контейнер
const container = document.createElement('div');
container.id = emoteId;
container.className = 'emote';
// Определяем размер
let scale = cfg.emoteScale;
if (cfg.randomScale) {
scale = cfg.emoteMinScale + Math.random() * (cfg.emoteMaxScale - cfg.emoteMinScale);
}
const size = 128 * scale; // Базовый размер 128px
container.style.width = `${size}px`;
container.style.height = `${size}px`;
// Тень
if (cfg.dropShadow) {
container.style.filter = `drop-shadow(${cfg.shadowBlur}px ${cfg.shadowBlur}px ${cfg.shadowBlur}px ${cfg.shadowColor})`;
}
// Создаем изображение
const img = document.createElement('img');
img.src = url;
img.alt = name;
img.draggable = false;
// Добавляем обработчик ошибок загрузки
img.onerror = () => {
warn(`Не удалось загрузить эмодзи: ${name}`);
container.style.display = 'none';
// Удаляем из тестового пула если есть
testEmotesPool = testEmotesPool.filter(emote => emote.name !== name);
collectedTestEmotes.delete(name);
updateStats();
};
// Добавляем изображение в контейнер
container.appendChild(img);
return { id: emoteId, element: container, name, url };
}
// === Получение позиции для появления ===
function getSpawnPosition(animationType) {
const margin = cfg.margin;
const width = window.innerWidth;
const height = window.innerHeight;
// Для rain - старт сверху за пределами экрана
if (animationType === 'rain') {
const x = margin + Math.random() * (width - 2 * margin);
const y = -100; // Начинаем выше экрана
return { x, y };
}
// Для других анимаций - случайная позиция на экране
const x = margin + Math.random() * (width - 2 * margin);
const y = margin + Math.random() * (height - 2 * margin);
return { x, y };
}
// === Применение анимации движения ===
function applyMovementAnimation(element, animationType) {
// Всегда применяем fade-in анимацию
element.style.animation = `fadeIn ${cfg.fadeInDuration}ms ease-out`;
// Добавляем дополнительную анимацию в зависимости от типа
if (animationType === 'float') {
element.style.animation += `, float ${2/cfg.floatSpeed}s infinite ease-in-out`;
}
// Возвращаем, нужна ли физика
return animationType === 'physics' || animationType === 'rain';
}
// === Добавление эмодзи на стену ===
function addEmoteToWall(name, url, fromTest = false) {
// Проверяем ограничение по количеству
if (activeEmotes.size >= cfg.maxEmotesOnScreen) {
// Удаляем самое старое эмодзи
const oldestId = Array.from(activeEmotes.keys())[0];
removeEmote(oldestId);
}
const now = Date.now();
// Проверяем ограничение по времени, только если maxEmotesPerSecond > 0
if (cfg.maxEmotesPerSecond > 0) {
if (now - lastSpawnTime < 1000 / cfg.maxEmotesPerSecond) {
return null;
}
}
// Проверяем спам-фильтр, только если он включен
if (cfg.spamFilterEnabled && lastEmoteTimes.has(name)) {
const lastTime = lastEmoteTimes.get(name);
if (now - lastTime < cfg.spamFilterTime) {
return null;
}
}
// Проверяем комбо
if (cfg.comboRequirement > 0) {
if (name === lastEmoteName) {
emoteCombo++;
} else {
emoteCombo = 1;
lastEmoteName = name;
}
if (emoteCombo < cfg.comboRequirement) {
return null;
}
}
// Игнорирование дубликатов
if (cfg.ignoreDuplicates && name === lastEmoteName) {
return null;
}
// Выбираем тип анимации
const animationType = selectAnimationType();
// Создаем элемент
const emoteData = createEmoteElement(name, url);
const { id, element } = emoteData;
// Устанавливаем позицию в зависимости от типа анимации
const pos = getSpawnPosition(animationType);
element.style.left = `${pos.x}px`;
element.style.top = `${pos.y}px`;
// Переменные для скорости и вращения
let vx = 0, vy = 0;
let rotation = 0; // Угол вращения в градусах
// Настраиваем начальную скорость и вращение в зависимости от типа анимации
if (animationType === 'rain') {
// Для rain: падение с отклонением от основного угла
const angleDeviation = cfg.rainAngleDeviation || 0;
const randomAngle = cfg.rainAngle + (Math.random() * 2 - 1) * angleDeviation;
const rad = randomAngle * Math.PI / 180;
vx = Math.cos(rad) * cfg.rainSpeed;
vy = Math.sin(rad) * cfg.rainSpeed;
// Вращаем эмодзи в направлении движения (чтобы "лицом" вниз по направлению)
// Преобразуем угол движения (от горизонтали) в угол вращения
// Но вычитаем 90°, потому что в CSS 0° - это вправо, а 90° - вниз
rotation = randomAngle - 90;
// Применяем вращение
element.style.transform = `rotate(${rotation}deg)`;
element.style.transformOrigin = 'center';
} else if (animationType === 'physics') {
// Для физики: случайное начальное движение
vx = (Math.random() - 0.5) * 5;
vy = -5; // Начальная скорость вверх для эффекта подбрасывания
}
// Применяем анимацию и получаем, нужна ли физика
const needsPhysics = applyMovementAnimation(element, animationType);
// Добавляем на стену
emoteWall.appendChild(element);
activeEmotes.set(id, emoteData);
lastEmoteTimes.set(name, now);
lastSpawnTime = now;
// Если нужна физика
if (needsPhysics && animationType) {
physicsEmotes.set(id, {
element: element,
vx: vx,
vy: vy,
x: pos.x,
y: pos.y,
animationType: animationType,
rotation: rotation // Сохраняем угол вращения для rain
});
}
// Устанавливаем таймер удаления
setTimeout(() => {
removeEmote(id);
}, cfg.emoteDuration);
// Если не из тестового режима и включен тестовый режим, добавляем в пул
if (!fromTest && cfg.testMode) {
addEmoteToTestPool(name, url);
}
// Логируем только обычные эмодзи, не тестовые
if (!fromTest && cfg.debug) {
log(`➕ Добавлено эмодзи: ${name} (анимация: ${animationType || 'нет'}, угол: ${rotation}°)`);
}
updateStats();
return id;
}
// === Удаление эмодзи ===
function removeEmote(id) {
if (!activeEmotes.has(id)) return;
const emoteData = activeEmotes.get(id);
const element = emoteData.element;
// Эффект исчезновения
element.style.animation = `fadeOut ${cfg.fadeOutDuration}ms ease-out`;
// Удаляем после анимации
setTimeout(() => {
if (element.parentNode) {
element.parentNode.removeChild(element);
}
activeEmotes.delete(id);
physicsEmotes.delete(id);
updateStats();
}, cfg.fadeOutDuration);
}
// === Обработка физики ===
function updatePhysics() {
physicsEmotes.forEach((data, id) => {
const isRain = data.animationType === 'rain';
// Обновляем позицию
if (!isRain) {
// Для physics добавляем гравитацию
data.vy += cfg.gravity;
}
data.x += data.vx;
data.y += data.vy;
// Проверяем столкновение с границами
const element = data.element;
const rect = element.getBoundingClientRect();
// Для rain: удаляем если ушли за нижнюю границу
if (isRain && data.y > window.innerHeight) {
removeEmote(id);
return;
}
// Для physics: обрабатываем отскоки от границ
if (!isRain) {
// Правая граница
if (data.x + rect.width > window.innerWidth) {
data.x = window.innerWidth - rect.width;
data.vx = -Math.abs(data.vx) * cfg.bounceDamping;
}
// Левая граница
if (data.x < 0) {
data.x = 0;
data.vx = Math.abs(data.vx) * cfg.bounceDamping;
}
// Нижняя граница
if (data.y + rect.height > window.innerHeight) {
data.y = window.innerHeight - rect.height;
data.vy = -Math.abs(data.vy) * cfg.bounceDamping;
// Если скорость слишком мала, останавливаем
if (Math.abs(data.vy) < 0.5) {
data.vy = 0;
}
}
// Верхняя граница
if (data.y < 0) {
data.y = 0;
data.vy = Math.abs(data.vy) * cfg.bounceDamping;
}
}
// Применяем позицию
element.style.left = `${data.x}px`;
element.style.top = `${data.y}px`;
// Для rain: сохраняем постоянное вращение (не крутимся)
if (isRain) {
// Просто применяем сохраненное вращение, не меняя его
element.style.transform = `rotate(${data.rotation}deg)`;
}
});
}
// === Тестовый режим ===
function startTestMode() {
if (!cfg.testMode || testInterval) return;
log("🧪 Запуск тестового режима");
log(`🧪 Тестовый пул: ${testEmotesPool.length} эмодзи`);
// Если пул пустой, добавляем стандартные
if (testEmotesPool.length === 0) {
initTestEmotesPool();
}
testInterval = setInterval(() => {
// Проверяем, есть ли эмодзи в пуле
if (testEmotesPool.length === 0) {
return;
}
// Выбираем случайное эмодзи из пула
const randomIndex = Math.floor(Math.random() * testEmotesPool.length);
const testEmote = testEmotesPool[randomIndex];
// Показываем эмодзи
if (testEmote && testEmote.url) {
addEmoteToWall(testEmote.name, testEmote.url, true);
}
}, cfg.testInterval);
}
function stopTestMode() {
if (testInterval) {
clearInterval(testInterval);
testInterval = null;
log("🧪 Тестовый режим остановлен");
}
}
// === Главный цикл анимации ===
function animationLoop(timestamp) {
updateFPS(timestamp);
updatePhysics();
updateStats();
requestAnimationFrame(animationLoop);
}
// === Обработка сообщений из чата ===
function processChatMessage(message, tags, username) {
// Извлекаем слова из сообщения
const words = message.split(/\s+/);
let emoteFound = false;
for (const word of words) {
const cleanWord = word.trim();
if (!cleanWord) continue;
// Ищем эмодзи в загруженных коллекциях
const url = findEmoteUrl(cleanWord);
if (url) {
addEmoteToWall(cleanWord, url, false);
emoteFound = true;
break; // Показываем только первое найденное эмодзи из сообщения
}
}
// Также проверяем Twitch эмодзи из тегов
if (!emoteFound && cfg.enableTwitch && tags.emotes) {
const emoteData = tags.emotes;
if (typeof emoteData === 'string') {
const emotes = emoteData.split('/');
for (const emote of emotes) {
const [emoteId, positions] = emote.split(':');
if (emoteId) {
// Twitch эмодзи имеют специальный URL
const emoteUrl = `https://static-cdn.jtvnw.net/emoticons/v2/${emoteId}/default/dark/3.0`;
addEmoteToWall(`twitch_${emoteId}`, emoteUrl, false);
break;
}
}
}
}
}
// === Подключение к Twitch чату ===
function connectToTwitchChat(channel) {
const ws = new WebSocket("wss://irc-ws.chat.twitch.tv:443");
ws.onopen = () => {
ws.send("CAP REQ :twitch.tv/tags twitch.tv/commands");
ws.send("PASS SCHMOOPIIE");
ws.send("NICK justinfan12345");
ws.send(`JOIN #${channel}`);
log(`📥 Подключено к чату #${channel}`);
};
ws.onmessage = (event) => {
const raw = event.data;
// Отвечаем на PING
if (raw.startsWith("PING")) {
ws.send("PONG :tmi.twitch.tv");
return;
}
// Пропускаем не-PRIVMSG сообщения
if (!raw.includes("PRIVMSG")) return;
// Парсим теги
const parts = raw.split(' ');
const tags = {};
if (parts[0].startsWith('@')) {
const tagString = parts[0].substring(1);
tagString.split(';').forEach(tag => {
const [key, value] = tag.split('=');
tags[key] = value;
});
}
// Извлекаем сообщение
const messageMatch = raw.match(/PRIVMSG #[^ ]+ :(.+)/);
if (!messageMatch) return;
const message = messageMatch[1];
const displayName = tags['display-name'] || 'unknown';
if (cfg.debug) {
log(`💬 [${displayName}]: ${message}`);
}
// Обрабатываем сообщение
processChatMessage(message, tags, displayName);
};
ws.onerror = (e) => error("WebSocket ошибка:", e);
ws.onclose = () => {
warn("Соединение закрыто. Переподключение через 5 сек...");
setTimeout(() => connectToTwitchChat(channel), 5000);
};
}
// === Инициализация ===
async function init() {
info("🚀 EmoteWall запускается...");
info(`📺 Канал: ${cfg.nickname}`);
// Загружаем конфиг
const userId = await getTwitchUserId(cfg.nickname);
if (!userId) {
error("❌ Не удалось получить User ID для канала");
hideLoadingIndicator();
return;
}
// Загружаем эмодзи
await loadEmotes(userId, cfg.nickname);
// Подключаемся к чату
connectToTwitchChat(cfg.nickname);
// Запускаем главный цикл
requestAnimationFrame(animationLoop);
// Запускаем тестовый режим, если включен
if (cfg.testMode) {
startTestMode();
}
// Скрываем индикатор загрузки
hideLoadingIndicator();
info("✅ EmoteWall готов к работе!");
info(`🧪 Тестовый режим: ${cfg.testMode ? 'ВКЛ' : 'ВЫКЛ'}`);
// Показываем веса анимаций
const weights = normalizeAnimationWeights();
info(`🎬 Веса анимаций: float=${weights.float}, physics=${weights.physics}, rain=${weights.rain}`);
}
// Запускаем инициализацию
init();
})();