Example #1
0
        private void UpdateFusionRowDisplay(
            DataGridViewRow currentFusionRow, Domain.Demon demon1, Domain.Demon demon2, Domain.Demon demon3)
        {
            currentFusionRow.Cells[DGV_DEMONFUSION_COL_ID].Style = GlobalObjects.GetDefaultDgvcStyle(DGV_FUSIONS_FONT_SIZE);

            currentFusionRow.Cells[DGV_DEMONFUSION_COL_LEVEL1].ReadOnly                    =
                currentFusionRow.Cells[DGV_DEMONFUSION_COL_RACE1].ReadOnly                 =
                    currentFusionRow.Cells[DGV_DEMONFUSION_COL_NAME1].ReadOnly             =
                        currentFusionRow.Cells[DGV_DEMONFUSION_COL_LEVEL2].ReadOnly        =
                            currentFusionRow.Cells[DGV_DEMONFUSION_COL_RACE2].ReadOnly     =
                                currentFusionRow.Cells[DGV_DEMONFUSION_COL_NAME2].ReadOnly = true;

            currentFusionRow.Cells[DGV_DEMONFUSION_COL_LEVEL1].Style        =
                currentFusionRow.Cells[DGV_DEMONFUSION_COL_RACE1].Style     =
                    currentFusionRow.Cells[DGV_DEMONFUSION_COL_NAME1].Style = GlobalObjects.GetDefaultDgvcStyle
                                                                                  (fontSize: DGV_FUSIONS_FONT_SIZE, enabled: false, inParty: demon1.IsInParty);

            currentFusionRow.Cells[DGV_DEMONFUSION_COL_LEVEL2].Style        =
                currentFusionRow.Cells[DGV_DEMONFUSION_COL_RACE2].Style     =
                    currentFusionRow.Cells[DGV_DEMONFUSION_COL_NAME2].Style = GlobalObjects.GetDefaultDgvcStyle
                                                                                  (fontSize: DGV_FUSIONS_FONT_SIZE, enabled: false, inParty: demon2.IsInParty);

            currentFusionRow.Cells[DGV_DEMONFUSION_COL_LEVEL3].Style        =
                currentFusionRow.Cells[DGV_DEMONFUSION_COL_RACE3].Style     =
                    currentFusionRow.Cells[DGV_DEMONFUSION_COL_NAME3].Style = GlobalObjects.GetDefaultDgvcStyle(
                        fontSize: DGV_FUSIONS_FONT_SIZE,
                        enabled: demon3 == null,
                        inParty: demon3 == null ? false : demon3.IsInParty);
        }
Example #2
0
        private Tuple <Domain.Demon, int, Domain.Race> FindDemonFromFusion(
            Domain.Demon d1, Domain.Demon d2, Domain.FusionRace f)
        {
            // TODO rework
            Domain.Demon returnDemon = null;
            int          returnLevel = -1;

            Domain.Race returnRace = GlobalObjects.ImpossibleToFuseRace;

            if (f != null)
            {
                if (f.IdRace3 == GlobalObjects.ImpossibleToFuseRace.Id)
                {
                    returnDemon = _dbSession.Get <Domain.Demon>(0);
                }
                else
                {
                    returnRace = GlobalObjects.CurrentGame.Races.Where(x => x.Id == f.IdRace3).FirstOrDefault();

                    returnDemon = GlobalObjects.CurrentGame.demonsById.Values
                                  .Where(x =>
                                         x.Race.Id == f.IdRace3 &&
                                         x.Level > (d1.Level + d2.Level) / 2)
                                  .OrderBy(x => x.Level)
                                  .FirstOrDefault();

                    returnLevel = returnDemon != null ? returnDemon.Level :
                                  (int)Math.Ceiling((double)((int)(d1.Level + d2.Level) / 2));
                }
            }
            return(new Tuple <Domain.Demon, int, Domain.Race>(returnDemon, returnLevel, returnRace));
        }
