public static CommandResponse CurWar(CommandEventArgs args) { if (String.IsNullOrEmpty(_token)) { return(new CommandResponse("Ask the bot owner to set their CoC API key")); } var u = args.SourceUser; var clantag = u.GetSetting <string>("CoCClan", args.DatabaseInstance, null); if (String.IsNullOrEmpty(clantag)) { return(new CommandResponse("Please set your clan tag with /setclan")); } using (var wc = new HttpClient()) { wc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token); var url = String.Format(UrlConstants.GetCurrentWarInformationTemplate, clantag).Replace("#", "%23"); WarResponse war = null; try { var json = wc.GetStringAsync(url).Result; war = JsonConvert.DeserializeObject <WarResponse>(json); } catch (Exception e) { return(new CommandResponse($"Unable to find war information")); } //let's take a quick look var state = war.state; state = char.ToUpper(state[0]) + state.Substring(1); var r = new Regex(@" (?<=[A-Z])(?=[A-Z][a-z]) | (?<=[^A-Z])(?=[A-Z]) | (?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace); state = r.Replace(state, " "); if (state == "War Ended") { if (war.clan.stars > war.opponent.stars) { state += " (Win)"; } if (war.clan.stars == war.opponent.stars) { //get average desctruction var us = war.clan.members.Sum(x => x.attacks.Sum(a => a.destructionPercentage)) / war.clan.attacks; var them = war.opponent.members.Sum(x => x.attacks.Sum(a => a.destructionPercentage)) / war.opponent.attacks; if (us > them) { state += " (Win)"; } if (us == them) { state += " (Draw)"; } if (us < them) { state += " (Lose)"; } } if (war.clan.stars < war.opponent.stars) { state += " (Lose)"; } } var response = $"Stage: {state}" + $"\n{war.teamSize} v {war.teamSize}" + $"\n----------------------------------" + $"\n{war.clan.name}: {war.clan.stars} ⭐️" + $"\n{war.clan.attacks} attacks made out of {war.teamSize * 2}" + $"\n----------------------------------" + $"\n{war.opponent.name}: {war.opponent.stars} ⭐️" + $"\n{war.opponent.attacks} attacks made out of {war.teamSize * 2}" + $"\n----------------------------------" + $"\nMembers:" + $"\n----------------------------------"; foreach (var member in war.clan.members.OrderBy(x => x.mapPosition)) { response += $"\n{member.name}" + $"\n{member.attacks?.Length ?? 0} ⚔️ - {member.attacks?.Sum(x => x.stars) ?? 0} ⭐️ ({member.attacks?.Average(x => x.destructionPercentage) ?? 0:N0}% damage)" + $"\n--------------------"; } return(new CommandResponse(response) { ImageUrl = war.clan.badgeUrls.medium, ImageDescription = $"{war.clan.name} v {war.opponent.name}", ImageTitle = state }); } }
public static string GetWarReport(WarResponse war, ClanResponse c) { var result = "War Report:"; if (war.clan.stars > war.opponent.stars) { result += " (Win)"; } if (war.clan.stars == war.opponent.stars) { //get average desctruction var us = war.clan.members.Sum(x => x.attacks.Sum(a => a.destructionPercentage)) / war.clan.attacks; var them = war.opponent.members.Sum(x => x.attacks.Sum(a => a.destructionPercentage)) / war.opponent.attacks; if (us > them) { result += " (Win)"; } if (us == them) { result += " (Draw)"; } if (us < them) { result += " (Lose)"; } } if (war.clan.stars < war.opponent.stars) { result += " (Lose)"; } result += $" {war.clan.stars} to {war.opponent.stars}\n\nFlagged Members:\n\n"; foreach (var m in war.clan.members) { var attacks = m.attacks?.Length ?? 0; //check which members did not attack at all if (attacks == 0) { //no attack whatsoever result += $"{m.name} no attacks.\n"; continue; } //check which members failed to get 2 stars var stars = m.attacks?.Sum(x => x.stars) ?? 0; if (stars < 2) { result += $"{m.name} got {stars} stars.\n"; } //check which members did not attack twice if (attacks == 1) { result += $"{m.name} attacked once.\n"; } //check which members attacked outside their mirrors var pos = m.mapPosition; if (m.attacks != null) { foreach (var attack in m.attacks) { var opp = war.opponent.members.FirstOrDefault(x => x.tag == attack.defenderTag)?.mapPosition ?? 0; if (Math.Abs(opp - pos) > 2 && opp != 0) { result += $"{m.name} (#{pos}) attacked #{opp}\n"; } } } } return(result); }