/// <summary> /// Attempts to match all ":player" variables against a player /// </summary> /// <param name="match">The result of a regular expression match against a compiled command</param> /// <param name="result">THe result to append matched data against</param> /// <returns>True if matching was successful, false if no data was found.</returns> protected bool TryMatchPlayers(Match match, TextCommandMatchModel result) { bool matching = true; int maximumNameLength = this.Connection.ProtocolState.Players.Count > 0 ? this.Connection.ProtocolState.Players.Values.Max(player => player.Name.Length) : 0; result.Players = new List<PlayerModel>(); for (var offset = 0; match.Groups["player" + offset].Success == true && matching == true; offset++) { var text = match.Groups["player" + offset].Value; PlayerModel player = this.Connection.ProtocolState.Players.Values.FirstOrDefault(p => Math.Max(p.NameStripped.DePluralStringSimularity(text), p.Name.DePluralStringSimularity(text)) >= this.MinimumSimilarity(55, 70, maximumNameLength, p.Name.Length)); if (player != null) { result.Players.Add(player); } else { matching = false; } } return matching; }
/// <summary> /// Attempts to match all ":map" variables against a map name /// </summary> /// <param name="match">The result of a regular expression match against a compiled command</param> /// <param name="result">THe result to append matched data against</param> /// <returns>True if matching was successful, false if no data was found.</returns> protected bool TryMatchMaps(Match match, TextCommandMatchModel result) { bool matching = true; result.Maps = new List<MapModel>(); for (var offset = 0; match.Groups["map" + offset].Success == true && matching == true; offset++) { var text = match.Groups["map" + offset].Value; MapModel map = this.Connection.ProtocolState.MapPool.Values.FirstOrDefault(m => Math.Max(m.FriendlyName.DePluralStringSimularity(text), m.Name.DePluralStringSimularity(text)) >= 60); if (map != null) { result.Maps.Add(map); } else { matching = false; } } return matching; }
/// <summary> /// Attempts to convert all ":number" variables into float /// </summary> /// <param name="match">The result of a regular expression match against a compiled command</param> /// <param name="result">THe result to append matched data against</param> /// <returns>True if matching was successful, false if conversion failed.</returns> protected bool TryMatchNumbers(Match match, TextCommandMatchModel result) { bool matching = true; result.Numeric = new List<float>(); for (var offset = 0; match.Groups["number" + offset].Success == true && matching == true; offset++) { float number = 0.0F; if (float.TryParse(match.Groups["number" + offset].Value, out number)) { result.Numeric.Add(number); } else { matching = false; } } return matching; }
/// <summary> /// Parses all additional models from a regular expression match /// </summary> /// <param name="match">The result of a regular expression match against a compiled command</param> /// <returns>A built text command match model, or null if some data couldn't be found.</returns> protected TextCommandMatchModel BuildTextCommandMatch(Match match) { TextCommandMatchModel textCommandMatchModel = new TextCommandMatchModel(); bool matches = this.TryMatchPlayers(match, textCommandMatchModel) && this.TryMatchMaps(match, textCommandMatchModel) && this.TryMatchTexts(match, textCommandMatchModel) && this.TryMatchNumbers(match, textCommandMatchModel); return matches == true ? textCommandMatchModel : null; }
/// <summary> /// Appends all :text values to the quotes property of the text command result. /// </summary> /// <param name="match">The result of a regular expression match against a compiled command</param> /// <param name="result">THe result to append matched data against</param> /// <returns>Always true</returns> protected bool TryMatchTexts(Match match, TextCommandMatchModel result) { result.Quotes = new List<String>(); for (var offset = 0; match.Groups["text" + offset].Success == true; offset++) { result.Quotes.Add(match.Groups["text" + offset].Value); } return true; }