/// <summary> /// Parses a detailed summary entry into a scoreboard details object. /// </summary> /// <param name="dataEntries">The data.</param> protected virtual void ParseDetailedSummaryEntry(ScoreboardDetails details, string[] dataEntries) { var summary = new ScoreboardSummaryEntry(); details.Summary = summary; // ID, Division (labeled location, their bug), Location (labeled division, their bug), tier, scored image count, play time, score time, warnings, current score summary.TeamId = TeamId.Parse(dataEntries[0]); // [not in data, matched from categoryProvider] all service category summary.Category = _categoryProvider?.GetCategory(summary.TeamId); // tier and division if (Utilities.TryParseEnumSpaceless <Division>(dataEntries[2], out Division division)) { summary.Division = division; } summary.Location = dataEntries[1]; if (Enum.TryParse <Tier>(dataEntries[3], true, out Tier tier)) { summary.Tier = tier; } // number of images summary.ImageCount = int.Parse(dataEntries[4].Trim()); // times summary.PlayTime = Utilities.ParseHourMinuteSecondTimespan(dataEntries[5]); details.ScoreTime = Utilities.ParseHourMinuteSecondTimespan(dataEntries[6]); // warnings and total score string warnStr = dataEntries[7]; summary.Warnings |= warnStr.Contains("T") ? ScoreWarnings.TimeOver : 0; summary.Warnings |= warnStr.Contains("M") ? ScoreWarnings.MultiImage : 0; summary.TotalScore = double.Parse(dataEntries.Last().Trim()); }
public Task InitializeAsync(IServiceProvider provider) { if (Database == null) { Database = provider.GetService <LiteDatabase>(); if (Database == null) { var conf = provider.GetRequiredService <IConfiguration>(); Database = new LiteDatabase(conf["databaseFilename"]); } } // this is code specific to this program, the rest of this class is fairly generic // index each model GetCollection <Models.Guild>().EnsureIndex(g => g.Id, true); GetCollection <Models.User>().EnsureIndex(u => u.Id, true); Database.Mapper.RegisterType <TeamId> ( serialize: (teamId) => teamId.ToString(), deserialize: (bson) => TeamId.Parse(bson.AsString) ); return(Task.CompletedTask); }
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { switch (value) { case short tempShort: if (destinationType == typeof(TeamId)) { // ctor does bounds checking return(new TeamId(DefaultCompetition, tempShort)); } break; case int i: if (destinationType == typeof(TeamId)) { // ctor does bounds checking return(new TeamId(DefaultCompetition, i)); } break; case string s: if (destinationType == typeof(string)) { return(s); } else if (destinationType == typeof(TeamId)) { return(TeamId.Parse(s)); } break; case TeamId t: if (destinationType == typeof(string)) { return(t.ToString()); } else if (destinationType == typeof(TeamId)) { return(t); } break; } throw new NotSupportedException("The specified conversion is not supported."); }
public async Task InitializeAsync(IServiceProvider provider) { _allServiceCategoryMap = new Dictionary <TeamId, string>(); string allServiceCategoryMapFile = provider.GetRequiredService <IConfiguration>().GetValue <string>("allServiceCategoryMapFile", null); if (!string.IsNullOrWhiteSpace(allServiceCategoryMapFile) && File.Exists(allServiceCategoryMapFile)) { foreach (var line in await File.ReadAllLinesAsync(allServiceCategoryMapFile).ConfigureAwait(false)) { try { string[] components = line.Split(new[] { ':' }, 2); _allServiceCategoryMap[TeamId.Parse(components[0].Trim())] = components[1].Trim(); } catch { // oh well } } } }
protected virtual ScoreboardSummaryEntry ParseSummaryEntry(string[] dataEntries) { ScoreboardSummaryEntry summary = new ScoreboardSummaryEntry(); summary.TeamId = TeamId.Parse(dataEntries[0]); summary.Category = _categoryProvider?.GetCategory(summary.TeamId); summary.Location = dataEntries[1]; if (Utilities.TryParseEnumSpaceless <Division>(dataEntries[2], out Division division)) { summary.Division = division; } if (Enum.TryParse <Tier>(dataEntries[3]?.Trim(), true, out Tier tier)) { summary.Tier = tier; } summary.ImageCount = int.Parse(dataEntries[4]); summary.PlayTime = Utilities.ParseHourMinuteTimespan(dataEntries[5]); summary.TotalScore = int.Parse(dataEntries[7]); summary.Warnings |= dataEntries[6].Contains("T") ? ScoreWarnings.TimeOver : 0; summary.Warnings |= dataEntries[6].Contains("M") ? ScoreWarnings.MultiImage : 0; return(summary); }
protected override void ParseDetailedSummaryEntry(ScoreboardDetails details, string[] dataEntries) { var summary = new ScoreboardSummaryEntry(); details.Summary = summary; // ID, Division (labeled location, their bug), Location (labeled division, their bug), tier, scored img, play time, score time, current score, warn summary.TeamId = TeamId.Parse(dataEntries[0]); summary.Category = _categoryProvider?.GetCategory(summary.TeamId); if (Utilities.TryParseEnumSpaceless <Division>(dataEntries[1], out Division division)) { summary.Division = division; } summary.Location = dataEntries[2]; if (Enum.TryParse <Tier>(dataEntries[3], true, out Tier tier)) { summary.Tier = tier; } summary.ImageCount = int.Parse(dataEntries[4].Trim()); summary.PlayTime = Utilities.ParseHourMinuteTimespan(dataEntries[5]); string scoreTimeText = dataEntries[6]; // to deal with legacy scoreboards int scoreTimeIndOffset = 0; if (scoreTimeText.Contains(":")) { details.ScoreTime = Utilities.ParseHourMinuteTimespan(dataEntries[6]); } else { details.ScoreTime = summary.PlayTime; scoreTimeIndOffset = -1; } summary.TotalScore = int.Parse(dataEntries[7 + scoreTimeIndOffset].Trim()); string warnStr = dataEntries[8 + scoreTimeIndOffset]; summary.Warnings |= warnStr.Contains("T") ? ScoreWarnings.TimeOver : 0; summary.Warnings |= warnStr.Contains("M") ? ScoreWarnings.MultiImage : 0; }
static void Main(string[] args) { string jsonPath; if (args.Length < 1) { Console.Error.WriteLine("Enter path to JSON file:"); jsonPath = Console.ReadLine(); } else { jsonPath = args[0]; } Console.Error.WriteLine($"Got {jsonPath} as JSON path"); string categoryPath; if (args.Length < 2) { Console.Error.WriteLine("Enter path to category map file:"); categoryPath = Console.ReadLine(); } else { categoryPath = args[1]; } Console.Error.WriteLine($"Got {categoryPath} as category mapfile path"); var input = JsonConvert.DeserializeObject <Output>(File.ReadAllText(jsonPath)); var teamCategoryDictionary = File.ReadAllLines(categoryPath).Select(l => l.Trim().Split(new[] { ':' }, 2)).Where(l => TeamId.TryParse(l[0], out TeamId _)).ToDictionary(l => TeamId.Parse(l[0]), l => ServiceCategoryExtensions.ParseCanonicalName(l[1])); // fix summary data foreach (var teamSummary in input.summary.TeamList) { if (teamSummary.Category == null && teamCategoryDictionary.TryGetValue(teamSummary.TeamId, out ServiceCategory newCategory)) { teamSummary.Category = newCategory; } } // fix details foreach (var knownCategory in teamCategoryDictionary) { if (input.teams.TryGetValue(knownCategory.Key, out var teamDetails) && teamDetails.Summary.Category == null) { teamDetails.Summary.Category = knownCategory.Value; } } Console.Write(JsonConvert.SerializeObject(input)); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { return(TeamId.Parse((string)reader.Value)); }
static void Main(string[] args) { Console.WriteLine("Enter path to CSV file:"); string path = Console.ReadLine(); Console.WriteLine("Enter timestamp:"); string timestamp = Console.ReadLine(); Console.WriteLine("Enter path to all service category map file (or empty string):"); string servicePath = Console.ReadLine(); Console.WriteLine("Enter round number:"); int roundNumber = int.Parse(Console.ReadLine()); Console.WriteLine("Enter origin URI:"); string originUri = Console.ReadLine(); Dictionary <TeamId, string> categories = new Dictionary <TeamId, string>(); if (servicePath != "") { categories = File.ReadAllLines(servicePath).Select(x => x.Split(':')).Where(x => TeamId.TryParse(x[0], out TeamId _)).ToDictionary(x => TeamId.Parse(x[0]), x => x[1]); } var lines = File.ReadAllLines(path); CompleteScoreboardSummary summary = new CompleteScoreboardSummary(); summary.TeamList = new List <ScoreboardSummaryEntry>(); summary.SnapshotTimestamp = DateTimeOffset.Parse(timestamp); summary.OriginUri = string.IsNullOrEmpty(originUri) ? null : new Uri(originUri); Console.WriteLine("Loading score data"); foreach (string[] data in lines.Skip(1).Select(line => line.Split(','))) { ScoreboardSummaryEntry entry = new ScoreboardSummaryEntry { TeamId = TeamId.Parse(data[0]), Division = Enum.Parse <Division>(data[1].Replace(" ", ""), true), Category = string.IsNullOrEmpty(data[2]) ? categories.TryGetValue(TeamId.Parse(data[0]), out string c) ? c : null : data[2], Location = data[3], Tier = Enum.TryParse <Tier>(data[4], true, out Tier t) ? t : (Tier?)null, ImageCount = int.Parse(data[5]), PlayTime = ParseTimeSpan(data[6]), TotalScore = int.Parse(data[7]), Warnings = (data[8].Contains("M") ? ScoreWarnings.MultiImage : 0) | (data[8].Contains("T") ? ScoreWarnings.TimeOver : 0) }; summary.TeamList.Add(entry); } Console.WriteLine("Generating output data"); var o = new Output { round = roundNumber, summary = summary, teams = summary.TeamList.Select(x => new ScoreboardDetails { Images = new List <ScoreboardImageDetails> { new ScoreboardImageDetails { ImageName = "All Points", Penalties = 0, PlayTime = x.PlayTime, PointsPossible = x.ImageCount * 100, Score = x.TotalScore, VulnerabilitiesFound = 0, VulnerabilitiesRemaining = 0, Warnings = x.Warnings } }, ImageScoresOverTime = null, OriginUri = null, ScoreTime = x.PlayTime, SnapshotTimestamp = DateTimeOffset.Parse(timestamp), Summary = x }).ToDictionary(x => x.TeamId, x => x) }; File.WriteAllText("scores.json", JsonConvert.SerializeObject(o)); Console.WriteLine("Done"); Console.ReadKey(); }
private async Task MessageReceived(SocketMessage rawMessage) { // Ignore system messages and messages from bots if (!(rawMessage is SocketUserMessage message)) { return; } if (message.Source != MessageSource.User) { return; } // show embed for team links Match scoreboardMatch = _teamUrlRegex.Match(message.Content); if (scoreboardMatch != null && scoreboardMatch.Success) { await message.Channel.SendMessageAsync(string.Empty, embed : _messageBuilder .CreateTeamDetailsEmbed( await _scoreRetriever.GetDetailsAsync(TeamId.Parse(scoreboardMatch.Groups[1].Value)).ConfigureAwait(false)) .Build()).ConfigureAwait(false); } }