Ejemplo n.º 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));
        }
Ejemplo n.º 2
0
        public static Domain.Race InsertRaceMaybe(string raceName)
        {
            string location = new StackFrame().GetMethod().DeclaringType.ToString();

            _logger.OpenSection(location);

            string message = "Inserting new family '" + raceName + "'";

            _logger.Info("Asking user: '******'");

            Domain.Race returnRace = null;

            DialogResult dr = MessageBox.Show(message, "New family",
                                              MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

            if (dr == DialogResult.Cancel)
            {
                _logger.Info("User cancelled; race will not be inserted.");
            }
            else
            {
                var insertedRace = new Domain.Race()
                {
                    Name = raceName,
                    Game = GlobalObjects.CurrentGame
                };
                _logger.Info("Inserting race...");
                _dbSession.Save(insertedRace);
                _logger.Info("Inserted race: " + insertedRace.ToString());
                returnRace = insertedRace;
            }

            _logger.CloseSection(location);
            return(returnRace);
        }
Ejemplo n.º 3
0
        private void AddOneColumn(Domain.Race oneRace)
        {
            string raceName  = (oneRace == null ? "" : oneRace.Name);
            var    oneColumn = new DataGridViewTextBoxColumn()
            {
                HeaderText       = raceName,
                Name             = "col" + raceName,
                Width            = 60,
                ReadOnly         = (oneRace == null),
                DefaultCellStyle = GlobalObjects.GetDefaultDgvcStyle(FONT_SIZE)
            };

            this.dgvFusions.Columns.Add(oneColumn);
        }
Ejemplo n.º 4
0
        public void LoadData()
        {
            string location = this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name;

            _logger.OpenSection(location);

            RemoveHandlers();

            var game = GlobalObjects.CurrentGame;

            this.dgvFusions.Enabled = (game != null);
            if (game == null)
            {
                _logger.Info("No data to load; no game is chosen.");
                this.dgvFusions.Rows.Clear();
            }
            else
            {
                _logger.Info("Game '" + game.Name + "' chosen; will load list of fusions.");

                var currentGameRaces   = game.Races.ToList();
                var currentGameRaceIds = currentGameRaces.Select(x => x.Id).ToList();
                var allFusions         =
                    _dbSession.CreateCriteria <Domain.Fusion>().List <Domain.Fusion>()
                    .Where(x => currentGameRaceIds.Contains(x.IdRace1))
                    .OrderBy(y => y.IdRace1)
                    .ThenBy(z => z.IdRace2).ToList();

                AddOneColumn(null);
                foreach (var oneRace in currentGameRaces)
                {
                    AddOneColumn(oneRace);
                }

                // first row
                object[] oneRow = new object[currentGameRaces.Count + 1];
                oneRow[0] = " ";
                for (int i = 0; i < currentGameRaces.Count; i++)
                {
                    oneRow[i + 1] = currentGameRaces[i].Name;
                }

                // race tag on every cell of the first row except the first one
                this.dgvFusions.Rows.Add(oneRow);
                for (int i = 0; i < currentGameRaces.Count; i++)
                {
                    this.dgvFusions.Rows[0].Cells[i + 1].Tag = currentGameRaces[i];
                }

                // following rows
                DataGridViewRow oneDgvr;
                foreach (var oneRace in currentGameRaces)
                {
                    oneDgvr = new DataGridViewRow();
                    oneDgvr.Cells.Add(new DataGridViewTextBoxCell()
                    {
                        Value = oneRace.Name,
                        Tag   = oneRace
                    });
                    foreach (var anotherRace in currentGameRaces)
                    {
                        int?idRaceResult =
                            allFusions.Where(x =>
                                             x.IdRace1 == oneRace.Id &&
                                             x.IdRace2 == anotherRace.Id)
                            .Select(y => y.IdRace3).FirstOrDefault();
                        if (idRaceResult == null)
                        {
                            idRaceResult =
                                allFusions.Where(x =>
                                                 x.IdRace2 == oneRace.Id &&
                                                 x.IdRace1 == anotherRace.Id)
                                .Select(y => y.IdRace3).FirstOrDefault();
                        }
                        Domain.Race raceResult =
                            (idRaceResult == null ? null :
                             _dbSession.Get <Domain.Race>(idRaceResult.GetValueOrDefault()));

                        oneDgvr.Cells.Add(new DataGridViewTextBoxCell()
                        {
                            Value = (raceResult == null ? null : raceResult.Name),
                            Tag   = raceResult
                        });
                    }
                    this.dgvFusions.Rows.Add(oneDgvr);
                }

                foreach (DataGridViewCell oneCell in this.dgvFusions.Rows[0].Cells)
                {
                    oneCell.ReadOnly = true;
                    oneCell.Style    = GlobalObjects.GetDefaultDgvcStyle(FONT_SIZE, false);
                }

                foreach (DataGridViewRow aRow in this.dgvFusions.Rows)
                {
                    aRow.Cells[0].ReadOnly = true;
                    aRow.Cells[0].Style    = GlobalObjects.GetDefaultDgvcStyle(FONT_SIZE, false);
                }

                AddHandlers();
                _logger.Info("Adding complete.");

                SetFormats();
            }
            _logger.CloseSection(location);
        }
Ejemplo n.º 5
0
        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);
            }
        }
Ejemplo n.º 6
0
 public Demon(int level, string name, Race race)
 {
     Level = level;
     Name  = name;
     Race  = race;
 }
Ejemplo n.º 7
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;
                }
            }
        }