public void UpdateEmpire(DBCurrentEmpireRanking dbRank, CurrentEmpireRanking currentRank)
        {
            if (dbRank.Score.HasValue) //sojusze tylko dla ogólnej punktacji są
            {
                dbRank.AlianceName = currentRank.AlianceName;
            }

            dbRank.UpdateDT = DateTime.Now;

            if (currentRank.Rank.HasValue)
            {
                if (dbRank.Id.HasValue)
                {
                    dbRank.RankLastChange = dbRank.Rank - currentRank.Rank;
                }

                dbRank.Rank = currentRank.Rank;
            }

            if (currentRank.Score.HasValue)
            {
                if (dbRank.Id.HasValue)
                {
                    dbRank.ScoreLastChange = currentRank.Score - dbRank.Score;
                }

                dbRank.Score = currentRank.Score;
            }

            if (currentRank.CitiesNo.HasValue)
            {
                if (dbRank.Id.HasValue)
                {
                    dbRank.CitiesNoLastChange = currentRank.CitiesNo - dbRank.CitiesNo;
                }

                dbRank.CitiesNo = currentRank.CitiesNo;
            }

            if (currentRank.UnitsKills.HasValue)
            {
                dbRank.UnitsKills = currentRank.UnitsKills;
            }

            if (currentRank.UnitsKillsRank.HasValue)
            {
                dbRank.UnitsKillsRank = currentRank.UnitsKillsRank;
            }

            if (currentRank.Caverns.HasValue)
            {
                dbRank.Caverns = currentRank.Caverns;
            }

            if (currentRank.CavernsRank.HasValue)
            {
                dbRank.CavernsRank = currentRank.CavernsRank;
            }

            if (currentRank.DefReputation.HasValue)
            {
                dbRank.DefReputation = currentRank.DefReputation;
            }

            if (currentRank.DefReputationRank.HasValue)
            {
                dbRank.DefReputationRank = currentRank.DefReputationRank;
            }

            if (currentRank.OffReputation.HasValue)
            {
                dbRank.OffReputation = currentRank.OffReputation;
            }

            if (currentRank.OffReputationRank.HasValue)
            {
                dbRank.OffReputationRank = currentRank.OffReputationRank;
            }

            if (currentRank.Score.HasValue)
            {
                //historia głównego rankingu
                var histRank = new DBRankingsEmpireScore();
                histRank.AlianceName = dbRank.AlianceName;
                histRank.CitiesNo    = dbRank.CitiesNo.Value;
                histRank.Continent   = dbRank.Continent;
                histRank.CreateDT    = dbRank.UpdateDT;
                histRank.PlayerName  = dbRank.PlayerName;
                histRank.Rank        = dbRank.Rank.Value;
                histRank.Score       = dbRank.Score.Value;
                Session.SaveOrUpdate(histRank);

                /*
                 * var last3Rows = Session.QueryOver<DBRankingsEmpireScore>()
                 *  .Where(x => x.PlayerName == dbRank.PlayerName && x.Continent == dbRank.Continent)
                 *  .OrderBy(x => x.CreateDT).Desc
                 *  .Take(3)
                 *  .List();
                 */

                //odczytuję historię ostanich 3 pomiarów do obliczenia średniej z przyrostów
                var last3Rows = m_ScoreHistCache
                                .Where(x => x.PlayerName == dbRank.PlayerName && x.Continent == dbRank.Continent)
                                .OrderByDescending(x => x.CreateDT)
                                .ToList();

                if (last3Rows.Count > 0)
                {
                    dbRank.ScoreDiffAvg = (last3Rows.Max(x => x.Score) - last3Rows.Min(x => x.Score)) / last3Rows.Count;
                }
                else
                {
                    dbRank.ScoreDiffAvg = 0;
                }
            }

            if (currentRank.UnitsKills.HasValue)
            {
                //historia ubitych jednostek
                var histKills = new DBUnitsKillsHistory();
                histKills.PlayerName = dbRank.PlayerName;
                histKills.Rank       = dbRank.UnitsKillsRank.Value;
                histKills.Score      = dbRank.UnitsKills.Value;
                histKills.CreateDT   = dbRank.UpdateDT;
                Session.SaveOrUpdate(histKills);

                //odczytuję historię ostanich 3 pomiarów do obliczenia średniego przyrostu
                var last3Rows = m_KilsHistCache
                                .Where(x => x.PlayerName == dbRank.PlayerName)
                                .OrderByDescending(x => x.CreateDT)
                                .ToList();

                if (last3Rows.Count > 0)
                {
                    dbRank.UnitsKillsDiffAvg = (last3Rows.Max(x => x.Score) - last3Rows.Min(x => x.Score)) / last3Rows.Count;
                }
                else
                {
                    dbRank.UnitsKillsDiffAvg = 0;
                }
            }

            if (currentRank.DefReputation.HasValue)
            {
                //historia chwały w obronie
                var hist = new DBDefReputationHistory();
                hist.PlayerName = dbRank.PlayerName;
                hist.Rank       = dbRank.DefReputationRank.Value;
                hist.Score      = dbRank.DefReputation.Value;
                hist.CreateDT   = dbRank.UpdateDT;
                Session.SaveOrUpdate(hist);

                //odczytuję historię ostanich 3 pomiarów do obliczenia średniego przyrostu
                var last3Rows = m_DefHistCache
                                .Where(x => x.PlayerName == dbRank.PlayerName)
                                .OrderByDescending(x => x.CreateDT)
                                .ToList();

                if (last3Rows.Count > 0)
                {
                    dbRank.DefReputationDiffAvg = (last3Rows.Max(x => x.Score) - last3Rows.Min(x => x.Score)) / last3Rows.Count;
                }
                else
                {
                    dbRank.DefReputationDiffAvg = 0;
                }
            }

            if (currentRank.OffReputation.HasValue)
            {
                //historia chwały w ataku
                var hist = new DBOffReputationHistory();
                hist.PlayerName = dbRank.PlayerName;
                hist.Rank       = dbRank.OffReputationRank.Value;
                hist.Score      = dbRank.OffReputation.Value;
                hist.CreateDT   = dbRank.UpdateDT;
                Session.SaveOrUpdate(hist);

                //odczytuję historię ostanich 3 pomiarów do obliczenia średniego przyrostu
                var last3Rows = m_OffHistCache
                                .Where(x => x.PlayerName == dbRank.PlayerName)
                                .OrderByDescending(x => x.CreateDT)
                                .ToList();

                if (last3Rows.Count > 0)
                {
                    dbRank.OffReputationDiffAvg = (last3Rows.Max(x => x.Score) - last3Rows.Min(x => x.Score)) / last3Rows.Count;
                }
                else
                {
                    dbRank.OffReputationDiffAvg = 0;
                }
            }

            if (currentRank.Caverns.HasValue)
            {
                //historia lochów
                var histCav = new DBCavernsHistory();
                histCav.PlayerName = dbRank.PlayerName;
                histCav.Rank       = dbRank.CavernsRank.Value;
                histCav.Score      = dbRank.Caverns.Value;
                histCav.CreateDT   = dbRank.UpdateDT;
                Session.SaveOrUpdate(histCav);
            }

            //tutaj bo wyżej jeszcze średnią wyliczam...
            Session.SaveOrUpdate(dbRank);
        }
        public override void Execute()
        {
            if (CurrentRankings.Count == 0)
            {
                return;
            }

            LoadEmpires2Cache(); //to zawsze

            //pozostała część... w zależności od danych... zakładam że są jednego 'typu' czyli score, off, deff.. itd...
            var tmpEmpire = CurrentRankings.First();

            if (tmpEmpire.UnitsKills.HasValue)
            {
                LoadKillsCache();
            }

            if (tmpEmpire.OffReputation.HasValue)
            {
                LoadOffCache();
            }

            if (tmpEmpire.DefReputation.HasValue)
            {
                LoadDefCache();
            }

            m_TotalSteps  = CurrentRankings.Count;
            m_CurrentStep = 0;
            Stopwatch watch = new Stopwatch();

            watch.Restart();

            foreach (var emp in CurrentRankings)
            {
                //m_Log.InfoFormat("Empire: {0}", emp.PlayerName);

                var dbRank = m_EmpiresCache
                             .Where(x => x.Continent == emp.Continent && x.PlayerName == emp.PlayerName)
                             .SingleOrDefault();

                //dodajemy
                if (dbRank == null)
                {
                    //m_Log.InfoFormat("New empire: {0}", emp.PlayerName);
                    dbRank            = new DBCurrentEmpireRanking();
                    dbRank.Continent  = emp.Continent;
                    dbRank.PlayerName = emp.PlayerName;
                }

                //m_Log.Info("Updatin db...");
                UpdateEmpire(dbRank, emp);
                //m_Log.Info("Db updated.");
                m_CurrentStep++;

                if (watch.ElapsedMilliseconds > 500)
                {
                    m_Log.Info("Elapsed: " + watch.ElapsedMilliseconds.ToString());
                    DoProgress(string.Format("~Db updating.... {0}/{1}", m_CurrentStep, m_TotalSteps));
                    watch.Restart();
                }
            }
        }
