Rewrite of vehicle system
This commit is contained in:
@@ -37,6 +37,10 @@ namespace reallife_gamemode.Model
|
||||
modelBuilder.Entity<Server.Entities.User>()
|
||||
.HasIndex(u => u.BusinessId)
|
||||
.IsUnique(true);
|
||||
|
||||
modelBuilder.Entity<Server.Entities.ServerVehicle>()
|
||||
.Property(sv => sv.Active)
|
||||
.HasDefaultValue(true);
|
||||
}
|
||||
|
||||
//User
|
||||
@@ -80,5 +84,8 @@ namespace reallife_gamemode.Model
|
||||
|
||||
// Control Panel
|
||||
public DbSet<Server.Entities.News> News { get; set; }
|
||||
|
||||
// Server Vehicles
|
||||
public DbSet<Server.Entities.ServerVehicle> ServerVehicles { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1835,7 +1835,7 @@ namespace reallife_gamemode.Server.Commands
|
||||
var dataSet = new UserVehicle
|
||||
{
|
||||
UserId = player.GetUser().Id,
|
||||
Model = (VehicleHash) playerVehicle.Model,
|
||||
Model = (VehicleHash)playerVehicle.Model,
|
||||
PositionX = playerVehicle.Position.X,
|
||||
PositionY = playerVehicle.Position.Y,
|
||||
PositionZ = playerVehicle.Position.Z,
|
||||
@@ -1844,8 +1844,7 @@ namespace reallife_gamemode.Server.Commands
|
||||
PrimaryColor = Convert.ToByte(playerVehicle.PrimaryColor),
|
||||
SecondaryColor = Convert.ToByte(playerVehicle.SecondaryColor),
|
||||
Locked = playerVehicle.Locked,
|
||||
Engine = playerVehicle.EngineStatus,
|
||||
Dimension = Convert.ToByte(playerVehicle.Dimension),
|
||||
Active = true
|
||||
};
|
||||
playerVehicle.SetData("ownerId",player.GetUser().Id);
|
||||
saveVehicle.UserVehicles.Add(dataSet);
|
||||
@@ -1928,10 +1927,10 @@ namespace reallife_gamemode.Server.Commands
|
||||
{
|
||||
ShopVehicle sVehicle = getShopVehicle.ShopVehicles.FirstOrDefault(u => u.Id == shopVehicleId);
|
||||
Entities.Faction receiverUser = getShopVehicle.Factions.FirstOrDefault(u => u.Name == "LSPD");
|
||||
BankManager.TransferMoney(player.GetUser(), receiverUser, sVehicle.Price, "Fahrzeug gekauft: " + sVehicle.ModelName);
|
||||
BankManager.TransferMoney(player.GetUser(), receiverUser, sVehicle.Price, "Fahrzeug gekauft: " + NAPI.Vehicle.GetVehicleDisplayName(sVehicle.Model));
|
||||
//TODO Anpassen
|
||||
Vehicle boughtVehicle = NAPI.Vehicle.CreateVehicle(sVehicle.Model, new Vector3(sVehicle.PositionX, sVehicle.PositionY + 10, sVehicle.PositionZ + 0.5), sVehicle.Heading,
|
||||
sVehicle.PrimaryColor, sVehicle.SecondaryColor, "LoG", sVehicle.Alpha, false, true, 0);
|
||||
sVehicle.PrimaryColor, sVehicle.SecondaryColor, "LoG", 255, false, true, 0);
|
||||
player.SetIntoVehicle(boughtVehicle, 0);
|
||||
//TODO fügt das Fahrzeug als Userfahrzeug hinzu
|
||||
CmdAdminOwn(player);
|
||||
|
||||
@@ -14,28 +14,11 @@ using System.Text;
|
||||
|
||||
namespace reallife_gamemode.Server.Entities
|
||||
{
|
||||
public class FactionVehicle
|
||||
[Table("FactionVehicles")]
|
||||
public class FactionVehicle : ServerVehicle
|
||||
{
|
||||
[Key]
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public int Id { get; set; }
|
||||
[ForeignKey("Faction")]
|
||||
public int? FactionId { get; set; }
|
||||
public Faction Faction { get; set; }
|
||||
|
||||
public VehicleHash Model { get; set; }
|
||||
public float PositionX { get; set; }
|
||||
public float PositionY { get; set; }
|
||||
public float PositionZ { get; set; }
|
||||
public float Heading { get; set; }
|
||||
[StringLength(8)]
|
||||
public string NumberPlate { get; set; }
|
||||
public byte Alpha { get; set; }
|
||||
public byte PrimaryColor { get; set; }
|
||||
public byte SecondaryColor { get; set; }
|
||||
public bool Locked { get; set; }
|
||||
public bool Engine { get; set; }
|
||||
public byte Dimension { get; set; }
|
||||
public bool Active { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
40
Server/Entities/ServerVehicle.cs
Normal file
40
Server/Entities/ServerVehicle.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using GTANetworkAPI;
|
||||
using reallife_gamemode.Model;
|
||||
using reallife_gamemode.Server.Managers;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Text;
|
||||
|
||||
namespace reallife_gamemode.Server.Entities
|
||||
{
|
||||
public abstract class ServerVehicle
|
||||
{
|
||||
[Key]
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public int Id { get; set; }
|
||||
|
||||
public VehicleHash Model { get; set; }
|
||||
public float PositionX { get; set; }
|
||||
public float PositionY { get; set; }
|
||||
public float PositionZ { get; set; }
|
||||
public float Heading { get; set; }
|
||||
[StringLength(8)]
|
||||
public string NumberPlate { get; set; }
|
||||
public int PrimaryColor { get; set; }
|
||||
public int SecondaryColor { get; set; }
|
||||
public bool Locked { get; set; }
|
||||
public bool Active { get; set; }
|
||||
|
||||
[NotMapped]
|
||||
public Vector3 Position => new Vector3(PositionX, PositionY, PositionZ);
|
||||
|
||||
public Vehicle Spawn()
|
||||
{
|
||||
Vehicle veh = NAPI.Vehicle.CreateVehicle(this.Model, this.Position, this.Heading, this.PrimaryColor, this.SecondaryColor, this.NumberPlate, locked: this.Locked, engine: false);
|
||||
VehicleManager.AddVehicle(this, veh);
|
||||
return veh;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,30 +14,12 @@ using System.Text;
|
||||
|
||||
namespace reallife_gamemode.Server.Entities
|
||||
{
|
||||
public class ShopVehicle
|
||||
[Table("ShopVehicles")]
|
||||
public class ShopVehicle : ServerVehicle
|
||||
{
|
||||
[Key]
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public int Id { get; set; }
|
||||
|
||||
[ForeignKey("Shop")]
|
||||
public int? ShopId { get; set; }
|
||||
public Shop Shop { get; set; }
|
||||
|
||||
public VehicleHash Model { get; set; }
|
||||
[StringLength(32)]
|
||||
public string ModelName { get; set; }
|
||||
public int Price { get; set; }
|
||||
public float PositionX { get; set; }
|
||||
public float PositionY { get; set; }
|
||||
public float PositionZ { get; set; }
|
||||
public float Heading { get; set; }
|
||||
[StringLength(8)]
|
||||
public string NumberPlate { get; set; }
|
||||
public byte Alpha { get; set; }
|
||||
public byte PrimaryColor { get; set; }
|
||||
public byte SecondaryColor { get; set; }
|
||||
public byte Dimension { get; set; }
|
||||
public bool Active { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,27 +14,11 @@ using System.Text;
|
||||
|
||||
namespace reallife_gamemode.Server.Entities
|
||||
{
|
||||
public class UserVehicle
|
||||
[Table("UserVehicles")]
|
||||
public class UserVehicle : ServerVehicle
|
||||
{
|
||||
[Key]
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public int Id { get; set; }
|
||||
[ForeignKey("User")]
|
||||
public int UserId { get; set; }
|
||||
public User User { get; set; }
|
||||
|
||||
public VehicleHash Model { get; set; }
|
||||
public float PositionX { get; set; }
|
||||
public float PositionY { get; set; }
|
||||
public float PositionZ { get; set; }
|
||||
public float Heading { get; set; }
|
||||
[StringLength(8)]
|
||||
public string NumberPlate { get; set; }
|
||||
public byte Alpha { get; set; }
|
||||
public byte PrimaryColor { get; set; }
|
||||
public byte SecondaryColor { get; set; }
|
||||
public bool Locked { get; set; }
|
||||
public bool Engine { get; set; }
|
||||
public byte Dimension { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
using GTANetworkAPI;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using reallife_gamemode.Model;
|
||||
using reallife_gamemode.Server.Entities;
|
||||
using reallife_gamemode.Server.Managers;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
/**
|
||||
* @overview Life of German Reallife - Vehicle Extension (VehicleExtension.cs)
|
||||
* @author hydrant
|
||||
* @copyright (c) 2008 - 2018 Life of German
|
||||
*/
|
||||
|
||||
namespace reallife_gamemode.Server.Extensions
|
||||
{
|
||||
public static class VehicleExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// Gibt zurück, ob das Vehicle ein Fraktions-Fahrzeug ist
|
||||
/// </summary>
|
||||
/// <param name="vehicle">Das Vehicle, von dem man wissen möchte, ob es ein Fraktionsfahrzeug ist</param>
|
||||
/// <returns></returns>
|
||||
public static bool IsFactionVehicle(this Vehicle vehicle)
|
||||
{
|
||||
return LoadManager.FactionVehicleList.Contains(vehicle);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,44 +40,39 @@ namespace reallife_gamemode.Server.Managers
|
||||
GotoPointList.Add(g);
|
||||
}
|
||||
}
|
||||
foreach (Saves.SavedVehicle v in loadData.Vehicles)
|
||||
|
||||
foreach(ServerVehicle veh in loadData.ServerVehicles)
|
||||
{
|
||||
if (v.Active == true)
|
||||
if (!veh.Active) continue;
|
||||
|
||||
Vehicle current = veh.Spawn();
|
||||
|
||||
if (veh is Saves.SavedVehicle savedV)
|
||||
{
|
||||
NAPI.Vehicle.CreateVehicle((uint)v.Model, new Vector3(v.PositionX, v.PositionY, v.PositionZ), v.Heading, (v.PrimaryColor),
|
||||
v.SecondaryColor, v.NumberPlate, v.Alpha, v.Locked, v.Engine = false, v.Dimension);
|
||||
NAPI.Vehicle.CreateVehicle((uint)savedV.Model, new Vector3(savedV.PositionX, savedV.PositionY, savedV.PositionZ), savedV.Heading, savedV.PrimaryColor,
|
||||
savedV.SecondaryColor, savedV.NumberPlate, 255, savedV.Locked);
|
||||
}
|
||||
}
|
||||
foreach (FactionVehicle v in loadData.FactionVehicles)
|
||||
{
|
||||
if (v.Active == true)
|
||||
if (veh is FactionVehicle factionV)
|
||||
{
|
||||
Vehicle current = NAPI.Vehicle.CreateVehicle((uint)v.Model, new Vector3(v.PositionX, v.PositionY, v.PositionZ), v.Heading, (v.PrimaryColor),
|
||||
v.SecondaryColor, v.NumberPlate, v.Alpha, v.Locked, v.Engine = false, v.Dimension);
|
||||
current.SetData("factionId", v.FactionId);
|
||||
current.SetData("factionId", factionV.FactionId);
|
||||
FactionVehicleList.Add(current);
|
||||
}
|
||||
}
|
||||
foreach (ShopVehicle v in loadData.ShopVehicles)
|
||||
{
|
||||
if (v.Active == true)
|
||||
if (veh is ShopVehicle shopV)
|
||||
{
|
||||
Vehicle current = NAPI.Vehicle.CreateVehicle((uint)v.Model, new Vector3(v.PositionX, v.PositionY, v.PositionZ), v.Heading, (v.PrimaryColor),
|
||||
v.SecondaryColor, v.NumberPlate, v.Alpha, false, false, v.Dimension);
|
||||
string displayName = NAPI.Vehicle.GetVehicleDisplayName(shopV.Model);
|
||||
ShopVehicleList.Add(current);
|
||||
NAPI.Vehicle.SetVehicleEngineHealth(current, 0);
|
||||
var tLabel = NAPI.TextLabel.CreateTextLabel(v.ModelName + " | " + v.Price + "~g~$", new Vector3(v.PositionX, v.PositionY, v.PositionZ + 1.5), 10, 1, 0, new Color(255, 255, 255), false, v.Dimension);
|
||||
current.SetData("shopVehicleId", v.Id);
|
||||
var tLabel = NAPI.TextLabel.CreateTextLabel(displayName + " | " + shopV.Price + "~g~$", new Vector3(shopV.PositionX, shopV.PositionY, shopV.PositionZ + 1.5), 10, 1, 0, new Color(255, 255, 255), false);
|
||||
current.SetData("shopVehicleId", shopV.Id);
|
||||
tLabel.AttachTo(current, "chassis", new Vector3(0, 0, 1.5), new Vector3(0, 0, 0));
|
||||
}
|
||||
if (veh is UserVehicle userV)
|
||||
{
|
||||
UserVehicleList.Add(current);
|
||||
}
|
||||
}
|
||||
foreach (UserVehicle v in loadData.UserVehicles)
|
||||
{
|
||||
Vehicle current = NAPI.Vehicle.CreateVehicle((uint)v.Model, new Vector3(v.PositionX, v.PositionY, v.PositionZ), v.Heading, (v.PrimaryColor),
|
||||
v.SecondaryColor, v.NumberPlate, v.Alpha, false, false, v.Dimension);
|
||||
current.SetData("ownerId", v.UserId);
|
||||
UserVehicleList.Add(current);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using GTANetworkAPI;
|
||||
using reallife_gamemode.Server.Entities;
|
||||
using reallife_gamemode.Server.Extensions;
|
||||
using reallife_gamemode.Server.Managers;
|
||||
using System;
|
||||
@@ -68,11 +69,9 @@ namespace reallife_gamemode.Server.Events
|
||||
PrimaryColor = vehiclePrimaryColor,
|
||||
SecondaryColor = vehicleSecondaryColor,
|
||||
Locked = vehicleLocked,
|
||||
Engine = vehicleEngine,
|
||||
Dimension = vehicleDimension,
|
||||
Active = true
|
||||
};
|
||||
saveData.Vehicles.Add(dataSet);
|
||||
saveData.ServerVehicles.Add(dataSet);
|
||||
saveData.SaveChanges();
|
||||
}
|
||||
}
|
||||
@@ -93,11 +92,9 @@ namespace reallife_gamemode.Server.Events
|
||||
PrimaryColor = vehiclePrimaryColor,
|
||||
SecondaryColor = vehicleSecondaryColor,
|
||||
Locked = vehicleLocked,
|
||||
Engine = vehicleEngine,
|
||||
Dimension = vehicleDimension,
|
||||
Active = true
|
||||
};
|
||||
saveData.FactionVehicles.Add(dataSet);
|
||||
saveData.ServerVehicles.Add(dataSet);
|
||||
saveData.SaveChanges();
|
||||
}
|
||||
}
|
||||
@@ -109,7 +106,6 @@ namespace reallife_gamemode.Server.Events
|
||||
var dataSet = new Entities.ShopVehicle
|
||||
{
|
||||
Model = vehicleModel,
|
||||
ModelName = vehicleModelName,
|
||||
PositionX = vehiclePosition.X,
|
||||
PositionY = vehiclePosition.Y,
|
||||
PositionZ = vehiclePosition.Z,
|
||||
@@ -117,10 +113,9 @@ namespace reallife_gamemode.Server.Events
|
||||
NumberPlate = vehicleNumberPlate,
|
||||
PrimaryColor = vehiclePrimaryColor,
|
||||
SecondaryColor = vehicleSecondaryColor,
|
||||
Dimension = vehicleDimension,
|
||||
Active = true
|
||||
};
|
||||
saveData.ShopVehicles.Add(dataSet);
|
||||
saveData.ServerVehicles.Add(dataSet);
|
||||
saveData.SaveChanges();
|
||||
}
|
||||
}
|
||||
@@ -139,44 +134,31 @@ namespace reallife_gamemode.Server.Events
|
||||
};
|
||||
saveData.GotoPoints.Add(dataSet);
|
||||
saveData.SaveChanges();
|
||||
LoadManager.GotoPointList.Add(dataSet);
|
||||
}
|
||||
}
|
||||
public static void SaveAllOnSave()
|
||||
{
|
||||
// Alle Fahrzeuge
|
||||
using (var saveAll = new Model.DatabaseContext())
|
||||
{
|
||||
//TODO Fahrzeugschäden und Tankfüllstände
|
||||
//User Vehicle
|
||||
foreach (Vehicle v in LoadManager.UserVehicleList)
|
||||
foreach(ServerVehicle veh in saveAll.ServerVehicles)
|
||||
{
|
||||
int ownerId = v.GetData("ownerId");
|
||||
Entities.UserVehicle userVehicle = saveAll.UserVehicles.FirstOrDefault(u => u.UserId == ownerId);
|
||||
userVehicle.PositionX = v.Position.X;
|
||||
userVehicle.PositionY = v.Position.Y;
|
||||
userVehicle.PositionZ = v.Position.Z;
|
||||
userVehicle.Heading = v.Heading;
|
||||
}
|
||||
Vehicle v = VehicleManager.GetVehicleFromServerVehicle(veh);
|
||||
|
||||
//Faction Vehicle
|
||||
foreach (Vehicle v in LoadManager.FactionVehicleList)
|
||||
{
|
||||
int factionId = v.GetData("factionId");
|
||||
Entities.FactionVehicle factionVehicle = saveAll.FactionVehicles.FirstOrDefault(u => u.FactionId == factionId);
|
||||
factionVehicle.PositionX = v.Position.X;
|
||||
factionVehicle.PositionY = v.Position.Y;
|
||||
factionVehicle.PositionZ = v.Position.Z;
|
||||
factionVehicle.Heading = v.Heading;
|
||||
veh.PositionX = v.Position.X;
|
||||
veh.PositionY = v.Position.Y;
|
||||
veh.PositionZ = v.Position.Z;
|
||||
veh.Heading = v.Heading;
|
||||
}
|
||||
|
||||
//Alle Spieler
|
||||
foreach (Client player in NAPI.Pools.GetAllPlayers())
|
||||
{
|
||||
int userId = player.GetUser().Id;
|
||||
Entities.User user = saveAll.Users.FirstOrDefault(u => u.Id == userId);
|
||||
user.PositionX = player.Position.X;
|
||||
user.PositionY = player.Position.Y;
|
||||
user.PositionZ = player.Position.Z;
|
||||
Vector3 pos = player.Position;
|
||||
User user = player.GetUser(saveAll);
|
||||
user.PositionX = pos.X;
|
||||
user.PositionY = pos.Y;
|
||||
user.PositionZ = pos.Z;
|
||||
}
|
||||
|
||||
saveAll.SaveChanges();
|
||||
|
||||
39
Server/Managers/VehicleManager.cs
Normal file
39
Server/Managers/VehicleManager.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using GTANetworkAPI;
|
||||
using reallife_gamemode.Server.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace reallife_gamemode.Server.Managers
|
||||
{
|
||||
public class VehicleManager
|
||||
{
|
||||
private static Dictionary<int, NetHandle> _serverVehicles = new Dictionary<int, NetHandle>();
|
||||
|
||||
public static void AddVehicle(ServerVehicle serverVehicle, Vehicle vehicle)
|
||||
{
|
||||
if(_serverVehicles.ContainsKey(serverVehicle.Id))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_serverVehicles.ContainsValue(vehicle.Handle))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_serverVehicles[serverVehicle.Id] = vehicle.Handle;
|
||||
}
|
||||
|
||||
public static Vehicle GetVehicleFromServerVehicle(ServerVehicle serverVehicle)
|
||||
{
|
||||
Console.WriteLine("calling GetVehicleFromServerVehicle | " + serverVehicle.Id);
|
||||
if(!_serverVehicles.ContainsKey(serverVehicle.Id))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return NAPI.Pools.GetAllVehicles().Find(v => v.Handle == _serverVehicles[serverVehicle.Id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using System.Text;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using GTANetworkAPI;
|
||||
using reallife_gamemode.Server.Entities;
|
||||
|
||||
/**
|
||||
* @overview Life of German Reallife - Saves SavedVehicle.cs
|
||||
@@ -13,24 +14,6 @@ using GTANetworkAPI;
|
||||
|
||||
namespace reallife_gamemode.Server.Saves
|
||||
{
|
||||
public class SavedVehicle
|
||||
{
|
||||
[Key]
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public int Id { get; set; }
|
||||
public VehicleHash Model { get; set; }
|
||||
public float PositionX { get; set; }
|
||||
public float PositionY { get; set; }
|
||||
public float PositionZ { get; set; }
|
||||
public float Heading { get; set; }
|
||||
[StringLength(8)]
|
||||
public string NumberPlate { get; set; }
|
||||
public byte Alpha { get; set; }
|
||||
public byte PrimaryColor { get; set; }
|
||||
public byte SecondaryColor { get; set; }
|
||||
public bool Locked { get; set; }
|
||||
public bool Engine { get; set; }
|
||||
public byte Dimension { get; set; }
|
||||
public bool Active { get; set; }
|
||||
}
|
||||
public class SavedVehicle : ServerVehicle
|
||||
{ }
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace reallife_gamemode.Server.Util
|
||||
NAPI.Util.ConsoleOutput("Initializing database...");
|
||||
using(var dbContext = new DatabaseContext())
|
||||
{
|
||||
dbContext.Users.First();
|
||||
dbContext.Users.FirstOrDefault();
|
||||
dbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user