public void Run(IrcMessage theMessage) { if (!theMessage.HasArgs) { theMessage.Answer(ModelPage); return; } string input = theMessage.CommandLine.ToLower(); bool wildcard = false; if (theMessage.CommandArgs.LastOrDefault() == "all") { input = theMessage.CommandArgs.Take(theMessage.CommandArgs.Count - 1).Join(" "); wildcard = true; } else if (input.EndsWith("*")) { input = input.TrimEnd('*'); wildcard = true; } string sharpSplit = String.Empty; if (input.Contains('#')) { string[] split = input.Split(new[] { '#' }, 2); Contract.Assume(split.Length == 2); sharpSplit = "#" + split[1]; input = split[0]; } int inputLength = input.Length; TableBox Box = null; List <TableBox> Boxen = new List <TableBox>(); Dictionary <string, TableBox> packages = PackagesCache.GetItem(true); if (packages != null) { if (wildcard || !packages.TryGetValue(input, out Box)) { List <TableBox> likelyKey = packages.Where(x => x.Key.StartsWith(input, StringComparison.OrdinalIgnoreCase)).Select(x => x.Value).ToList(); if (likelyKey.Count > 0) { Box = likelyKey.FirstOrDefault(); Boxen = likelyKey; } else { int lowestDifference = 1000; foreach (KeyValuePair <string, TableBox> one in packages) { int result = StringSimilarity.Compare(input, one.Key, true); if (result < lowestDifference) { Box = one.Value; lowestDifference = result; } } } } if (!Boxen.Contains(Box)) { Boxen.Add(Box); } } if (Box == null) { theMessage.Answer("Da ist etwas fürchterlich schief gelaufen"); return; } StringBuilder sb = new StringBuilder("Freetz Modell "); if (theMessage.CommandName == "ff") { sb.Append(Box.Name); if (NotEmpty(Box.CPU)) { sb.Append(", CPU: "); sb.Append(Box.CPU); } if (NotEmpty(Box.RAM)) { sb.Append(", RAM: "); sb.Append(Box.RAM); } if (NotEmpty(Box.Flash)) { sb.Append(", Flash: "); sb.Append(Box.Flash); } if (NotEmpty(Box.USBHost)) { sb.Append(", USB-Host: "); sb.Append(Box.USBHost); } if (NotEmpty(Box.Annex)) { sb.Append(", Annex: "); sb.Append(Box.Annex); } if (NotEmpty(Box.FreetzVersion)) { sb.Append(", Unterstütze Freetz Version: "); sb.Append(Box.FreetzVersion); if (NotEmpty(Box.AngepassteFirmware)) { sb.Append(" ("); sb.Append(Box.AngepassteFirmware); sb.Append(")"); } } if (NotEmpty(Box.Sprache)) { sb.Append(", Sprachen: "); sb.Append(Box.Sprache); } if (NotEmpty(Box.Url)) { sb.Append(", Detailseite(n): "); sb.Append(Boxen.Select(x => "http://freetz.org" + x.Url).Join(" , ")); } else { sb.Append(", Noch keine Detailseite vorhanden"); } } else { if (Boxen.Count > 0) { sb.Append(Boxen.Select(x => x.FreetzType + " http://freetz.org" + x.Url).Join(" , ")); } else { sb.Append(Box.FreetzType); sb.Append(" http://freetz.org"); sb.Append(Box.Url); } } theMessage.Answer(sb.ToString()); }
public void Run(IrcMessage theMessage) { if (!theMessage.HasArgs) { theMessage.Answer(PackagesPage); return; } string input = theMessage.CommandLine; string anchor = String.Empty; if (input.Contains('#')) { string[] split = input.Split(new[] { '#' }, 2); anchor = "#" + split[1]; input = split[0]; } int lowestFuzzyDifference = 0, lowestStartsWithDifference = 0; FreetzPackage?exactMatch = null, fuzzyMatch = null, startsWithMatch = null; if (PackagesCache.GetItem(true) is { } packages&& (exactMatch = packages.FirstOrDefault(x => x.Name.Equals(input, StringComparison.OrdinalIgnoreCase))) == null) { FreetzPackage likelyKey = packages.FirstOrDefault(x => x.Name.StartsWith(input, StringComparison.OrdinalIgnoreCase)); if (likelyKey != null) { lowestStartsWithDifference = Math.Abs(likelyKey.Name.Length - input.Length); startsWithMatch = likelyKey; } else { lowestFuzzyDifference = 1000; foreach (FreetzPackage one in packages) { int result = StringSimilarity.Compare(input, one.Name, true); if (result < lowestFuzzyDifference) { fuzzyMatch = one; lowestFuzzyDifference = result; } } } } if (exactMatch != null) { Answer(theMessage, exactMatch, false, anchor); } else if (startsWithMatch != null && lowestStartsWithDifference < 5) { Answer(theMessage, startsWithMatch, lowestStartsWithDifference > 1, anchor); } else if (fuzzyMatch != null && lowestFuzzyDifference < 4) { Answer(theMessage, fuzzyMatch, true, anchor); } else if (startsWithMatch != null) { Answer(theMessage, startsWithMatch, true, anchor); } else { theMessage.Answer("Ich habe kein solches Paket gefunden"); } }