Exemple #1
0
        /// <summary>
        ///     Checks if new VehicleHashes exists.
        /// </summary>
        private static void CheckVehicleProperties()
        {
            List <VehicleHash> dbExistingVehicleHashes = new List <VehicleHash>();

            using (VehicleContext context = VehicleRepository.GetVehicleContext())
            {
                dbExistingVehicleHashes.AddRange(context.VehicleProperties.Select(contextVehicleProperty =>
                                                                                  contextVehicleProperty.VehicleHash));
            }

            bool noticeWritten = false;

            string addedVehicleHashes = "";

            using (VehicleContext context = VehicleRepository.GetVehicleContext())
            {
                foreach (VehicleHash vehicleHash in Enum.GetValues(typeof(VehicleHash)))
                {
                    if (dbExistingVehicleHashes.Contains(vehicleHash))
                    {
                        continue;
                    }

                    // Write notice for long waiting time.
                    if (!noticeWritten)
                    {
                        ConsoleOutput.WriteLine(ConsoleType.Database,
                                                $"Setting default vehicle properties. This may take a moment.");
                        noticeWritten = true;
                    }

                    context.VehicleProperties.Add(new VehiclePropertiesDto
                    {
                        VehicleHash = vehicleHash,
                        BuildYear   = 1996,
                        Consumption = 5,
                        DisplayName = API.shared.getVehicleDisplayName(vehicleHash),
                        DoorCount   = 5,
                        FuelType    = FuelType.Gasoline,
                        MaxSpeed    = 999,
                        TankSize    = 10,
                        TrunkSize   = 12
                    });
                    addedVehicleHashes += $" {vehicleHash}";

                    // No vehicleHashes updated -> return;
                    if (string.IsNullOrEmpty(addedVehicleHashes))
                    {
                        return;
                    }


                    dbExistingVehicleHashes.Add(vehicleHash);
                }
                context.SaveChanges();
            }
            ConsoleOutput.WriteLine(ConsoleType.Database,
                                    $"Set default vehicle properties for ~o~{addedVehicleHashes}");
        }
Exemple #2
0
        /// <summary>
        ///     Saves the current Properties to the database.
        /// </summary>
        public void Save()
        {
            _vehicle = Properties;

            // Start new transaction for possibillity rollback
            using (DbContextTransaction contextTransaction =
                       VehicleRepository.GetVehicleContext().Database.BeginTransaction())
            {
                // Get wanted repository context
                using (VehicleContext context = VehicleRepository.GetVehicleContext())
                {
                    // Try to update values & commit values to transaction
                    try
                    {
                        context.Vehicles.AddOrUpdate(Properties);

                        // Doorstates
                        if (Properties.DoorStates != null)
                        {
                            foreach (DoorStateDto doorState in Properties.DoorStates)
                            {
                                context.DoorStates.AddOrUpdate(doorState);
                            }
                        }

                        // Modifications
                        if (Properties.Modifications != null)
                        {
                            foreach (VehicleModificationDto modification in Properties.Modifications)
                            {
                                context.VehicleModifications.AddOrUpdate(modification);
                            }
                        }

                        context.SaveChanges();
                        contextTransaction.Commit();

                        // Save VehicleId to the Vehicle
                        if (!Vehicle.IsNull)
                        {
                            API.shared.setEntityData(Vehicle, "VehicleId", Properties.VehicleId);
                        }
                    }
                    catch (Exception e)
                    {
                        ConsoleOutput.WriteLine(ConsoleType.Database, "Error on Saving ExtendedVehicle!");
                        ConsoleOutput.WriteException($"{e}");
                        // Rollback changes on failure
                        contextTransaction.Rollback();
                    }
                }

                Debug("Save - Extended Vehicle saved to database.");
            }
        }
