Files
reallife-gamemode/Server/Managers/SaveManager.cs
2018-12-22 15:02:05 +01:00

180 lines
7.0 KiB
C#

using GTANetworkAPI;
using reallife_gamemode.Model;
using reallife_gamemode.Server.Business;
using reallife_gamemode.Server.Entities;
using reallife_gamemode.Server.Extensions;
using reallife_gamemode.Server.Managers;
using reallife_gamemode.Server.Saves;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace reallife_gamemode.Server.Managers
{
public class SaveManager : Script
{
[RemoteEvent("OnSaveBlipData")]
public static void OnSaveBlipData(Client player, string blipSprite, string blipName, string blipScale, string blipColor,
string blipAlpha, string blipDrawDistance, string blipShortRange, string blipRotation, string blipDimension)
{
float x = player.Position.X;
float y = player.Position.Y;
float z = player.Position.Z;
short sprite = short.Parse(blipSprite);
string name = blipName;
float scale = float.Parse(blipScale);
byte color = Convert.ToByte(blipColor);
byte alpha = Convert.ToByte(blipAlpha);
float drawDistance = float.Parse(blipDrawDistance);
bool shortRange = bool.Parse(blipShortRange);
float rotation = float.Parse(blipRotation);
byte dimension = Convert.ToByte(blipDimension);
NAPI.Blip.CreateBlip(uint.Parse(blipSprite), new Vector3(x,y,z), scale, color, name, alpha, drawDistance, shortRange, short.Parse(blipRotation), dimension);
using (var saveData = new DatabaseContext())
{
var dataSet = new SavedBlip
{
Sprite = sprite,
PositionX = x,
PositionY = y,
PositionZ = z,
Name = blipName,
Scale = scale,
Color = color,
Alpha = alpha,
DrawDistance = drawDistance,
ShortRange = shortRange,
Rotation = rotation,
Dimension = dimension,
Active = true
};
saveData.Blips.Add(dataSet);
saveData.SaveChanges();
}
}
public static Vehicle SaveVehicleData(Vehicle veh, VehicleHash vehicleModel, Vector3 vehiclePosition, float vehicleHeading,
string vehicleNumberPlate, int vehiclePrimaryColor, int vehicleSecondaryColor, bool vehicleLocked)
{
using (var saveData = new DatabaseContext())
{
var dataSet = new SavedVehicle
{
Model = vehicleModel,
PositionX = vehiclePosition.X,
PositionY = vehiclePosition.Y,
PositionZ = vehiclePosition.Z,
Heading = vehicleHeading,
NumberPlate = vehicleNumberPlate,
PrimaryColor = vehiclePrimaryColor,
SecondaryColor = vehicleSecondaryColor,
Locked = vehicleLocked,
Active = true
};
saveData.Vehicles.Add(dataSet);
saveData.SaveChanges();
return dataSet.Spawn(veh);
}
}
public static Vehicle SaveFactionVehicleData(Vehicle veh, VehicleHash vehicleModel, Vector3 vehiclePosition, float vehicleHeading,
string vehicleNumberPlate, int vehiclePrimaryColor, int vehicleSecondaryColor, bool vehicleLocked, bool vehicleEngine, int? factionId)
{
using (var saveData = new DatabaseContext())
{
var dataSet = new FactionVehicle
{
Model = vehicleModel,
FactionId = factionId,
PositionX = vehiclePosition.X,
PositionY = vehiclePosition.Y,
PositionZ = vehiclePosition.Z,
Heading = vehicleHeading,
NumberPlate = vehicleNumberPlate,
PrimaryColor = vehiclePrimaryColor,
SecondaryColor = vehicleSecondaryColor,
Locked = vehicleLocked,
Active = true
};
saveData.FactionVehicles.Add(dataSet);
saveData.SaveChanges();
return dataSet.Spawn(veh);
}
}
public static Vehicle SaveShopVehicleData(Vehicle veh, VehicleHash vehicleModel, string vehicleModelName, Vector3 vehiclePosition, float vehicleHeading,
string vehicleNumberPlate, int vehiclePrimaryColor, int vehicleSecondaryColor, BusinessBase business, int price)
{
using (var saveData = new DatabaseContext())
{
var dataSet = new ShopVehicle
{
Model = vehicleModel,
PositionX = vehiclePosition.X,
PositionY = vehiclePosition.Y,
PositionZ = vehiclePosition.Z,
Heading = vehicleHeading,
NumberPlate = vehicleNumberPlate,
PrimaryColor = vehiclePrimaryColor,
SecondaryColor = vehicleSecondaryColor,
Active = true,
BusinessId = business.Id,
Price = price
};
saveData.ShopVehicles.Add(dataSet);
saveData.SaveChanges();
return dataSet.Spawn(veh);
}
}
public static void SaveGotoPoint(Client player, string description)
{
using (var saveData = new Model.DatabaseContext())
{
var dataSet = new Entities.GotoPoint
{
Description = description,
X = player.Position.X,
Y = player.Position.Y,
Z = player.Position.Z,
Active = true
};
saveData.GotoPoints.Add(dataSet);
saveData.SaveChanges();
}
}
public static void SaveAllOnSave()
{
// Alle Fahrzeuge
using (var saveAll = new Model.DatabaseContext())
{
foreach(ServerVehicle veh in saveAll.ServerVehicles)
{
Vehicle v = VehicleManager.GetVehicleFromServerVehicle(veh);
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())
{
Vector3 pos = player.Position;
User user = player.GetUser(saveAll);
user.PositionX = pos.X;
user.PositionY = pos.Y;
user.PositionZ = pos.Z;
}
saveAll.SaveChanges();
}
}
}
}