private void CreateRecentBattle(MemoryTables mt) { //try //{ Dossier fileB = new Dossier(GetCurrentPlayerFile(), _playerName, _messages); WOTStats stats = fileB.GetStats(); double avgTier = stats.AverageTier; double vics = stats.Victory_Percentage; double defPoints = stats.AverageDefencePoints; List <string> cur = (from x in mt.GetTable("File_Total").AsEnumerable() select x["foLastBattleTime"].ToString()).ToList(); List <string> saved; DataTable currentRecord; DataTable Kills; using (IDBHelpers db = new DBHelpers(WOTHelper.GetDBPath(_playerName), false)) { saved = (from x in db.GetDataTable("select foLastBattleTime from File_Total").AsEnumerable() select x["foLastBattleTime"].ToString()).ToList(); currentRecord = db.GetDataTable(@"select case when T2.bpBattleMode = 7 then 7 else 15 end as bpBattleMode, T1.cmID, T1.cmCountryID, T1.cmTankID, T1.cmUpdated, (T2.bpBattleCount) bpBattleCount, (t2.bpSpotted) bpSpotted, (t2.bpHits) bpHits, (t2.bpMaxFrags) bpMaxFrags, (t2.bpMaxXP) bpMaxXP, (t2.bpWins) bpWins, (t2.bpCapturePoints) bpCapturePoints, (t2.bpLosses) bpLosses, (t2.bpSurvivedBattles) bpSurvivedBattles, min(T4.foBattleLifeTime) foBattleLifeTime, (t2.bpDefencePoints) bpDefencePoints, min(t4.foLastBattleTime) foLastBattleTime, (t2.bpDamageReceived) bpDamageReceived, (t2.bpShots) bpShots, (t2.bpWinAndSurvive) bpWinAndSurvive, (t2.bpFrags8P) bpFrags8P, (t2.bpXP) bpXP, (t2.bpDamageDealt) bpDamageDealt, (t2.bpDamageAssistedRadio) bpDamageAssistedRadio, (t2.bpDamageAssistedTracks) bpDamageAssistedTracks, (t2.bpMileage) bpMileage, (t2.bpRatingEffWeight)/(t2.bpBattleCount) bpRatingEff, (t2.bpRatingBRWeight)/(t2.bpBattleCount) bpRatingBR, (t2.bpRatingWN7Weight)/(t2.bpBattleCount) bpRatingWN7, (t2.bpRatingWN8Weight)/(t2.bpBattleCount) bpRatingWN8, (t2.bpFrags) bpFrags, t4.foTreesCut, t3.faMarkOfMastery from File_TankDetails T1 inner join File_Battles T2 on T1.cmID = T2.bpParentID inner join File_Achievements T3 on T1.cmID = T3.faParentID inner join File_Total T4 on T1.cmID = T4.foParentID where cmFileID = " + GetCurrentPlayerFileKey() + " group by T2.bpBattleMode, T1.cmID, T1.cmCountryID, T1.cmTankID, T1.cmUpdated, t4.foTreesCut, t3.faMarkOfMastery"); Kills = db.GetDataTable(String.Format(@"select fgParentID, fgCountryID, fgTankID, sum(fgValue) kills from File_TankDetails inner join File_FragList on cmID = fgParentID where cmFileID = {0} group by fgParentID, fgCountryID, fgTankID", GetCurrentPlayerFileKey())); } List <string> diff = cur.Except(saved).ToList(); RecentBattles lb = new RecentBattles(GetPlayerName, _messages); var battleCheck = from x in mt.GetTable("File_TankDetails").AsEnumerable() join y in mt.GetTable("File_Battles").AsEnumerable() on x.GetSafeInt("cmID") equals y.GetSafeInt("bpParentID") join z in mt.GetTable("File_Total").AsEnumerable() on x.GetSafeInt("cmID") equals z.GetSafeInt("foParentID") join d in diff on z["foLastBattleTime"].ToString() equals d group y by new { ID = x.GetSafeInt("cmID"), countryID = x.GetSafeInt("cmCountryID"), tankID = x.GetSafeInt("cmTankID"), battleTime = z["foLastBattleTime"].ToString(), BattleMode = y["bpBattleMode"] } into tank select new { ID = tank.Key.ID, CountryID = tank.Key.countryID, TankID = tank.Key.tankID, BattleTime = tank.Key.battleTime, BattleCount = tank.Sum(y => y.GetSafeInt("bpBattleCount")), Hits = tank.Sum(y => y.GetSafeInt("bpHits")), Shots = tank.Sum(y => y.GetSafeInt("bpShots")), Victory = tank.Sum(y => y.GetSafeInt("bpWins")), Losses = tank.Sum(y => y.GetSafeInt("bpLosses")), Survived = tank.Sum(y => y.GetSafeInt("bpSurvivedBattles")), DefencePoints = tank.Sum(y => y.GetSafeInt("bpDefencePoints")), CapturePoints = tank.Sum(y => y.GetSafeInt("bpCapturePoints")), Spotted = tank.Sum(y => y.GetSafeInt("bpSpotted")), DamageDealt = tank.Sum(y => y.GetSafeInt("bpDamageDealt")), DamageAssistedRadio = tank.Sum(y => y.GetSafeInt("bpDamageAssistedRadio")), DamageAssistedTracks = tank.Sum(y => y.GetSafeInt("bpDamageAssistedTracks")), DamageReceived = tank.Sum(y => y.GetSafeInt("bpDamageReceived")), Kills = tank.Sum(y => y.GetSafeInt("bpFrags")), Mileage = tank.Sum(y => y.GetSafeInt("bpMileage")), RatingEff = tank.Sum(y => y.GetSafeInt("bpRatingEff")), RatingBR = tank.Sum(y => y.GetSafeInt("bpRatingBR")), RatingWN7 = tank.Sum(y => y.GetSafeInt("bpRatingWN7")), RatingWN8 = tank.Sum(y => y.GetSafeInt("bpRatingWN8")), XPReceived = tank.Sum(y => y.GetSafeInt("bpXP")), BattleMode = int.Parse(tank.Key.BattleMode.ToString()) }; if (currentRecord.Rows.Count > 0) { foreach (var item in battleCheck) { //WOTHelper.AddToLog("BM 1: " + item.BattleMode); DataRow dr = currentRecord.Select(String.Format("cmCountryID = {0} and cmTankID = {1} and bpBattleMode = {2}", item.CountryID, item.TankID, item.BattleMode)).DefaultIfEmpty(null).FirstOrDefault(); if (dr != null) { if (item.BattleCount - dr.GetSafeInt("bpBattleCount") > 0) { int i = 0; List <string> newKills = new List <string>(); foreach (DataRow killRow in mt.GetTable("File_FragList").Select("fgParentID = " + item.ID)) { DataRow dr_oldKills = Kills.Select(String.Format(@"fgParentID = {0} and fgCountryID = {1} and fgTankID = {2}", dr.GetSafeInt("cmID"), killRow.GetSafeInt("fgCountryID"), killRow.GetSafeInt("fgTankID"))).FirstOrDefault(); if (dr_oldKills != null) { if (killRow.GetSafeInt("fgValue") > dr_oldKills.GetSafeInt("kills")) { newKills.Add(String.Format("{0}:{1}_{2}", i, killRow.GetSafeInt("fgCountryID"), killRow.GetSafeInt("fgTankID"))); i++; } } else { newKills.Add(String.Format("{0}:{1}_{2}", i, killRow.GetSafeInt("fgCountryID"), killRow.GetSafeInt("fgTankID"))); i++; } } int vic = 0; if (item.Victory - dr.GetSafeInt("bpWins") > 0) { vic = 0; } else if (item.Losses - dr.GetSafeInt("bpLosses") > 0) { vic = 1; } else { vic = 2; } //WOTHelper.AddToLog("ASD: " + dr.GetSafeInt("bpRatingWN8")); lb.Add(int.Parse(item.BattleTime), new RecentBattle() { CountryID = item.CountryID, TankID = item.TankID, Battles = item.BattleCount - dr.GetSafeInt("bpBattleCount"), BattleTime = int.Parse(item.BattleTime), Hits = item.Hits - dr.GetSafeInt("bpHits"), Shot = item.Shots - dr.GetSafeInt("bpShots"), Victory = vic, Survived = item.Survived - dr.GetSafeInt("bpSurvivedBattles"), DefencePoints = item.DefencePoints - dr.GetSafeInt("bpDefencePoints"), CapturePoints = item.CapturePoints - dr.GetSafeInt("bpCapturePoints"), Spotted = item.Spotted - dr.GetSafeInt("bpSpotted"), DamageDealt = item.DamageDealt - dr.GetSafeInt("bpDamageDealt"), DamageAssistedRadio = item.DamageAssistedRadio - dr.GetSafeInt("bpDamageAssistedRadio"), DamageAssistedTracks = item.DamageAssistedTracks - dr.GetSafeInt("bpDamageAssistedTracks"), DamageReceived = item.DamageReceived - dr.GetSafeInt("bpDamageReceived"), Kills = item.Kills - dr.GetSafeInt("bpFrags"), Mileage = item.Mileage - dr.GetSafeInt("bpMileage"), RatingEff = item.RatingEff - dr.GetSafeInt("bpRatingEff"), RatingBR = item.RatingBR - dr.GetSafeInt("bpRatingBR"), RatingWN7 = item.RatingWN7 - dr.GetSafeInt("bpRatingWN7"), RatingWN8 = item.RatingWN8 - dr.GetSafeInt("bpRatingWN8"), XPReceived = item.XPReceived - dr.GetSafeInt("bpXP"), OriginalBattleCount = item.BattleCount - dr.GetSafeInt("bpBattleCount"), FragList = string.Join(";", newKills.ToArray().Take(15)), GlobalAvgTier = avgTier, GlobalWinPercentage = vics, GlobalAvgDefencePoints = defPoints, BattleMode = item.BattleMode, DBEntry = false }); lb.Save(); } } else { int i = 0; List <string> newKills = new List <string>(); WOTHelper.AddToLog("BM 2: " + item.BattleMode); dr = currentRecord.Select(String.Format("cmCountryID = {0} and cmTankID = {1} and bpBattleMode <> '{2}'", item.CountryID, item.TankID, item.BattleMode)).DefaultIfEmpty(null).FirstOrDefault(); if (dr != null) { foreach (DataRow killRow in mt.GetTable("File_FragList").Select("fgParentID = " + item.ID)) { DataRow dr_oldKills = Kills.Select(String.Format(@"fgParentID = {0} and fgCountryID = {1} and fgTankID = {2}", dr.GetSafeInt("cmID"), killRow.GetSafeInt("fgCountryID"), killRow.GetSafeInt("fgTankID"))).FirstOrDefault(); if (dr_oldKills != null) { if (killRow.GetSafeInt("fgValue") > dr_oldKills.GetSafeInt("kills")) { newKills.Add(String.Format("{0}:{1}_{2}", i, killRow.GetSafeInt("fgCountryID"), killRow.GetSafeInt("fgTankID"))); i++; } } else { newKills.Add(String.Format("{0}:{1}_{2}", i, killRow.GetSafeInt("fgCountryID"), killRow.GetSafeInt("fgTankID"))); i++; } } } else { foreach (DataRow killRow in mt.GetTable("File_FragList").Select("fgParentID = " + item.ID)) { newKills.Add(String.Format("{0}:{1}_{2}", i, killRow.GetSafeInt("fgCountryID"), killRow.GetSafeInt("fgTankID"))); i++; } } int vic = 0; if (item.Victory - 0 > 0) { vic = 0; } else if (item.Losses - 0 > 0) { vic = 1; } else { vic = 2; } lb.Add(int.Parse(item.BattleTime), new RecentBattle() { CountryID = item.CountryID, TankID = item.TankID, Battles = item.BattleCount, BattleTime = int.Parse(item.BattleTime), Hits = item.Hits, Shot = item.Shots, Victory = vic, Survived = item.Survived, DefencePoints = item.DefencePoints, CapturePoints = item.CapturePoints, Spotted = item.Spotted, DamageDealt = item.DamageDealt, DamageAssistedRadio = item.DamageAssistedRadio, DamageAssistedTracks = item.DamageAssistedTracks, DamageReceived = item.DamageReceived, Kills = item.Kills, Mileage = item.Mileage, XPReceived = item.XPReceived, OriginalBattleCount = item.BattleCount, FragList = string.Join(";", newKills.ToArray().Take(15)), GlobalAvgTier = avgTier, GlobalWinPercentage = vics, GlobalAvgDefencePoints = defPoints, BattleMode = item.BattleMode, RatingEff = item.RatingEff, RatingBR = item.RatingBR, RatingWN7 = item.RatingWN7, RatingWN8 = item.RatingWN8, DBEntry = false }); lb.Save(); } } } //} //catch (Exception ex) //{ // _messages.Add(String.Format("Error : Cannot create last battle ({0}) - {1}", GetPlayerName, ex.Message)); //} }