Example #3
0
        public FusionDemon(int idGame, Demon d1, Demon d2, Demon d3)
        {
            IdGame = idGame;

            Demon1 = d1;
            Demon2 = d2;
            Demon3 = d3;
        }
Example #4
0
 private void ChangeCellColorsBasedOnParty(Domain.Demon demon, DataGridViewCell[] cells)
 {
     if (demon != null)
     {
         foreach (DataGridViewCell oneCell in cells)
         {
             oneCell.Style.BackColor =
                 demon.InPartyBoolean ? GlobalObjects.InPartyCell : GlobalObjects.DefaultCell;
         }
     }
 }
Example #5
0
        private Domain.Demon GetDemonFromDataGridViewRow(DataGridViewRow dgvr, bool canInsertRace)
        {
            Domain.Demon returnDemon = null;

            if (!String.IsNullOrEmpty(dgvr.Cells[(int)MyDataGridColumns.colId3].Value.ToString()))
            {
                // Selecting / updating demon
                MessageBox.Show("UPDATING fusions is not implemented. Please refresh window to reload data.");
                returnDemon = null;
            }
            else if (((FusionObject)dgvr.Cells[(int)MyDataGridColumns.colFusionObject].Value).FusionIsImpossible)
            {
                MessageBox.Show("UPDATING fusions from impossible to possible is not implemented. " +
                                "Please refresh window to reload data.");
                returnDemon = null;
            }
            else
            {
                // Inserting new demon
                object nameValue = dgvr.Cells[(int)MyDataGridColumns.colName3].Value;

                if (nameValue != null)
                {
                    returnDemon = GetDemonFromDataGridViewRowDemonColumn(dgvr);
                }
                else
                {
                    object idValue    = dgvr.Cells[(int)MyDataGridColumns.colId3].Value;
                    object levelValue = dgvr.Cells[(int)MyDataGridColumns.colLevel3].Value;
                    object raceValue  = dgvr.Cells[(int)MyDataGridColumns.colRace3].Value;

                    if (levelValue != null && nameValue != null && raceValue != null)
                    {
                        var actualDemonRace = _dbSession.CreateCriteria <Domain.Race>().List <Domain.Race>().
                                              Where(x => x.Name == raceValue.ToString()).FirstOrDefault();
                        if (actualDemonRace == null && canInsertRace)
                        {
                            actualDemonRace = GlobalObjects.InsertRaceMaybe(raceValue.ToString());
                        }

                        if (actualDemonRace != null)
                        {
                            returnDemon       = new Domain.Demon();
                            returnDemon.Level = Convert.ToInt32(levelValue.ToString());
                            returnDemon.Name  = (string)nameValue;
                            returnDemon.Race  = actualDemonRace;
                        }
                    }
                }
            }

            return(returnDemon);
        }
Example #6
0
        private object[] CreateRow(Domain.Demon d)
        {
            var returnObjectArray = new object[5];

            returnObjectArray[COLUMN_IN_PARTY] = (d.InParty > 0);
            returnObjectArray[COLUMN_ID]       = d.Id;
            returnObjectArray[COLUMN_LEVEL]    = d.Level;
            returnObjectArray[COLUMN_RACE]     = d.Race.Name;
            returnObjectArray[COLUMN_NAME]     = d.Name;

            return(returnObjectArray);
        }
Example #7
0
        public FusionObject(Domain.Demon d1, Domain.Demon d2, Domain.Demon d3)
        {
            _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);
            Demon3 = d3;

            _logger.CloseSection(location);
        }
        private object[] CreateRow(Domain.Demon d)
        {
            var returnObjectArray = new object[Enum.GetNames(typeof(MyDataGridColumns)).Length];

            returnObjectArray[(int)MyDataGridColumns.columnObject]       = d;
            returnObjectArray[(int)MyDataGridColumns.columnId]           = d.Id;
            returnObjectArray[(int)MyDataGridColumns.columnUseInFusions] = d.UseInFusionCalculatorBoolean;
            returnObjectArray[(int)MyDataGridColumns.columnInParty]      = d.InPartyBoolean;
            returnObjectArray[(int)MyDataGridColumns.columnLevel]        = d.Level;
            returnObjectArray[(int)MyDataGridColumns.columnRace]         = d.Race.Name;
            returnObjectArray[(int)MyDataGridColumns.columnName]         = d.Name;

            // TODO maybe:
            // do a select * on all races of the game
            // create a combobox with it
            // assign the combo box to object array 2
            // select the index of the correct race

            return(returnObjectArray);
        }
