-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
220 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
export default class FirebaseManager { | ||
constructor() { | ||
// Your web app's Firebase configuration | ||
const firebaseConfig = { | ||
apiKey: "AIzaSyASSEoYYNf-jb5FlSbq9Fj5CNxj4X-69_I", | ||
authDomain: "puchis-adventure.firebaseapp.com", | ||
projectId: "puchis-adventure", | ||
storageBucket: "puchis-adventure.appspot.com", | ||
messagingSenderId: "836031237974", | ||
appId: "1:836031237974:web:81d54b55d78025b39cd0f7" | ||
}; | ||
firebase.initializeApp(firebaseConfig); | ||
|
||
this.db = firebase.firestore(); | ||
} | ||
|
||
async addScore(initials, score) { | ||
try { | ||
await this.db.collection("scores").add({ | ||
initials, | ||
score, | ||
timestamp: firebase.firestore.FieldValue.serverTimestamp() | ||
}); | ||
} catch (error) { | ||
console.error("Error adding score to Firebase", error); | ||
} | ||
} | ||
|
||
async getScores() { | ||
try { | ||
const scoresSnapshot = await this.db.collection("scores") | ||
.orderBy("score", "desc") | ||
.limit(50) | ||
.get(); | ||
|
||
return scoresSnapshot.docs.map(doc => doc.data()); | ||
} catch (error) { | ||
console.error("Error fetching scores from Firebase", error); | ||
return []; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import Game from './Game.js'; | ||
|
||
export default class RankingManager { | ||
constructor(firebaseManager) { | ||
this.firebaseManager = firebaseManager; | ||
this.scores = []; | ||
} | ||
|
||
async getScoresFromFirebase() { | ||
this.scores = await this.firebaseManager.getScores(); | ||
this.renderScores(); | ||
} | ||
|
||
addNewScore(initials, score) { | ||
this.firebaseManager.addScore(initials, score) | ||
.then(() => this.getScoresFromFirebase()) | ||
.catch(error => console.error("Error adding new score", error)); | ||
} | ||
|
||
renderScores() { | ||
this.formatRanking(); | ||
} | ||
|
||
async updateRankingUI() { | ||
await this.getScoresFromFirebase(); // Asume que este método obtiene las puntuaciones de Firebase | ||
this.formatRanking(); | ||
} | ||
|
||
formatRanking() { | ||
const rankingList = document.getElementById('scoreList'); | ||
rankingList.innerHTML = ''; // Limpia la lista actual | ||
|
||
this.scores.forEach((score, index) => { | ||
const listItem = document.createElement('li'); | ||
let points = parseInt(score.score); | ||
let score_text = score.score.toString(); | ||
if (0 < points) { | ||
score_text = score_text.replace(/\B(?=(\d{3})+(?!\d))/g, ".") | ||
} | ||
listItem.textContent = `${score.initials}: ${score_text}`; | ||
rankingList.appendChild(listItem); | ||
}); | ||
} | ||
} | ||
|
||
|
||
// Asegúrate de que el DOM esté completamente cargado antes de ejecutar tu script | ||
document.addEventListener('DOMContentLoaded', function () { | ||
// Aquí puedes instanciar y configurar tu juego y rankingManager | ||
const game = new Game(); | ||
const rankingManager = new RankingManager(); | ||
let isSaveButtonEventAttached = false; | ||
if (isSaveButtonEventAttached) return; | ||
|
||
// Configurar el manejador del evento click para el botón de guardar | ||
document.getElementById('saveScoreButton').addEventListener('click', async function () { | ||
const initials = document.getElementById('playerInitials').value; | ||
initials.focus(); | ||
const score = game.getCurrentScore(); // Suponiendo que esta función existe en game.js | ||
|
||
if (initials && null !== score) { | ||
await rankingManager.addNewScore(initials, score); | ||
await rankingManager.updateScores(); // Esto actualizará y mostrará el ranking | ||
} | ||
}); | ||
isSaveButtonEventAttached = true; | ||
|
||
// Aquí puedes incluir cualquier otra configuración inicial | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// RankingIntegration.js | ||
import FirebaseManager from '../FirebaseManager.js'; | ||
import RankingManager from '../RankingManager.js'; | ||
|
||
const firebaseManager = new FirebaseManager(); | ||
const rankingManager = new RankingManager(firebaseManager); | ||
|
||
export async function updateRanking(game) { | ||
document.getElementById('saveScoreButton').addEventListener('click', function () { | ||
const initials = document.getElementById('playerInitials').value; | ||
const score = game.getCurrentScore(); | ||
|
||
// Guardar la puntuación en Firebase y actualizar el ranking | ||
rankingManager.addNewScore(initials, score); | ||
}); | ||
rankingManager.getScoresFromFirebase(); | ||
await rankingManager.updateRankingUI(); // Actualiza y muestra el ranking | ||
} | ||
|
||
export function submitScore(initials, score) { | ||
rankingManager.addNewScore(initials, score); | ||
} |