-
Notifications
You must be signed in to change notification settings - Fork 9
/
Timer.cs
365 lines (301 loc) · 13.7 KB
/
Timer.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
using System;
namespace FFACETools
{
public partial class FFACE
{
/// <summary>
/// Class wrapper to consume timer methods
/// </summary>
public class TimerTools
{
#region Classes
/// <summary>
/// Structure containing
/// </summary>
public struct VanaTime
{
#region Members
/// <summary>
/// The current day of the week (ie: earthsday)
/// </summary>
public Weekday DayType { get; set; }
/// <summary>
/// Current game day
/// </summary>
public byte Day { get; set; }
/// <summary>
/// Current game month
/// </summary>
public byte Month { get; set; }
/// <summary>
/// Current game year
/// </summary>
public short Year { get; set; }
/// <summary>
/// Current game hour
/// </summary>
public byte Hour { get; set; }
/// <summary>
/// Current game minute
/// </summary>
public byte Minute { get; set; }
/// <summary>
/// Current game second
/// </summary>
public byte Second { get; set; }
/// <summary>
/// Current moon percent
/// </summary>
public byte MoonPercent { get; set; }
/// <summary>
/// Current moon phase
/// </summary>
public MoonPhase MoonPhase { get; set; }
/// <summary>
/// If true, Moon phase is waxing, otherwise false
/// </summary>
public bool Waxing { get; set; }
#endregion
/// <summary>
/// Returns a string representation of the Vana'Diel time details
/// </summary>
/// <returns></returns>
public override string ToString ()
{
string vTime = Month + "/" + Day + "/" + Year + ", "
+ GetDayOfWeekName(DayType) + ", "
+ Hour + ":";
if (10 > Minute)
vTime += "0" + Minute + ", "
+ GetMoonPhaseName(MoonPhase) + " (" + MoonPercent + "%)";
else
vTime += Minute + ", "
+ GetMoonPhaseName(MoonPhase) + " (" + MoonPercent + "%)";
return vTime;
} // @ public override string ToString()
/// <summary>
/// Will get a string representation of the current day of the week
/// </summary>
/// <param name="day">Weekday to convert to string</param>
public string GetDayOfWeekName (Weekday day)
{
string dayweek = String.Empty;
switch (day)
{
case Weekday.Darksday:
dayweek = "Darksday";
break;
case Weekday.Earthsday:
dayweek = "Earthsday";
break;
case Weekday.Firesday:
dayweek = "Firesday";
break;
case Weekday.Iceday:
dayweek = "Iceday";
break;
case Weekday.Lightningday:
dayweek = "Lightningday";
break;
case Weekday.Lightsday:
dayweek = "Lightsday";
break;
case Weekday.Watersday:
dayweek = "Watersday";
break;
case Weekday.Windsday:
dayweek = "Windsday";
break;
} // @ switch (day)
return dayweek;
} // @ public string GetDayOfWeekName(Weekday day)
/// <summary>
/// Will get a string representation of the current moon phase
/// </summary>
/// <param name="phase">MoonPhase to get a proper String equivalent of.</param>
/// <returns></returns>
public string GetMoonPhaseName (MoonPhase phase)
{
string phaseName = String.Empty;
switch (phase)
{
case MoonPhase.FirstQuarter:
phaseName = "First Quarter";
break;
case MoonPhase.Full:
phaseName = "Full";
break;
case MoonPhase.LastQuarter:
phaseName = "Last Quarter";
break;
case MoonPhase.New:
phaseName = "New";
break;
case MoonPhase.WaningCrescent:
case MoonPhase.WaningCrescent2:
phaseName = "Waning Crescent";
break;
case MoonPhase.WaningGibbous:
case MoonPhase.WaningGibbous2:
phaseName = "Waning Gibbous";
break;
case MoonPhase.WaxingCrescent:
case MoonPhase.WaxingCrescent2:
phaseName = "Waxing Crescent";
break;
case MoonPhase.WaxingGibbous:
case MoonPhase.WaxingGibbous2:
phaseName = "Waxing Gibbous";
break;
} // @ switch (phase)
return phaseName;
} // @ public string GetMoonPhaseName(MoonPhase phase)
} // @ public struct VanaTime
#endregion
#region Constructor
/// <summary>
/// Constructor
/// </summary>
/// <param name="instanceID">Instance ID generated by FFACE</param>
public TimerTools (int instanceID)
{
_InstanceID = instanceID;
} // @ public TimerWrapper(int instance)
#endregion
#region Members
/// <summary>
/// Instance ID generated by FFACE
/// </summary>
private int _InstanceID { get; set; }
/// <summary>
/// Maximum Ability Index for use with GetAbilityRecast/GetAbilityID
/// </summary>
static readonly public byte MAX_ABILITY_INDEX = 32;
/// <summary>
/// Current server time in UTC
///
/// NOTE: Japan is +9 hours from UTC (ie: UTC midnight is 9am JST)
/// </summary>
public DateTime ServerTimeUTC
{
get
{
// get UTC start time
TimeSpan tsUTCStart = new TimeSpan(new DateTime(1970, 1, 1).Ticks);
// get the server time (in seconds) and add the UTC start (in seconds)
long baseTime = (long)GetVanaUTC(_InstanceID) + (long)tsUTCStart.TotalSeconds;
// calculate how many hours there are since too many seconds to fit in an int
int hours = Convert.ToInt32(baseTime / 3600);
// calculate our seconds minusing the hours
int seconds = Convert.ToInt32(baseTime % ( baseTime / 3600 ));
// return the results
return new DateTime(new TimeSpan(hours, 0, seconds).Ticks);
}
} // @ public DateTime ServerTimeUTC
public int GetVanaUTC
{
get { return GetVanaUTC(_InstanceID); }
}
#endregion
#region Methods
/// <summary>
/// Will get the time left in seconds before being able to recast a spell
/// </summary>
/// <param name="spell">Spell to check recast timer on</param>
public short GetSpellRecast (short id)
{
// get recast time from fface
short time = FFACE.GetSpellRecast(_InstanceID, (SpellList)id);
// FFACE seems to return the recast 1 second shorter then it is
if (0 < time)
time += 60;
return (short)( time / 60 );
} // @ public short GetSpellRecast(short id)
/// <summary>
/// Will get the time left in seconds before being able to recast a spell
/// </summary>
/// <param name="spell">Spell to check recast timer on</param>
public short GetSpellRecast (SpellList spell)
{
// get recast time from fface
short time = FFACE.GetSpellRecast(_InstanceID, spell);
// FFACE seems to return the recast 1 second shorter then it is
if (0 < time)
time += 60;
return (short)( time / 60 );
} // @ public TimeSpan GetSpellRecast(eSpellList spell)
/// <summary>
/// Gets the time left in seconds before being able to reuse an ability (use sparingly)
/// </summary>
/// <param name="abil">Ability List</param>
/// <returns>-1 if Ability is not "equipped", Recast of the ability otherwise</returns>
public int GetAbilityRecast (AbilityList abil)
{
for (byte i = 0; i < MAX_ABILITY_INDEX; i++)
if (GetAbilityID(i) == abil)
return (int) Math.Ceiling((decimal) (FFACE.GetAbilityRecast(_InstanceID, i) / 60));
// not found.
return -1;
}
/// <summary>
/// Gets the time left in seconds before being able to reuse an ability
/// </summary>
/// <param name="index">Index of the ability</param>
public int GetAbilityRecast (byte index)
{
if (index >= 0 && index <= MAX_ABILITY_INDEX)
return (int) Math.Ceiling((decimal) (FFACE.GetAbilityRecast(_InstanceID, index) / 60));
else
throw new ArgumentOutOfRangeException("Must be within 0 to MAX_ABILITY_INDEX");
} // @ public int GetAbilityRecast(byte index)
/// <summary>
/// Gets the ID of an ability by the index
/// </summary>
/// <param name="index">Index of the ability</param>
public AbilityList GetAbilityID (byte index)
{
if (index >= 0 && index <= MAX_ABILITY_INDEX)
return FFACE.GetAbilityID(_InstanceID, index);
else
throw new ArgumentOutOfRangeException("Must be within 0 to MAX_ABILITY_INDEX");
} // @ public byte GetAbilityID(byte index)
/// <summary>
/// Gets the current Vana'Diel time information
/// </summary>
public VanaTime GetVanaTime ()
{
// get the server time (in seconds)
long baseTime = (long)GetVanaUTC(_InstanceID);
// calculate the difference between server time and
// 1/1/1970 00:00:00 unix time -> "Vana'Diel time in seconds"
long timeInSeconds = ( (long)baseTime + 92514960 ) * 25;
// how many days
decimal dayOfYear = Math.Floor((decimal)( timeInSeconds / (decimal)86400 ));
VanaTime vanaTime = new VanaTime();
vanaTime.DayType = (Weekday)( dayOfYear % 8 );
vanaTime.Day = (byte)( ( dayOfYear % 30 ) + 1 );
vanaTime.Month = (byte)( ( ( dayOfYear % 360 ) / 30 ) + 1 );
vanaTime.Year = (short)( dayOfYear / 360 );
vanaTime.Hour = (byte)( ( timeInSeconds / 3600 ) % 24 );
vanaTime.Minute = (byte)( ( timeInSeconds / 60 ) % 60 );
// can't floor on a long, so need to shrink it first
vanaTime.Second = (byte)( ( timeInSeconds - ( System.Math.Floor((decimal)( timeInSeconds / 60 )) * 60 ) ) );
// calculate moon phase/percent
decimal moonPhase = ( dayOfYear + (decimal)26 ) % 84;
// calculate moon percent
decimal moonPercent = ( ( ( 42 - moonPhase ) * 100 ) / 42 );
if (0 > moonPercent)
moonPercent = Math.Abs(moonPercent);
// get final moon percent
vanaTime.MoonPercent = (byte)Math.Floor(( moonPercent + (decimal)0.5 ));
// get final moon phase
if (38 <= moonPhase)
vanaTime.MoonPhase = (MoonPhase)Math.Floor(( moonPhase - (decimal)38 ) / (decimal)7);
else
vanaTime.MoonPhase = (MoonPhase)Math.Floor(( moonPhase + (decimal)46 ) / (decimal)7);
return vanaTime;
} // @ public VanaTime GetVanaTime()
#endregion
} // @ public class TimerTools
} // @ public partial class FFACE
}