I am just staring off with firebase and with NoSQL in general and I came across an issue with data structuring for best usage of the application.
I have an application for saving results of 4 player foosball (table soccer) games. Each player has hes own rating and after each game the rating is recalculated. Also there are individual game results (goals, own goals) and player positions saved each game. I have 2 main views in the app - Game View where i insert game results and Ratings view where i have a scoreboard of players ranked by their rating and avg goals scored.
At the moment I have the following structure:
// players
{
"playerID": {
firstName: string,
lastName: string,
ratings: {
overall: { mu: string, number: string }
}
}
}
// games
{
"gameID": {
participants: {
"HOME_DEF": {
playerID: string,
firstName: string,
lastName: string,
ratings: { overall: { mu: string, number: string } }
adjustedRatings: { overall: { mu: string, number: string } }
goals: number,
ownGoals: number,
},
"AWAY_DEF": { ... },
"HOME_OFF": { ... },
"AWAY_OFF": { ... },
}
time: elapsedTime,
}
}
With this I am saving every game as an object and recalculate the ratings with Cloud Function on game onCreate event. From what I read during the onCreate event I can not access other nodes (like players) so I can not update players rating.
I have 2 questions:
What would be necessary to adjust this schema so I could easily update data for player rating and have it available for views?
How could I calculate avg goals scored and save it in players object?