Example #9
0
        private Domain.Demon GetDemonFromDataGridViewRowDemonColumn(DataGridViewRow dgvr)
        {
            string location = this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name;

            _logger.OpenSection(location);

            Domain.Demon returnDemon = null;

            object nameValue = dgvr.Cells[(int)MyDataGridColumns.colName3].Value;

            _logger.Info("Cell contains name '" + nameValue + "'");

            if (nameValue != null)
            {
                string nameString       = nameValue.ToString();
                var    currentGameRaces = _dbSession.Get <Domain.Game>(GlobalObjects.CurrentGame.Id).Races;
                returnDemon = currentGameRaces.SelectMany(x => x.Demons).Where(y => y.Name == nameString).FirstOrDefault();
                _logger.Info(returnDemon == null ? "Could not find a demon." : "Found demon '" + returnDemon.ToString() + "'");
            }
            return(returnDemon);
        }
        private Domain.Demon GetDemonFromDataGridViewRow(DataGridViewRow dgvr, bool canInsertRace)
        {
            Domain.Demon returnDemon = null;

            if (dgvr.Cells[(int)MyDataGridColumns.columnObject].Value != null)
            {
                // Selecting / updating demon
                returnDemon = (Domain.Demon)dgvr.Cells[(int)MyDataGridColumns.columnObject].Value;
            }
            else
            {
                // Inserting new demon
                object idValue    = dgvr.Cells[(int)MyDataGridColumns.columnId].Value;
                object levelValue = dgvr.Cells[(int)MyDataGridColumns.columnLevel].Value;
                object nameValue  = dgvr.Cells[(int)MyDataGridColumns.columnName].Value;
                object raceValue  = dgvr.Cells[(int)MyDataGridColumns.columnRace].Value;

                if (levelValue != null && nameValue != null && raceValue != null)
                {
                    var actualDemonRace = _dbSession.CreateCriteria <Domain.Race>().List <Domain.Race>().
                                          Where(x => x.Name == raceValue.ToString()).FirstOrDefault();
                    if (actualDemonRace == null && canInsertRace)
                    {
                        actualDemonRace = GlobalObjects.InsertRaceMaybe(raceValue.ToString());
                    }

                    if (actualDemonRace != null)
                    {
                        returnDemon       = new Domain.Demon();
                        returnDemon.Level = Convert.ToInt32(levelValue.ToString());
                        returnDemon.Name  = (string)nameValue;
                        returnDemon.Race  = actualDemonRace;
                    }
                }
            }

            return(returnDemon);
        }
Example #11
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 #12
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);
            }
        }
