Switch to DateTime.UtcNow for everything

This commit is contained in:
Drew DeVault 2015-07-02 12:14:55 -06:00
parent bda2e11885
commit 8b1930eeac
17 changed files with 41 additions and 51 deletions

View File

@ -96,7 +96,7 @@ namespace TrueCraft.Client.Interface
public ChatMessage(string message) public ChatMessage(string message)
{ {
Message = message; Message = message;
Time = DateTime.Now; Time = DateTime.UtcNow;
} }
} }
@ -135,7 +135,7 @@ namespace TrueCraft.Client.Interface
for (int i = 0; i < Messages.Count; i++) for (int i = 0; i < Messages.Count; i++)
{ {
var message = Messages[i]; var message = Messages[i];
if ((DateTime.Now - message.Time).TotalSeconds > 10) if ((DateTime.UtcNow - message.Time).TotalSeconds > 10)
{ {
Messages.RemoveAt(i); Messages.RemoveAt(i);
i--; i--;

View File

@ -49,7 +49,7 @@ namespace TrueCraft.Client
private BoundingBox TempBoundingBox; private BoundingBox TempBoundingBox;
public void Update() public void Update()
{ {
double multipler = (DateTime.Now - LastUpdate).TotalMilliseconds / MillisecondsBetweenUpdates; double multipler = (DateTime.UtcNow - LastUpdate).TotalMilliseconds / MillisecondsBetweenUpdates;
if (LastUpdate == DateTime.MinValue) if (LastUpdate == DateTime.MinValue)
multipler = 1; multipler = 1;
if (multipler > 5) multipler = 5; if (multipler > 5) multipler = 5;
@ -69,7 +69,7 @@ namespace TrueCraft.Client
} }
} }
} }
LastUpdate = DateTime.Now; LastUpdate = DateTime.UtcNow;
} }
private void CheckWithTerrain(IAABBEntity entity, ReadOnlyWorld world) private void CheckWithTerrain(IAABBEntity entity, ReadOnlyWorld world)

View File

@ -343,7 +343,7 @@ namespace TrueCraft.Client
if (PendingMainThreadActions.TryTake(out action)) if (PendingMainThreadActions.TryTake(out action))
action(); action();
if (NextPhysicsUpdate < DateTime.Now && Client.LoggedIn) if (NextPhysicsUpdate < DateTime.UtcNow && Client.LoggedIn)
{ {
IChunk chunk; IChunk chunk;
var adjusted = Client.World.World.FindBlockPosition(new Coordinates3D((int)Client.Position.X, 0, (int)Client.Position.Z), out chunk); var adjusted = Client.World.World.FindBlockPosition(new Coordinates3D((int)Client.Position.X, 0, (int)Client.Position.Z), out chunk);
@ -358,7 +358,7 @@ namespace TrueCraft.Client
Client.QueuePacket(new PlayerGroundedPacket { OnGround = true }); Client.QueuePacket(new PlayerGroundedPacket { OnGround = true });
Client.QueuePacket(new PlayerPositionAndLookPacket(Client.Position.X, Client.Position.Y, Client.QueuePacket(new PlayerPositionAndLookPacket(Client.Position.X, Client.Position.Y,
Client.Position.Y + MultiplayerClient.Height, Client.Position.Z, Client.Yaw, Client.Pitch, false)); Client.Position.Y + MultiplayerClient.Height, Client.Position.Z, Client.Yaw, Client.Pitch, false));
NextPhysicsUpdate = DateTime.Now.AddMilliseconds(1000 / 20); NextPhysicsUpdate = DateTime.UtcNow.AddMilliseconds(1000 / 20);
} }
if (Delta != Microsoft.Xna.Framework.Vector3.Zero) if (Delta != Microsoft.Xna.Framework.Vector3.Zero)

View File

@ -17,7 +17,7 @@ namespace TrueCraft.Core.Entities
{ {
EnablePropertyChange = true; EnablePropertyChange = true;
EntityID = -1; EntityID = -1;
SpawnTime = DateTime.Now; SpawnTime = DateTime.UtcNow;
} }
public DateTime SpawnTime { get; set; } public DateTime SpawnTime { get; set; }

View File

