Skip to content

Commit

Permalink
Timestamp User add
Browse files Browse the repository at this point in the history
Co-authored-by: DavideBri <DavideBri@users.noreply.github.com>
  • Loading branch information
s-dimaria and DavideBri committed May 24, 2023
1 parent 18d5bfb commit bf88451
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/components/game/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export const GAME_RESULT_MESSAGES = [
"😅 Per un soffio... domani andrà meglio! 😉"
];

export const TIME_TO_DELETE = 604800000;

export const GAME_RESULT_FAILED_MESSAGE = "Non ti preoccupare, chiedi ai tuoi amici, magari loro la sanno!";

export const STRING_COMPARE_LOCALE = "it";
Expand Down
2 changes: 1 addition & 1 deletion src/components/player/NextTimer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function NextTimer() {
clearInterval(interval);
setTimeout(() => {
window.location.reload(true);
}, 2000);
}, 5000);
}

today.setSeconds(today.getSeconds() + 1);
Expand Down
8 changes: 4 additions & 4 deletions src/components/player/NextTimerScore.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ function NextTimerScore() {

if (timeUntilMonday < 0) {
clearInterval(interval);
setTimeout(() => {
console.debug("---- RESET SCORE ----");
resetAllScoreOfUsers();
}, 1500);
//setTimeout(async() => {
console.debug("---- RESET SCORE ----");
resetAllScoreOfUsers();
//}, 1500);
}

today.setSeconds(today.getSeconds() + 1);
Expand Down
15 changes: 12 additions & 3 deletions src/components/utils/firebaseRealtime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getDatabase, ref, set, get, update} from "firebase/database";
import { getDatabase, ref, set, get, update, remove} from "firebase/database";
import { uid } from "uid";
import './firebase';
import { TIME_TO_DELETE } from "../game/Constants";

const db = getDatabase();

Expand Down Expand Up @@ -33,13 +34,21 @@ const getUserByUid = async (uid: any) => {


async function updateUserByUid(uid: any, score: any) {
await update(ref(db, "users/" + uid), score);
await update(ref(db, "users/" + uid), {score: score, timestamp: new Date().getTime()});
}

async function resetAllScoreOfUsers() {
await get(ref(db, "users/")).then((snapshot) => {
snapshot.forEach((u) => {
update(ref(db,"users/" + u.key), {score: 0})
if(u.val().timestamp != undefined) {
if(new Date().getTime() - u.val().timestamp >= TIME_TO_DELETE) {
localStorage.setItem("firstTime", "false");
localStorage.removeItem("Game");
remove(ref(db,"users/" + u.key));
}
update(ref(db,"users/" + u.key), {score: 0})
}

})
});
}
Expand Down

0 comments on commit bf88451

Please sign in to comment.