Example #1
0
        void EnterPressed(List <ConsoleKeyInfo> keysPressed)
        {
            if (keysPressed.Count > 0)
            {
                act = null;
                //reconstruct
                string typed = string.Empty;

                for (int i = 0; i < keysPressed.Count; i++)
                {
                    typed += keysPressed[i].KeyChar.ToString().ToLower();
                }

                keysPressed.Clear();                                            //clear the stack after giving command

                char[] delimiterChars = { ' ', ',', '.', ':', '\t', '!', '\r' };

                string[] words = typed.Split(delimiterChars);

                string matchingVerb = string.Empty;

                foreach (string word in words)                                  //is there one word matching one action?
                {
                    foreach (var action in Actions)
                    {
                        var verb = action.Verbs.Find(v => v.Term.Equals(word));
                        if (verb != null)
                        {
                            act          = action;
                            matchingVerb = word;                                //store the typed everb which triggered the action
                            break;
                        }
                    }
                }

                if (act != null)                                                //if there's an action available...
                {
                    if (act.Objects.Count == 0)                                 //...and is objectless...
                    {
                        if (!act.EvaluateSimple())                              //if for some reason Kriss can't do it, say it...
                        {
                            CustomRefusal(act.Condition.Refusal);
                        }
                        else
                        {
                            if (act.Effect != null)                             //in case the action has an Effect (inventory)
                            {
                                act.StoreItem(act.Effect);
                            }

                            DisplaySuccess(act.Answer, act.ChildId);            //...just do it
                        }
                    }
                    else
                    {                                                           //...otherwise, examine Objects
                        for (int i = 0; i < act.Objects.Count; i++)             //this is not O^2. only iterates over <10 x <10 items
                        {
                            Models.Object o = act.Objects[i];

                            foreach (string word in words)                      //is there a matching object available? just hand me the first you find please
                            {
                                if (o.Obj.Contains(word))                       //the action is right, and there is a acceptable object specified
                                {
                                    if (!act.EvaluateCombination(o))            //if for some reason Kriss can't do it, say it...
                                    {
                                        CustomRefusal(o.Condition.Refusal);
                                    }
                                    else                                        //...otherwise, do it
                                    {
                                        if (o.Effect != null)                   //in case the obj has an Effect (inventory)
                                        {
                                            act.StoreItem(o.Effect);
                                        }

                                        DisplaySuccess(o.Answer, o.ChildId);
                                    }
                                }
                            }
                        }
                        CustomRefusal(act.GetOpinion(matchingVerb));            //the action is right, but no required object is specified
                    }
                }
                else                                                            //if there's no action available, redraw node and display standard refuse
                {
                    RedrawNode();
                    BottomMessage = string.Empty;
                    PrepareForAction(false);
                }
            }
            else
            {
                RedrawNode();
                BottomMessage = string.Empty;
                PrepareForAction(true);
            }
        }