Example #1
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 #2
0
        private void dgvRaces_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (_cellRaceChanged)
            {
                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.dgvRaces.Rows[e.RowIndex];

                if (_addingRace)
                {
                    var raceName = this.dgvRaces.Rows[this.dgvRaces.Rows.Count - 2].Cells[DGV_RACE_COL_NAME].Value.ToString();

                    var raceExists = GlobalObjects.CurrentGame.Races.Where(x => x.Name == raceName).FirstOrDefault() != null;

                    if (raceExists)
                    {
                        MessageBox.Show("This race already exists.");
                        RemoveHandlers();
                        this.dgvRaces.Rows.RemoveAt(this.dgvRaces.Rows.Count - 2);
                        AddHandlers();
                    }
                    else
                    {
                        try
                        {
                            using (var transaction = _dbSession.BeginTransaction())
                            {
                                _changesWereDone = true;
                                var allRacesSoFar = GlobalObjects.CurrentGame.Races
                                                    .Where(x => x.Id != IMPOSSIBLE_TO_FUSE_RACE).ToList();

                                var newRace = new Domain.Race(raceName, GlobalObjects.CurrentGame);
                                _dbSession.Save(newRace);
                                GlobalObjects.CurrentGame.Races.Add(newRace);

                                foreach (Domain.Race oneRace in allRacesSoFar)
                                {
                                    var newFusion = new Domain.FusionRace(
                                        GlobalObjects.CurrentGame.Id, oneRace.Id, newRace.Id, null);
                                    GlobalObjects.CurrentGame.FusionRaces.Add(newFusion);
                                }

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

                    _addingRace = false;
                }
            }
        }