forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameTime.cs
More file actions
93 lines (72 loc) · 2.62 KB
/
Copy pathGameTime.cs
File metadata and controls
93 lines (72 loc) · 2.62 KB
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
using System;
using System.Threading.Tasks;
using L2dotNET.DataContracts.Shared.Enums;
using L2dotNET.Models.Player;
using L2dotNET.Network;
using L2dotNET.Network.serverpackets;
using L2dotNET.World;
using NLog;
namespace L2dotNET.Controllers
{
public class GameTime
{
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
public static bool Night { get; private set; }
public static int IngameTime => (int)localTime.TotalSeconds / 6;
private static readonly GameserverPacket _dayPk = new SunRise();
private static readonly GameserverPacket _nightPk = new SunSet();
private static TimeSpan localTime;
// TODO: move to config
private static readonly int TimeSpeed = 1;
private static readonly int DayStart = 6;
private static readonly int DayEnd = 22;
public static void Initialize()
{
UpdateIngameTime();
Task.Factory.StartNew(UpdateTime);
Log.Info($"[GameTime] Time is set to {localTime.Hours}:{localTime.Minutes}");
}
private static async void UpdateTime()
{
while (true)
{
if (UpdateIngameTime())
{
UpdateTimeForAll();
}
await Task.Delay(60 * 1000 / TimeSpeed);
}
}
private static bool UpdateIngameTime()
{
int dayLength = 60 * 24 / TimeSpeed;
int localTimeInMinutes = (int)DateTime.Now.TimeOfDay.TotalMinutes % dayLength * TimeSpeed;
localTime = TimeSpan.FromMinutes(localTimeInMinutes);
bool night = localTimeInMinutes < DayStart && localTimeInMinutes >= DayEnd;
if (night != Night)
{
Night = night;
return true;
}
return false;
}
private static void UpdateTimeForAll()
{
L2World.GetPlayers().ForEach(UpdateTimeForPlayer);
}
public static void UpdateTimeForPlayer(L2Player p)
{
p.SendPacketAsync(Night ? _nightPk : _dayPk);
}
public static async Task ShowInfoAsync(L2Player player)
{
DateTime dt = new DateTime(2000, 1, 1, 0, 0, 0).AddSeconds(IngameTime * 6);
SystemMessage sm = new SystemMessage(Night
? SystemMessageId.TimeS1S2InTheNight
: SystemMessageId.TimeS1S2InTheDay);
sm.AddString(dt.ToString("hh"));
sm.AddString(dt.ToString("mm:ss"));
await player.SendPacketAsync(sm);
}
}
}