Spaces:
Running
Running
Update index.html
Browse files- index.html +29 -40
index.html
CHANGED
|
@@ -13,13 +13,13 @@
|
|
| 13 |
// 載入 Firebase SDK
|
| 14 |
import { initializeApp } from "https://www.gstatic.com/firebasejs/11.6.1/firebase-app.js";
|
| 15 |
import { getAuth, signInAnonymously, signInWithCustomToken } from "https://www.gstatic.com/firebasejs/11.6.1/firebase-auth.js";
|
| 16 |
-
import { getFirestore, setDoc, doc, collection, getDocs, query, where, serverTimestamp, setLogLevel, Timestamp } from "https://www.gstatic.com/firebasejs/11.6.1/firebase-firestore.js";
|
| 17 |
|
| 18 |
// 將 SDK 功能掛載到 window 物件,讓下方的非模組 <script> 可以存取
|
| 19 |
window.firebaseSDK = {
|
| 20 |
initializeApp,
|
| 21 |
getAuth, signInAnonymously, signInWithCustomToken,
|
| 22 |
-
getFirestore, setDoc, doc, collection, getDocs, query, where, serverTimestamp, setLogLevel, Timestamp
|
| 23 |
};
|
| 24 |
</script>
|
| 25 |
|
|
@@ -1176,22 +1176,18 @@
|
|
| 1176 |
fbSDK = window.firebaseSDK;
|
| 1177 |
|
| 1178 |
try {
|
| 1179 |
-
// --- NEW: Firebase Config Logic ---
|
| 1180 |
-
//
|
| 1181 |
-
|
| 1182 |
-
|
| 1183 |
-
|
| 1184 |
-
|
| 1185 |
-
|
| 1186 |
-
|
| 1187 |
-
storageBucket: "ai-adventure-scoreboard.firebasestorage.app",
|
| 1188 |
-
messagingSenderId: "814089769337",
|
| 1189 |
-
appId: "1:814089769337:web:09d46c9e623d0db95f1fa8"
|
| 1190 |
-
};
|
| 1191 |
// --- END NEW ---
|
| 1192 |
|
| 1193 |
if (!firebaseConfig.apiKey) {
|
| 1194 |
-
console.warn("Firebase config
|
| 1195 |
return;
|
| 1196 |
}
|
| 1197 |
|
|
@@ -1200,23 +1196,16 @@
|
|
| 1200 |
auth = fbSDK.getAuth(app);
|
| 1201 |
fbSDK.setLogLevel('Debug'); // 方便除錯
|
| 1202 |
|
| 1203 |
-
//
|
| 1204 |
-
//
|
| 1205 |
-
// Since we are using a hard-coded Firebase project,
|
| 1206 |
-
// we must ignore any environment-provided custom tokens (like __initial_auth_token),
|
| 1207 |
-
// as they will not match this project and will cause an auth/custom-token-mismatch error.
|
| 1208 |
-
// We will always sign in anonymously.
|
| 1209 |
-
|
| 1210 |
-
/*
|
| 1211 |
if (typeof __initial_auth_token !== 'undefined') {
|
|
|
|
| 1212 |
await fbSDK.signInWithCustomToken(auth, __initial_auth_token);
|
| 1213 |
} else {
|
|
|
|
| 1214 |
await fbSDK.signInAnonymously(auth);
|
| 1215 |
}
|
| 1216 |
-
|
| 1217 |
-
await fbSDK.signInAnonymously(auth);
|
| 1218 |
-
// --- END FIX ---
|
| 1219 |
-
|
| 1220 |
|
| 1221 |
// 取得並儲存這個(匿名)使用者的唯一 ID
|
| 1222 |
userId = auth.currentUser ? auth.currentUser.uid : crypto.randomUUID();
|
|
@@ -1241,16 +1230,20 @@
|
|
| 1241 |
|
| 1242 |
try {
|
| 1243 |
const appId = typeof __app_id !== 'undefined' ? __app_id : 'default-app-id';
|
| 1244 |
-
// 文件路徑:.../scores/[玩家的UserID]
|
| 1245 |
-
const scoreDocRef = fbSDK.doc(db, `artifacts/${appId}/public/data/scores`, userId);
|
| 1246 |
|
| 1247 |
-
//
|
| 1248 |
-
//
|
| 1249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1250 |
displayName: displayName,
|
| 1251 |
score: totalScore, // 全域變數中的最新分數
|
| 1252 |
-
lastUpdated: fbSDK.serverTimestamp() // 儲存伺服器時間
|
| 1253 |
-
|
|
|
|
|
|
|
| 1254 |
|
| 1255 |
console.log("Score saved successfully to Firebase:", totalScore);
|
| 1256 |
} catch (error) {
|
|
@@ -3219,8 +3212,8 @@
|
|
| 3219 |
if (scores.length === 0) {
|
| 3220 |
leaderboardHtml = '<p class="text-lg">今天還沒有人完成遊戲!</p>';
|
| 3221 |
} else {
|
| 3222 |
-
//
|
| 3223 |
-
scores.
|
| 3224 |
leaderboardHtml += `
|
| 3225 |
<li class="p-2 rounded ${index === 0 ? 'bg-yellow-100 border border-yellow-300' : ''}">
|
| 3226 |
<span class="font-bold text-lg text-blue-600">${entry.score} 分</span> -
|
|
@@ -3243,7 +3236,3 @@
|
|
| 3243 |
</script>
|
| 3244 |
</body>
|
| 3245 |
</html>
|
| 3246 |
-
|
| 3247 |
-
|
| 3248 |
-
|
| 3249 |
-
|
|
|
|
| 13 |
// 載入 Firebase SDK
|
| 14 |
import { initializeApp } from "https://www.gstatic.com/firebasejs/11.6.1/firebase-app.js";
|
| 15 |
import { getAuth, signInAnonymously, signInWithCustomToken } from "https://www.gstatic.com/firebasejs/11.6.1/firebase-auth.js";
|
| 16 |
+
import { getFirestore, setDoc, doc, collection, getDocs, query, where, serverTimestamp, setLogLevel, Timestamp, addDoc } from "https://www.gstatic.com/firebasejs/11.6.1/firebase-firestore.js";
|
| 17 |
|
| 18 |
// 將 SDK 功能掛載到 window 物件,讓下方的非模組 <script> 可以存取
|
| 19 |
window.firebaseSDK = {
|
| 20 |
initializeApp,
|
| 21 |
getAuth, signInAnonymously, signInWithCustomToken,
|
| 22 |
+
getFirestore, setDoc, doc, collection, getDocs, query, where, serverTimestamp, setLogLevel, Timestamp, addDoc
|
| 23 |
};
|
| 24 |
</script>
|
| 25 |
|
|
|
|
| 1176 |
fbSDK = window.firebaseSDK;
|
| 1177 |
|
| 1178 |
try {
|
| 1179 |
+
// --- NEW: Correct Firebase Config Logic ---
|
| 1180 |
+
// 檢查 __firebase_config 變數是否存在
|
| 1181 |
+
if (typeof __firebase_config === 'undefined') {
|
| 1182 |
+
console.warn("Firebase config (__firebase_config) not found. Scoreboard will not work.");
|
| 1183 |
+
return;
|
| 1184 |
+
}
|
| 1185 |
+
|
| 1186 |
+
const firebaseConfig = JSON.parse(__firebase_config);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1187 |
// --- END NEW ---
|
| 1188 |
|
| 1189 |
if (!firebaseConfig.apiKey) {
|
| 1190 |
+
console.warn("Firebase config is invalid. Scoreboard will not work.");
|
| 1191 |
return;
|
| 1192 |
}
|
| 1193 |
|
|
|
|
| 1196 |
auth = fbSDK.getAuth(app);
|
| 1197 |
fbSDK.setLogLevel('Debug'); // 方便除錯
|
| 1198 |
|
| 1199 |
+
// --- NEW: Correct Authentication Logic ---
|
| 1200 |
+
// 檢查 __initial_auth_token 並登入
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1201 |
if (typeof __initial_auth_token !== 'undefined') {
|
| 1202 |
+
console.log("Signing in with custom token...");
|
| 1203 |
await fbSDK.signInWithCustomToken(auth, __initial_auth_token);
|
| 1204 |
} else {
|
| 1205 |
+
console.log("Signing in anonymously...");
|
| 1206 |
await fbSDK.signInAnonymously(auth);
|
| 1207 |
}
|
| 1208 |
+
// --- END NEW ---
|
|
|
|
|
|
|
|
|
|
| 1209 |
|
| 1210 |
// 取得並儲存這個(匿名)使用者的唯一 ID
|
| 1211 |
userId = auth.currentUser ? auth.currentUser.uid : crypto.randomUUID();
|
|
|
|
| 1230 |
|
| 1231 |
try {
|
| 1232 |
const appId = typeof __app_id !== 'undefined' ? __app_id : 'default-app-id';
|
|
|
|
|
|
|
| 1233 |
|
| 1234 |
+
// --- FIX: 改為 addDoc 來儲存每一次的紀錄 ---
|
| 1235 |
+
// 1. 取得 collection 的參照
|
| 1236 |
+
const scoresCollectionRef = fbSDK.collection(db, `artifacts/${appId}/public/data/scores`);
|
| 1237 |
+
|
| 1238 |
+
// 2. 使用 addDoc 來新增文件,Firebase 會自動產生ID
|
| 1239 |
+
// 這樣就能儲存���一次的遊玩紀錄,而不是覆蓋舊紀錄
|
| 1240 |
+
await fbSDK.addDoc(scoresCollectionRef, {
|
| 1241 |
displayName: displayName,
|
| 1242 |
score: totalScore, // 全域變數中的最新分數
|
| 1243 |
+
lastUpdated: fbSDK.serverTimestamp(), // 儲存伺服器時間
|
| 1244 |
+
userId: userId // 儲存玩家ID以便追蹤
|
| 1245 |
+
});
|
| 1246 |
+
// --- END FIX ---
|
| 1247 |
|
| 1248 |
console.log("Score saved successfully to Firebase:", totalScore);
|
| 1249 |
} catch (error) {
|
|
|
|
| 3212 |
if (scores.length === 0) {
|
| 3213 |
leaderboardHtml = '<p class="text-lg">今天還沒有人完成遊戲!</p>';
|
| 3214 |
} else {
|
| 3215 |
+
// --- FIX: 移除 .slice(0, 10) 來顯示當天所有成績 ---
|
| 3216 |
+
scores.forEach((entry, index) => {
|
| 3217 |
leaderboardHtml += `
|
| 3218 |
<li class="p-2 rounded ${index === 0 ? 'bg-yellow-100 border border-yellow-300' : ''}">
|
| 3219 |
<span class="font-bold text-lg text-blue-600">${entry.score} 分</span> -
|
|
|
|
| 3236 |
</script>
|
| 3237 |
</body>
|
| 3238 |
</html>
|
|
|
|
|
|
|
|
|
|
|
|