public static string PackVehicleData(Vehicle vehicle)
        {
            try
            {
                var data = new VehicleDataModel
                {
                    LicensePlate = vehicle.Mods.LicensePlate,
                    Class        = (int)vehicle.ClassType,
                    //VehicleFuel = vehicle.HasDecor("Vehicle.Fuel") ? vehicle.GetDecor<float>("Vehicle.Fuel") : 100.0f,
                    Model             = vehicle.Model,
                    EngineHealth      = vehicle.EngineHealth,
                    BodyHealth        = vehicle.BodyHealth,
                    Livery            = vehicle.Mods.Livery,
                    PrimaryColor      = vehicle.Mods.PrimaryColor,
                    SecondaryColor    = vehicle.Mods.SecondaryColor,
                    PearlescentColor  = vehicle.Mods.PearlescentColor,
                    RimColor          = vehicle.Mods.RimColor,
                    WheelType         = vehicle.Mods.WheelType,
                    WindowTint        = vehicle.Mods.WindowTint,
                    LicensePlateStyle = vehicle.Mods.LicensePlateStyle
                };
                foreach (var mod in vehicle.Mods.GetAllMods())
                {
                    if (!data.Mods.ContainsKey(mod.ModType))
                    {
                        data.Mods.Add(mod.ModType, mod.Index);
                    }
                }
                data.CustomWheelVariation = API.IsVehicleExtraTurnedOn(vehicle.Handle, (int)VehicleModType.FrontWheel);
                Enumerable.Range(0, 50).ToList().ForEach(e =>
                {
                    data.Extras[e] = API.IsVehicleExtraTurnedOn(vehicle.Handle, e);
                });
                Enum.GetValues(typeof(VehicleToggleModType)).Cast <VehicleToggleModType>().ToList().ForEach(o =>
                {
                    var toggleMod      = vehicle.Mods[o];
                    data.ToggleMods[o] = toggleMod.IsInstalled;
                });
                Enum.GetValues(typeof(VehicleNeonLight)).Cast <VehicleNeonLight>().ToList().ForEach(o =>
                {
                    data.NeonLights[o] = vehicle.Mods.IsNeonLightsOn(o);
                });
                var neonColour = vehicle.Mods.NeonLightsColor;
                data.NeonLightsColour = new int[] { neonColour.R, neonColour.G, neonColour.B };

                return(JsonConvert.SerializeObject(data));
            }
            catch (Exception e)
            {
                Log.Error(e);
                return(null);
            }
        }
Beispiel #2
0
        private void OnDataRequest([FromSource] Player source, int vehId)
        {
            Session.Session playerSession = Sessions.GetPlayer(source);
            if (playerSession == null)
            {
                return;
            }

            Log.Verbose($"Attempting to load vehicle ({vehId}) for {source.Name} (CharID: {playerSession.GetGlobalData<int>("Character.CharID")})");
            MySQL.execute("SELECT * FROM vehicle_data WHERE VehID = @id", new Dictionary <string, dynamic>
            {
                ["@id"] = vehId
            }, new Action <List <dynamic> >(data =>
            {
                if (data[0].Impounded)
                {
                    RetrieveImpoundVehicle(playerSession, data[0]);
                    return;
                }

                //VehicleDataModel modData = JsonConvert.DeserializeObject<VehicleDataModel>(data[0].Mods);
                var didParse = ((string)data[0].Mods).TryParseJson(out VehicleDataModel modData);

                if (!didParse)
                {
                    modData = new VehicleDataModel();
                }

                try
                {
                    modData.Model = CitizenFX.Core.Native.API.GetHashKey(VehicleStoreItems.StoreMenus.FirstOrDefault(o => o.Value.FirstOrDefault(b => b.displayName == data[0].VehicleName) != null).Value.FirstOrDefault(o => o.displayName == data[0].VehicleName)?.modelName);
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }

                source.TriggerEvent("Vehicle.SpawnGarageVehicle", data[0].Plate, data[0].VehID, /*data[0].Mods*/ JsonConvert.SerializeObject(modData));
                currentVehicles.Add(new Models.Vehicle(data[0], playerSession));
                AddPlayerOwnedVehicle(playerSession, data[0].VehID);
                MySQL.execute("UPDATE vehicle_data SET OutGarage = true WHERE VehID = @id", new Dictionary <string, dynamic>
                {
                    ["@id"] = vehId
                });

                Log.Verbose($"Loaded vehicle ({vehId}) for {source.Name} (CharID: {playerSession.GetGlobalData<int>("Character.CharID")})");
            }));
        }