@ -99,7 +99,7 @@ namespace TrueCraft.Core.Entities
public override void Update(IEntityManager entityManager) public override void Update(IEntityManager entityManager)
{ {
var nearbyEntities = entityManager.EntitiesInRange(Position, PickupRange); var nearbyEntities = entityManager.EntitiesInRange(Position, PickupRange);
if ((DateTime.Now - SpawnTime).TotalSeconds > 1) if ((DateTime.UtcNow - SpawnTime).TotalSeconds > 1)
{ {
var player = nearbyEntities.FirstOrDefault(e => e is PlayerEntity && (e as PlayerEntity).Health != 0 var player = nearbyEntities.FirstOrDefault(e => e is PlayerEntity && (e as PlayerEntity).Health != 0
&& e.Position.DistanceTo(Position) <= PickupRange); && e.Position.DistanceTo(Position) <= PickupRange);
@ -109,24 +109,8 @@ namespace TrueCraft.Core.Entities
playerEntity.OnPickUpItem(this); playerEntity.OnPickUpItem(this);
entityManager.DespawnEntity(this); entityManager.DespawnEntity(this);
} }
/* TODO: Merging item entities (this code behaves strangely
var item = nearbyEntities.FirstOrDefault(e => e is ItemEntity
&& e != this
&& (DateTime.Now - (e as ItemEntity).SpawnTime).TotalSeconds > 1
&& (e as ItemEntity).Item.ID == Item.ID && (e as ItemEntity).Item.Metadata == Item.Metadata
&& (e as ItemEntity).Item.Nbt == Item.Nbt
&& e.Position.DistanceTo(Position) < PickupRange);
if (item != null)
{
// Merge
entityManager.DespawnEntity(item);
var newItem = Item;
newItem.Count += (item as ItemEntity).Item.Count;
Item = newItem;
OnPropertyChanged("Metadata");
}*/
} }
if ((DateTime.Now - SpawnTime).TotalMinutes > 5) if ((DateTime.UtcNow - SpawnTime).TotalMinutes > 5)
entityManager.DespawnEntity(this); entityManager.DespawnEntity(this);
base.Update(entityManager); base.Update(entityManager);
} }

View File

@ -8,7 +8,7 @@ namespace TrueCraft.Core.Logging
{ {
public static class LogHelpers public static class LogHelpers
{ {
public static string GetTimestamp(bool utc = false, string timeFormat = "yyyy-MM-dd H:mm:ss", string suffix = " ") public static string GetTimestamp(bool utc = true, string timeFormat = "yyyy-MM-dd H:mm:ss", string suffix = " ")
{ {
return (utc ? DateTime.UtcNow : DateTime.Now).ToString(timeFormat) + suffix; return (utc ? DateTime.UtcNow : DateTime.Now).ToString(timeFormat) + suffix;
} }

View File

@ -50,7 +50,7 @@ namespace TrueCraft.Core.Logic.Blocks
{ {
var chunk = world.FindChunk(coords); var chunk = world.FindChunk(coords);
server.Scheduler.ScheduleEvent( server.Scheduler.ScheduleEvent(
chunk, DateTime.Now.AddSeconds(MathHelper.Random.Next(30, 60)), chunk, DateTime.UtcNow.AddSeconds(MathHelper.Random.Next(30, 60)),
(_server) => GrowBlock(_server, world, coords)); (_server) => GrowBlock(_server, world, coords));
} }
} }
@ -67,7 +67,7 @@ namespace TrueCraft.Core.Logic.Blocks
public override void BlockPlaced(BlockDescriptor descriptor, BlockFace face, IWorld world, IRemoteClient user) public override void BlockPlaced(BlockDescriptor descriptor, BlockFace face, IWorld world, IRemoteClient user)
{ {
var chunk = world.FindChunk(descriptor.Coordinates); var chunk = world.FindChunk(descriptor.Coordinates);
user.Server.Scheduler.ScheduleEvent(chunk, DateTime.Now.AddSeconds(MathHelper.Random.Next(30, 60)), user.Server.Scheduler.ScheduleEvent(chunk, DateTime.UtcNow.AddSeconds(MathHelper.Random.Next(30, 60)),
(server) => GrowBlock(server, world, descriptor.Coordinates + MathHelper.BlockFaceToCoordinates(face))); (server) => GrowBlock(server, world, descriptor.Coordinates + MathHelper.BlockFaceToCoordinates(face)));
} }
} }

View File

