Server time

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

  • Cheat protection: users will not be able to influence game processes by changing the time on their device.
  • Game events: based on it, you can add activities and rewards that require a trusted time source (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.

1const ysdk = await YaGames.init();
2
3// Returns time in ms, synchronized with the server.
4ysdk.serverTime(); // For example, 1720613073778.
5
6// Call it again after some time.
7ysdk.serverTime(); // For example, 1720613132635.
1YaGames.init()
2    .then((ysdk) => {
3        // Returns time in ms, synchronized with the server.
4        ysdk.serverTime(); // For example, 1720613073778.
5
6        // Some time later, call it again.
7        ysdk.serverTime(); // For example, 1720613132635.
8    });

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

 1YaGames.init()
 2    .then(async (ysdk) => {
 3        // Player initialization.
 4        const player = await ysdk.getPlayer();
 5
 6        // Get saved data.
 7        const data = await player.getData();
 8
 9        // Current server time.
10        const currentTime = ysdk.serverTime();
11
12        // Time when the last reward was received (if not set, use 0).
13        const lastRewardTime = data.lastRewardTime || 0;
14
15        // 24 hours in milliseconds.
16        const DAY_IN_MS = 24 * 60 * 60 * 1000;
17
18        if (currentTime - lastRewardTime >= DAY_IN_MS) {
19            // More than 24 hours have passed — the reward can be given.
20            await giveReward(); // Your function for awarding a reward.
21
22            // Save the new reward claim time.
23            await player.setData({
24                lastRewardTime: currentTime
25            });
26        }
27    });

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

 1YaGames.init()
 2    .then(async (ysdk) => {
 3        // Player initialization.
 4        const player = await ysdk.getPlayer();
 5
 6        // Get saved data.
 7        const data = await player.getData();
 8
 9        // Current server time.
10        const currentTime = ysdk.serverTime();
11
12        // Get the date of the last reward in "YYYY-MM-DD" format.
13        const lastRewardDate = data.lastRewardDate || '';
14
15        // Get the current date in "YYYY-MM-DD" format.
16        const currentDate = new Date(currentTime).toISOString().split('T')[0];
17
18        if (currentDate !== lastRewardDate) {
19            // The reward hasn't been claimed yet today.
20            await giveReward(); // Your function for awarding a reward.
21
22            // Save the reward claim date.
23            await player.setData({
24                lastRewardDate: currentDate
25            });
26        }
27    });

Note

Support staff help you publish a ready-made game on the Yandex Games platform. For applied questions about development and testing, other developers will answer in the Discord community.

If you encounter a problem or have a question while using the Yandex Games SDK, contact support:

Write to chat