public override void Invoke(IAdventurePlayer player, ChatCommandEventArgs e) { var args = e.ArgsAsList; if (args.Count == 1) { player.ChatClient.PostDirectMessage(player, Verbs.Contains(args[0]) ? $"I don't know how to just `{args[0]}`. Can you be a little more explicit?" : $"Sorry, I don't understand the verb `{args[0]}`."); return; } var itemToInteractWith = args[1]; var itemInInventory = player.Inventory.GetItems().FirstOrDefault(i => i.IsMatch(itemToInteractWith)); if (itemInInventory is null) { var containedItems = player.Inventory.GetContainedItems(); itemInInventory = containedItems.FirstOrDefault(i => i.IsMatch(itemToInteractWith)); } var itemAtLocation = player.CurrentLocation.Items.FirstOrDefault(i => i.IsMatch(itemToInteractWith)); if (itemInInventory is null && itemAtLocation is null) { player.ChatClient.PostDirectMessage(player, $"Which `{itemToInteractWith}` are you referring to?"); player.ChatClient.PostDirectMessage(player, "Tip: Don't use words like 'the' or 'at', try just <verb> <noun> instead."); return; } bool actionWasMatched; // Interact with inventory items as a preference ... actionWasMatched = itemInInventory?.Interact(args[0], player) ?? itemAtLocation.Interact(args[0], player); if (!actionWasMatched) { player.ChatClient.PostDirectMessage(player, $"I don't know how to `{args[0]}` a `{itemToInteractWith}`. " + "Can you be clearer?"); } }
/// <summary> /// For performance withPathInfoParts should already be a lower case string /// to minimize redundant matching operations. /// </summary> public bool IsMatch(string httpMethod, string[] withPathInfoParts, ILogger logger, out int wildcardMatchCount) { wildcardMatchCount = 0; if (withPathInfoParts.Length != this.PathComponentsCount && !this.IsWildCardPath) { //logger.Info("withPathInfoParts mismatch for {0} for {1}", httpMethod, string.Join("/", withPathInfoParts)); return(false); } if (!Verbs.Contains(httpMethod, StringComparer.OrdinalIgnoreCase)) { //logger.Info("allowsAllVerbs mismatch for {0} for {1} allowedverbs {2}", httpMethod, string.Join("/", withPathInfoParts), this.allowedVerbs); return(false); } if (!ExplodeComponents(ref withPathInfoParts)) { //logger.Info("ExplodeComponents mismatch for {0} for {1}", httpMethod, string.Join("/", withPathInfoParts)); return(false); } if (this.TotalComponentsCount != withPathInfoParts.Length && !this.IsWildCardPath) { //logger.Info("TotalComponentsCount mismatch for {0} for {1}", httpMethod, string.Join("/", withPathInfoParts)); return(false); } int pathIx = 0; for (var i = 0; i < this.TotalComponentsCount; i++) { if (this.isWildcard[i]) { if (i < this.TotalComponentsCount - 1) { // Continue to consume up until a match with the next literal while (pathIx < withPathInfoParts.Length && !LiteralsEqual(withPathInfoParts[pathIx], this.literalsToMatch[i + 1])) { pathIx++; wildcardMatchCount++; } // Ensure there are still enough parts left to match the remainder if ((withPathInfoParts.Length - pathIx) < (this.TotalComponentsCount - i - 1)) { //logger.Info("withPathInfoParts length mismatch for {0} for {1}", httpMethod, string.Join("/", withPathInfoParts)); return(false); } } else { // A wildcard at the end matches the remainder of path wildcardMatchCount += withPathInfoParts.Length - pathIx; pathIx = withPathInfoParts.Length; } } else { var literalToMatch = this.literalsToMatch[i]; if (literalToMatch == null) { // Matching an ordinary (non-wildcard) variable consumes a single part pathIx++; continue; } if (withPathInfoParts.Length <= pathIx || !LiteralsEqual(withPathInfoParts[pathIx], literalToMatch)) { //logger.Info("withPathInfoParts2 length mismatch for {0} for {1}. not equals: {2} != {3}.", httpMethod, string.Join("/", withPathInfoParts), withPathInfoParts[pathIx], literalToMatch); return(false); } pathIx++; } } return(pathIx == withPathInfoParts.Length); }
/// <summary> /// For performance withPathInfoParts should already be a lower case string /// to minimize redundant matching operations. /// </summary> public bool IsMatch(string httpMethod, string[] withPathInfoParts, out int wildcardMatchCount) { wildcardMatchCount = 0; if (withPathInfoParts.Length != this.PathComponentsCount && !this.IsWildCardPath) { return(false); } if (!Verbs.Contains(httpMethod, StringComparer.OrdinalIgnoreCase)) { return(false); } if (!ExplodeComponents(ref withPathInfoParts)) { return(false); } if (this.TotalComponentsCount != withPathInfoParts.Length && !this.IsWildCardPath) { return(false); } int pathIx = 0; for (var i = 0; i < this.TotalComponentsCount; i++) { if (this.isWildcard[i]) { if (i < this.TotalComponentsCount - 1) { // Continue to consume up until a match with the next literal while (pathIx < withPathInfoParts.Length && !string.Equals(withPathInfoParts[pathIx], this.literalsToMatch[i + 1], StringComparison.InvariantCultureIgnoreCase)) { pathIx++; wildcardMatchCount++; } // Ensure there are still enough parts left to match the remainder if ((withPathInfoParts.Length - pathIx) < (this.TotalComponentsCount - i - 1)) { return(false); } } else { // A wildcard at the end matches the remainder of path wildcardMatchCount += withPathInfoParts.Length - pathIx; pathIx = withPathInfoParts.Length; } } else { var literalToMatch = this.literalsToMatch[i]; if (literalToMatch == null) { // Matching an ordinary (non-wildcard) variable consumes a single part pathIx++; continue; } if (withPathInfoParts.Length <= pathIx || !string.Equals(withPathInfoParts[pathIx], literalToMatch, StringComparison.InvariantCultureIgnoreCase)) { return(false); } pathIx++; } } return(pathIx == withPathInfoParts.Length); }
private void BuildQueryStringAndBodyRouteParts() { var actionParameters = Method.Parameters .Where(p => Controller.RouteParts.All(brp => brp.ParameterName != p.Name) && RouteParts.All(rp => rp.ParameterName != p.Name)) .ToList(); var isBodyAllowed = Verbs.Contains(WebApiHttpVerb.Post) || Verbs.Contains(WebApiHttpVerb.Put); var fromBodyAttributeName = "FromBodyAttribute"; var fromUriAttributeName = "FromUriAttribute"; var isThereAnythingFromBody = actionParameters .Any(ap => Helpers.HasCustomAttribute(ap, fromBodyAttributeName)); foreach (var actionParameter in actionParameters) { var isFromBody = Helpers.HasCustomAttribute(actionParameter, fromBodyAttributeName); var isFromUri = Helpers.HasCustomAttribute(actionParameter, fromUriAttributeName); var isPrimitive = actionParameter.ParameterType.IsPrimitive; if (!isPrimitive) { var strippedType = TypeService.StripGenerics(actionParameter.ParameterType, actionParameter.Name, out bool isNullable, out int collectionLevel); isPrimitive = isNullable && TypeService.GetPrimitiveTypeScriptType(strippedType.FullName) != null; } if (isBodyAllowed && ((isThereAnythingFromBody && isFromBody) || (!isThereAnythingFromBody && !isFromUri && !isPrimitive))) { BodyParameters.Add(new WebApiRoutePart { Name = actionParameter.Name, ParameterName = actionParameter.Name, Parameter = actionParameter, CustomAttributes = new List <string> { fromBodyAttributeName }, IsOptional = false }); } else { var queryStringRoutePart = new WebApiRoutePart { Name = actionParameter.Name, ParameterName = actionParameter.Name, Parameter = actionParameter, IsOptional = true }; if (actionParameter.HasCustomAttributes) { queryStringRoutePart.CustomAttributes = actionParameter.CustomAttributes .Select(a => a.AttributeType.Name) .ToList(); } QueryStringParameters.Add(queryStringRoutePart); } } }
// Find an opinion word target 90% protected string GetOpinionWordTarget(string OpinionWord, int index, string sentence) { FillConjunctions(); FillAdverbs(); FillComparatives(); FillDecreasers(); FillFutureWords(); FillIncreasers(); FillVerbs(); FillPronouns(); FillNegations(); OpinionWord = WordsInSentence[index]; if (OpinionWord != null) { if (sentence.Contains(OpinionWord)) { int myvalue = WordsInSentence.GetUpperBound(0); if (myvalue >= index + 1) { Target = WordsInSentence[index + 1]; Target = Target.ToLower(); if ((!OpinionLexicon.Contains(Target)) && (!Conjunctions.Contains(Target)) && (!Comparatives.Contains(Target)) && (!FutureWords.Contains(Target)) && (!Adverbs.Contains(Target)) && (!Increasers.Contains(Target)) && (!Decreasers.Contains(Target)) && (!Verbs.Contains(Target)) && (!Pronouns.Contains(Target)) && (!Negations.Contains(Target))) { return(Target); } { if (Conjunctions.Contains(WordsInSentence[index + 1])) { Target = WordsInSentence[index + 2]; if ((!OpinionLexicon.Contains(Target)) && (!Conjunctions.Contains(Target)) && (!Comparatives.Contains(Target)) && (!FutureWords.Contains(Target)) && (!Adverbs.Contains(Target)) && (!Increasers.Contains(Target)) && (!Decreasers.Contains(Target)) && (!Verbs.Contains(Target)) && (!Pronouns.Contains(Target)) && (!Negations.Contains(Target))) { return(Target); } } } } else { return(null); } } } return(null); }
//Orientation=> if pos = true else false 80% protected bool GetOpinionWordOrientation(string word, int position, string sentence) { FillPositiveWords(); FillNegativeWords(); FillAdverbs(); FillComparatives(); FillConjunctions(); FillDecreasers(); FillFutureWords(); FillIncreasers(); FillVerbs(); FillNegations(); //{pos} if (PositiveWords.Contains(word)) { WordOrientation = true; } if (NegativeWords.Contains(word)) { WordOrientation = false; } if (position != 0) { //{pos} {pos} if (PositiveWords.Contains(WordsInSentence[position - 1])) { WordOrientation = true; } //{neg} {pos} if (NegativeWords.Contains(WordsInSentence[position - 1])) { WordOrientation = false; } if (position >= 2) { //{fut} {verb} {pos} if (FutureWords.Contains(WordsInSentence[position - 2]) && Verbs.Contains(WordsInSentence[position - 1]) && PositiveWords.Contains(WordsInSentence[position])) { WordOrientation = true; } //{pos} {neg} if (PositiveWords.Contains(WordsInSentence[position - 1]) && NegativeWords.Contains(WordsInSentence[position])) { WordOrientation = false; } //{decr} {comp} {pos} if (Decreasers.Contains(WordsInSentence[position - 2]) && Comparatives.Contains( WordsInSentence[position - 1]) && PositiveWords.Contains( WordsInSentence[position])) { WordOrientation = true; } if (position >= 3) { //{pos} {conj} {nego} {cpos} if (PositiveWords.Contains(WordsInSentence[position - 3]) && Conjunctions.Contains( WordsInSentence[position - 2]) && Negations.Contains( WordsInSentence[position - 1]) && PositiveWords.Contains( WordsInSentence[position])) { WordOrientation = true; } //{neg} {conj} {incr} {cneg} if (NegativeWords.Contains(WordsInSentence[position - 3]) && Conjunctions.Contains( WordsInSentence[position - 2]) && Increasers.Contains( WordsInSentence[position - 1]) && NegativeWords.Contains( WordsInSentence[position])) { WordOrientation = false; } } } } return(WordOrientation); }