Server time

Yandex Games SDK allows you to get time synchronized with the server. This method is useful for:

  • Protection against cheating: users won't be able to affect game processes by changing the time on their device.
  • Game events: you can add activities and rewards for which a trusted source of time is important, for example: daily or weekly bonuses, seasonal events, and quests.

ysdk.serverTime()

This method returns a timestamp, the server time in milliseconds, the same for all devices. It is similar to Date.now() in the format of the result, however, the latter returns the user's device time, which may differ from server time and is not protected against cheating by players. The ysdk.serverTime() method is resistant to tampering with the device's system time, making it more reliable.

Call it every time you need to get the current time.

const ysdk = await YaGames.init();

// Returns time in ms, synchronized with the server.
ysdk.serverTime(); // For example, 1720613073778.

// Call it again after some time.
ysdk.serverTime(); // For example, 1720613132635.

YaGames.init().then(ysdk => {

  // Returns time in ms, synchronized with the server.
  ysdk.serverTime(); // For example, 1720613073778.

  // Some time later, call it again.
  ysdk.serverTime(); // For example, 1720613132635.
});

Examples of Implementing Daily Rewards

  • ysdk.serverTime() is used to get reliable server time.
  • Data is saved using player.setData().
  • Duplicate reward claiming is prevented.
  • The time is compared in a secure way.

Warning

The giveReward() function in the examples is your own implementation of granting a reward to the player.

Reward 24 hours after the last game visit

YaGames.init().then(async ysdk => {
    // Player initialization.
    const player = await ysdk.getPlayer();

    // Get saved data.
    const data = await player.getData();

    // Current server time.
    const currentTime = ysdk.serverTime();

    // Time when the last reward was received (if not set, use 0).
    const lastRewardTime = data.lastRewardTime || 0;

    // 24 hours in milliseconds.
    const DAY_IN_MS = 24 * 60 * 60 * 1000;

    if (currentTime - lastRewardTime >= DAY_IN_MS) {
        // More than 24 hours have passed — the reward can be given.
        await giveReward(); // Your function for awarding a reward.

        // Save the new reward claim time.
        await player.setData({
            lastRewardTime: currentTime
        });
    }
});

Reward is given once per calendar day (reset at midnight UTC).

YaGames.init().then(async ysdk => {
    // Player initialization.
    const player = await ysdk.getPlayer();

    // Get saved data.
    const data = await player.getData();

    // Current server time.
    const currentTime = ysdk.serverTime();

    // Get the date of the last reward in "YYYY-MM-DD" format.
    const lastRewardDate = data.lastRewardDate || '';

    // Get the current date in "YYYY-MM-DD" format.
    const currentDate = new Date(currentTime).toISOString().split('T')[0];

    if (currentDate !== lastRewardDate) {
        // The reward hasn't been claimed yet today.
        await giveReward(); // Your function for awarding a reward.

        // Save the reward claim date.
        await player.setData({
            lastRewardDate: currentDate
        });
    }
});

Note

Our support team can help publish finished games on Yandex Games. If you have any questions about development or testing, ask them in the Discord channel.

If you are facing an issue or have a question regarding the use of Yandex Games SDK, please contact support:

Write to chat