Exemple #3
0
        /// <summary>
        ///     Updates the Properties.Modifications by the NetHandle.
        ///     Adds also new modification combinations to the VehicleModifications table.
        /// </summary>
        public void UpdateVehicleModifications()
        {
            if (Properties.Modifications == null)
            {
                Properties.Modifications = new List <VehicleModificationDto>();
            }

            using (VehicleContext context = VehicleRepository.GetVehicleContext())
            {
                foreach (VehicleModType modification in Enum.GetValues(typeof(VehicleModType)))
                {
                    // Search for existing Modification
                    int value = API.shared.getVehicleMod(Vehicle, (int)modification);

                    ModificationDto modificationDto = context.Modifications
                                                      .FirstOrDefault(modDto => modDto.Slot == modification && modDto.Value == value);

                    // No existing modification found -> Create new
                    if (modificationDto == null)
                    {
                        modificationDto = context.Modifications.Add(new ModificationDto
                        {
                            Value = value,
                            Slot  = modification
                        });
                        context.SaveChanges();
                    }

                    // Check for existing modification mapping or create new
                    if (Properties.Modifications.Any(vmdto =>
                                                     vmdto.Modification != null && vmdto.Modification.Slot == modification))
                    {
                        Properties.Modifications.First(vmdto =>
                                                       vmdto.Modification != null && vmdto.Modification.Slot == modification)
                        .Modification = modificationDto;
                    }
                    else
                    {
                        Properties.Modifications.Add(new VehicleModificationDto
                        {
                            VehicleId      = Properties.VehicleId,
                            ModificationId = modificationDto.ModificationId
                        });
                    }
                }
            }

            Debug("Update - Vehicle Modifications updated.");
        }
Exemple #4
0
        /// <summary>
        ///     Creates an Extendend Vehicle from an NetHandle.
        ///     Creates the saved ExtendedVehicle if the NetHandle has the "VehicleId" entity data.
        ///     If this is not the case, it creates a new ExtendedVehicle
        /// </summary>
        /// <param name="vehicle">The vehicle from wich the extendedVehicle should created.</param>
        internal ExtendedVehicle(NetHandle vehicle)
        {
            Vehicle = vehicle;

            VehicleHash vehicleHash = (VehicleHash)API.shared.getEntityModel(vehicle);

            // Get Database entry if given
            if (API.shared.hasEntityData(vehicle, "VehicleId"))
            {
                using (VehicleContext context = VehicleRepository.GetVehicleContext())
                {
                    InitFromDatabase((int)API.shared.getEntityData(vehicle, "VehicleId"));
                }
            }
            else // Create new extendedVehcile
            {
                InitNew(vehicleHash,
                        API.shared.getEntityPosition(vehicle), API.shared.getEntityRotation(vehicle),
                        API.shared.getEntityDimension(vehicle));
            }
        }
Exemple #5
0
        /// <summary>
        ///     Initalize the Extenden Vehicle from the database
        /// </summary>
        /// <param name="vehicleId">The vehicleId of the wanted Extended Vehicle</param>
        private void InitFromDatabase(int vehicleId)
        {
            using (VehicleContext context = VehicleRepository.GetVehicleContext())
            {
                Properties = context.Vehicles
                             .Include(vDto => vDto.SecondaryColor)
                             .Include(vDto => vDto.PrimaryColor)
                             .Include(vDto => vDto.VehicleProperties)
                             .Include(vdto => vdto.TyreSmokingColor)
                             .Include(vdto => vdto.VehicleLivery)
                             .First(vdto => vdto.VehicleId == vehicleId);

                Properties.DoorStates =
                    context.DoorStates.Where(doorState => doorState.VehicleId == vehicleId).ToList();

                Properties.Modifications =
                    context.VehicleModifications.Where(vcDto => vcDto.VehicleId == vehicleId)
                    .Include(dto => dto.Modification).ToList();
            }

            Debug("Init - Loaded from database.");
        }
