The main "Ping"/"Time" function for the game is this:
Net Code For Server - Time/Ping
Outside of the switch, TimeSync() functions like this:
private void TimeSync()
{
YGConnection.Send("Time", DateTime.UtcNow.Millisecond);
}
It sends the current UTCNow.Millisecond time to the server.
Finally, getTime() gets the current time + the offset of the server.
private double getTime()
{
return Math.Round((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds) + Offset;
}
It all works, but the numbers produced are like this:
Version 0.1.5 : Server time offset: -486335789940 - Ping: -943491433067
The server basically sends getTime() back, but without the offset. I'm wondering what is going on with the negative numbers and if there is anything I can do to fix that.
DateTime.Milliseconds is the number of milliseconds (thousandths of a second) within the current second. If you only compare Milliseconds, you will lose any information like the second it occurred in.
At midnight it is 00:00:00.000
500 milliseconds later it is 00:00:00.500
500 milliseconds later it is 00:00:01.000
If you are just comparing the milliseconds, the difference between the first two times will be 500.
The difference between the last two will be -500.
What you probably want to do is return a whole date/time value rather than just the milliseconds. If you subtract 2 DateTime objects, you get a TimeSpan object. From that you can find the TimeSpan.TotalMilliseconds will give you how many milliseconds there are between those two times regardless of how many seconds have elapsed.