Ejemplo n.º 1
0
        protected Tasks chooseTaskHw(MyInfo info)
        {
            var ratios = new Ratios();

            if (info.CPU < Tasks.CPU.Max)
            {
                ratios[Tasks.CPU] = info.CPU;
            }
            if (info.HDD < Tasks.HDD.Max)
            {
                ratios[Tasks.HDD] = info.HDD;
            }
            if (info.Internet < Tasks.Internet.Max)
            {
                ratios[Tasks.Internet] = info.Internet;
            }
            if (info.RAM < Tasks.RAM.Max)
            {
                ratios[Tasks.RAM] = info.RAM;
            }

            if (ratios.Count == 0)
            {
                return(null); // all HW stats are fully upgraded
            }
            var ordered = ratios.OrderBy(r => r.Value);
            var task    = ordered.First();

            return(task.Key);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Fills the torque ratio Listview
        /// </summary>
        private void PopulateTorqueRatios()
        {
            // Clear out any old items
            ratioListView.Items.Clear();

            int i = 1;

            foreach (TorqueRatio ratio in Ratios.OrderBy(x => x.RpmLevel))
            {
                ListViewItem item = new ListViewItem(i.ToString());
                item.SubItems.Add(ratio.RpmLevel.ToString());
                item.SubItems.Add(Math.Round(ratio.Ratio * 100, 2).ToString(Program.NumberFormat));
                item.Tag = Ratios.IndexOf(ratio);
                ratioListView.Items.Add(item);
                i++;
            }
        }
Ejemplo n.º 3
0
        private void torqueBox_ValueChanged(object sender, EventArgs e)
        {
            // Update label
            labelNM.Text = (Program.Config.UnitSystem == UnitSystem.Imperial)
                ? String.Concat(Metrics.TorqueToNewtonMeters(torqueBox.Value), " (Nm)")
                : String.Concat(Metrics.NewtonMetersToTorque(torqueBox.Value), " (Trq)");

            // disable buttons
            addPointButton.Enabled    = false;
            removePointButton.Enabled = false;

            // Clear old chart points
            chart1.Series[0].Points.Clear();
            chart1.Series[1].Points.Clear();

            DataPoint   lastPoint  = null;
            TorqueRatio highest    = null;
            double      torque     = 0;
            double      horsepower = 0;

            // Fill torque ratios
            foreach (TorqueRatio ratio in Ratios.OrderBy(x => x.RpmLevel))
            {
                // Plot the torque point
                torque = Math.Round((double)torqueBox.Value * ratio.Ratio, 2);
                int       index = chart1.Series[0].Points.AddXY(ratio.RpmLevel, torque);
                DataPoint point = chart1.Series[0].Points[index];

                // Set tool tip and covert chart value for later
                if (Program.Config.UnitSystem == UnitSystem.Imperial)
                {
                    point.ToolTip = $"{torque} lb-ft @ {ratio.RpmLevel} RPM";
                }
                else
                {
                    point.ToolTip = $"{torque} Nm @ {ratio.RpmLevel} RPM";
                    torque        = Metrics.NewtonMetersToTorque(torque, 2);
                }

                // Set the highest and earliest ratio
                if (highest == null || ratio.Ratio > highest.Ratio)
                {
                    highest = ratio;
                }

                // === Plot Horsepower
                if (lastPoint != null)
                {
                    // Calculate the last plots starting Torque value
                    double torqueAtPoint = lastPoint.YValues[0];
                    // Get torque difference from last point
                    double deltaY = point.YValues[0] - lastPoint.YValues[0];
                    // Get RPM difference from last point
                    double deltaX = point.XValue - lastPoint.XValue;

                    // Convert Nm to Torque if using the Metric System
                    if (Program.Config.UnitSystem == UnitSystem.Metric)
                    {
                        deltaY        = Metrics.NewtonMetersToTorque(deltaY, 2);
                        torqueAtPoint = Metrics.NewtonMetersToTorque(torqueAtPoint, 2);
                    }

                    // Calculate torque rise (rpm distance * (torque rise per rpm)),
                    // Then walk from the last point, to right before the current one
                    double torqueRise = HP_POINT_DIST * (deltaY / deltaX);
                    double rpm        = lastPoint.XValue + HP_POINT_DIST;
                    double stop       = point.XValue;

                    // Now we plot the horsepower points, up until we hit the next torque point
                    for (; rpm < stop; rpm += HP_POINT_DIST)
                    {
                        // increment torque rating by our dermined curve rise
                        torqueAtPoint += torqueRise;

                        // Plot the horsepower point
                        horsepower = Metrics.TorqueToHorsepower(torqueAtPoint, rpm, 0);
                        index      = chart1.Series[1].Points.AddXY(rpm, horsepower);
                        chart1.Series[1].Points[index].ToolTip = $"#VALY HP @ {rpm} RPM";
                    }
                }

                // Plot the Horsepower plots
                horsepower = Metrics.TorqueToHorsepower(torque, ratio.RpmLevel, 0);
                index      = chart1.Series[1].Points.AddXY(ratio.RpmLevel, horsepower);
                chart1.Series[1].Points[index].ToolTip     = $"#VALY HP @ {ratio.RpmLevel} RPM";
                chart1.Series[1].Points[index].MarkerStyle = MarkerStyle.Circle;
                chart1.Series[1].Points[index].MarkerColor = Color.Black;

                // Set the last point
                lastPoint = point;
            }

            // Update labels
            if (chart1.Series[1].Points.Count > 0)
            {
                DataPoint pnt = chart1.Series[1].Points.FindMaxByValue("Y");
                maxHpLabel.Text    = $"{pnt.YValues[0]} @ {pnt.XValue} RPM";
                torque             = Math.Round((double)torqueBox.Value * highest.Ratio, 0);
                maxTrqLabel.Text   = $"{torque} @ {highest.RpmLevel} RPM";
                peakRPMBox.Value   = highest?.RpmLevel ?? 1200;
                peakRPMBox.Enabled = false; // no user edit
            }
            else
            {
                peakRPMBox.Enabled = true;
                maxHpLabel.Text    = "";
                maxTrqLabel.Text   = "";
            }

            // Enable buttons
            addPointButton.Enabled    = true;
            removePointButton.Enabled = true;
        }
Ejemplo n.º 4
0
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            // Check UnitName
            // Check for a valid identifier string
            if (!SiiFileBuilder.IsValidUnitName(unitNameBox.Text))
            {
                // Tell the user this isnt allowed
                MessageBox.Show(
                    "Invalid Engine Sii Unit Name. Tokens must be 1 to 12 characters in length, seperated by a dot, "
                    + "and contain alpha-numeric or underscores only",
                    "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning
                    );

                return;
            }

            // Check engine name
            if (engineNameBox.Text.Length < 2 || engineNameBox.Text.Contains('"'))
            {
                // Tell the user this isnt allowed
                MessageBox.Show(
                    "Invalid Engine Name string. The name must be at least 2 characters long and contain no quotes",
                    "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning
                    );

                return;
            }

            // Set new attribute values
            Engine.SeriesId                 = ((EngineSeries)engineModelBox.SelectedItem).Id;
            Engine.UnitName                 = unitNameBox.Text.Trim();
            Engine.Name                     = engineNameBox.Text.Trim();
            Engine.Price                    = (int)priceBox.Value;
            Engine.Unlock                   = (int)unlockBox.Value;
            Engine.Horsepower               = (int)horsepowerBox.Value;
            Engine.PeakRpm                  = (int)peakRPMBox.Value;
            Engine.IdleRpm                  = (int)idleRpmBox.Value;
            Engine.RpmLimit                 = (int)rpmLimitBox.Value;
            Engine.RpmLimitNeutral          = (int)neutralRpmBox.Value;
            Engine.BrakeStrength            = brakeStrengthBox.Value;
            Engine.BrakePositions           = (int)brakePositionsBox.Value;
            Engine.BrakeDownshift           = automaticDSCheckBox.Checked;
            Engine.MinRpmRange_LowGear      = (int)rpmRangeBox1.Value;
            Engine.MaxRpmRange_LowGear      = (int)rpmRangeBox2.Value;
            Engine.MinRpmRange_HighGear     = (int)rpmRangeBox3.Value;
            Engine.MaxRpmRange_HighGear     = (int)rpmRangeBox4.Value;
            Engine.LowRpmRange_PowerBoost   = (int)rpmRangeBox5.Value;
            Engine.HighRpmRange_PowerBoost  = (int)rpmRangeBox6.Value;
            Engine.ResistanceTorque         = (int)resistanceBox.Value;
            Engine.FuelConsumption          = Math.Round(consumptionBox.Value / 100, 2);
            Engine.NoAdbluePowerLimit       = adBlueNoPowerLimit.Value;
            Engine.AdblueConsumption        = adBlueConsumption.Value;
            Engine.LowRpmRange_EngineBrake  = (int)engineBrakeLow.Value;
            Engine.HighRpmRange_EngineBrake = (int)engineBrakeHigh.Value;
            Engine.Defaults                 = fileDefaultsTextBox.Lines;
            Engine.Overrides                = fileOverridesTextBox.Lines;
            Engine.Comment                  = fileCommentTextBox.Lines;
            Engine.Conflicts                = conflictsTextBox.Lines;

            // Torque metrics
            if (Program.Config.UnitSystem == UnitSystem.Imperial)
            {
                Engine.Torque = (int)torqueBox.Value;
            }
            else
            {
                Engine.NewtonMetres = (int)torqueBox.Value;
            }

            // Figure out the filename
            if (!String.IsNullOrWhiteSpace(filenameTextBox.Text))
            {
                Engine.FileName = filenameTextBox.Text.Trim();
            }

            // Validate and Save
            using (AppDatabase db = new AppDatabase())
                using (SQLiteTransaction trans = db.BeginTransaction())
                {
                    // Verify that the series.Id and engine.UnitName are unique
                    string query  = "SELECT * FROM `Engine` WHERE `SeriesId`=@P0 AND `UnitName`=@P1";
                    var    engine = db.Query <Engine>(query, Engine.SeriesId, Engine.UnitName).FirstOrDefault();
                    if (engine != null && (NewEngine || engine.Id != Engine.Id))
                    {
                        // Tell the user this isnt allowed
                        MessageBox.Show(
                            $"The selected Engine Series already contains an engine with the Sii Unit Name of \""
                            + Engine.UnitName + "\"! Please select a different Engine Series or change the Sii "
                            + "Unit Name to something unique.",
                            "Unique Constraint Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning
                            );
                        return;
                    }

                    // Wrap database changes in a try-catch block so we can Rollback on error
                    try
                    {
                        // Update or Add engine
                        db.Engines.AddOrUpdate(Engine);

                        // If pre-existing engine, delete all changed data
                        if (!NewEngine)
                        {
                            // Delete any and all TorueRatios from thed database
                            if (RatiosChanged)
                            {
                                foreach (TorqueRatio ratio in Engine.TorqueRatios)
                                {
                                    db.TorqueRatios.Remove(ratio);
                                }
                            }

                            // Set Conflicts
                            if (ConflictsChanged)
                            {
                                // Remove old
                                foreach (var conflict in Engine.TransmissionConflicts)
                                {
                                    db.AccessoryConflicts.Remove(conflict);
                                }
                            }

                            // Set Suitables
                            if (SuitablesChanged)
                            {
                                foreach (var item in Engine.SuitableTransmissions)
                                {
                                    db.SuitableAccessories.Remove(item);
                                }
                            }

                            // Set compatible trucks
                            if (TrucksChanged)
                            {
                                foreach (var item in Engine.ItemOf)
                                {
                                    db.TruckEngines.Remove(item);
                                }
                            }
                        }

                        // Add conflicts
                        if ((NewEngine || ConflictsChanged) && conflictListView.CheckedItems.Count > 0)
                        {
                            var ids = conflictListView.CheckedItems.Cast <ListViewItem>().Select(x => (int)x.Tag);
                            foreach (var item in ids)
                            {
                                db.AccessoryConflicts.Add(new AccessoryConflict()
                                {
                                    EngineId       = Engine.Id,
                                    TransmissionId = item
                                });
                            }
                        }

                        // Add suitible fors
                        if ((NewEngine || SuitablesChanged) && suitsListView.CheckedItems.Count > 0)
                        {
                            var ids = suitsListView.CheckedItems.Cast <ListViewItem>().Select(x => (int)x.Tag);
                            foreach (var item in ids)
                            {
                                db.SuitableAccessories.Add(new SuitableAccessory()
                                {
                                    EngineId       = Engine.Id,
                                    TransmissionId = item
                                });
                            }
                        }

                        // Add new truck engines
                        if ((NewEngine || TrucksChanged) && truckListView.CheckedItems.Count > 0)
                        {
                            var ids = truckListView.CheckedItems.Cast <ListViewItem>().Select(x => (int)x.Tag);
                            foreach (var item in ids)
                            {
                                db.TruckEngines.Add(new TruckEngine()
                                {
                                    EngineId = Engine.Id,
                                    TruckId  = item
                                });
                            }
                        }

                        // Add the new torque ratios
                        if (NewEngine || RatiosChanged)
                        {
                            foreach (TorqueRatio ratio in Ratios.OrderBy(x => x.RpmLevel))
                            {
                                ratio.EngineId = Engine.Id;
                                db.TorqueRatios.AddOrUpdate(ratio);
                            }
                        }

                        trans.Commit();
                    }
                    catch (Exception ex)
                    {
                        trans.Rollback();

                        // Tell the user about the failed validation error
                        MessageBox.Show(ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }

            this.DialogResult = DialogResult.OK;
        }