-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathranking.html
More file actions
606 lines (507 loc) · 20.3 KB
/
ranking.html
File metadata and controls
606 lines (507 loc) · 20.3 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
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ドックンバトル - ランキング</title>
<!-- Firebase SDK -->
<script type="module">
import { initializeApp } from 'https://www.gstatic.com/firebasejs/10.7.1/firebase-app.js';
import { getFirestore, collection, getDocs, addDoc, query, orderBy, deleteDoc, doc, onSnapshot } from 'https://www.gstatic.com/firebasejs/10.7.1/firebase-firestore.js';
// Firebase設定
const firebaseConfig = {
apiKey: 'AIzaSyBfCuUFAxzhxuHhPYFkH_-rhwsKB9XthCc',
authDomain: 'hacku-8ea4e.firebaseapp.com',
projectId: 'hacku-8ea4e',
storageBucket: 'hacku-8ea4e.firebasestorage.app',
messagingSenderId: '91046515339',
appId: '1:91046515339:web:6cbbec4ef6d6e7b7287a8e',
measurementId: 'G-K315PJD6CC',
};
// Firebase初期化
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
// グローバルにFirebaseインスタンスを公開
window.firebaseApp = app;
window.firestoreDb = db;
window.firestoreCollection = collection;
window.firestoreGetDocs = getDocs;
window.firestoreAddDoc = addDoc;
window.firestoreQuery = query;
window.firestoreOrderBy = orderBy;
window.firestoreDeleteDoc = deleteDoc;
window.firestoreDoc = doc;
window.firestoreOnSnapshot = onSnapshot;
</script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Courier New', monospace;
background: linear-gradient(135deg, #0c0c0c 0%, #1a1a2e 50%, #16213e 100%);
color: #fff;
min-height: 100vh;
padding: 20px;
display: flex;
justify-content: center;
align-items: center;
}
.ranking-container {
max-width: 1000px;
width: 100%;
background: rgba(0, 0, 0, 0.7);
border-radius: 20px;
border: 2px solid rgba(0, 255, 255, 0.5);
box-shadow: 0 0 50px rgba(0, 255, 255, 0.3);
padding: 40px;
animation: containerGlow 3s ease-in-out infinite alternate;
}
@keyframes containerGlow {
0% {
box-shadow: 0 0 30px rgba(0, 255, 255, 0.3);
border-color: rgba(0, 255, 255, 0.5);
}
100% {
box-shadow: 0 0 60px rgba(0, 255, 255, 0.6);
border-color: rgba(0, 255, 255, 0.8);
}
}
.page-header {
text-align: center;
margin-bottom: 40px;
}
.page-title {
font-size: 3rem;
font-weight: bold;
color: #00ffff;
text-shadow: 0 0 20px rgba(0, 255, 255, 0.8);
margin-bottom: 10px;
animation: titlePulse 2s ease-in-out infinite;
}
@keyframes titlePulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.02); }
}
.page-subtitle {
font-size: 1.2rem;
color: #ff6666;
text-shadow: 0 0 10px rgba(255, 102, 102, 0.6);
}
.realtime-status {
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
margin-top: 10px;
font-size: 0.9rem;
color: #cccccc;
}
.status-indicator {
width: 8px;
height: 8px;
border-radius: 50%;
background-color: #00ff00;
animation: pulse 2s infinite;
}
.status-indicator.disconnected {
background-color: #ff4444;
animation: none;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.3; }
}
.ranking-content {
display: none;
}
.ranking-content.active {
display: block;
}
.ranking-table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
background: rgba(0, 0, 0, 0.3);
border-radius: 10px;
overflow: hidden;
}
.ranking-table th,
.ranking-table td {
padding: 15px 20px;
text-align: center;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.ranking-table th {
background: linear-gradient(45deg, #1a1a2e, #16213e);
color: #00ffff;
font-weight: bold;
text-shadow: 0 0 10px rgba(0, 255, 255, 0.6);
}
.ranking-table tr:nth-child(even) {
background: rgba(255, 255, 255, 0.05);
}
.ranking-table tr:hover {
background: rgba(0, 255, 255, 0.1);
transform: scale(1.01);
transition: all 0.3s ease;
}
.rank-number {
font-weight: bold;
font-size: 1.2rem;
}
.rank-1 { color: #ffd700; text-shadow: 0 0 10px #ffd700; }
.rank-2 { color: #c0c0c0; text-shadow: 0 0 10px #c0c0c0; }
.rank-3 { color: #cd7f32; text-shadow: 0 0 10px #cd7f32; }
.score-value {
color: #00ff00;
font-weight: bold;
text-shadow: 0 0 5px rgba(0, 255, 0, 0.6);
}
.name-value {
color: #ffcc00;
font-weight: bold;
text-shadow: 0 0 5px rgba(255, 204, 0, 0.6);
}
.no-data {
text-align: center;
color: #cccccc;
font-style: italic;
padding: 40px;
background: rgba(255, 255, 255, 0.05);
border-radius: 10px;
margin: 20px 0;
}
.controls {
display: flex;
justify-content: center;
gap: 20px;
margin-top: 40px;
flex-wrap: wrap;
}
.control-button {
padding: 12px 30px;
font-size: 1.1rem;
font-weight: bold;
background: linear-gradient(45deg, #1a1a2e, #16213e);
color: #fff;
border: 2px solid transparent;
border-radius: 25px;
cursor: pointer;
transition: all 0.3s ease;
text-decoration: none;
display: inline-block;
position: relative;
overflow: hidden;
}
.control-button::before {
content: '';
position: absolute;
top: 0;
left: -100%;
width: 100%;
height: 100%;
background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
transition: left 0.5s;
}
.control-button:hover::before {
left: 100%;
}
.back-button {
background: linear-gradient(45deg, #00aaff, #66ccff);
border-color: #00aaff;
box-shadow: 0 0 20px rgba(0, 170, 255, 0.4);
}
.back-button:hover {
background: linear-gradient(45deg, #66ccff, #cceeFF);
box-shadow: 0 0 30px rgba(0, 170, 255, 0.6);
transform: translateY(-2px);
}
/* レスポンシブ対応 */
@media (max-width: 768px) {
.ranking-container {
padding: 20px;
margin: 10px;
}
.page-title {
font-size: 2rem;
}
.ranking-table th,
.ranking-table td {
padding: 10px 15px;
font-size: 0.9rem;
}
.control-button {
padding: 10px 20px;
font-size: 1rem;
}
}
@media (max-width: 480px) {
.ranking-container {
padding: 15px;
margin: 5px;
}
.page-title {
font-size: 1.5rem;
}
.ranking-table th,
.ranking-table td {
padding: 8px 10px;
font-size: 0.8rem;
}
}
</style>
</head>
<body>
<div class="ranking-container">
<div class="page-header">
<h1 class="page-title">ランキング</h1>
<p class="page-subtitle">あなたの戦績を確認しよう!</p>
<div class="realtime-status">
<div class="status-indicator" id="statusIndicator"></div>
<span id="statusText">リアルタイム更新中</span>
</div>
</div>
<!-- スコアランキング -->
<div id="scoreRanking" class="ranking-content active">
<table class="ranking-table">
<thead>
<tr>
<th>順位</th>
<th>スコア</th>
<th>プレイヤー名</th>
<th>記録日時</th>
</tr>
</thead>
<tbody id="scoreTableBody">
<!-- JavaScriptで動的に生成 -->
</tbody>
</table>
</div>
<div class="controls">
<a href="index.html" class="control-button back-button">タイトルに戻る</a>
</div>
</div>
<script>
// Firebaseが初期化されるまで待機
let isFirebaseReady = false;
let firestoreUsers = [];
// Firebase初期化確認
function waitForFirebase() {
return new Promise((resolve) => {
const checkFirebase = () => {
if (window.firestoreDb) {
isFirebaseReady = true;
resolve();
} else {
setTimeout(checkFirebase, 100);
}
};
checkFirebase();
});
}
// リアルタイムリスナーの参照
let unsubscribe = null;
// Firestoreリアルタイムリスナーを設定
async function setupRealtimeListener() {
try {
if (!isFirebaseReady) {
await waitForFirebase();
}
const q = window.firestoreQuery(
window.firestoreCollection(window.firestoreDb, 'users'),
window.firestoreOrderBy('score', 'desc') // スコアで降順ソート
);
// 既存のリスナーがあれば解除
if (unsubscribe) {
unsubscribe();
}
// リアルタイムリスナーを設定
unsubscribe = window.firestoreOnSnapshot(q, (querySnapshot) => {
const users = [];
querySnapshot.forEach((doc) => {
const data = doc.data();
users.push({
id: doc.id,
name: data.name || 'Unknown Player',
score: data.score || 0,
date: data.createdAt
? new Date(data.createdAt.seconds * 1000).toLocaleString('ja-JP')
: new Date().toLocaleString('ja-JP')
});
});
firestoreUsers = users;
console.log('リアルタイム更新:', users);
// ステータスを更新
updateConnectionStatus(true);
// ランキングテーブルを即座に更新
updateScoreRankingTable();
}, (error) => {
console.error('リアルタイムリスナーエラー:', error);
// ステータスを更新
updateConnectionStatus(false);
// エラー時はフォールバックとして一度だけ取得
getFirestoreUsersOnce();
});
} catch (error) {
console.error('リアルタイムリスナー設定エラー:', error);
// エラー時はフォールバックとして一度だけ取得
getFirestoreUsersOnce();
}
}
// フォールバック用の一回限りの取得
async function getFirestoreUsersOnce() {
try {
if (!isFirebaseReady) {
await waitForFirebase();
}
const q = window.firestoreQuery(
window.firestoreCollection(window.firestoreDb, 'users'),
window.firestoreOrderBy('score', 'desc') // スコアで降順ソート
);
const querySnapshot = await window.firestoreGetDocs(q);
const users = [];
querySnapshot.forEach((doc) => {
const data = doc.data();
users.push({
id: doc.id,
name: data.name || 'Unknown Player',
score: data.score || 0,
date: data.createdAt
? new Date(data.createdAt.seconds * 1000).toLocaleString('ja-JP')
: new Date().toLocaleString('ja-JP')
});
});
firestoreUsers = users;
console.log('Firestoreからユーザーデータを取得:', users);
updateScoreRankingTable();
} catch (error) {
console.error('Firestoreデータ取得エラー:', error);
}
}
// スコアランキングテーブルを更新(テーブル部分のみ)
function updateScoreRankingTable() {
const tbody = document.getElementById('scoreTableBody');
try {
// Firestoreデータにscoreがある場合のみ使用
const validFirebaseData = firestoreUsers.filter(user =>
user.score !== undefined && user.score > 0
).map(user => ({
score: user.score,
name: user.name || 'Unknown Player',
date: user.date
}));
if (validFirebaseData.length === 0) {
tbody.innerHTML = '<tr><td colspan="4" class="no-data">まだ記録がありません</td></tr>';
return;
}
// データは既にソート済み(Firestoreクエリで降順ソート)
tbody.innerHTML = '';
validFirebaseData.forEach((record, index) => {
const row = document.createElement('tr');
const rank = index + 1;
// 新しい行にアニメーション効果を追加
row.style.opacity = '0';
row.style.transform = 'translateY(10px)';
row.innerHTML = `
<td class="rank-number ${getRankClass(rank)}">${rank}</td>
<td class="score-value">${record.score.toLocaleString()}</td>
<td class="name-value">${record.name}</td>
<td>${record.date}</td>
`;
tbody.appendChild(row);
// アニメーション実行
setTimeout(() => {
row.style.transition = 'all 0.3s ease';
row.style.opacity = '1';
row.style.transform = 'translateY(0)';
}, index * 50); // 順次表示
});
console.log(`ランキング表示完了: Firebase ${validFirebaseData.length}件`);
} catch (error) {
console.error('テーブル更新エラー:', error);
tbody.innerHTML = '<tr><td colspan="4" class="no-data">データの表示中にエラーが発生しました</td></tr>';
}
}
// 初期ランキング取得(リアルタイムリスナー設定)
async function initializeRanking() {
const tbody = document.getElementById('scoreTableBody');
tbody.innerHTML = '<tr><td colspan="4" class="no-data">読み込み中...</td></tr>';
try {
// リアルタイムリスナーを設定
await setupRealtimeListener();
} catch (error) {
console.error('初期化エラー:', error);
tbody.innerHTML = '<tr><td colspan="4" class="no-data">Firebaseに接続できません</td></tr>';
}
}
// 順位に応じたCSSクラスを取得
function getRankClass(rank) {
switch(rank) {
case 1: return 'rank-1';
case 2: return 'rank-2';
case 3: return 'rank-3';
default: return '';
}
}
// タブ切り替え(スコアのみなので不要)
// function showTab(tabName) {
// // スコアランキングのみ表示
// updateScoreRanking();
// }
// 接続ステータスを更新
function updateConnectionStatus(isConnected) {
const statusIndicator = document.getElementById('statusIndicator');
const statusText = document.getElementById('statusText');
if (isConnected) {
statusIndicator.classList.remove('disconnected');
statusText.textContent = 'リアルタイム更新中';
} else {
statusIndicator.classList.add('disconnected');
statusText.textContent = '接続エラー';
}
}
// クリック効果音
function playClickSound() {
try {
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
oscillator.frequency.setValueAtTime(600, audioContext.currentTime);
oscillator.frequency.exponentialRampToValueAtTime(800, audioContext.currentTime + 0.1);
gainNode.gain.setValueAtTime(0.1, audioContext.currentTime);
gainNode.gain.exponentialRampToValueAtTime(0.01, audioContext.currentTime + 0.1);
oscillator.start(audioContext.currentTime);
oscillator.stop(audioContext.currentTime + 0.1);
} catch (error) {
console.log('クリック音生成エラー:', error);
}
}
// ページが閉じられる時にリスナーをクリーンアップ
window.addEventListener('beforeunload', () => {
if (unsubscribe) {
unsubscribe();
console.log('リアルタイムリスナーを停止しました');
}
});
// 初期化
document.addEventListener('DOMContentLoaded', async () => {
try {
// リアルタイムランキングを開始
await initializeRanking();
// ボタンクリック時の効果音
document.querySelectorAll('button, .control-button').forEach(element => {
element.addEventListener('click', playClickSound);
});
console.log('ランキング画面 読み込み完了(リアルタイム更新対応)');
} catch (error) {
console.error('初期化エラー:', error);
}
});
</script>
</body>
</html>