Home > Software design >  How can I add this data (data in form of Range) in Firestore database field?
How can I add this data (data in form of Range) in Firestore database field?

Time:10-05

I am making a Gaming app. I want to store some data in field of firestore database, Regarding the Player prize distribution. The User Whose Rank is 1 to 200 gets 5000 coins , Rank 201 to 400 gets 2500coins , and rank 401 to 500 gets 1000coins. How can I add these field in the database and use later to compare how user performed in the game and award them the coins?

Please NOTE- I am not asking about querying, i am asking that how to add and store the range data in field of Firestore. I want the data stored just as shown in the image.

I am figuring out how to add the range data...This is what i am doing right now..See the imageenter image description here

Basically its a range...

Rank 1 to 200 = 5000coins
Rank 201 to 400 = 2500coins
Rank 401 to 500 = 1000coins

CodePudding user response:

Not sure this is the best approach for Firestore (coming from a SQL background and I'm still learning best practices for NoSQL/Firestore), but here's how I would do it: I would add to the Game/Event document a field prizeRange that's of type array, and it contains 3 maps, each map having a min, max and prizeVal number fields.

firestore range

And your document field will look like this:

firestore range array of maps

And when you want to add a prize to a player, you simply read that Game/Event document and you'll have access to prizeRange array, on which you can iterate and compare the player's rank with the min and max for each of those 3 maps. Just tried something like this and it works as expected:

data = res.data();
data.prizeRanges.forEach((range: any) => {
  if (range.min <= playerRank && playerRank <= range.max) {
    this.playerPrize = range.prizeVal;
  }
});
  • Related