Example #13
0
        private void dgvDemons_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.dgvDemons.Rows[e.RowIndex];

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

                using (var transaction = _dbSession.BeginTransaction())
                {
                    rowDemon = GetDemonFromDataGridViewRowDemonColumn(currentRow);
                    this.RemoveHandlers();
                    if (e.ColumnIndex == COLUMN_NAME)
                    {
                        this.dgvDemons.Rows.Remove(currentRow);
                    }
                    if (rowDemon != null)
                    {
                        if (e.ColumnIndex == COLUMN_IN_PARTY)
                        {
                            rowDemon.InParty = Math.Abs(1 - rowDemon.InParty);
                            //_dbSession.SaveOrUpdate(rowDemon);
                        }
                        else if (e.ColumnIndex == COLUMN_NAME)
                        {
                            if (rowDemon.UseInFusionCalculatorBoolean)
                            {
                                MessageBox.Show("Cannot have more than one " + rowDemon.Name + ".");
                            }
                            else
                            {
                                _logger.Info("Adding the following demon to fusion calculator: '" + rowDemon.ToString() + "'");
                                rowDemon.UseInFusionCalculator = 1;
                                _dbSession.SaveOrUpdate(rowDemon);
                                _logger.Info("Added to fusion calculator.");

                                _logger.Info("Updating interface...");
                                this.dgvDemons.Rows.Add(CreateRow(rowDemon));
                                _logger.Info("Updated.");
                            }
                        }
                        SetDataGridViewReadOnlyPropertyAndColors();
                    } // if (rowDemon != null)

                    this.AddHandlers();
                    transaction.Commit();
                } // using (var transaction = _dbSession.BeginTransaction())

                if (_addingRow)
                {
                    _addingRow = false;
                    GlobalObjects.MainForm.UpdateFusions();
                }
                _logger.CloseSection(location);
            }
        }
