private static Clan CalculateClan(ClanPlataform clan, DbProvider provider, DbRecorder recorder) { Log.DebugFormat("Calculando cla {0}@{1}...", clan.ClanTag, clan.Plataform); var cc = provider.GetClan(clan.Plataform, clan.ClanId); if (cc == null) { Log.Warn("O cla ainda não teve nenhum membro atualizado."); return(null); } if (cc.Count == 0) { Log.Warn("O cla ainda não teve nenhum membro atualizado."); return(null); } Log.InfoFormat("------------------------------------------------------------------"); Log.InfoFormat("cla: {0}@{1}", cc.ClanTag, cc.Plataform); Log.InfoFormat("# Membros: {0};{1};{2} - Patched: {3}", cc.Count, cc.Active, 0, cc.NumberOfPatchedPlayers); Log.InfoFormat("Batalhas: T:{0:N0};A:{1:N0};W:{2:N0}", cc.TotalBattles, cc.ActiveBattles, 0); recorder.SetClanCalculation(cc); return(cc); }
/// <summary> /// Calcula todos os clas habilitados e os salva /// </summary> private static void CalculateAllClans() { string connectionString = ConfigurationManager.ConnectionStrings["Main"].ConnectionString; DbProvider provider = new DbProvider(connectionString); ClanPlataform[] clans = provider.GetClans().ToArray(); Log.InfoFormat("{0} clas devem ser calculados.", clans.Length); DbRecorder recorder = new DbRecorder(connectionString); FtpPutter putterXbox = new FtpPutter(ConfigurationManager.AppSettings["FtpFolder"], ConfigurationManager.AppSettings["FtpUser"], ConfigurationManager.AppSettings["FtpPassworld"]); FtpPutter putterPs = new FtpPutter(ConfigurationManager.AppSettings["PsFtpFolder"], ConfigurationManager.AppSettings["PsFtpUser"], ConfigurationManager.AppSettings["PsFtpPassworld"]); string resultDirectory = ConfigurationManager.AppSettings["ResultDirectory"]; string resultDirectoryPs = ConfigurationManager.AppSettings["PsResultDirectory"]; HashSet <string> already = new HashSet <string>(File.ReadAllLines(Path.Combine(resultDirectory, "CalcTask.txt"))); HashSet <string> alreadyPs = new HashSet <string>(File.ReadAllLines(Path.Combine(resultDirectoryPs, "CalcTask.txt"))); object o = new object(); // Calcula cada cla int doneCount = 0; Stopwatch sw = Stopwatch.StartNew(); Parallel.For(0, clans.Length, new ParallelOptions { MaxDegreeOfParallelism = 2 }, i => { ClanPlataform clan = clans[i]; bool done = false; if (clan.Plataform == Platform.XBOX) { done = already.Contains(clan.ClanTag); } else if (clan.Plataform == Platform.PS) { done = alreadyPs.Contains(clan.ClanTag); } if (done) { Log.InfoFormat("cla {0} de {1}: {2}@{3} feito anteriormente.", i + 1, clans.Length, clan.ClanTag, clan.Plataform); Interlocked.Increment(ref doneCount); return; } Log.InfoFormat("Processando cla {0} de {1}: {2}@{3}...", i + 1, clans.Length, clan.ClanTag, clan.Plataform); Stopwatch csw = Stopwatch.StartNew(); Clan cc = CalculateClan(clan, provider, recorder); Log.InfoFormat("Calculado cla {0} de {1}: {2}@{3} em {4:N1}s...", i + 1, clans.Length, clan.ClanTag, clan.Plataform, csw.Elapsed.TotalSeconds); if (cc != null) { Stopwatch fsw = Stopwatch.StartNew(); switch (cc.Plataform) { case Platform.XBOX: { string fileName = cc.ToFile(resultDirectory); Log.InfoFormat("Arquivo de resultado escrito em '{0}'", fileName); putterXbox.PutClan(fileName); lock (o) { File.AppendAllText(Path.Combine(resultDirectory, "CalcTask.txt"), $"{cc.ClanTag}\r\n", Encoding.UTF8); } } break; case Platform.PS: { string fileName = cc.ToFile(resultDirectoryPs); Log.InfoFormat("Arquivo de resultado escrito em '{0}'", fileName); putterPs.PutClan(fileName); lock (o) { File.AppendAllText(Path.Combine(resultDirectoryPs, "CalcTask.txt"), $"{cc.ClanTag}\r\n", Encoding.UTF8); } } break; case Platform.Virtual: break; default: throw new ArgumentOutOfRangeException(); } Log.InfoFormat("Upload do cla {0} de {1}: {2}@{3} em {4:N1}s...", i + 1, clans.Length, clan.ClanTag, clan.Plataform, fsw.Elapsed.TotalSeconds); } Interlocked.Increment(ref doneCount); Log.InfoFormat("Processado cla {0} de {1}: {2}@{3} em {4:N1}s. {5} totais.", i + 1, clans.Length, clan.ClanTag, clan.Plataform, csw.Elapsed.TotalSeconds, doneCount); }); TimeSpan calculationTime = sw.Elapsed; }