@ -87,7 +87,7 @@ namespace TrueCraft.Core.Logic.Blocks
} }
var chunk = world.FindChunk(coords); var chunk = world.FindChunk(coords);
server.Scheduler.ScheduleEvent(chunk, server.Scheduler.ScheduleEvent(chunk,
DateTime.Now.AddSeconds(UpdateIntervalSeconds), DateTime.UtcNow.AddSeconds(UpdateIntervalSeconds),
_server => HydrationCheckEvent(_server, coords, world)); _server => HydrationCheckEvent(_server, coords, world));
} }
@ -99,7 +99,7 @@ namespace TrueCraft.Core.Logic.Blocks
} }
var chunk = world.FindChunk(descriptor.Coordinates); var chunk = world.FindChunk(descriptor.Coordinates);
user.Server.Scheduler.ScheduleEvent(chunk, user.Server.Scheduler.ScheduleEvent(chunk,
DateTime.Now.AddSeconds(UpdateIntervalSeconds), DateTime.UtcNow.AddSeconds(UpdateIntervalSeconds),
server => HydrationCheckEvent(server, descriptor.Coordinates, world)); server => HydrationCheckEvent(server, descriptor.Coordinates, world));
} }
} }

View File

@ -71,7 +71,7 @@ namespace TrueCraft.Core.Logic.Blocks
{ {
var chunk = world.FindChunk(coords); var chunk = world.FindChunk(coords);
server.Scheduler.ScheduleEvent(chunk, server.Scheduler.ScheduleEvent(chunk,
DateTime.Now.AddSeconds(SecondsBetweenUpdates), (_server) => DateTime.UtcNow.AddSeconds(SecondsBetweenUpdates), (_server) =>
AutomataUpdate(_server, world, coords)); AutomataUpdate(_server, world, coords));
} }
@ -106,7 +106,7 @@ namespace TrueCraft.Core.Logic.Blocks
{ {
var chunk = world.FindChunk(coords); var chunk = world.FindChunk(coords);
server.Scheduler.ScheduleEvent(chunk, server.Scheduler.ScheduleEvent(chunk,
DateTime.Now.AddSeconds(SecondsBetweenUpdates), (_server) => DateTime.UtcNow.AddSeconds(SecondsBetweenUpdates), (_server) =>
AutomataUpdate(_server, world, coords)); AutomataUpdate(_server, world, coords));
} }
} }
@ -161,7 +161,7 @@ namespace TrueCraft.Core.Logic.Blocks
world.SetMetadata(target.TargetBlock, target.Level); world.SetMetadata(target.TargetBlock, target.Level);
var chunk = world.FindChunk(target.TargetBlock); var chunk = world.FindChunk(target.TargetBlock);
server.Scheduler.ScheduleEvent(chunk, server.Scheduler.ScheduleEvent(chunk,
DateTime.Now.AddSeconds(SecondsBetweenUpdates), DateTime.UtcNow.AddSeconds(SecondsBetweenUpdates),
s => AutomataUpdate(s, world, target.TargetBlock)); s => AutomataUpdate(s, world, target.TargetBlock));
} }

View File

@ -105,13 +105,13 @@ namespace TrueCraft.Core.Logic.Blocks
{ {
world.SetBlockID(coords + Coordinates3D.Up, BlockID); world.SetBlockID(coords + Coordinates3D.Up, BlockID);
server.Scheduler.ScheduleEvent(chunk, server.Scheduler.ScheduleEvent(chunk,
DateTime.Now.AddSeconds(MathHelper.Random.Next(MinGrowthSeconds, MaxGrowthSeconds)), DateTime.UtcNow.AddSeconds(MathHelper.Random.Next(MinGrowthSeconds, MaxGrowthSeconds)),
(_server) => TryGrowth(_server, coords + Coordinates3D.Up, world)); (_server) => TryGrowth(_server, coords + Coordinates3D.Up, world));
} }
else else
{ {
server.Scheduler.ScheduleEvent(chunk, server.Scheduler.ScheduleEvent(chunk,
DateTime.Now.AddSeconds(MathHelper.Random.Next(MinGrowthSeconds, MaxGrowthSeconds)), DateTime.UtcNow.AddSeconds(MathHelper.Random.Next(MinGrowthSeconds, MaxGrowthSeconds)),
(_server) => TryGrowth(_server, coords, world)); (_server) => TryGrowth(_server, coords, world));
} }
} }
@ -121,7 +121,7 @@ namespace TrueCraft.Core.Logic.Blocks
{ {
var chunk = world.FindChunk(descriptor.Coordinates); var chunk = world.FindChunk(descriptor.Coordinates);
user.Server.Scheduler.ScheduleEvent(chunk, user.Server.Scheduler.ScheduleEvent(chunk,
DateTime.Now.AddSeconds(MathHelper.Random.Next(MinGrowthSeconds, MaxGrowthSeconds)), DateTime.UtcNow.AddSeconds(MathHelper.Random.Next(MinGrowthSeconds, MaxGrowthSeconds)),
(server) => TryGrowth(server, descriptor.Coordinates, world)); (server) => TryGrowth(server, descriptor.Coordinates, world));
} }
} }

