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

@@ -10,47 +10,10 @@ namespace ReallifeGamemode.Server.Events
{
class Update : Script
{
private Dictionary<NetHandle, Vector3> lastPositions = new Dictionary<NetHandle, Vector3>();
private DateTime lastSave = DateTime.UtcNow;
[ServerEvent(Event.Update)]
void UpdateEvent()
public void UpdateEvent()
{
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") : 0;
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();
}
}
}
}
}