Beispiel #3
0
        public ActionResult AllVehicles()
        {
            ConvertToBase64 convertTo = new ConvertToBase64();

            List <VehicleDataModel> vehicleDataModels = new List <VehicleDataModel>();

            foreach (var v in db.Vehicles)
            {
                String img = "";
                if (v.Images.Count() > 0)
                {
                    img = convertTo.convertToBase64(v.Images.First().ImagePath);
                }
                else
                {
                    img = v.ImageUrl;
                }

                VehicleDataModel dataModel = new VehicleDataModel
                {
                    Brand       = v.make,
                    Name        = v.make + " " + v.model,
                    RegNumber   = v.RegNumber,
                    RentalPrice = string.Format("{0:c}", double.Parse(v.RentalPrice)),
                    year        = v.year,
                    id          = v.Id,
                    Image       = img
                };

                vehicleDataModels.Add(dataModel);
            }


            JsonResult res = new JsonResult
            {
                ContentEncoding     = Encoding.Default,
                ContentType         = "application/json",
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                Data          = Json(vehicleDataModels),
                MaxJsonLength = Int32.MaxValue
            };

            return(res);
        }
Beispiel #4
0
        public VehicleManager(Server server) : base(server)
        {
            //Session = Sessions;
            //server.RegisterEventHandler("Vehicle.RequestGarageVehicles", new Action<Player, string>(OnVehiclesRequest));
            //server.RegisterEventHandler("Vehicle.RequestVehicleData", new Action<Player, int>(OnDataRequest));
            //server.RegisterEventHandler("Vehicle.CreateExternalVehicle", new Action<Player, string>(OnCreateExternalVehicle));
            //server.RegisterEventHandler("Vehicle.GiveKeyAccess", new Action<Player, int, int>(OnGiveKeyAccess));
            //server.RegisterEventHandler("Vehicle.GiveVehicleOwnership", new Action<Player, int, int>(OnGiveVehOwnership));
            //server.RegisterEventHandler("Vehicle.ImpoundVehicle", new Action<Player, int>(OnImpoundVehicle));
            //server.RegisterEventHandler("Vehicles.UpdateVehicleData", new Action<Player, int, string>(OnDataUpdate));
            //server.RegisterEventHandler("Vehicles.StoreOwnedVehicle", new Action<Player, int, string, string>(OnStoreVehicle));
            //server.RegisterEventHandler("Vehicles.SetIndicatorStatus", new Action<Player, string, bool>(OnSetIndicator));
            server.RegisterTickHandler(SaveVehicleTick);
            CommandRegister.RegisterAdminCommand("listvehdata", cmd =>
            {
                var veh = GetVehicle(cmd.GetArgAs <int>(0));
                foreach (var property in veh.GetType().GetRuntimeProperties())
                {
                    dynamic propertyValue = property.GetValue(veh, null);
                    var propertyString    = propertyValue.ToString();
                    if (propertyValue.GetType() == typeof(List <int>))
                    {
                        propertyString = "";
                        foreach (var val in propertyValue)
                        {
                            propertyString += $"{val},";
                        }
                    }
                    Log.Info($"{property.Name}: {propertyString}");
                }
            }, AdminLevel.Developer);
            CommandRegister.RegisterAdminCommand("savevehicles", cmd => SaveAllVehicles(), AdminLevel.SuperAdmin);
            Task.Factory.StartNew(async() =>
            {
                await BaseScript.Delay(5000);
                MySQL.execute("UPDATE vehicle_data SET OutGarage = false");

                MySQL.execute("SELECT * FROM vehicle_data", new Dictionary <string, dynamic>
                {
                }, new Action <List <dynamic> >(data =>
                {
                    foreach (var veh in data)
                    {
                        try
                        {
                            VehicleDataModel mods = JsonConvert.DeserializeObject <VehicleDataModel>(veh.mods);
                            if (mods.Model == 0)
                            {
                                MySQL.execute("UPDATE bank_accounts SET Balance = Balance + @price WHERE OwnerID = @id", new Dictionary <string, dynamic>
                                {
                                    {
                                        "@price", veh.VehiclePrice
                                    },
                                    {
                                        "@id", veh.CharID
                                    }
                                });
                                MySQL.execute("DELETE FROM vehicle_data WHERE VehID = @id", new Dictionary <string, dynamic>
                                {
                                    {
                                        "@id", veh.VehID
                                    }
                                });
                            }
                        }
                        catch { }
                    }
                }));
            });
        }
        public static async Task <Vehicle> UnpackVehicleData(VehicleDataModel vehicleModel, Vector3 position, float heading)
        {
            try
            {
                var vehModel = new Model(vehicleModel.Model);
                if (!vehModel.IsInCdImage)
                {
                    Log.ToChat($"Unable to spawn vehicle with model {vehicleModel.Model} as it doesn't exist?");
                    return(null);
                }

                while (!vehModel.IsLoaded)
                {
                    await vehModel.Request(0);
                }

                Vehicle vehicle = await World.CreateVehicle(vehModel, position, heading);

                vehicle.Mods.InstallModKit();
                if (vehicleModel.BodyHealth < 150.0f)
                {
                    vehicleModel.BodyHealth = 150.0f;
                }
                vehicle.BodyHealth = vehicleModel.BodyHealth;
                if (vehicleModel.EngineHealth < 150.0f)
                {
                    vehicleModel.EngineHealth = 150.0f;
                }
                vehicle.EngineHealth      = vehicleModel.EngineHealth;
                vehicle.Mods.LicensePlate = vehicleModel.LicensePlate;
                //vehicle.SetDecor("Vehicle.Fuel", vehicleModel.VehicleFuel);
                vehicle.Mods.Livery            = vehicleModel.Livery;
                vehicle.Mods.PrimaryColor      = vehicleModel.PrimaryColor;
                vehicle.Mods.SecondaryColor    = vehicleModel.SecondaryColor;
                vehicle.Mods.PearlescentColor  = vehicleModel.PearlescentColor;
                vehicle.Mods.RimColor          = vehicleModel.RimColor;
                vehicle.Mods.WheelType         = vehicleModel.WheelType;
                vehicle.Mods.WindowTint        = vehicleModel.WindowTint;
                vehicle.Mods.LicensePlateStyle = vehicleModel.LicensePlateStyle;
                vehicleModel.Mods?.ToList().ForEach(m =>
                {
                    vehicle.Mods[m.Key].Index = m.Value;
                });
                Function.Call(Hash.SET_VEHICLE_EXTRA, vehicle.Handle, VehicleModType.FrontWheel, vehicleModel.CustomWheelVariation ? 0 : -1);
                vehicleModel.Extras?.ToList().ForEach(e =>
                {
                    Function.Call(Hash.SET_VEHICLE_EXTRA, vehicle.Handle, e.Key, e.Value ? 0 : -1);
                });

                vehicleModel.ToggleMods?.ToList().ForEach(o =>
                {
                    vehicle.Mods[o.Key].IsInstalled = o.Value;
                });

                vehicleModel.NeonLights?.ToList().ForEach(o =>
                {
                    vehicle.Mods.SetNeonLightsOn(o.Key, o.Value);
                    if (o.Value)
                    {
                        vehicle.Mods.NeonLightsColor = Color.FromArgb(vehicleModel.NeonLightsColour[0], vehicleModel.NeonLightsColour[1], vehicleModel.NeonLightsColour[2]);
                    }
                });

                return(vehicle);
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
            return(null);
        }