Example #1
0
        public FusionObject(Domain.Demon d1, Domain.Demon d2, Domain.Fusion f)
        {
            _logger = GlobalObjects.Logger;
            string location = this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name;

            _logger.OpenSection(location);
            _dbSession = GlobalObjects.DbSession;

            Demon1 = (d1.Level <= d2.Level ? d1 : d2);
            Demon2 = (d1.Level <= d2.Level ? d2 : d1);
            Fusion = f;
            Demon3 = FindDemonFromFusion(d1, d2, f);

            _logger.CloseSection(location);
        }
Example #2
0
        private Domain.Demon FindDemonFromFusion(
            Domain.Demon d1, Domain.Demon d2, Domain.Fusion f)
        {
            Domain.Demon returnDemon = null;

            if (f != null)
            {
                if (f.IdRace3 == GlobalObjects.ImpossibleToFuseRace.Id)
                {
                    FusionIsImpossible = true;
                }
                else
                {
                    returnDemon = _dbSession.CreateCriteria <Domain.Demon>().List <Domain.Demon>()
                                  .Where(x =>
                                         x.Race.Id == f.IdRace3 &&
                                         x.Level > (d1.Level + d2.Level) / 2)
                                  .OrderBy(x => x.Level)
                                  .FirstOrDefault();
                }
            }
            return(returnDemon);
        }
