package com.example.memorymaster; import androidx.annotation.NonNull; import…
- User
package com.example.memorymaster;
import androidx.annotation.NonNull;
import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.*;
public class FirebaseHelper { private DatabaseReference databaseReference;
public FirebaseHelper() { FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance(); databaseReference = firebaseDatabase.getReference(); } // Callback interface public interface UpdateStatsCallback { void onUpdateCompleted(boolean newRecord); void onUpdateFailed(Exception e); } // -------- Memory Game Stats -------- public void updateMemoryGameStats(String userId, String difficulty, int newScore, long newTime, UpdateStatsCallback callback) { String path = "memory_game_statistics/" + userId + "/" + difficulty; DatabaseReference userStatsRef = databaseReference.child(path); userStatsRef.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot snapshot) { MemoryGameStats stats = snapshot.getValue(MemoryGameStats.class); final boolean[] newRecord = {false}; if (stats == null) { stats = new MemoryGameStats(newScore, newTime, newTime, newScore); newRecord[0] = true; } else { if (newTime < stats.getBestTime()) { stats.setBestTime(newTime); stats.setBestTimeScore(newScore); newRecord[0] = true; } if (newScore > stats.getBestScore()) { stats.setBestScore(newScore); stats.setBestScoreTime(newTime); newRecord[0] = true; } } userStatsRef.setValue(stats).addOnCompleteListener(task1 -> { if (task1.isSuccessful()) { if (callback != null) { callback.onUpdateCompleted(newRecord[0]); } } else { if (callback != null) { callback.onUpdateFailed(task1.getException()); } } }); } @Override public void onCancelled(@NonNull DatabaseError error) { if (callback != null) { callback.onUpdateFailed(error.toException()); } } }); } // -------- Reaction Game Stats -------- public void updateReactionGameStats(String userId, int reactionTime, UpdateStatsCallback callback) { String path = "reaction_game_statistics/" + userId; DatabaseReference userStatsRef = databaseReference.child(path); userStatsRef.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot snapshot) { Integer bestReactionTime = snapshot.getValue(Integer.class); boolean newRecord = false; if (bestReactionTime == null || reactionTime < bestReactionTime) { userStatsRef.setValue(reactionTime).addOnCompleteListener(task1 -> { if (task1.isSuccessful()) { if (callback != null) { callback.onUpdateCompleted(true); } } else { if (callback != null) { callback.onUpdateFailed(task1.getException()); } } }); } else { if (callback != null) { callback.onUpdateCompleted(false); } } } @Override public void onCancelled(@NonNull DatabaseError error) { if (callback != null) { callback.onUpdateFailed(error.toException()); } } }); } // -------- Pair Matching Game Stats -------- public void updatePairMatchingStats(String userId, int moves, long time, UpdateStatsCallback callback) { String path = "pair_matching_statistics/" + userId; DatabaseReference userStatsRef = databaseReference.child(path); userStatsRef.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot snapshot) { PairMatchingStats stats = snapshot.getValue(PairMatchingStats.class); final boolean[] newRecord = {false}; if (stats == null) { stats = new PairMatchingStats(moves, time, moves, time); newRecord[0] = true; } else { if (moves < stats.getBestMoves()) { stats.setBestMoves(moves); stats.setBestTime(time); newRecord[0] = true; } if (time < stats.getFastestTime()) { stats.setFastestMoves(moves); stats.setFastestTime(time); newRecord[0] = true; } } userStatsRef.setValue(stats).addOnCompleteListener(task1 -> { if (task1.isSuccessful()) { if (callback != null) { callback.onUpdateCompleted(newRecord[0]); } } else { if (callback != null) { callback.onUpdateFailed(task1.getException()); } } }); } @Override public void onCancelled(@NonNull DatabaseError error) { if (callback != null) { callback.onUpdateFailed(error.toException()); } } }); } // MemoryGameStats model class public static class MemoryGameStats { private int bestScore; private long bestScoreTime; private long bestTime; private int bestTimeScore; // Required for Firebase public MemoryGameStats() { } public MemoryGameStats(int bestScore, long bestScoreTime, long bestTime, int bestTimeScore) { this.bestScore = bestScore; this.bestScoreTime = bestScoreTime; this.bestTime = bestTime; this.bestTimeScore = bestTimeScore; } public int getBestScore() { return bestScore; } public void setBestScore(int bestScore) { this.bestScore = bestScore; } public long getBestScoreTime() { return bestScoreTime; } public void setBestScoreTime(long bestScoreTime) { this.bestScoreTime = bestScoreTime; } public long getBestTime() { return bestTime; } public void setBestTime(long bestTime) { this.bestTime = bestTime; } public int getBestTimeScore() { return bestTimeScore; } public void setBestTimeScore(int bestTimeScore) { this.bestTimeScore = bestTimeScore; } } // PairMatchingStats model class public static class PairMatchingStats { private int bestMoves; private long bestTime; private int fastestMoves; private long fastestTime; // Required for Firebase public PairMatchingStats() { } public PairMatchingStats(int bestMoves, long bestTime, int fastestMoves, long fastestTime) { this.bestMoves = bestMoves; this.bestTime = bestTime; this.fastestMoves = fastestMoves; this.fastestTime = fastestTime; } public int getBestMoves() { return bestMoves; } public void setBestMoves(int bestMoves) { this.bestMoves = bestMoves; } public long getBestTime() { return bestTime; } public void setBestTime(long bestTime) { this.bestTime = bestTime; } public int getFastestMoves() { return fastestMoves; } public void setFastestMoves(int fastestMoves) { this.fastestMoves = fastestMoves; } public long getFastestTime() { return fastestTime; } public void setFastestTime(long fastestTime) { this.fastestTime = fastestTime; } } - Assistant