Skip to content

Commit

Permalink
Fix script to reset scoreboard
Browse files Browse the repository at this point in the history
  • Loading branch information
s-dimaria committed Feb 5, 2024
1 parent 13d6b57 commit 1ef570d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 20 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/resetParameters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ name: Reset Firebase Database

on:
schedule:
- cron: '0 0 * * 1' # Esegui ogni lunedì alle 00:00
- cron: '0 23 * * 1'

jobs:
reset-firebase-database:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
Expand All @@ -23,12 +23,12 @@ jobs:
- name: Check Script File
run: |
if [ ! -f "heardle-ita/script-score-reset.js" ]; then
if [ ! -f "script-score-reset.js" ]; then
echo "Error: Script file not found."
exit 1
fi
- name: Run Reset Script
env:
FIREBASE_CRED: ${{ secrets.FIREBASE_CRED }}
run: node heardle-ita/script-score-reset.js
run: node script-score-reset.js
57 changes: 41 additions & 16 deletions script-score-reset.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var admin = require("firebase-admin");

var serviceAccount = JSON.parse(process.env.FIREBASE_CRED);
const admin = require("firebase-admin");
const serviceAccount = JSON.parse(process.env.FIREBASE_CRED);

admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
Expand All @@ -10,18 +9,44 @@ admin.initializeApp({
const TIME_TO_DELETE = 604800000;

// As an admin, the app has access to read and write all data, regardless of Security Rules
var db = admin.database();
var ref = db.ref("users");
ref.once("value", function(snapshot) {
console.debug("Users candidated:", snapshot.numChildren());
snapshot.forEach((u) => {
if (new Date().getTime() - u.val().timestamp >= TIME_TO_DELETE) {
// Use remove method to delete the node
ref.child(u.key).remove();
const db = admin.database();
const usersRef = db.ref("users");

// Function to remove all users
const removeAllUsers = async () => {
try {
const snapshot = await usersRef.once('value');

// Check if there are users in the database
if (snapshot.exists()) {
// Array to store promises for update operations
const updatePromises = [];

snapshot.forEach((userSnapshot) => {
if (!userSnapshot.val().timestamp || new Date().getTime() - userSnapshot.val().timestamp >= TIME_TO_DELETE) {
// Use remove method to delete the node
updatePromises.push(usersRef.child(userSnapshot.key).remove());
} else {
// Use update method to update the score for specific user
updatePromises.push(usersRef.child(userSnapshot.key).update({ score: 0 }));
}
});

// Wait for all update promises to complete
await Promise.all(updatePromises);

console.log('All users removed or updated successfully.');
} else {
console.log('No users to remove.');
}
ref.child(u.key).update({ score: 0 });
});
} catch (error) {
console.error('Error removing users:', error);
} finally {
// Exit the script
process.exit(0);
}
};

// Call the function to remove all users
removeAllUsers();

console.debug("Users final:", snapshot.numChildren());
process.exit(0); // 0 indicates successful termination
});

0 comments on commit 1ef570d

Please sign in to comment.