-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbatch.html
More file actions
716 lines (627 loc) · 37.7 KB
/
Copy pathbatch.html
File metadata and controls
716 lines (627 loc) · 37.7 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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Broken Arrow Maggot Batch - 批量蛆指数</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
body { background-color: #0f172a; color: #e2e8f0; font-family: 'Segoe UI', sans-serif; }
.glass-panel {
background: rgba(30, 41, 59, 0.7);
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.1);
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.5);
}
.upload-zone {
border: 2px dashed rgba(6, 182, 212, 0.4);
background: rgba(15, 23, 42, 0.6);
transition: all 0.3s;
}
.upload-zone.dragover {
border-color: #22d3ee;
background: rgba(6, 182, 212, 0.1);
}
.card-enter { animation: slideUp 0.3s ease-out forwards; }
@keyframes slideUp { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } }
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #475569; border-radius: 3px; }
.score-god { color: #4ade80; text-shadow: 0 0 10px rgba(74, 222, 128, 0.3); }
.score-mid { color: #facc15; }
.score-maggot { color: #ef4444; text-shadow: 0 0 10px rgba(239, 68, 68, 0.3); }
.lang-btn {
font-size: 0.75rem; padding: 4px 8px; border: 1px solid rgba(255,255,255,0.2); border-radius: 4px; cursor: pointer; transition: all 0.2s;
}
.lang-btn.active { background-color: #06b6d4; border-color: #06b6d4; color: white; }
</style>
</head>
<body class="min-h-screen p-4 flex flex-col items-center">
<div class="w-full max-w-7xl flex flex-col md:flex-row justify-between items-center mb-6 gap-4">
<h1 class="text-2xl font-bold text-transparent bg-clip-text bg-gradient-to-r from-cyan-400 to-blue-500 flex items-center">
<span id="lbl_title">批量蛆指数</span>
<span class="text-xs text-slate-500 font-normal ml-2 border border-slate-700 rounded px-1">AI Vision</span>
</h1>
<div class="flex gap-3 items-center flex-wrap justify-center">
<div class="flex gap-1 mr-2">
<button onclick="setLanguage('en')" id="btn_en" class="lang-btn">EN</button>
<button onclick="setLanguage('zh')" id="btn_zh" class="lang-btn">中文</button>
<button onclick="setLanguage('ru')" id="btn_ru" class="lang-btn">RU</button>
</div>
<button onclick="window.location.href='index.html'" class="px-4 py-2 text-sm rounded bg-slate-800 hover:bg-slate-700 border border-slate-600 transition flex items-center gap-2">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"></path></svg>
<span id="lbl_single_query">单人查询</span>
</button>
<button onclick="toggleSettings()" class="px-4 py-2 text-sm rounded bg-cyan-900/50 hover:bg-cyan-800/50 border border-cyan-700 text-cyan-100 transition flex items-center gap-2">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path></svg>
<span id="lbl_ai_settings">AI 设置</span>
</button>
<button onclick="refreshAllCards()" class="px-4 py-2 text-sm rounded bg-emerald-900/30 hover:bg-emerald-900/50 border border-emerald-800 text-emerald-200 transition flex items-center gap-2" title="Refresh All">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"></path></svg>
<span id="lbl_refresh_all">刷新全部</span>
</button>
<button onclick="clearAllData()" class="px-4 py-2 text-sm rounded bg-red-900/30 hover:bg-red-900/50 border border-red-800 text-red-200 transition flex items-center gap-2" title="Clear all cards">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path></svg>
</button>
</div>
</div>
<div id="settingsPanel" class="hidden fixed inset-0 z-50 flex items-center justify-center bg-black/80 backdrop-blur-sm">
<div class="glass-panel p-6 rounded-xl max-w-md w-full shadow-2xl">
<h2 class="text-xl font-bold mb-4 text-white" id="lbl_cfg_title">AI Configuration</h2>
<div class="space-y-4">
<div>
<label class="block text-xs text-slate-400 mb-1">API Base URL (OpenAI Compatible)</label>
<input type="text" id="cfg_url" placeholder="https://api.openai.com/v1" class="w-full bg-slate-900 border border-slate-700 rounded p-2 text-sm text-white focus:border-cyan-500 outline-none">
</div>
<div>
<label class="block text-xs text-slate-400 mb-1">API Key</label>
<input type="password" id="cfg_key" placeholder="sk-..." class="w-full bg-slate-900 border border-slate-700 rounded p-2 text-sm text-white focus:border-cyan-500 outline-none">
</div>
<div>
<label class="block text-xs text-slate-400 mb-1">Model Name (Vision Supported)</label>
<input type="text" id="cfg_model" placeholder="gpt-4o / claude-3-5-sonnet" class="w-full bg-slate-900 border border-slate-700 rounded p-2 text-sm text-white focus:border-cyan-500 outline-none">
</div>
</div>
<div class="mt-6 flex gap-3">
<button onclick="saveSettings()" class="flex-1 bg-cyan-600 hover:bg-cyan-500 text-white py-2 rounded text-sm transition" id="btn_cfg_save">保存</button>
<button onclick="toggleSettings()" class="flex-1 bg-slate-700 hover:bg-slate-600 text-slate-300 py-2 rounded text-sm transition" id="btn_cfg_cancel">取消</button>
</div>
</div>
</div>
<div id="uploadZone" class="w-full max-w-7xl h-32 upload-zone rounded-xl flex flex-col items-center justify-center cursor-pointer mb-8 relative overflow-hidden group">
<input type="file" id="fileInput" accept="image/*" class="absolute inset-0 opacity-0 cursor-pointer z-10" onchange="handleFileSelect(this)">
<div class="text-slate-400 group-hover:text-cyan-400 transition flex flex-col items-center pointer-events-none">
<svg class="w-8 h-8 mb-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"></path></svg>
<p class="text-sm font-medium" id="lbl_upload_main">Ctrl+V 粘贴图片,或拖拽上传</p>
<p class="text-xs text-slate-500 mt-1" id="lbl_upload_sub">自动识别 ID 并批量查询</p>
</div>
<div id="visionLoading" class="hidden absolute inset-0 bg-slate-900/90 flex items-center justify-center z-20">
<div class="flex flex-col items-center">
<div class="animate-spin rounded-full h-8 w-8 border-b-2 border-cyan-500 mb-2"></div>
<p class="text-cyan-400 text-xs animate-pulse" id="lbl_vision_loading">AI 正在识别...</p>
</div>
</div>
</div>
<div id="resultsGrid" class="w-full max-w-7xl grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4"></div>
<div class="w-full max-w-7xl mt-4 flex justify-center pb-10">
<button onclick="addManualCard()" class="text-slate-500 hover:text-cyan-400 text-sm flex items-center gap-1 transition">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path></svg>
<span id="lbl_manual_add">手动添加</span>
</button>
</div>
<script>
// ================= Multi-Language System =================
const translations = {
zh: {
title: "批量蛆指数",
single_query: "单人查询",
ai_settings: "AI 设置",
cfg_title: "AI 配置",
cfg_save: "保存",
cfg_cancel: "取消",
upload_main: "Ctrl+V 粘贴图片,或拖拽上传",
upload_sub: "自动识别 ID 并批量查询",
vision_loading: "AI 正在识别...",
manual_add: "手动添加",
card_placeholder: "玩家名",
card_btn_search: "搜",
card_wait: "等待查询...",
card_not_found: "未找到玩家",
card_api_error: "API 错误",
card_analyzing: "正在分析战绩...",
card_insufficient: "战绩不足",
card_valid_low: "有效局数不足",
lbl_total: "综合",
lbl_kill: "击杀",
lbl_cap: "占点",
lbl_trend: "近 X 场走势",
link_detail: "详情 >",
god: "神",
maggot: "蛆",
msg_cfg_incomplete: "请填写完整配置",
msg_ai_format_error: "AI 返回格式错误",
msg_vision_fail: "识别失败: ",
refresh_all: "刷新全部",
maggot_levels: ["👑 绝世大腿", "🦁 团队中坚", "😐 平平淡淡", "🐛 只有一点蛆", "💩 纯种蛆王"]
},
en: {
title: "Batch Maggot Index",
single_query: "Single Query",
ai_settings: "AI Settings",
cfg_title: "AI Configuration",
cfg_save: "Save",
cfg_cancel: "Cancel",
upload_main: "Ctrl+V to Paste, or Drag & Drop",
upload_sub: "Auto-identify names and batch search",
vision_loading: "AI Identifying...",
manual_add: "Add Manual",
card_placeholder: "Player Name",
card_btn_search: "Go",
card_wait: "Waiting...",
card_not_found: "Player Not Found",
card_api_error: "API Error",
card_analyzing: "Analyzing...",
card_insufficient: "Not enough data",
card_valid_low: "Low valid matches",
lbl_total: "Avg",
lbl_kill: "Kill",
lbl_cap: "Cap",
lbl_trend: "Recent X Trend",
link_detail: "Detail >",
god: "GOD",
maggot: "MAGGOT",
msg_cfg_incomplete: "Please fill all fields",
msg_ai_format_error: "AI Format Error",
msg_vision_fail: "Vision Failed: ",
refresh_all: "Refresh All",
maggot_levels: ["👑 GODLIKE", "🦁 TEAM CARRY", "😐 AVERAGE JOE", "🐛 BIT OF A MAGGOT", "💩 PURE MAGGOT"]
},
ru: {
title: "Пакетный Индекс",
single_query: "Поиск Игрока",
ai_settings: "Настройки ИИ",
cfg_title: "Конфигурация ИИ",
cfg_save: "Сохранить",
cfg_cancel: "Отмена",
upload_main: "Ctrl+V Вставить / Перетащить",
upload_sub: "Авто-распознавание и поиск",
vision_loading: "ИИ Распознает...",
manual_add: "Добавить вручную",
card_placeholder: "Имя игрока",
card_btn_search: "Искать",
card_wait: "Ожидание...",
card_not_found: "Игрок не найден",
card_api_error: "Ошибка API",
card_analyzing: "Анализ...",
card_insufficient: "Мало данных",
card_valid_low: "Мало валидных игр",
lbl_total: "Сред",
lbl_kill: "Убийства",
lbl_cap: "Точки",
lbl_trend: "Тренд X игр",
link_detail: "Инфо >",
god: "БОГ",
maggot: "ЛИЧИНКА",
msg_cfg_incomplete: "Заполните все поля",
msg_ai_format_error: "Ошибка формата ИИ",
msg_vision_fail: "Ошибка: ",
refresh_all: "Обновить все",
maggot_levels: ["👑 БОГ", "🦁 ТАЩЕР", "😐 СРЕДНЯЧОК", "🐛 НЕМНОГО ЛИЧИНКА", "💩 100% ЛИЧИНКА"]
}
};
let currentLang = 'zh';
function setLanguage(lang) {
currentLang = lang;
localStorage.setItem('ba_lang', lang);
// Toggle Buttons
['en', 'zh', 'ru'].forEach(l => {
const btn = document.getElementById(`btn_${l}`);
if (l === lang) btn.classList.add('active');
else btn.classList.remove('active');
});
const t = translations[lang];
// Update Static Text
setText('lbl_title', t.title);
setText('lbl_single_query', t.single_query);
setText('lbl_ai_settings', t.ai_settings);
setText('lbl_cfg_title', t.cfg_title);
setText('btn_cfg_save', t.cfg_save);
setText('btn_cfg_cancel', t.cfg_cancel);
setText('lbl_upload_main', t.upload_main);
setText('lbl_upload_sub', t.upload_sub);
setText('lbl_vision_loading', t.vision_loading);
setText('lbl_manual_add', t.manual_add);
setText('lbl_refresh_all', t.refresh_all);
// Refresh dynamic cards text (placeholders etc)
document.querySelectorAll('.player-input').forEach(el => el.placeholder = t.card_placeholder);
document.querySelectorAll('.search-btn').forEach(el => el.innerText = t.card_btn_search);
}
function setText(id, text) {
const el = document.getElementById(id);
if (el) el.innerText = text;
}
// ================= State Persistence =================
// Saves the current state of cards to LocalStorage
function saveState() {
const cards = [];
const grid = document.getElementById('resultsGrid');
Array.from(grid.children).forEach(cardDiv => {
const cardId = cardDiv.id;
const input = document.getElementById(`input-${cardId}`);
// Check if card has a result
const hasResult = cardDiv.dataset.hasResult === 'true';
const steamId = cardDiv.dataset.steamId;
const playerLevel = cardDiv.dataset.playerLevel;
const stats = cardDiv.dataset.stats ? JSON.parse(cardDiv.dataset.stats) : null;
cards.push({
id: cardId,
name: input ? input.value : '',
hasResult: hasResult,
steamId: steamId,
level: playerLevel,
stats: stats
});
});
localStorage.setItem('ba_batch_state', JSON.stringify(cards));
localStorage.setItem('ba_batch_counter', cardCounter);
}
function loadState() {
const savedLang = localStorage.getItem('ba_lang');
if (savedLang) setLanguage(savedLang);
const savedCards = JSON.parse(localStorage.getItem('ba_batch_state') || '[]');
const savedCounter = localStorage.getItem('ba_batch_counter');
if (savedCounter) cardCounter = parseInt(savedCounter);
if (savedCards.length > 0) {
savedCards.forEach(cardData => {
restoreCard(cardData);
});
} else {
// Default empty state
}
}
function restoreCard(data) {
const html = createCardHTML(data.id, data.name);
document.getElementById('resultsGrid').insertAdjacentHTML('beforeend', html);
if (data.hasResult && data.stats) {
renderResultCard(data.id, data.stats, data.steamId, data.level, false); // false = don't save (already loading)
} else if (data.name) {
// Just restore name
}
}
function clearAllData() {
if(confirm('Clear all cards?')) {
document.getElementById('resultsGrid').innerHTML = '';
localStorage.removeItem('ba_batch_state');
cardCounter = 0;
}
}
function refreshAllCards() {
const grid = document.getElementById('resultsGrid');
Array.from(grid.children).forEach(cardDiv => {
const cardId = cardDiv.id;
const steamId = cardDiv.dataset.steamId;
if (steamId) {
refreshCard(cardId);
} else {
const inputVal = document.getElementById(`input-${cardId}`).value;
if (inputVal) searchPlayer(cardId);
}
});
}
function refreshCard(cardId) {
const cardDiv = document.getElementById(cardId);
const steamId = cardDiv.dataset.steamId;
const inputVal = document.getElementById(`input-${cardId}`).value;
const level = cardDiv.dataset.playerLevel;
if (!steamId) return;
// Re-trigger calculation
calculateMaggot(cardId, steamId, inputVal, level);
}
// ================= Settings =================
function toggleSettings() {
const panel = document.getElementById('settingsPanel');
panel.classList.toggle('hidden');
if (!panel.classList.contains('hidden')) {
document.getElementById('cfg_url').value = localStorage.getItem('ba_ai_url') || '';
document.getElementById('cfg_key').value = localStorage.getItem('ba_ai_key') || '';
document.getElementById('cfg_model').value = localStorage.getItem('ba_ai_model') || 'gpt-4o';
}
}
function saveSettings() {
const url = document.getElementById('cfg_url').value.trim().replace(/\/+$/, '');
const key = document.getElementById('cfg_key').value.trim();
const model = document.getElementById('cfg_model').value.trim();
const t = translations[currentLang];
if (!url || !key || !model) { alert(t.msg_cfg_incomplete); return; }
localStorage.setItem('ba_ai_url', url);
localStorage.setItem('ba_ai_key', key);
localStorage.setItem('ba_ai_model', model);
toggleSettings();
}
// ================= Image Processing =================
const uploadZone = document.getElementById('uploadZone');
window.addEventListener('paste', e => {
const items = (e.clipboardData || e.originalEvent.clipboardData).items;
for (const item of items) {
if (item.kind === 'file' && item.type.startsWith('image/')) {
processImage(item.getAsFile());
break;
}
}
});
uploadZone.addEventListener('dragover', e => { e.preventDefault(); uploadZone.classList.add('dragover'); });
uploadZone.addEventListener('dragleave', e => { e.preventDefault(); uploadZone.classList.remove('dragover'); });
uploadZone.addEventListener('drop', e => {
e.preventDefault();
uploadZone.classList.remove('dragover');
if (e.dataTransfer.files.length) processImage(e.dataTransfer.files[0]);
});
function handleFileSelect(input) { if (input.files.length) processImage(input.files[0]); }
function processImage(file) {
const reader = new FileReader();
reader.onload = function(e) { callVisionAPI(e.target.result); };
reader.readAsDataURL(file);
}
async function callVisionAPI(base64Image) {
const url = localStorage.getItem('ba_ai_url');
const key = localStorage.getItem('ba_ai_key');
const model = localStorage.getItem('ba_ai_model');
const t = translations[currentLang];
if (!url || !key) { toggleSettings(); alert(t.msg_cfg_incomplete); return; }
document.getElementById('visionLoading').classList.remove('hidden');
try {
const response = await fetch(`${url}/chat/completions`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${key}` },
body: JSON.stringify({
model: model,
messages: [
{ role: "system", content: "Identify all player names in the image. Return ONLY a valid JSON array of strings, e.g., [\"Name1\", \"Name2\"]. No markdown." },
{ role: "user", content: [{ type: "image_url", image_url: { url: base64Image } }] }
]
})
});
const data = await response.json();
let content = data.choices[0].message.content.replace(/```json/g, '').replace(/```/g, '').trim();
const names = JSON.parse(content);
if (Array.isArray(names)) {
names.forEach(name => createPlayerCard(name));
saveState(); // Save after batch adding
}
else alert(t.msg_ai_format_error);
} catch (e) { alert(t.msg_vision_fail + e.message); }
finally { document.getElementById('visionLoading').classList.add('hidden'); }
}
// ================= Card Logic =================
let cardCounter = 0;
// Initialize on load
window.onload = loadState;
function addManualCard() {
createPlayerCard("");
saveState(); // Save on manual add
}
function removeCard(id) {
document.getElementById(id).remove();
saveState(); // Save on remove
}
function createCardHTML(cardId, initialName) {
const t = translations[currentLang];
return `
<div id="${cardId}" data-has-result="false" class="glass-panel p-4 rounded-lg card-enter relative min-h-[220px] flex flex-col shadow-xl">
<button onclick="removeCard('${cardId}')" class="absolute top-2 right-2 text-slate-600 hover:text-red-400">×</button>
<div class="flex gap-2 mb-4 pr-6">
<input type="text" id="input-${cardId}" value="${initialName}" placeholder="${t.card_placeholder}"
class="player-input flex-1 bg-slate-900 border border-slate-700 rounded px-2 py-1 text-sm text-white focus:border-cyan-500 outline-none"
onkeypress="if(event.key === 'Enter') searchPlayer('${cardId}')" onchange="saveState()">
<button onclick="searchPlayer('${cardId}')" class="search-btn bg-cyan-700 hover:bg-cyan-600 text-white px-2 rounded text-xs">${t.card_btn_search}</button>
</div>
<div id="content-${cardId}" class="flex-1 flex flex-col justify-center items-center">
<span class="text-slate-500 text-xs">${t.card_wait}</span>
</div>
</div>
`;
}
function createPlayerCard(initialName) {
cardCounter++;
const cardId = `card-${cardCounter}`;
const html = createCardHTML(cardId, initialName);
document.getElementById('resultsGrid').insertAdjacentHTML('beforeend', html);
if (initialName) searchPlayer(cardId);
}
async function searchPlayer(cardId) {
const inputVal = document.getElementById(`input-${cardId}`).value.trim();
const contentDiv = document.getElementById(`content-${cardId}`);
const t = translations[currentLang];
document.getElementById(`input-${cardId}`).setAttribute('value', inputVal);
saveState();
if (!inputVal) return;
contentDiv.innerHTML = `<div class="animate-spin rounded-full h-5 w-5 border-b-2 border-cyan-500"></div>`;
try {
// Limit = 20
const res = await fetch(`https://batrace.aoeiaol.top/api/v1/search/players?q=${encodeURIComponent(inputVal)}&limit=20`);
const data = await res.json();
const results = data.results || [];
if (results.length === 0) {
contentDiv.innerHTML = `<span class="text-red-400 text-xs">${t.card_not_found}</span>`;
return;
}
const exactMatch = results.find(p => p.name.toLowerCase() === inputVal.toLowerCase());
// 只有一个结果 或 精确匹配且干扰少
if (results.length === 1 || (exactMatch && results.length < 3)) {
const target = exactMatch || results[0];
calculateMaggot(cardId, target.steam_id, target.name, target.level);
} else {
renderSelectionList(cardId, results);
}
} catch (e) { contentDiv.innerHTML = `<span class="text-red-400 text-xs">${t.card_api_error}</span>`; }
}
function renderSelectionList(cardId, players) {
const contentDiv = document.getElementById(`content-${cardId}`);
let html = `<div class="w-full flex flex-col gap-1 overflow-y-auto max-h-[150px]">`;
players.forEach(p => {
html += `
<button onclick="calculateMaggot('${cardId}', '${p.steam_id}', '${escapeHtml(p.name)}', ${p.level})"
class="text-left px-2 py-1.5 bg-slate-800 hover:bg-cyan-900/50 rounded border border-slate-700 text-xs flex justify-between items-center group">
<span class="truncate max-w-[100px] text-slate-300 group-hover:text-white">${escapeHtml(p.name)}</span>
<span class="text-[10px] text-yellow-500 border border-yellow-800/50 px-1 rounded">Lv.${p.level}</span>
</button>
`;
});
html += `</div>`;
contentDiv.innerHTML = html;
}
async function calculateMaggot(cardId, steamId, playerName, playerLevel) {
const contentDiv = document.getElementById(`content-${cardId}`);
document.getElementById(`input-${cardId}`).value = playerName;
const t = translations[currentLang];
contentDiv.innerHTML = `
<div class="flex flex-col items-center">
<div class="animate-spin rounded-full h-6 w-6 border-b-2 border-cyan-500 mb-2"></div>
<span class="text-xs text-slate-400">${t.card_analyzing}</span>
</div>
`;
try {
const res = await fetch(`https://batrace.aoeiaol.top/api/v1/matches_steam64?steam64=${steamId}`);
const rawText = await res.text();
const allObjects = parseJSON(rawText);
const matches = allObjects.filter(obj => obj.type === 'match');
if (matches.length < 5) {
contentDiv.innerHTML = `<span class="text-yellow-500 text-xs">${t.card_insufficient} (${matches.length})</span>`;
return;
}
matches.sort((a, b) => (b.data?.EndTime || 0) - (a.data?.EndTime || 0));
const stats = processMatches(matches);
if (stats.validMatches < 5) {
contentDiv.innerHTML = `<span class="text-yellow-500 text-xs">${t.card_valid_low}</span>`;
return;
}
renderResultCard(cardId, stats, steamId, playerLevel, true); // true = save state
} catch (e) {
console.error(e);
contentDiv.innerHTML = `<span class="text-red-400 text-xs">${t.card_api_error}</span>`;
}
}
function parseJSON(content) {
const objects = [];
let idx = content.indexOf('[');
if (idx === -1) { try { return [JSON.parse(content)]; } catch(e) { return []; } }
idx += 1;
let depth = 0;
let objStart = -1;
while (idx < content.length) {
if (content[idx] === '{') {
if (depth === 0) objStart = idx;
depth++;
} else if (content[idx] === '}') {
depth--;
if (depth === 0) {
try { objects.push(JSON.parse(content.substring(objStart, idx + 1))); } catch (e) {}
}
}
idx++;
}
return objects;
}
function processMatches(matches) {
let validMatches = 0, totalRank = 0, killRankSum = 0, capRankSum = 0;
const history = [];
const idFrequency = {};
matches.slice(0, 20).forEach(m => {
if (m.data?.Data) Object.keys(m.data.Data).forEach(id => {
idFrequency[id] = (idFrequency[id] || 0) + 1;
});
});
let playerId = Object.keys(idFrequency).reduce((a, b) => idFrequency[a] > idFrequency[b] ? a : b);
for (const m of matches) {
if (validMatches >= 12) break;
if (!m.data?.Data || !m.data.Data[playerId]) continue;
const data = m.data.Data;
const me = data[playerId];
const teammates = Object.values(data).filter(p => p.TeamId === me.TeamId);
if (teammates.length < 5) continue;
const processed = teammates.map(p => {
const d = p.DestructionScore || 0;
const l = p.LossesScore || 0;
const obj = p.ObjectivesCaptured || 0;
const kill = (d - l) / 1000;
return { id: p.Id, score: kill + obj, killScore: kill, capScore: obj, isMe: p.Id == playerId };
});
if (processed.every(p => p.score === 0)) continue;
processed.sort((a, b) => b.score - a.score);
const myRank = processed.findIndex(p => p.isMe) + 1;
const killSorted = [...processed].sort((a, b) => b.killScore - a.killScore);
const myKillRank = killSorted.findIndex(p => p.isMe) + 1;
const capSorted = [...processed].sort((a, b) => b.capScore - a.capScore);
const myCapRank = capSorted.findIndex(p => p.isMe) + 1;
totalRank += myRank;
killRankSum += myKillRank;
capRankSum += myCapRank;
history.push(myRank);
validMatches++;
}
const avgRank = validMatches > 0 ? totalRank / validMatches : 0;
const normalized = (avgRank - 1) / 4;
const clamped = Math.max(0, Math.min(1, normalized));
const sCurve = (1 - Math.cos(clamped * Math.PI)) / 2;
const maggotIndex = 1 + sCurve * 9;
return { maggotIndex, avgRank, avgKillRank: validMatches ? killRankSum/validMatches : 0, avgCapRank: validMatches ? capRankSum/validMatches : 0, validMatches, history };
}
function renderResultCard(cardId, stats, steamId, level, shouldSave = true) {
const contentDiv = document.getElementById(`content-${cardId}`);
const cardDiv = document.getElementById(cardId);
const t = translations[currentLang];
// Store data for persistence
cardDiv.dataset.hasResult = "true";
cardDiv.dataset.steamId = steamId;
cardDiv.dataset.playerLevel = level || "";
cardDiv.dataset.stats = JSON.stringify(stats);
let scoreClass = "score-mid";
const score = stats.maggotIndex;
if (score <= 3.0) scoreClass = "score-god";
else if (score >= 7.0) scoreClass = "score-maggot";
// Match exactly with index.html logic
let labelIndex = 0;
if (score <= 2) labelIndex = 0;
else if (score <= 4) labelIndex = 1;
else if (score <= 6) labelIndex = 2;
else if (score <= 8) labelIndex = 3;
else labelIndex = 4;
const label = t.maggot_levels[labelIndex];
let historyHtml = '<div class="flex gap-0.5 mt-2">';
stats.history.reverse().forEach(rank => {
let color = "bg-slate-700";
if (rank === 1) color = "bg-green-500"; else if (rank === 5) color = "bg-red-500"; else if (rank === 2) color = "bg-blue-500";
historyHtml += `<div class="w-1.5 h-3 ${color} rounded-sm" title="Rank ${rank}"></div>`;
});
historyHtml += '</div>';
const levelHtml = level ? `<span class="text-xs text-yellow-500 font-mono border border-yellow-800 px-1 rounded ml-2">Lv.${level}</span>` : '';
// Add Refresh Button (absolute positioned next to close button)
const refreshBtnHtml = `
<button onclick="refreshCard('${cardId}')" class="absolute top-2 right-8 text-slate-600 hover:text-cyan-400 transition" title="刷新">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"></path></svg>
</button>
`;
contentDiv.innerHTML = `
${refreshBtnHtml}
<div class="flex flex-col items-center w-full">
<div class="flex items-center justify-center w-full mb-1">
<div class="text-5xl font-black ${scoreClass}">${score.toFixed(1)}</div>
${levelHtml}
</div>
<div class="text-xs font-bold uppercase tracking-wider mb-3 text-slate-400 border border-slate-700 px-2 rounded text-center truncate w-full">${label}</div>
<div class="grid grid-cols-3 gap-2 w-full text-center text-xs mb-2">
<div class="bg-slate-800/50 p-1 rounded"><div class="text-slate-500 scale-75">${t.lbl_total}</div><div class="font-bold text-white">#${stats.avgRank.toFixed(1)}</div></div>
<div class="bg-slate-800/50 p-1 rounded"><div class="text-slate-500 scale-75">${t.lbl_kill}</div><div class="font-bold text-red-300">#${stats.avgKillRank.toFixed(1)}</div></div>
<div class="bg-slate-800/50 p-1 rounded"><div class="text-slate-500 scale-75">${t.lbl_cap}</div><div class="font-bold text-blue-300">#${stats.avgCapRank.toFixed(1)}</div></div>
</div>
<div class="text-[10px] text-slate-600">${t.lbl_trend.replace('X', stats.validMatches)}</div>
${historyHtml}
<a href="index.html?steamId=${steamId}" target="_blank" class="mt-4 text-[10px] text-cyan-600 hover:text-cyan-400 underline">${t.link_detail}</a>
</div>
`;
if (shouldSave) saveState();
}
function escapeHtml(text) { if (!text) return ""; return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">"); }
</script>
</body>
</html>