Exemple #6
0
        /// <summary>
        ///     Initalize the extended Vehicle from a few informations
        /// </summary>
        /// <param name="vehicleHash">The VehicleHash</param>
        /// <param name="position">The Vehicle position</param>
        /// <param name="rotation">The Vehicle rotation</param>
        /// <param name="dimension">The vehicle dimension</param>
        private void InitNew(VehicleHash vehicleHash, Vector3 position, Vector3 rotation, int dimension)
        {
            //Get Vehicle properties
            using (VehicleContext context = VehicleRepository.GetVehicleContext())
            {
                Properties = context.Vehicles.Add(new VehicleDto
                {
                    VehicleHash       = vehicleHash,
                    VehicleProperties = context.VehicleProperties.First(dto => dto.VehicleHash == vehicleHash),
                    Position          = position,
                    Rotation          = rotation,
                    Dimension         = dimension
                });
                context.SaveChanges();

                // Save VehicleId to the Vehicle
                if (!Vehicle.IsNull)
                {
                    API.shared.setEntityData(Vehicle, "VehicleId", Properties.VehicleId);
                }
            }

            Debug("Init - By vehicleHash, position, rotation, dimension.");
        }
Exemple #7
0
        /// <summary>
        ///     Updates the Properties.primaryColor, Properties.secondaryColor and the tyreColor by the NetHandle.
        ///     Adds also new color combinations to the VehicleColors table.
        /// </summary>
        public void UpdateColor()
        {
            if (Vehicle.IsNull)
            {
                return;
            }

            Color primaryColor   = API.shared.getVehicleCustomPrimaryColor(Vehicle);
            Color secondaryColor = API.shared.getVehicleCustomSecondaryColor(Vehicle);
            Color tyreColor      = API.shared.getVehicleTyreSmokeColor(Vehicle);

            using (VehicleContext context = VehicleRepository.GetVehicleContext())
            {
                // Search for existing color combination
                VehicleColorDto primaryColorDto = context.VehicleColors.FirstOrDefault(vcDto =>
                                                                                       vcDto.Blue == primaryColor.blue && vcDto.Green == primaryColor.green &&
                                                                                       vcDto.Red == primaryColor.red);

                // Create color if not exist
                if (primaryColorDto == null)
                {
                    primaryColorDto = context.VehicleColors.Add(new VehicleColorDto
                    {
                        Green = primaryColor.green,
                        Blue  = primaryColor.blue,
                        Red   = primaryColor.red
                    });
                    context.SaveChanges();
                }

                VehicleColorDto secondaryColorDto = context.VehicleColors.FirstOrDefault(vcDto =>
                                                                                         vcDto.Blue == secondaryColor.blue && vcDto.Green == secondaryColor.green &&
                                                                                         vcDto.Red == secondaryColor.red);

                if (secondaryColorDto == null)
                {
                    secondaryColorDto = context.VehicleColors.Add(new VehicleColorDto
                    {
                        Green = secondaryColor.green,
                        Blue  = secondaryColor.blue,
                        Red   = secondaryColor.red
                    });
                    context.SaveChanges();
                }

                VehicleColorDto tyreColorDto = context.VehicleColors.FirstOrDefault(vcDto =>
                                                                                    vcDto.Blue == tyreColor.blue && vcDto.Green == tyreColor.green &&
                                                                                    vcDto.Red == tyreColor.red);

                if (tyreColorDto == null)
                {
                    tyreColorDto = context.VehicleColors.Add(new VehicleColorDto
                    {
                        Green = tyreColor.green,
                        Blue  = tyreColor.blue,
                        Red   = tyreColor.red
                    });
                    context.SaveChanges();
                }

                // Save color to vehicle
                Properties.PrimaryColor       = primaryColorDto;
                Properties.SecondaryColor     = secondaryColorDto;
                Properties.TyreSmokingColor   = tyreColorDto;
                Properties.PrimaryColorId     = primaryColorDto.VehicleColorId;
                Properties.SecondaryColorId   = secondaryColorDto.VehicleColorId;
                Properties.TyreSmokingColorId = tyreColorDto.VehicleColorId;
            }

            Debug("Update - Vehicle color updated.");
        }