Moved tuning garages to database, moved vehicle distance driven counter timer from update event to timer

This commit is contained in:
hydrant
2019-04-12 19:03:23 +02:00
parent 5749b15151
commit 434e19e6ea
10 changed files with 1194 additions and 46 deletions

View File

@@ -3,6 +3,7 @@ using ReallifeGamemode.Server.Entities;
using ReallifeGamemode.Server.Models;
using System;
using System.Collections.Generic;
using System.Timers;
namespace ReallifeGamemode.Server.Managers
{
@@ -63,7 +64,56 @@ namespace ReallifeGamemode.Server.Managers
"dominator3"
};
private static Dictionary<int, NetHandle> _serverVehicles = new Dictionary<int, NetHandle>();
private static readonly Dictionary<int, NetHandle> _serverVehicles = new Dictionary<int, NetHandle>();
private static readonly Dictionary<NetHandle, Vector3> lastPositions = new Dictionary<NetHandle, Vector3>();
private static DateTime lastSave = DateTime.UtcNow;
public static void StartTimer()
{
Timer timer = new Timer(500);
timer.Elapsed += VehicleTimerTick;
timer.Start();
}
private static void VehicleTimerTick(object sender, ElapsedEventArgs e)
{
NAPI.Pools.GetAllVehicles().ForEach(v =>
{
Vector3 lastPosition = v.Position;
if (lastPositions.ContainsKey(v.Handle)) lastPosition = lastPositions[v.Handle];
double distanceDriven = v.HasSharedData("drivenDistance") ? v.GetSharedData("drivenDistance") : 0D;
distanceDriven += (lastPosition.DistanceTo(v.Position) / 1000.0);
v.SetSharedData("drivenDistance", (float)distanceDriven);
lastPositions[v.Handle] = v.Position;
});
if (DateTime.UtcNow.Subtract(lastSave).Seconds >= 30)
{
lastSave = DateTime.UtcNow;
// save to db
using (var dbContext = new DatabaseContext())
{
foreach (var key in lastPositions.Keys)
{
Vehicle v = key.Entity<Vehicle>();
if (!v.HasSharedData("drivenDistance")) continue;
ServerVehicle sVeh = VehicleManager.GetServerVehicleFromVehicle(v, dbContext);
if (sVeh == null) continue;
sVeh.DistanceDriven = (float)v.GetSharedData("drivenDistance");
}
dbContext.SaveChanges();
}
}
}
public static void AddVehicle(ServerVehicle serverVehicle, Vehicle vehicle)
{