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

@@ -0,0 +1,25 @@
using GTANetworkAPI;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace ReallifeGamemode.Server.Entities
{
public class TuningGarage
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public float X { get; set; }
public float Y { get; set; }
public float Z { get; set; }
[NotMapped]
public Vector3 Position => new Vector3(X, Y, Z);
}
}

View File

@@ -7,7 +7,7 @@ namespace ReallifeGamemode.Server.Events
{
class Siren : Script
{
private Dictionary<NetHandle, bool> _sirenStates = new Dictionary<NetHandle, bool>();
private readonly Dictionary<NetHandle, bool> _sirenStates = new Dictionary<NetHandle, bool>();
[RemoteEvent("keyPress:B:toggleSiren")]
public void ToggleSirenEvent(Client player)

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();
}
}
}
}
}

View File

@@ -32,13 +32,18 @@ namespace ReallifeGamemode.Server
InventoryManager.LoadItems();
TuningManager.AddTuningGarage(new Vector3(-341, -134, 38.5)); // Downtown LS
TuningManager.AddTuningGarage(new Vector3(732, -1088, 21)); // LS Intersection
TuningManager.AddTuningGarage(new Vector3(-1155, -2006, 12)); // LS Airport
TuningManager.AddTuningGarage(new Vector3(110, 6628, 31)); // Paleto Bay
TuningManager.AddTuningGarage(new Vector3(1175, 2639, 37)); // Route 69
TuningManager.LoadTuningGarages();
//TuningManager.AddTuningGarage(new Vector3(-341, -134, 38.5)); // Downtown LS
//TuningManager.AddTuningGarage(new Vector3(732, -1088, 21)); // LS Intersection
//TuningManager.AddTuningGarage(new Vector3(-1155, -2006, 12)); // LS Airport
//TuningManager.AddTuningGarage(new Vector3(110, 6628, 31)); // Paleto Bay
//TuningManager.AddTuningGarage(new Vector3(1175, 2639, 37)); // Route 69
TimeManager.StartTimeManager();
VehicleManager.StartTimer();
DatabaseHelper.InitDatabaseFirstTime();

View File

@@ -11,6 +11,16 @@ namespace ReallifeGamemode.Server.Managers
{
private static List<ColShape> tuningGarages = new List<ColShape>();
public static void LoadTuningGarages()
{
using (var dbContext = new DatabaseContext())
{
foreach (TuningGarage garage in dbContext.TuningGarages)
{
AddTuningGarage(garage.Position);
}
}
}
/// <summary>
/// Fügt eine Tuning-Garage zum Spiel hinzu
/// </summary>

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)
{

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,32 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
namespace ReallifeGamemode.Migrations
{
public partial class TuningGarage : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "TuningGarages",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
X = table.Column<float>(nullable: false),
Y = table.Column<float>(nullable: false),
Z = table.Column<float>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_TuningGarages", x => x.Id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "TuningGarages");
}
}
}

View File

@@ -704,6 +704,22 @@ namespace ReallifeGamemode.Migrations
b.HasDiscriminator<string>("Discriminator").HasValue("ServerVehicle");
});
modelBuilder.Entity("ReallifeGamemode.Server.Entities.TuningGarage", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<float>("X");
b.Property<float>("Y");
b.Property<float>("Z");
b.HasKey("Id");
b.ToTable("TuningGarages");
});
modelBuilder.Entity("ReallifeGamemode.Server.Entities.User", b =>
{
b.Property<int>("Id")

View File

@@ -91,5 +91,8 @@ namespace ReallifeGamemode.Server.Models
// Interiors
public DbSet<Entities.Interior> Interiors { get; set; }
// Tuning Garages
public DbSet<Entities.TuningGarage> TuningGarages { get; set; }
}
}