Example #3
0
        private void dgvPartyFusions_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (_cellChanged)
            {
                string location = this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name;
                _logger.OpenSection(location);

                _logger.Info("Called with row index " + e.RowIndex + ", column index = " + e.ColumnIndex);
                var currentRow = this.dgvPartyFusions.Rows[e.RowIndex];

                Domain.Demon rowDemon         = null;
                Domain.Demon databaseDemon    = null;
                bool         insertedNewDemon = false;

                // THIS CODE IS COPIED FROM THE DEMONS LIST FORM
                // FIXME: REFACTOR / DO SOMETHING BETTER
                using (var transaction = _dbSession.BeginTransaction())
                {
                    rowDemon = GetDemonFromDataGridViewRow(currentRow, true);
                    if (rowDemon != null)
                    {
                        databaseDemon = rowDemon;
                        bool insertDemon  = true;
                        bool insertFusion = true;
                        if (rowDemon.Id == null)
                        {
                            // ID is null but maybe the demon is already in the DB. Look in the database
                            _logger.Info("Looking in database if this demon already exists...");
                            databaseDemon = _dbSession.CreateCriteria <Domain.Race>().List <Domain.Race>().
                                            SelectMany(x => x.Demons).Where(y => y.Name == rowDemon.Name).FirstOrDefault();
                            if (databaseDemon != null)
                            {
                                _logger.Info("Found demon ID " + databaseDemon.Id + ".");
                                if (databaseDemon.Equals(rowDemon))
                                {
                                    _logger.Info("Demons are exactly the same; no need to update DB.");
                                }
                            }
                            else
                            {
                                insertDemon   = true;
                                databaseDemon = rowDemon;
                            }
                        }


                        if (insertDemon)
                        {
                            _logger.Info("Inserting new demon...");

                            // TODO: UPDATING
                            _logger.Info("Row demon is " + rowDemon.ToString() + ". " +
                                         (databaseDemon.Id == null ? "Inserting..." : "Updating..."));

                            if (databaseDemon.Id == null)
                            {
                                _dbSession.Save(databaseDemon); // insert
                                insertedNewDemon = true;
                            }

                            _logger.Info("Demon saved (for now; need to commit).");
                        }

                        if (insertFusion)
                        {
                            _logger.Info("Inserting new fusion...");
                            Domain.Fusion f = new Domain.Fusion(
                                (Domain.Race)currentRow.Cells[(int)MyDataGridColumns.colRaceObject1].Value,
                                (Domain.Race)currentRow.Cells[(int)MyDataGridColumns.colRaceObject2].Value,
                                databaseDemon.Race);
                            _dbSession.Save(f);

                            _logger.Info("Fusion saved (for now; need to commit).");
                        }

                        if (insertDemon || insertFusion)
                        {
                            try
                            {
                                _logger.Info("Committing...");
                                transaction.Commit();
                                _logger.Info("Transaction complete.");
                                LoadData();
                            }
                            catch (Exception ex)
                            {
                                _logger.Error(ex);
                                MessageBox.Show("ERROR: " + ex.Message +
                                                ex.InnerException == null ? "" : "\r\n" + ex.InnerException.Message);
                            }
                        }
                    }
                } // using (var transaction = _dbSession.BeginTransaction())

                var numberOfDemonsForFusions =
                    _dbSession.CreateCriteria <Domain.Race>().List <Domain.Race>().
                    SelectMany(x => x.Demons).Where(y => y.UseInFusionCalculatorBoolean).ToList();
                if (numberOfDemonsForFusions.Count == 2)
                {
                    GlobalObjects.MainForm.ForceUpdateFusions();
                }

                if (insertedNewDemon)
                {
                    RemoveHandlers();
                    currentRow.Cells[(int)MyDataGridColumns.colId3].Value = databaseDemon.Id;
                    AddHandlers();
                }
                // END OF COPIED CODE
                // FIXME: REFACTOR / DO SOMETHING BETTER

                _logger.CloseSection(location);
            }
        }
        private void dgvFusions_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            List <Domain.Fusion> allFusions;

            if (_cellChanged)
            {
                string location = this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name;
                _logger.OpenSection(location);

                _logger.Info("Called with row index " + e.RowIndex + ", column index = " + e.ColumnIndex);
                Domain.Race race1      = this.dgvFusions.Rows[0].Cells[e.ColumnIndex].Tag as Domain.Race;
                Domain.Race race2      = this.dgvFusions.Rows[e.RowIndex].Cells[0].Tag as Domain.Race;
                var         resultCell = this.dgvFusions.Rows[e.RowIndex].Cells[e.ColumnIndex];

                Domain.Race race3 = null;

                if (resultCell.Value != null)
                {
                    race3 = GlobalObjects.CurrentGame.Races.
                            Where(x => x.Name == resultCell.Value.ToString()).FirstOrDefault();

                    if (race3 == null && resultCell.ToString() == GlobalObjects.ImpossibleToFuseRace.Name)
                    {
                        race3 = GlobalObjects.ImpossibleToFuseRace;
                    }

                    if (race3 == null)
                    {
                        race3 = _dbSession.CreateCriteria <Domain.Race>().List <Domain.Race>()
                                .Where(x => x.Name == resultCell.Value.ToString()).FirstOrDefault();
                        if (race3 == null)
                        {
                            race3 = GlobalObjects.InsertRaceMaybe(resultCell.ToString());
                        }
                    }
                }

                if (race3 == null && resultCell.Value != null)
                {
                    _logger.Info("Insertion cancelled.");
                }
                else
                {
                    using (var transaction = _dbSession.BeginTransaction())
                    {
                        var currentFusion =
                            _dbSession.CreateCriteria <Domain.Fusion>().List <Domain.Fusion>()
                            .Where(x => x.IdRace1 == race2.Id && x.IdRace2 == race1.Id)
                            .FirstOrDefault();
                        if (currentFusion == null)
                        {
                            currentFusion = _dbSession.CreateCriteria <Domain.Fusion>().List <Domain.Fusion>()
                                            .Where(x => x.IdRace1 == race1.Id && x.IdRace2 == race2.Id)
                                            .FirstOrDefault();
                        }

                        if (currentFusion != null)
                        {
                            _logger.Info("Got fusion " + currentFusion.ToString());
                            if (race3 != null)
                            {
                                _logger.Info("Result of fusion will now be " + race3.ToString());
                                currentFusion.IdRace3 = race3.Id;
                                _logger.Info("Saving fusion " + currentFusion.ToString());
                                _dbSession.Update(currentFusion);
                                _logger.Info("Saved.");
                            }
                            else
                            {
                                _logger.Info("Fusion will be deleted from database.");
                                _dbSession.Delete(currentFusion);
                                _logger.Info("Deleted.");
                            }
                        }
                        else
                        {
                            currentFusion = new Domain.Fusion(race1, race2, race3);
                            _logger.Info("Creating new fusion " + currentFusion.ToString());
                            _dbSession.Save(currentFusion);
                            _logger.Info("Created.");
                        }

                        transaction.Commit();
                        _logger.Info("Fusion saved.");
                    }
                }

                _logger.CloseSection(location);
            }
        }