Ejemplo n.º 3
0
        public void T006_ImportCurrentEmpireRankingCSV()
        {
            using (var s = Db.SessionFactory.OpenSession())
            {
                var    file = System.IO.File.OpenText(@"C:\projekty\ProITSoft\CotGLibs\CotGLibs\CotGBrowser\data\CurrentEmpireRanking.csv");
                string line;

                var t = s.BeginTransaction();

                while ((line = file.ReadLine()) != null)
                {
                    var fields = line.Split(';');

                    if (fields[0] == "Id")
                    {
                        continue; //header
                    }
                    var row     = new DBCurrentEmpireRanking();
                    int fieldIx = 0;
                    row.PlayerName  = fields[++fieldIx];
                    row.Rank        = int.Parse(fields[++fieldIx]);
                    row.Score       = int.Parse(fields[++fieldIx]);
                    row.AlianceName = fields[++fieldIx];
                    row.CitiesNo    = int.Parse(fields[++fieldIx]);
                    row.UpdateDT    = DateTime.Parse(fields[++fieldIx]);
                    row.Continent   = int.Parse(fields[++fieldIx]);

                    if (!string.IsNullOrWhiteSpace(fields[++fieldIx]))
                    {
                        row.UnitsKills = long.Parse(fields[fieldIx]);
                    }

                    if (!string.IsNullOrWhiteSpace(fields[++fieldIx]))
                    {
                        row.UnitsKillsRank = int.Parse(fields[fieldIx]);
                    }

                    if (!string.IsNullOrWhiteSpace(fields[++fieldIx]))
                    {
                        row.Caverns = long.Parse(fields[fieldIx]);
                    }

                    if (!string.IsNullOrWhiteSpace(fields[++fieldIx]))
                    {
                        row.CavernsRank = int.Parse(fields[fieldIx]);
                    }

                    if (!string.IsNullOrWhiteSpace(fields[++fieldIx]))
                    {
                        row.UnitsKillsDiffAvg = double.Parse(fields[fieldIx]);
                    }

                    if (!string.IsNullOrWhiteSpace(fields[++fieldIx]))
                    {
                        row.ScoreDiffAvg = double.Parse(fields[fieldIx]);
                    }

                    if (!string.IsNullOrWhiteSpace(fields[++fieldIx]))
                    {
                        row.DefReputation = long.Parse(fields[fieldIx]);
                    }

                    if (!string.IsNullOrWhiteSpace(fields[++fieldIx]))
                    {
                        row.DefReputationRank = int.Parse(fields[fieldIx]);
                    }

                    if (!string.IsNullOrWhiteSpace(fields[++fieldIx]))
                    {
                        row.OffReputation = long.Parse(fields[fieldIx]);
                    }

                    if (!string.IsNullOrWhiteSpace(fields[++fieldIx]))
                    {
                        row.OffReputationRank = int.Parse(fields[fieldIx]);
                    }

                    if (!string.IsNullOrWhiteSpace(fields[++fieldIx]))
                    {
                        row.DefReputationDiffAvg = double.Parse(fields[fieldIx]);
                    }

                    if (!string.IsNullOrWhiteSpace(fields[++fieldIx]))
                    {
                        row.OffReputationDiffAvg = double.Parse(fields[fieldIx]);
                    }

                    if (!string.IsNullOrWhiteSpace(fields[++fieldIx]))
                    {
                        row.RankLastChange = int.Parse(fields[fieldIx]);
                    }

                    if (!string.IsNullOrWhiteSpace(fields[++fieldIx]))
                    {
                        row.ScoreLastChange = int.Parse(fields[fieldIx]);
                    }

                    if (!string.IsNullOrWhiteSpace(fields[++fieldIx]))
                    {
                        row.CitiesNoLastChange = int.Parse(fields[fieldIx]);
                    }

                    if (!string.IsNullOrWhiteSpace(fields[++fieldIx]))
                    {
                        row.DefReputationRankLastChange = int.Parse(fields[fieldIx]);
                    }

                    s.SaveOrUpdate(row);
                }

                t.Commit();
            }
        }