View File

@ -42,11 +42,11 @@ namespace TrueCraft.Core.World
{ {
get get
{ {
return (long)((DateTime.Now - BaseTime).TotalSeconds * 20) % 24000; return (long)((DateTime.UtcNow - BaseTime).TotalSeconds * 20) % 24000;
} }
set set
{ {
BaseTime = DateTime.Now.AddSeconds(-value / 20); BaseTime = DateTime.UtcNow.AddSeconds(-value / 20);
} }
} }
@ -57,7 +57,7 @@ namespace TrueCraft.Core.World
public World() public World()
{ {
Regions = new Dictionary<Coordinates2D, IRegion>(); Regions = new Dictionary<Coordinates2D, IRegion>();
BaseTime = DateTime.Now; BaseTime = DateTime.UtcNow;
} }
public World(string name) : this() public World(string name) : this()

View File

@ -228,7 +228,7 @@ namespace TrueCraft
{ {
if (entity.Despawned) if (entity.Despawned)
return; return;
entity.SpawnTime = DateTime.Now; entity.SpawnTime = DateTime.UtcNow;
entity.EntityManager = this; entity.EntityManager = this;
entity.World = World; entity.World = World;
entity.EntityID = NextEntityID++; entity.EntityID = NextEntityID++;

View File

@ -30,7 +30,13 @@ namespace TrueCraft
Subjects.Add(subject); Subjects.Add(subject);
subject.Disposed += Subject_Disposed; subject.Disposed += Subject_Disposed;
} }
Events.Add(new ScheduledEvent { Subject = subject, When = when, Action = action }); int i;
for (i = 0; i < Events.Count; i++)
{
if (Events[i].When > when)
break;
}
Events.Insert(i, new ScheduledEvent { Subject = subject, When = when, Action = action });
} }
} }
@ -55,7 +61,7 @@ namespace TrueCraft
{ {
lock (EventLock) lock (EventLock)
{ {
var start = DateTime.Now; var start = DateTime.UtcNow;
for (int i = 0; i < Events.Count; i++) for (int i = 0; i < Events.Count; i++)
{ {
var e = Events[i]; var e = Events[i];

View File

@ -137,7 +137,7 @@ namespace TrueCraft.Handlers
{ {
server.SendMessage(ChatColor.Red + "WARNING: block provider for ID {0} is null (player placing)", block.Value.ID); server.SendMessage(ChatColor.Red + "WARNING: block provider for ID {0} is null (player placing)", block.Value.ID);
server.SendMessage(ChatColor.Red + "Error occured from client {0} at coordinates {1}", client.Username, block.Value.Coordinates); server.SendMessage(ChatColor.Red + "Error occured from client {0} at coordinates {1}", client.Username, block.Value.Coordinates);
server.SendMessage(ChatColor.Red + "Packet logged at {0}, please report upstream", DateTime.Now); server.SendMessage(ChatColor.Red + "Packet logged at {0}, please report upstream", DateTime.UtcNow);
return; return;
} }
if (!provider.BlockRightClicked(block.Value, packet.Face, client.World, client)) if (!provider.BlockRightClicked(block.Value, packet.Face, client.World, client))
@ -159,7 +159,7 @@ namespace TrueCraft.Handlers
{ {
server.SendMessage(ChatColor.Red + "WARNING: item provider for ID {0} is null (player placing)", block.Value.ID); server.SendMessage(ChatColor.Red + "WARNING: item provider for ID {0} is null (player placing)", block.Value.ID);
server.SendMessage(ChatColor.Red + "Error occured from client {0} at coordinates {1}", client.Username, block.Value.Coordinates); server.SendMessage(ChatColor.Red + "Error occured from client {0} at coordinates {1}", client.Username, block.Value.Coordinates);
server.SendMessage(ChatColor.Red + "Packet logged at {0}, please report upstream", DateTime.Now); server.SendMessage(ChatColor.Red + "Packet logged at {0}, please report upstream", DateTime.UtcNow);
} }
if (block != null) if (block != null)
{ {

View File

@ -70,8 +70,8 @@ namespace TrueCraft.Handlers
var entityManager = server.GetEntityManagerForWorld(remoteClient.World); var entityManager = server.GetEntityManagerForWorld(remoteClient.World);
entityManager.SpawnEntity(remoteClient.Entity); entityManager.SpawnEntity(remoteClient.Entity);
entityManager.SendEntitiesToClient(remoteClient); entityManager.SendEntitiesToClient(remoteClient);
server.Scheduler.ScheduleEvent(remoteClient, DateTime.Now.AddSeconds(10), remoteClient.SendKeepAlive); server.Scheduler.ScheduleEvent(remoteClient, DateTime.UtcNow.AddSeconds(10), remoteClient.SendKeepAlive);
server.Scheduler.ScheduleEvent(remoteClient, DateTime.Now.AddSeconds(1), remoteClient.ExpandChunkRadius); server.Scheduler.ScheduleEvent(remoteClient, DateTime.UtcNow.AddSeconds(1), remoteClient.ExpandChunkRadius);
if (!string.IsNullOrEmpty(Program.ServerConfiguration.MOTD)) if (!string.IsNullOrEmpty(Program.ServerConfiguration.MOTD))
remoteClient.SendMessage(Program.ServerConfiguration.MOTD); remoteClient.SendMessage(Program.ServerConfiguration.MOTD);

View File

@ -47,7 +47,7 @@ namespace TrueCraft
private BoundingBox TempBoundingBox; private BoundingBox TempBoundingBox;
public void Update() public void Update()
{ {
double multipler = (DateTime.Now - LastUpdate).TotalMilliseconds / MillisecondsBetweenUpdates; double multipler = (DateTime.UtcNow - LastUpdate).TotalMilliseconds / MillisecondsBetweenUpdates;
if (LastUpdate == DateTime.MinValue) if (LastUpdate == DateTime.MinValue)
multipler = 1; multipler = 1;
if (multipler > 5) multipler = 5; if (multipler > 5) multipler = 5;
@ -67,7 +67,7 @@ namespace TrueCraft
} }
} }
} }
LastUpdate = DateTime.Now; LastUpdate = DateTime.UtcNow;
} }
private void CheckWithTerrain(IAABBEntity entity, IWorld world) private void CheckWithTerrain(IAABBEntity entity, IWorld world)

View File

@ -226,7 +226,7 @@ namespace TrueCraft
public void Log(string message, params object[] parameters) public void Log(string message, params object[] parameters)
{ {
if (EnableLogging) if (EnableLogging)
SendMessage(ChatColor.Gray + string.Format("[" + DateTime.Now.ToShortTimeString() + "] " + message, parameters)); SendMessage(ChatColor.Gray + string.Format("[" + DateTime.UtcNow.ToShortTimeString() + "] " + message, parameters));
} }
public void QueuePacket(IPacket packet) public void QueuePacket(IPacket packet)
@ -389,7 +389,7 @@ namespace TrueCraft
{ {
ChunkRadius++; ChunkRadius++;
UpdateChunks(); UpdateChunks();
server.Scheduler.ScheduleEvent(this, DateTime.Now.AddSeconds(1), ExpandChunkRadius); server.Scheduler.ScheduleEvent(this, DateTime.UtcNow.AddSeconds(1), ExpandChunkRadius);
} }
}); });
} }
@ -397,7 +397,7 @@ namespace TrueCraft
internal void SendKeepAlive(IMultiplayerServer server) internal void SendKeepAlive(IMultiplayerServer server)
{ {
QueuePacket(new KeepAlivePacket()); QueuePacket(new KeepAlivePacket());
server.Scheduler.ScheduleEvent(this, DateTime.Now.AddSeconds(1), SendKeepAlive); server.Scheduler.ScheduleEvent(this, DateTime.UtcNow.AddSeconds(1), SendKeepAlive);
} }
internal void UpdateChunks() internal void UpdateChunks()