Moved tuning garages to database, moved vehicle distance driven counter timer from update event to timer
This commit is contained in:
25
ReallifeGamemode.Server/Entities/TuningGarage.cs
Normal file
25
ReallifeGamemode.Server/Entities/TuningGarage.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
1044
ReallifeGamemode.Server/Migrations/20190411180607_TuningGarage.Designer.cs
generated
Normal file
1044
ReallifeGamemode.Server/Migrations/20190411180607_TuningGarage.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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")
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user