Example #14
0
        private void dgvDemons_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (_cellDemonChanged)
            {
                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.dgvDemons.Rows[e.RowIndex];

                if (!_addingDemon)
                {
                    var id = currentRow.Cells[DGV_DEMON_COL_ID].Value.ToString();
                    id = id + id;
                }
                else
                {
                    var raceName  = this.dgvDemons.Rows[this.dgvDemons.Rows.Count - 2].Cells[DGV_DEMON_COL_RACE].Value.ToString();
                    var demonRace = GlobalObjects.CurrentGame.Races.Where(x => x.Name == raceName).FirstOrDefault();

                    if (demonRace == null)
                    {
                        MessageBox.Show("Race '" + raceName + "' does not exist.");
                        RemoveHandlers();
                        this.dgvDemons.Rows.RemoveAt(this.dgvDemons.Rows.Count - 2);
                        AddHandlers();
                        return;
                    }

                    var demonName   = this.dgvDemons.Rows[this.dgvDemons.Rows.Count - 2].Cells[DGV_DEMON_COL_NAME].Value.ToString();
                    var demonExists = GlobalObjects.CurrentGame.Races.SelectMany(x => x.Demons)
                                      .Where(x => x.Name == demonName).FirstOrDefault() != null;

                    if (demonExists)
                    {
                        MessageBox.Show("This demon already exists.");
                        RemoveHandlers();
                        this.dgvRaces.Rows.RemoveAt(this.dgvRaces.Rows.Count - 2);
                        AddHandlers();
                        return;
                    }

                    int demonLevel = -1;
                    if (!Int32.TryParse(
                            this.dgvDemons.Rows[this.dgvDemons.Rows.Count - 2].Cells[DGV_DEMON_COL_LEVEL].Value.ToString(), out demonLevel))
                    {
                        MessageBox.Show("Invalid level.");
                        RemoveHandlers();
                        this.dgvRaces.Rows.RemoveAt(this.dgvRaces.Rows.Count - 2);
                        AddHandlers();
                        return;
                    }

                    try
                    {
                        using (var transaction = _dbSession.BeginTransaction())
                        {
                            if (!_changesWereDone)
                            {
                                foreach (DataGridViewRow dr in dgvDemons.Rows)
                                {
                                    dr.Cells[DGV_DEMON_COL_INPARTY].Style  =
                                        dr.Cells[DGV_DEMON_COL_FUSE].Style =
                                            GlobalObjects.GetDefaultDgvcStyle(fontSize: DGV_DEMONS_FONT_SIZE, enabled: false);

                                    dr.Cells[DGV_DEMON_COL_LEVEL].Style        =
                                        dr.Cells[DGV_DEMON_COL_RACE].Style     =
                                            dr.Cells[DGV_DEMON_COL_NAME].Style =
                                                GlobalObjects.GetDefaultDgvcStyle(fontSize: DGV_DEMONS_FONT_SIZE, enabled: true);

                                    dr.Cells[DGV_DEMON_COL_INPARTY].ReadOnly  =
                                        dr.Cells[DGV_DEMON_COL_FUSE].ReadOnly = false;
                                }
                            }
                            _changesWereDone = true;

                            var allDemonsSoFar = GlobalObjects.CurrentGame.Races.SelectMany(x => x.Demons).ToList();
                            var fgg            = allDemonsSoFar.Count();

                            var newDemon = new Domain.Demon(demonLevel, demonName, demonRace);
                            _dbSession.Save(newDemon);
                            demonRace.Demons.Add(newDemon);

                            foreach (Domain.Demon oneDemon in allDemonsSoFar)
                            {
                                var fusionRaceObject = GlobalObjects.CurrentGame.FusionRaces.Where
                                                           (x =>
                                                           (x.IdRace1 == oneDemon.Race.Id && x.IdRace2 == newDemon.Race.Id) ||
                                                           (x.IdRace2 == oneDemon.Race.Id && x.IdRace1 == newDemon.Race.Id))
                                                       .FirstOrDefault();

                                var resultDemon = fusionRaceObject == null ? null :
                                                  FindDemonFromFusion(oneDemon, newDemon, fusionRaceObject).Item1;

                                var newFusion = newDemon.Level > oneDemon.Level ?
                                                new Domain.FusionDemon(GlobalObjects.CurrentGame.Id, oneDemon, newDemon, resultDemon) :
                                                new Domain.FusionDemon(GlobalObjects.CurrentGame.Id, newDemon, oneDemon, resultDemon);
                                GlobalObjects.CurrentGame.FusionDemons.Add(newFusion);
                            }

                            // Update fusions where the new demon might be the result of a previously unknown fusion
                            // e.g. fusion says Gouki 7+ unknown, then add Gouki 12 Kushi-mitama
                            var demonFusionsToUpdate = GlobalObjects.CurrentGame.FusionDemons.Where
                                                           (x => /*x.Demon3 == null && fgg: doit considérer le cas où quelqu'un insère
                                                                  * le level 23 Gouki avant le level 12 Gouki, et là les fusions
                                                                  * 5+6 pointent vers le level 23*/x.Race3 != null && x.Race3.Id == newDemon.Race.Id);
                            foreach (Domain.FusionDemon fusionToUpdate in demonFusionsToUpdate)
                            {
                                if (newDemon.Level >= fusionToUpdate.Level3)
                                {
                                    var fusionRaceObject = GlobalObjects.CurrentGame.FusionRaces.Where
                                                               (x =>
                                                               (x.IdRace1 == fusionToUpdate.Demon1.Race.Id && x.IdRace2 == fusionToUpdate.Demon2.Race.Id) ||
                                                               (x.IdRace2 == fusionToUpdate.Demon1.Race.Id && x.IdRace1 == fusionToUpdate.Demon2.Race.Id))
                                                           .FirstOrDefault();
                                    var demonFusionTuple = FindDemonFromFusion(fusionToUpdate.Demon1, fusionToUpdate.Demon2, fusionRaceObject);

                                    if (demonFusionTuple.Item1 == null)
                                    {
                                        fusionToUpdate.Demon3 = newDemon;
                                    }
                                    else if (fusionToUpdate.Demon3 != null &&
                                             demonFusionTuple.Item1 != null &&
                                             demonFusionTuple.Item1.Id != fusionToUpdate.Demon3.Id)
                                    {
                                        fusionToUpdate.Demon3 = newDemon;
                                    }
                                }
                            }

                            RemoveHandlers();
                            this.dgvDemons.Rows[this.dgvDemons.Rows.Count - 2].Cells[DGV_DEMON_COL_ID].Value = newDemon.Id;
                            AddHandlers();
                            transaction.Commit();
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.Error(ex);
                        throw;
                    }

                    _addingDemon = false;
                }
            }

            if (_demonFusionStatusChanged)
            {
                var currentGame = GlobalObjects.CurrentGame;

                var currentRow = dgvDemons.Rows[dgvDemons.SelectedCells[0].RowIndex];

                var selectedDemonId = Convert.ToInt32(currentRow.Cells[DGV_DEMON_COL_ID].Value);
                var useInFusions    = (bool)(currentRow.Cells[DGV_DEMON_COL_FUSE].Value);
                var fggcell         = currentRow.Cells[DGV_DEMON_COL_INPARTY];

                var inParty = (bool?)(currentRow.Cells[DGV_DEMON_COL_INPARTY].Value) ?? false;

                var selectedDemon = currentGame.demonsById[selectedDemonId];

                try
                {
                    using (var transaction = _dbSession.BeginTransaction())
                    {
                        selectedDemon.IsFused   = useInFusions;
                        selectedDemon.IsInParty = inParty;
                        _dbSession.Save(selectedDemon);
                        transaction.Commit();
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(ex);
                    throw;
                }

                var affectedFusions = currentGame.FusionDemons.Where(
                    x => x.Demon1.Id == selectedDemonId || x.Demon2.Id == selectedDemonId ||
                    (x.Demon3 != null && x.Demon3.Id == selectedDemonId)).ToList();

                foreach (Domain.FusionDemon oneFusion in affectedFusions)
                {
                    var currentFusionRow = this.dgvFusions.Rows[DataGridFusionRowIndexesPerFusionId[oneFusion.Id]];
                    currentFusionRow.Visible = oneFusion.Demon1.IsFused && oneFusion.Demon2.IsFused;
                    UpdateFusionRowDisplay(currentFusionRow, oneFusion.Demon1, oneFusion.Demon2, oneFusion.Demon3);
                }
                _demonFusionStatusChanged = false;
            }
        }
        private void dgvDemons_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.dgvDemons.Rows[e.RowIndex];

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

                using (var transaction = _dbSession.BeginTransaction())
                {
                    rowDemon = GetDemonFromDataGridViewRow(currentRow, true);
                    if (rowDemon != null)
                    {
                        databaseDemon = rowDemon;
                        bool proceedWithDatabaseOperation = 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.");
                                    proceedWithDatabaseOperation = false;
                                }
                            }
                            else
                            {
                                // if null just revert to the rowDemon again, we'll add that.
                                databaseDemon = rowDemon;
                            }
                        }

                        if (proceedWithDatabaseOperation)
                        {
                            _logger.Info("Row demon is " + rowDemon.ToString() + ". " +
                                         (databaseDemon.Id == null ? "Inserting..." : "Updating..."));

                            if (databaseDemon.Id == null)
                            {
                                _dbSession.Save(databaseDemon); // insert
                                insertedNewDemon = true;
                            }
                            else
                            {
                                if (e.ColumnIndex == (int)MyDataGridColumns.columnInParty)
                                {
                                    databaseDemon.InParty =
                                        (bool)this.dgvDemons.Rows[e.RowIndex].Cells[(int)MyDataGridColumns.columnInParty].Value ? 1 : 0;
                                }
                                else if (e.ColumnIndex == (int)MyDataGridColumns.columnUseInFusions)
                                {
                                    databaseDemon.UseInFusionCalculator =
                                        (bool)this.dgvDemons.Rows[e.RowIndex].Cells[(int)MyDataGridColumns.columnUseInFusions].Value ? 1 : 0;
                                }
                                _dbSession.SaveOrUpdate(databaseDemon); // update
                            }
                            transaction.Commit();
                            _logger.Info("Demon saved.");
                        }
                    }
                } // 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.columnObject].Value = databaseDemon;
                    currentRow.Cells[(int)MyDataGridColumns.columnId].Value     = databaseDemon.Id;
                    AddHandlers();
                }

                _logger.CloseSection(location);
            }
        }