-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTime.cs
executable file
·118 lines (104 loc) · 2.47 KB
/
Time.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using Newtonsoft.Json.Linq;
using System.Collections;
using Wizcorp.MageSDK.MageClient;
namespace Wizcorp.MageSDK.Time
{
public class Time : Module<Time>
{
public Timer Server = new Timer();
public Timer Client = new Timer();
/// <summary>
///
/// </summary>
/// <returns></returns>
public IEnumerator SetupTask() {
IEnumerator wait = Synchronize();
while (wait.MoveNext()) {
yield return null;
}
}
/// <summary>
///
/// </summary>
public double Offset {
get { return Client.OffsetMSec - Server.OffsetMSec; }
}
/// <summary>
///
/// </summary>
/// <param name="timestamp"></param>
/// <param name="msecOut"></param>
/// <returns></returns>
public double ServerToClientTime(double timestamp, bool msecOut = false) {
return Client.Translate(timestamp, msecOut);
}
/// <summary>
///
/// </summary>
public double ClientNow(bool msecOut = false) {
return Client.Now(msecOut);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public IEnumerator Synchronize() {
// Retrieve
UserCommandStatus status = Command("sync", new JObject {
{ "clientTime", Timer.DateNowMSec() }
});
while (!status.Done) {
yield return null;
}
// Handle error
if (status.Error != null) {
throw status.Error;
}
// Get time configuration
JToken config = status.Result;
// Configure server timer
Server.Configure(
(double)config["timer"]["offset"] - (double)config["delta"],
(double)config["timer"]["accelerationFactor"],
(double)config["timer"]["startAt"] + (double)config["delta"]
);
// Configure client timer
Client.Configure(
(double)config["timer"]["offset"],
(double)config["timer"]["accelerationFactor"],
(double)config["timer"]["startAt"] + (double)config["delta"]
);
}
/// <summary>
///
/// </summary>
/// <param name="timestamp"></param>
/// <param name="msecOut"></param>
/// <returns></returns>
public double Translate(double timestamp, bool msecOut = false) {
return Server.Translate(timestamp, msecOut);
}
/// <summary>
///
/// </summary>
/// <param name="msecOut"></param>
/// <returns></returns>
public double Now(bool msecOut = false) {
return Server.Now(msecOut);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public double MSec() {
return Server.MSec();
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public double Sec() {
return Server.Sec();
}
}
}