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)); //} }
private void SaveFile(string fileName, MemoryTables mt) { string folderName = String.Format(@"{0}\{1}{2}", WOTHelper.GetApplicationData(), _HistoryIndicator, _playerName); if (!Directory.Exists(folderName)) { Directory.CreateDirectory(folderName); } if (!Directory.Exists(String.Format(@"{0}\{1}{2}\LastBattle", WOTHelper.GetApplicationData(), _HistoryIndicator, _playerName))) { Directory.CreateDirectory(String.Format(@"{0}\{1}{2}\LastBattle", WOTHelper.GetApplicationData(), _HistoryIndicator, _playerName)); } //if (File.Exists(GetCurrentPlayerFile())) //{ // foreach (string file in Directory.GetFiles(String.Format(@"{0}\{1}{2}\LastBattle", WOTHelper.GetApplicationData(), _HistoryIndicator, _playerName)).Where(f => f.Contains(".wot") == false && f.Contains(".db") == false)) // { // File.Delete(file); // } // File.Copy(GetCurrentPlayerFile(), String.Format(@"{0}\{1}{2}\LastBattle\{3}.txt", WOTHelper.GetApplicationData(), _HistoryIndicator, _playerName, GetCurrentPlayerFileKey())); //} using (IDBHelpers db = new DBHelpers(WOTHelper.GetDBPath(_playerName), true)) { db.BeginTransaction(); // db.ExecuteNonQuery("delete from Cache_LastGame"); // db.ExecuteNonQuery(string.Format(@"insert into Cache_LastGame // (clFileID, clCountryID, clTankID, clBattleCount) // select cmFileID, cmCountryID, cmTankID, sum(bpBattleCount) battleCount from File_TankDetails // inner join File_Battles on cmID = bpParentID // where cmFileID = {0} --and ifnull(bpBattleMode,0) in (15,0) // group by cmFileID, cmCountryID, cmTankID", GetCurrentPlayerFile())); //check to see if we are inserting or updating db.ExecuteNonQuery("delete from File_Achievements where faParentID in (select cmID from File_TankDetails where cmFileID = " + fileName + ")"); db.ExecuteNonQuery("delete from File_Battles where bpParentID in (select cmID from File_TankDetails where cmFileID = " + fileName + ")"); db.ExecuteNonQuery("delete from File_Clan where clParentID in (select cmID from File_TankDetails where cmFileID = " + fileName + ")"); db.ExecuteNonQuery("delete from File_Historical where hsParentID in (select cmID from File_TankDetails where cmFileID = " + fileName + ")"); db.ExecuteNonQuery("delete from File_Company where fcParentID in (select cmID from File_TankDetails where cmFileID = " + fileName + ")"); db.ExecuteNonQuery("delete from File_FragList where fgParentID in (select cmID from File_TankDetails where cmFileID = " + fileName + ")"); db.ExecuteNonQuery("delete from File_Total where foParentID in (select cmID from File_TankDetails where cmFileID = " + fileName + ")"); db.ExecuteNonQuery("delete from File_TankDetails where cmFileID = " + fileName); db.ExecuteNonQuery("delete from Files where fiID = " + fileName); string[] tables = { "Files", "File_TankDetails", "File_Achievements", "File_Battles", "File_Clan", "File_Historical", "File_Company", "File_FragList", "File_Total" }; foreach (string table in tables) { using (DataTable files = mt.GetTable(table)) { List <string> updateFields = new List <string>(); List <string> updateValues = new List <string>(); foreach (DataColumn column in files.Columns) { updateFields.Add(column.ColumnName); } foreach (DataRow row in files.Rows) { List <string> values = new List <string>(); foreach (DataColumn column in files.Columns) { values.Add(GetSafeValue(row, column.ColumnName).ToString()); } updateValues.Add("Select " + string.Join(",", values.ToArray())); values.Clear(); } if (updateValues.Count >= 400) { foreach (var item in updateValues) { string updateFilesSQL = String.Format("Insert into {2} ({0}) {1} ", string.Join(", ", updateFields.ToArray()), item, table); db.ExecuteNonQuery(updateFilesSQL); } } else { if (updateValues.Count > 0) { string updateFilesSQL = String.Format("Insert into {2} ({0}) {1} ", string.Join(", ", updateFields.ToArray()), string.Join(" Union All ", updateValues.ToArray()), table); db.ExecuteNonQuery(updateFilesSQL); } } updateValues.Clear(); updateFields.Clear(); // string f = string.Join(" union all ", updateValues.Take(100).ToArray(); } } db.EndTransaction(); } }
private bool IsNewestFile(string fileName, MemoryTables mt) { using (DataTable tankDetail = mt.GetTable("File_TankDetails")) { using (DataTable battles = mt.GetTable("File_Battles")) { int file = GetCurrentPlayerFile(); DataTable dt; if (file == 0) { return(true); } else { string sql = String.Format(@"select T1.cmCountryID, T1.cmTankID, sum(T2.bpBattleCount) bpBattleCount from File_TankDetails T1 inner join File_Battles T2 on T1.cmID = T2.bpParentID where cmFileID = {0} group by T1.cmCountryID, T1.cmTankID --and bpBattleMode in (15, 0)", file); using (IDBHelpers db = new DBHelpers(WOTHelper.GetDBPath(_playerName))) { if (!UserSettings.ClearRecentBattlesFragList.Split(';').Contains(_playerName)) { //trying to clean up the mess need to remove this sometime in the future using (DataTable recordDelete = db.GetDataTable("select cmFileID, cmID from File_Battles inner join File_TankDetails on bpParentID = cmID where bpWins > bpBattleCount group by cmFileID")) { foreach (DataRow delRows in recordDelete.Rows) { db.ExecuteNonQuery(@"delete from File_Achievements where faParentID = " + delRows.GetSafeInt("cmID")); db.ExecuteNonQuery(@"delete from File_Battles where bpParentID = " + delRows.GetSafeInt("cmID")); db.ExecuteNonQuery(@"delete from File_Clan where clParentID = " + delRows.GetSafeInt("cmID")); db.ExecuteNonQuery(@"delete from File_Company where fcParentID = " + delRows.GetSafeInt("cmID")); db.ExecuteNonQuery(@"delete from File_Historical where hsParentID = " + delRows.GetSafeInt("cmID")); db.ExecuteNonQuery(@"delete from File_FragList where fgParentID = " + delRows.GetSafeInt("cmID")); db.ExecuteNonQuery(@"delete from File_Total where foParentID = " + delRows.GetSafeInt("cmID")); db.ExecuteNonQuery(@"delete from File_TankDetails where cmFileID = " + delRows.GetSafeInt("cmFileID")); db.ExecuteNonQuery(@"delete from Files where fiID = " + delRows.GetSafeInt("cmFileID")); db.ExecuteNonQuery(@"delete from recentbattles where rbBattles > 100"); } LoadDossierFiles(); } db.ExecuteNonQuery(@"delete from File_Battles where bpFrags8P > bpFrags"); db.ExecuteNonQuery(@"delete from File_Battles where bpMaxFrags > 15"); db.ExecuteNonQuery(@"update RecentBattles set rbFragList=''"); UserSettings.ClearRecentBattlesFragList = UserSettings.ConvertFlatFilesToDB + _playerName + ";"; } dt = db.GetDataTable(sql); } } int obj_Sum = 0; int obj_TestFile_Sum = 0; using (TankDescriptions tankDescription = new TankDescriptions(_messages)) { foreach (DataRow tank in dt.Rows) { if (tankDescription.Active(tank.GetSafeInt("cmCountryID"), tank.GetSafeInt("cmTankID"))) { obj_Sum += tank.GetSafeInt("bpBattleCount"); } } foreach (DataRow tank in tankDetail.Rows) { if (tankDescription.Active(tank.GetSafeInt("cmCountryID"), tank.GetSafeInt("cmTankID"))) { obj_TestFile_Sum += (from x in battles.AsEnumerable() where x.GetSafeInt("bpParentID") == tank.GetSafeInt("cmID") //&& (x.GetSafeInt("bpBattleMode") == 15 || x.GetSafeInt("bpBattleMode") == 0) select x.GetSafeInt("bpBattleCount")).Sum(); } } } if (obj_Sum <= obj_TestFile_Sum) { return(true); } else { return(false); } } } }