Esempio n. 1
0
        /// <summary>
        /// Funkcja uruchamiana wraz ze startem serwera. Ładuje wszystkie pojazdy do pamięci
        /// </summary>
        public static void LoadVehicles()
        {
            double startTime = Global.GetTimestampMs();

            using (Database.Database db = new Database.Database())
            {
                List <Vehicle> vehicles = db.Vehicles.ToList();
                foreach (Vehicle entry in vehicles)
                {
                    entry.Spawned      = false;
                    entry.MappedExtras = new Dictionary <int, bool>();
                    try
                    {
                        entry.MappedExtras = JsonConvert.DeserializeObject <Dictionary <int, bool> >(entry.Extras);
                    }
                    catch
                    {
                        entry.MappedExtras = new Dictionary <int, bool>();
                    }

                    VehiclesList.Add(entry.Id, entry);
                    if (entry.OwnerType == OwnerType.Group)
                    {
                        Group groupData = Groups.Library.GetGroupData(entry.Owner);
                        if (groupData == null) // Jeśli grupa nie istnieje
                        {
                            DestroyVehicle(entry.Id);
                        }
                        else
                        {
                            if (groupData.Type != GroupType.Family)
                            {
                                SpawnVehicle(entry.Id);
                            }
                        }
                    }
                }

                foreach (VehicleFuel entry in db.VehiclesFuel.ToList())
                {
                    VehConsumption entryConsumption = new VehConsumption
                    {
                        VehicleHash = NAPI.Util.GetHashKey(entry.VehicleName),
                        Consumption = entry.Consumption,
                        MaxFuel     = entry.MaxFuel
                    };
                    VehiclesFuel.Add(entryConsumption.VehicleHash, entryConsumption);
                }

                Log.ConsoleLog("VEHICLES",
                               $"Załadowano pojazdy ({VehiclesList.Count}) | {Global.GetTimestampMs() - startTime}ms");
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Tworzy nowy pojazd.
        /// </summary>
        /// <param name="vehicleHash"></param>
        /// <param name="position"></param>
        /// <param name="rotation"></param>
        /// <param name="colorFirst"></param>
        /// <param name="colorSecond"></param>
        /// <param name="ownerTypeType"></param>
        /// <param name="dimension"></param>
        /// <param name="owner"></param>
        /// <param name="name"></param>
        /// <param name="createdBy"></param>
        /// <returns></returns>
        public static Vehicle CreateVehicle(long vehicleHash, Vector3 position, Vector3 rotation,
                                            int colorFirst, int colorSecond, OwnerType ownerTypeType, int dimension, int owner, string name,
                                            string createdBy = "System")
        {
            VehConsumption vehCon = GetDefaultConsumption(vehicleHash);

            Vehicle veh = new Vehicle
            {
                VehicleHash    = vehicleHash,
                Name           = name,
                OwnerType      = ownerTypeType,
                Owner          = owner,
                Dimension      = dimension,
                NumberPlate    = "BRAK",
                PlateType      = 1,
                Spawned        = false,
                X              = position.X,
                Y              = position.Y,
                Z              = position.Z,
                Rx             = rotation.X,
                Ry             = rotation.Y,
                Rz             = rotation.Z,
                Closed         = true,
                FirstColor     = colorFirst,
                SecondColor    = colorSecond,
                MaxFuel        = vehCon.MaxFuel,
                Fuel           = (int)Math.Floor((double)vehCon.MaxFuel / 2),
                Livery         = 0,
                Extras         = "",
                FuelMultiplier = vehCon.Consumption,
                Health         = 1000.0f,
                Blocked        = false,
                BlockValue     = 0,
                Modifications  = "",
                Mileage        = 0.0,
                MappedExtras   = new Dictionary <int, bool>()
            };

            using (Database.Database db = new Database.Database())
            {
                db.Vehicles.Add(veh);
                db.SaveChanges();
                Log.ConsoleLog("VEHICLE", $"Utworzono pojazd \"{veh.Name}\" (UID: {veh.Id}) [{createdBy}]",
                               LogType.Debug);
                VehiclesList.Add(veh.Id, veh);
                SpawnVehicle(veh.Id);
                return(veh);
            }
        }