0

How can I reset the scores of the game on certain days using firebase in "Unity"

enter image description here

I want the scores I marked in the picture to be reset every day, every week, at the end of every month, in short, when their time comes. How can I do this?

  • You have several options by schedule jobs: [Windows service](https://stackoverflow.com/questions/19151363/windows-service-to-run-a-function-at-specified-time) for free, or paid [Cloud Scheduler](https://firebase.googleblog.com/2019/04/schedule-cloud-functions-firebase-cron.html) and [Azure Function](https://learn.microsoft.com/en-us/azure/azure-functions/) – Tấn Nguyên Jun 26 '20 at 04:12

1 Answers1

2

What you want to look into is the Cloud Functions feature called scheduled functions.

If you're only familiar with Unity, you'll want to follow this getting started guide for more details. The basic gist of it is that you'll create a tiny snippet of JavaScript that runs at a fixed schedule and lets you perform some administrative tasks on your database.

I'll try to encapsulate the basic setup:

  • install Node
  • run npm install -g firebase-tools
  • create a directory where you want to work on functions - you probably want to to do this outside of your Unity directory
  • run firebase login to log in to the Firebase CLI
  • run firebase init (or firebase init functions) and follow the steps in the wizard to create some functions code to test
  • when you're ready to use them in your game, you can use firebase deploy to send them off to the cloud.

From the Scheduled functions doc page, you can see this example of how to run a function every day:

exports.scheduledFunctionCrontab = functions.pubsub.schedule('5 11 * * *')
  .timeZone('America/New_York') // Users can choose timezone - default is America/Los_Angeles
  .onRun((context) => {
  console.log('This will be run every day at 11:05 AM Eastern!');
  return null;
});

You can use these with the Node Admin SDK. Something like:

// Import Admin SDK
var admin = require("firebase-admin");

// Get a database reference to our blog
var db = admin.database();

exports.scheduledFunctionCrontab = functions.pubsub.schedule('5 11 * * *')
  .timeZone('America/New_York') // Users can choose timezone - default is America/Los_Angeles
  .onRun((context) => {
  db.ref(`users/${user_id}/`).update({userGameScore: 0, userMonthScore: 0, userScore: 0, userWeeklyScore: 0});
  return null;
});

Of course, here I'm not iterating over user ids &c.

One final note: this is a very literal interpretation and answer to your question. It may be easier to (and save you some money if your game scales up) to write a score and timestamp (maybe using ServerValue.Timestamp) together into your database and just cause the scores to appear zeroed out as client logic. I would personally first try taking this approach and abandon it if it felt like it was getting too complex.

Patrick Martin
  • 2,993
  • 1
  • 13
  • 11
  • thanks for your help but i couldn't fully understand exactly where i should write these codes so i mean the above question automatically reset the score by firebase console on a certain date I am asking because I do not know if this sample score you gave is reset from within the game or by console as I said. and sorry for my bad english grammer – Bernard Montgomery Jun 26 '20 at 18:51
  • Ok, I made some edits that hopefully make it clearer! Basically the code I provided is JavaScript (rather than C#) that will run on Firebase using a feature called "Cloud Functions". This is a bit of a learning curve, but with "Scheduled Functions" you can run code in the cloud at a fixed interval and by using the Admin SDK you can make that code edit resources in your Firebase project. I hope that clears it up, and if not feel free to keep hashing it out here or reach out to @pux0r3 on Twitter! – Patrick Martin Jun 26 '20 at 20:39