I may indeed be a blunt object but I’ve been trying to figure out the best way to iterate through my high scores array which contains ‘scoreEntry’ maps. As I understand it, such behavior is prohibited by Google to avoid infinite looping issues & ‘abuse’ of the security rules system. Not that any of that makes sense to a smooth brain such as myself, but maybe there is already an easy solution which I just can’t find at the moment. Here is the db structure as it lies:
Classic (Document) // aka {gameMode}
│
└── ClassicScores (Array)
│
├── [0] (Map)
│ ├── milliseconds: <value>
│ ├── numberCorrect: <value>
│ ├── score: <value>
│ ├── timestamp: <value>
│ └── userID: <value>
│
├── [1] (Map)
│ ├── milliseconds: <value>
│ ├── numberCorrect: <value>
│ ├── score: <value>
│ ├── timestamp: <value>
│ └── userID: <value>
Currently the best implementation I’ve been able to get working is this:
match /HighScores/{gameMode} {
// Even null users may read the leaderboard
allow read;
// Only authenticated users may update the leaderboard
allow write: if request.auth != null && validateHighScores();
}
function validateHighScores() {
let highScores = request.resource.data.ClassicScores;
let score0 = isValidEntry(highScores[0]);
let score1 = isValidEntry(highScores[1]);
let score2 = isValidEntry(highScores[2]);
let score3 = isValidEntry(highScores[3]);
let score4 = isValidEntry(highScores[4]);
let score5 = isValidEntry(highScores[5]);
let score6 = isValidEntry(highScores[6]);
let score7 = isValidEntry(highScores[7]);
let score8 = isValidEntry(highScores[8]);
let score9 = isValidEntry(highScores[9]);
return score0 && score1 && score2 && score3 && score4 && score5 && score6 && score7 && score8 && score9;
}
function isValidEntry(scoreEntry) {
let validCorrect = scoreEntry.numberCorrect <= 63; // maximum of 63 correct
let validScore = scoreEntry.score < 100; // percentage score maximum 100%
let containsFields = scoreEntry.keys().hasAll(['milliseconds', 'numberCorrect', 'score', 'timestamp', 'userID']); //contains all required keys
return validCorrect && validScore && containsFields;
}
But of course, it would be nice to be able to iterate through the highScores array, or leastways be able to achieve the same functionality without writing 5 different functions & coupling them together to reach the maximum of 50 high score entries. Surely there is a better way…
Again sorry if I’m being braindead on this and I appreciate your assistance/recommendations.