Tracking user votes (one vote on ie Reddit)

I am working on an application that allows users to influence the ranking of players and trends on a live table (fantasy football).

enter image description here

As you can see, you can promote or lower players (only if you are logged in and authenticated through Firebase (Google Accounts)). Otherwise, if you are not logged in, no votes are registered. It is currently working fine.

My question is , what would be the easiest way so that each user can only vote for a player once . The current implementation allows you to unlimitedly increase or decrease the votes for any player. I wish I could promote Tom Brady but not redirect him again. From now on, I could only change it without a vote (by clicking again) or downvote. I see that there is a user UID in firebase, so I could check if the UserUID voted from a specific player in the Firebase database or not before allowing the vote, but what if the user just wants to switch from vote to vote down?

Here's the code regarding voting -

downvotePlayer(playerId) { this.state.user ? this.database.child(playerId).transaction(function (player) { if (player) { player.votes-- } return player; }) : console.log("Must log in to vote") } upvotePlayer(playerId) { this.state.user ? this.database.child(playerId).transaction(function (player) { if (player) { console.log(player) player.votes++ } return player; }) : console.log("Must be logged in to vote.") } 

Here's the layout on Firebase:

enter image description here

If I left all the critical points, please let me know and I will throw it into the essence. Thanks in advance.


Edit:

Source DB ref

  this.database = firebase.database().ref().child('players'); 

When adding an add-on to the player, I do this:

  this.database.child(playerId).transaction(function (player) { if (player) { var ref = firebase.database().ref('players').child('voters'); ref.child(uid).set(1); } 

Which does almost what it needs, except that it creates a new node all together in firebase.

enter image description here

Voters must be inside a unique player above him. Is my problem obvious? I appreciate the help sir.

+5
source share
2 answers

The easiest way to simulate this is to keep one counter per player for โ€œthings they can vote onโ€. For instance:

 "Votes": { "Tom Brady": { "uidOfVoter1": 1, "uidOfVoter2": -1 } } 

The uidOfVoter values โ€‹โ€‹are unique that you give each player, that is, the UID that they receive from Firebase Authentication. In such a structure, each UID can be present only once. Then you guarantee that each user can only write their own voice, with Firebase security rules:

 { "rules": { "Votes": { "$thing": { "$uid": { ".write": "auth.uid === $uid", ".validate": "data.isNumber() && data.val() >= -1 && data.val() <= 1" } } } } } 

Now, to get the total number of votes, follow these steps:

  • Download voices and count them on the client.
  • Refresh the counter when the user votes, spelling security rules to ensure that the vote they write and the count are the same.
  • Use the cloud features to update the counter whenever the voice is changed.
+3
source

Boolean. + true and - false. The existence of a UID indicates a vote (r).

I canโ€™t say that coding reacts specifically, but it seems that this will be the easiest (best) way to imagine what you are after.

Disable / disable the corresponding vote button in the client user interface after voting in any case. Even if they crack the user interface, the server side (db side) is logical, so you cannot true true for +2.

 DB Structure thing votes <uid1>:true, <uid2>:true, <uid3>:false, 

If the user cancels the vote, simply remove the uid from the item they voted on.

+2
source

Source: https://habr.com/ru/post/1274873/


All Articles