コード例 #1
0
ファイル: RollableTable.cs プロジェクト: DougLeary/GameBits
        /// <summary>
        /// Convert a text string to an IResolver
        /// </summary>
        /// <param name="itemText"></param>
        /// <returns></returns>
        public static IResolver ParseItem(string itemText)
        {
            char[] comma = { ',' };
            char[] blank = { ' ' };

            // if the item is a comma-separated list, parse it as an ItemList
            string[] items = itemText.Split(comma, StringSplitOptions.RemoveEmptyEntries);
            if (items.Length > 1)
            {
                ItemList itemList = new ItemList();
                foreach (string item in items)
                {
                    IResolver obj = ParseItem(item);
                    if (obj != null)
                    {
                        itemList.Add(obj);
                    }
                }
                return(itemList);
            }

            // parse an individual item
            else if (items.Length == 1)
            {
                DieRoll    dieRoll;
                GameObject gameObject;
                string     item = items[0];

                // ItemRoll
                string[] tokens = item.Split(blank, StringSplitOptions.RemoveEmptyEntries);
                if (DieRoll.TryParse(tokens[0], out dieRoll))
                {
                    string theRest = item.Substring(tokens[0].Length).Trim();
                    if (GameObject.TryParse(theRest, out gameObject))
                    {
                        return(new ItemRoll(gameObject, dieRoll));
                    }
                }

                // GameObjectInstance
                GameObjectInstance gameObjectInstance;
                if (GameObjectInstance.TryParse(item, out gameObjectInstance))
                {
                    return(gameObjectInstance);
                }

                // TableRoll
                if (item.ToLower().StartsWith("roll"))
                {
                    TableRoll tableRoll;
                    if (TableRoll.TryParse(item.Remove(0, 5), out tableRoll))
                    {
                        return(tableRoll);
                    }
                }

                // GameObject
                if (GameObject.TryParse(item, out gameObject))
                {
                    return(gameObject);
                }
            }
            return(null);
        }
コード例 #2
0
        public static bool TryParse(string st, out TableRoll outTableRoll)
        {
            outTableRoll = new TableRoll();

            char[]   blank   = { ' ' };
            string[] words   = st.ToLower().Split(blank, StringSplitOptions.RemoveEmptyEntries);
            int      i       = 0;
            int      rolls   = 1;
            DieRoll  dieRoll = null;

            if (words[i] == "once")
            {
                i++;
            }
            else if (words[i] == "twice")
            {
                i++;
                rolls = 2;
            }
            else if (int.TryParse(words[i], out rolls))
            {
                i++;
            }
            else if (DieRoll.TryParse(words[i], out dieRoll))
            {
                i++;
            }
            if (words[i] == "times")
            {
                i++;
            }

            if (words[i] == "on")
            {
                i++;
            }

            // get the RollableTable name enclosed in double quotes
            string remainder = String.Join(" ", words, words.Length - i);
            Regex  regex     = new Regex("\"([^\"]*)\"");
            Match  match     = regex.Match(remainder);

            if (match.Success)
            {
                int ignoreBelow = 0;
                int ignoreAbove = int.MaxValue;

                // scan for ignore options
                remainder.Remove(0, match.Length);
                words = remainder.Split(blank, StringSplitOptions.RemoveEmptyEntries);
                i     = 0;
                if (words[i] == "ignoring" && words[i] == "ignore")
                {
                    i++;
                    if (words[i] == "results")
                    {
                        i++;
                    }
                    switch (words[i])
                    {
                    case "above":
                    case ">":
                    case "greater":
                        i++;
                        if (words[i] == "than")
                        {
                            i++;
                        }
                        if (!int.TryParse(words[i], out ignoreAbove))
                        {
                            return(false);
                        }
                        break;

                    case "below":
                    case "<":
                    case "less":
                        i++;
                        if (words[i] == "than")
                        {
                            i++;
                        }
                        if (!int.TryParse(words[i], out ignoreBelow))
                        {
                            return(false);
                        }
                        break;
                    }
                }
                outTableRoll.Table       = Repository.GetTable(match.Value);
                outTableRoll.Rolls       = rolls;
                outTableRoll.IgnoreAbove = ignoreAbove;
                outTableRoll.IgnoreBelow = ignoreBelow;
            }

            return(true);
        }