question

Ozan Yilmaz avatar image
Ozan Yilmaz asked

What is equavilent of Date.getTime in C#?

Hello everyone,

I'm trying to return server time to a client when the client logs in to calculate some time based events. This is a one time process when the player logs in. When I return the time from the server, how can I read it correctly in C#?

I tried "double" variable but it sometimes works, sometimes doesn't. For example; an event that has a countdown of 1 minute is sometimes shown as 3 minutes.

Here's how I return the time from server.

handlers.GetServerTime = function(args, context) {
    return { "Time": new Date().getTime() };
}

Here's how I convert the value in the game.

    private void OnGetServerTimeSuccess(ExecuteCloudScriptResult data)
    {
        JsonObject jsonResult = (JsonObject)data.FunctionResult;
        object value;
        if (jsonResult.TryGetValue("Time", out value))
        {
            ServerTime = double.Parse(value.ToString());
        }
    }

And here's how I calculate the countdown. I save a time in the players' read only data. When they login, I substract two values to get how much time left.

double miliseconds = loginResult.UserReadOnlyData.ContainsKey("FreeLootbox") ? : double.Parse(loginResult.UserReadOnlyData["FreeLootbox"].Value) : ServerTime;
            

double difference = miliseconds - ServerTime;
DateTime nextLootbox = difference > 0 ? DateTime.Now.AddMilliseconds(difference) : DateTime.now;
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Seth Du avatar image
Seth Du answered

May I ask what is the scenario in details?

In terms of the timestamp, since The getTime() method in Javascript returns the number of milliseconds since midnight of January 1, 1970 and the specified date. You may refer to DateTimeOffset.ToUnixTimeMilliseconds Method, please see at: https://www.techiedelight.com/get-milliseconds-since-unix-epoch-csharp/

4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Ozan Yilmaz avatar image Ozan Yilmaz commented ·

Basically, the players can open a free loot box in every 10 minutes. Here's the what I do step by step. Let's say the player hasn't open the free loot box before.

- When the player opens the free loot box; in cloudscript, I save the opening time in the player's read only data and in the client side, I set a timer, so that the player cannot call the same function before that 10 minutes.

- If the player somehow calls the function to open the free loot box before the time limit, I check if that 10 minutes passed depending on the opening time. In cloudscript, I get the server time and the opening time and basically do this process;

//... get the player's read only data and save the opening time value in a variable called 'lastOpening'

var difference = new Date().getTime() - lastOpening;

// Check if 10 minutes passed
// 600*100 is 10 minutes
if(difference > 600 * 1000} {
     // 10 minutes didn't pass
     return 0;
}

//... open the free loot box<br>

(Due to the characters limit, I'll continue on the next comment)

0 Likes 0 ·
Ozan Yilmaz avatar image Ozan Yilmaz commented ·

- Let's say the player logged out and logged in again. In this case, the timer will be disabled since the game is closed. I basically do this and the codes below only work when the player logs in.

// Cloudscript
handlers.GetServerTime = function(args, context) {
    return { "Time": new Date().getTime() };
}

// Client side
    private void OnGetServerTimeSuccess(ExecuteCloudScriptResult data)
    {
        JsonObject jsonResult = (JsonObject)data.FunctionResult;
        object value;
        if (jsonResult.TryGetValue("Time", out value))
        {
            ServerTime = double.Parse(value.ToString());
        }
    }

In the code above, I parse the function result, which is the server time, to 'double' and save it.

(Due to the characters limit, I'll continue on the next comment)

0 Likes 0 ·
Ozan Yilmaz avatar image Ozan Yilmaz commented ·
// Check if the related data key exists. If not, set lastOpening to 0
double lastOpening = loginResult.UserReadOnlyData.ContainsKey("FreeLootbox") ? : double.Parse(loginResult.UserReadOnlyData["FreeLootbox"].Value) : 0;

double difference = ServerTime - lastOpening;
// Check if the difference is lower than 10 minutes. If so, get the current time and add the difference to the current. If not, the player can open the loot box now.
DateTime nextLootBoxTime = difference <= 600 * 1000 ? DateTime.Now.AddMiliseconds(difference) : DateTime.now;

In this case, the time (the variable, 'difference') sometimes must be not correct since the the limit is wrong sometimes. I hope, I could explained the process.

0 Likes 0 ·
Seth Du avatar image Seth Du ♦ Ozan Yilmaz commented ·

Your solution sounds feasible. I think you may also retrieve the "FreeLootbox" player data everytime the player logs into the game for the local timer. In terms of the claiming process, you should handle it completely on Cloud Script, where the function gets the current time and retrieve the timestamp in player read-only data for comparison.

If you feel like the timestamp is not accurate, you may consider logging those timestamp for more times to see where the problem is.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.