Beispiel #1
0
        private void B_Load_Click(object sender, EventArgs e)
        {
            //Read Import file for all currently specified Container Names
            _containerNames = lootContainernames.getNames();

            XmlDocument doc = new XmlDocument();
            doc.Load(T_XMLFile.Text);

            XmlNodeList ProbTmpl = doc.GetElementsByTagName("loot");
            readProbTemplates(ProbTmpl);

            XmlNodeList itemRefList = doc.GetElementsByTagName("item");

            //manually add the "Empty" Loot Group since it is missed in the Search for "item"
            lootGroup emptyLG = new lootGroup();
            lootGroupContents emptyLGContents = new lootGroupContents();
            emptyLGContents.count = 1;
            emptyLGContents.item = "nothing";
            emptyLGContents.prob = 1;
            emptyLG.count = 1;
            emptyLG.lgTotProb = 1;
            emptyLG.contents.Add(emptyLGContents);
            _lootGroups.Add("empty", emptyLG);

            foreach (XmlElement lg in itemRefList)
            {
                //Parse through each item value in the loot.xml, Since Item will be Name or Group.
                //Get the value of the parent element to get the Lootgroup name or loot container id
                //For each loot group, parse out each and every item, so in the end we have a Loot container with all possible items, no groups listed

                //First, check the Parent Element
                string parentNode = lg.ParentNode.Name;
                if(parentNode=="lootgroup")
                {
                    //Lootgroup, process as a lootgroup
                    string lootgroupName = lg.ParentNode.Attributes["name"].Value;
                    if(!_lootGroups.ContainsKey(lootgroupName))
                    {
                        //Lootgroup doesn't exist, add it
                        M_Output.AppendText("This is a Loot Group: " + lootgroupName + Environment.NewLine);

                        lootGroup tempGroup = new lootGroup();

                        //Parse the Count Value of the lootgroup
                        string lgcount;
                        try
                        {
                            lgcount = lg.ParentNode.Attributes["count"].Value;
                        }
                        catch
                        {
                            //Null Ref, no Count value for this Parent node specified, only 1 of the items is selected, assume count = 1
                            lgcount = "1";
                        }
                        countParse tempValueLG = new countParse();
                        if(parseCount(lgcount, tempValueLG))
                        {
                            tempGroup.count = tempValueLG.count;
                            tempGroup.minCount = tempValueLG.minCount;
                            tempGroup.maxCount = tempValueLG.maxCount;
                            tempGroup.minmax = tempValueLG.minmax;
                            tempGroup.incAll = tempValueLG.incAll;
                        }
                        else
                        {
                            //Failed to parse the Count properly
                            M_Output.AppendText("Failed to parse Count=" + lgcount + "Into its values properly for lootgroup(" + lootgroupName + Environment.NewLine);
                        }
                        _lootGroups.Add(lootgroupName, tempGroup);
                    }
                    //lootgroup already exists/now exists, check to see if this is a group or item

                    //Element is an Item, add it to the LG
                    //Parse the Count Parameter if there is one:
                    lootGroupContents tempLGContents = new lootGroupContents();
                    countParse tempValue = new countParse();
                    string count = lg.GetAttribute("count");
                    string prob = lg.GetAttribute("prob");
                    string probTmpl = lg.GetAttribute("loot_prob_template");

                    if (parseCount(count, tempValue))//Parse Count into its fields, if it fails, assume Count=1 by leaving it default.
                    {
                        tempLGContents.count = tempValue.count;
                        tempLGContents.minCount = tempValue.minCount;
                        tempLGContents.maxCount = tempValue.maxCount;
                        tempLGContents.minmax = tempValue.minmax;
                    }

                    //Try to parse Prob to a Decimal, if it fails assume it is a 1 by leaving it default.
                    decimal decValue;
                    if (probTmpl != "")
                    {
                        //This is a Probability Template, not a Straight Probability, find the matching Probability Template and pull the Probability form there
                        foreach (KeyValuePair<int, Decimal> kv in _LootProbTmpl[probTmpl])
                        {
                            if (Int32.Parse(T_Player.Text) < kv.Key)
                            {
                                tempLGContents.prob = kv.Value;
                                break;
                            }
                        }
                        M_Output.AppendText("");
                    }
                    else
                    {
                        if (Decimal.TryParse(prob, out decValue))
                        {
                            tempLGContents.prob = decValue;
                        }
                    }

                    string name="";
                    if (lg.GetAttribute("name") != "")
                    {
                        //Element is an Item, use Item Name
                        name = lg.GetAttribute("name");
                        tempLGContents.item = name;
                        tempLGContents.isGroup = false;

                    }
                    else if (lg.GetAttribute("group") != "")
                    {
                        //Element is a Group, Add it to the Contents of the current lootgroup
                        //Check to see if this group already exists in the Dictionary, if not error
                        name = lg.GetAttribute("group");
                        tempLGContents.item = name;
                        tempLGContents.isGroup = true;
                    }
                    else
                    {
                        //Element is unknown
                        M_Output.AppendText("Unknown Element" + Environment.NewLine);
                    }

                    _lootGroups[lootgroupName].contents.Add(tempLGContents);
                    _lootGroups[lootgroupName].lgTotProb += tempLGContents.prob;
                    M_Output.AppendText(name + ", " + count + ", " + prob + Environment.NewLine);



                }
                else if(parentNode=="lootcontainer")
                {
                    //Loot Container, parse loot items out and figure Probabilities
                    //First parse all parts of the Loot Container itself

                    //Check to see if the Container has already been saved
                    int containerID = Int32.Parse(lg.ParentNode.Attributes["id"].Value);
                    if (!_lootContainer.ContainsKey(containerID))
                    {
                        //LootContainer doesn't exist, add it
                        M_Output.AppendText("This is a Loot Container: " + containerID + Environment.NewLine);

                        lootContainer tempContainer = new lootContainer();

                        //Attempt to lookup the Name of this Container ID, and add it if it exists
                        if (!_containerNames.TryGetValue(containerID, out tempContainer.name))
                        {
                            //Container failed to get a name, add the Container ID to the _ContainerNames, with no name
                            _containerNames.Add(containerID, "");
                        }

                        //Parse the Count Value of the lootgroup
                        tempContainer.size = lg.ParentNode.Attributes["size"].Value; ;

                        countParse tempValueLC = new countParse();
                        string countLC = lg.ParentNode.Attributes["count"].Value;

                        if (parseCount(countLC, tempValueLC))//Parse Count into its fields, if it fails, assume Count=1 by leaving it default.
                        {
                            tempContainer.count = tempValueLC.count;
                            tempContainer.minCount = tempValueLC.minCount;
                            tempContainer.maxCount = tempValueLC.maxCount;
                            tempContainer.minmax = tempValueLC.minmax;
                        }
                        else
                        {
                            //Failed to parse the Count properly
                            M_Output.AppendText("Failed to parse Count=" + countLC + "Into its values properly for lootContainer: " + containerID + Environment.NewLine);
                        }
                        _lootContainer.Add(containerID, tempContainer);
                    }

                    //Container exists/now exists, parse this Item/Group into the List

                    //Parse Count value if there is one
                    lootGroupContents tempLGContents = new lootGroupContents();
                    countParse tempValue = new countParse();
                    string count = lg.GetAttribute("count");
                    string prob = lg.GetAttribute("prob");
                    string probTmpl = lg.GetAttribute("loot_prob_template");

                    if (parseCount(count, tempValue))//Parse Count into its fields, if it fails, assume Count=1 by leaving it default.
                    {
                        tempLGContents.count = tempValue.count;
                        tempLGContents.minCount = tempValue.minCount;
                        tempLGContents.maxCount = tempValue.maxCount;
                        tempLGContents.minmax = tempValue.minmax;
                    }
                    //Try to parse Prob to a Decimal, if it fails assume it is a 1 by leaving it default.
                    decimal decValue;
                    if (probTmpl != "")
                    {
                        //This is a Probability Template, not a Straight Probability, find the matching Probability Template and pull the Probability form there
                        foreach (KeyValuePair<int, Decimal> kv in _LootProbTmpl[probTmpl])
                        {
                            if (Int32.Parse(T_Player.Text) < kv.Key)
                            {
                                tempLGContents.prob = kv.Value;
                                tempLGContents.tempProbMod = kv.Value;
                                break;
                            }
                        }
                        M_Output.AppendText("");
                    }
                    else
                    {
                        if (Decimal.TryParse(prob, out decValue))
                        {
                            tempLGContents.prob = decValue;
                            tempLGContents.tempProbMod = decValue;
                        }
                    }

                    //Determine if this is an Item, or a Group
                    string name = "";
                    if (lg.GetAttribute("name") != "")
                    {
                        //Element is an Item, use Item Name
                        name = lg.GetAttribute("name");
                        tempLGContents.item = name;
                        tempLGContents.isGroup = false;
                        _lootContainer[containerID].contents.Add(tempLGContents);

                    }
                    else if (lg.GetAttribute("group") != "")
                    {
                        //Element is a Group, For Containers, we want to parse the Group out to individual Items.
                        name = lg.GetAttribute("group");

                        //Parse through the Group for its Items
                        List<lootGroupContents> explodedContents = new List<lootGroupContents>();
                        explodedContents = explodeGroup(ref name, ref tempLGContents);
                        _lootContainer[containerID].contents.AddRange(explodedContents);
                    }
                    else
                    {
                        //Element is unknown
                        M_Output.AppendText("Unknown Element" + Environment.NewLine);
                    }


                }


            }

            //Save All Found Container Names back to the XML File to allow user to edit it and add names for next run.
            System.Xml.Linq.XElement el = new System.Xml.Linq.XElement("root", _containerNames.Select(kv => new System.Xml.Linq.XElement("ID" + kv.Key.ToString(), kv.Value)));
            string fileName = "containernames.xml";
            el.Save(fileName);


            M_Output.Clear();
            //Equate Container's Total Probability:
            foreach (KeyValuePair<int,lootContainer> lc in _lootContainer)
            {
                M_Output.AppendText("Loot Container: " + lc.Key + Environment.NewLine);
                M_Output.AppendText(Environment.NewLine);

                decimal tempProb = 0;

                //Added to break code for testing specific Containers
                if (lc.Key == 5)
                {
                    M_Output.AppendText("");
                }

                foreach (lootGroupContents lGC in lc.Value.contents)
                {
                    tempProb += lGC.tempProbMod;
                }

                decimal containerTotProb = tempProb;

                List <lootGroupContents> SortedList = lc.Value.contents.OrderBy(o => o.item).ToList();
                foreach (lootGroupContents item in SortedList)
                {
                    item.prob = item.tempProbMod / containerTotProb;
                    item.tempProbMod = 0;
                    //M_Output.AppendText("     " + item.item + ", " + Math.Round(item.tempProbMod,3) + ", " + Math.Round(item.tempProbMod/containerTotProb, 3) + Environment.NewLine);

                //Add to the list of Item Names if not already in it.
                if(!_itemNames.Contains(item.item))
                    {
                        _itemNames.Add(item.item);
                    }
                }
            }

            V_LContainer.DataSource = new BindingSource(getContainerBinding(_lootContainer), null);
            V_LContainer.DisplayMember = "Value";
            V_LContainer.ValueMember = "Key";
            V_LContainer.Refresh();
            V_LContainer.Enabled = true;

            _itemNames.Sort();

            //Parse through the Containers and add Containers Witohut names to import file.

        }
Beispiel #2
0
        private void B_Load_Click(object sender, EventArgs e)
        {
            M_Output.Clear();
            V_LContainer.DataSource = null;
            _lootGroups.Clear();
            _lootContainer.Clear();
            _LootProbTmpl.Clear();

            //Read Import file for all currently specified Container Names
            _containerNames = lootContainernames.getNames();

            XmlDocument doc = new XmlDocument();

            doc.Load(T_XMLFile.Text);

            XmlNodeList ProbTmpl = doc.GetElementsByTagName("loot");

            readProbTemplates(ProbTmpl);

            XmlNodeList itemRefList = doc.GetElementsByTagName("item");

            //manually add the "Empty" Loot Group since it is missed in the Search for "item"
            lootGroup         emptyLG         = new lootGroup();
            lootGroupContents emptyLGContents = new lootGroupContents();

            emptyLGContents.count = 1;
            emptyLGContents.item  = "nothing";
            emptyLGContents.prob  = 1;
            emptyLG.count         = 1;
            emptyLG.lgTotProb     = 1;
            emptyLG.contents.Add(emptyLGContents);

            _lootGroups.Add("empty", emptyLG);

            foreach (XmlElement lg in itemRefList)
            {
                //Parse through each item value in the loot.xml, Since Item will be Name or Group.
                //Get the value of the parent element to get the Lootgroup name or loot container id
                //For each loot group, parse out each and every item, so in the end we have a Loot container with all possible items, no groups listed

                //First, check the Parent Element
                string parentNode = lg.ParentNode.Name;
                if (parentNode == "lootgroup")
                {
                    //Lootgroup, process as a lootgroup
                    string lootgroupName = lg.ParentNode.Attributes["name"].Value;
                    if (!_lootGroups.ContainsKey(lootgroupName))
                    {
                        //Lootgroup doesn't exist, add it
                        M_Output.AppendText("This is a Loot Group: " + lootgroupName + Environment.NewLine);

                        lootGroup tempGroup = new lootGroup();

                        //Parse the Count Value of the lootgroup
                        string lgcount;
                        try
                        {
                            lgcount = lg.ParentNode.Attributes["count"].Value;
                        }
                        catch
                        {
                            //Null Ref, no Count value for this Parent node specified, only 1 of the items is selected, assume count = 1
                            lgcount = "1";
                        }
                        countParse tempValueLG = new countParse();
                        if (parseCount(lgcount, tempValueLG))
                        {
                            tempGroup.count    = tempValueLG.count;
                            tempGroup.minCount = tempValueLG.minCount;
                            tempGroup.maxCount = tempValueLG.maxCount;
                            tempGroup.minmax   = tempValueLG.minmax;
                            tempGroup.incAll   = tempValueLG.incAll;
                        }
                        else
                        {
                            //Failed to parse the Count properly
                            M_Output.AppendText("Failed to parse Count=" + lgcount + "Into its values properly for lootgroup(" + lootgroupName + Environment.NewLine);
                        }
                        _lootGroups.Add(lootgroupName, tempGroup);
                    }
                    //lootgroup already exists/now exists, check to see if this is a group or item

                    //Element is an Item, add it to the LG
                    //Parse the Count Parameter if there is one:
                    lootGroupContents tempLGContents = new lootGroupContents();
                    countParse        tempValue      = new countParse();
                    string            count          = lg.GetAttribute("count");
                    string            prob           = lg.GetAttribute("prob");
                    string            probTmpl       = lg.GetAttribute("loot_prob_template");

                    if (parseCount(count, tempValue))//Parse Count into its fields, if it fails, assume Count=1 by leaving it default.
                    {
                        tempLGContents.count    = tempValue.count;
                        tempLGContents.minCount = tempValue.minCount;
                        tempLGContents.maxCount = tempValue.maxCount;
                        tempLGContents.minmax   = tempValue.minmax;
                    }

                    //Try to parse Prob to a Decimal, if it fails assume it is a 1 by leaving it default.
                    if (!_LootProbTmpl.ContainsKey(probTmpl) && probTmpl != "")
                    {
                        //  MessageBox.Show("Missing Probability: " + probTmpl);
                    }
                    decimal decValue;
                    if (probTmpl != "" && _LootProbTmpl.ContainsKey(probTmpl))
                    {
                        //This is a Probability Template, not a Straight Probability, find the matching Probability Template and pull the Probability form there
                        foreach (KeyValuePair <int, Decimal> kv in _LootProbTmpl[probTmpl])
                        {
                            if (Int32.Parse(T_Player.Text) < kv.Key)
                            {
                                tempLGContents.prob = kv.Value;
                                break;
                            }
                        }
                        M_Output.AppendText("");
                    }
                    else
                    {
                        if (Decimal.TryParse(prob, out decValue))
                        {
                            tempLGContents.prob = decValue;
                        }
                    }

                    string name = "";
                    if (lg.GetAttribute("name") != "")
                    {
                        //Element is an Item, use Item Name
                        name = lg.GetAttribute("name");
                        tempLGContents.item    = name;
                        tempLGContents.isGroup = false;
                    }
                    else if (lg.GetAttribute("group") != "")
                    {
                        //Element is a Group, Add it to the Contents of the current lootgroup
                        //Check to see if this group already exists in the Dictionary, if not error
                        name = lg.GetAttribute("group");
                        tempLGContents.item    = name;
                        tempLGContents.isGroup = true;
                    }
                    else
                    {
                        //Element is unknown
                        M_Output.AppendText("Unknown Element" + Environment.NewLine);
                    }

                    _lootGroups[lootgroupName].contents.Add(tempLGContents);
                    _lootGroups[lootgroupName].lgTotProb += tempLGContents.prob;
                    M_Output.AppendText(name + ", " + count + ", " + prob + Environment.NewLine);
                }
                else if (parentNode == "lootcontainer")
                {
                    //Loot Container, parse loot items out and figure Probabilities
                    //First parse all parts of the Loot Container itself


                    //Check to see if the Container has already been saved
                    int containerID = Int32.Parse(lg.ParentNode.Attributes["id"].Value);
                    if (!_lootContainer.ContainsKey(containerID))
                    {
                        this.V_LContainer.Items.Add(" Loot Container: " + containerID);
                        //LootContainer doesn't exist, add it
                        M_Output.AppendText("This is a Loot Container: " + containerID + Environment.NewLine);

                        lootContainer tempContainer = new lootContainer();

                        //Attempt to lookup the Name of this Container ID, and add it if it exists
                        if (!_containerNames.TryGetValue(containerID, out tempContainer.name))
                        {
                            //Container failed to get a name, add the Container ID to the _ContainerNames, with no name
                            _containerNames.Add(containerID, "");
                        }

                        //Parse the Count Value of the lootgroup
                        tempContainer.size = lg.ParentNode.Attributes["size"].Value;;

                        countParse tempValueLC = new countParse();
                        string     countLC     = "1";

                        if (lg.ParentNode.Attributes["count"] != null)
                        {
                            countLC = lg.ParentNode.Attributes["count"].Value;
                        }

                        if (parseCount(countLC, tempValueLC))//Parse Count into its fields, if it fails, assume Count=1 by leaving it default.
                        {
                            tempContainer.count    = tempValueLC.count;
                            tempContainer.minCount = tempValueLC.minCount;
                            tempContainer.maxCount = tempValueLC.maxCount;
                            tempContainer.minmax   = tempValueLC.minmax;
                        }
                        else
                        {
                            //Failed to parse the Count properly
                            M_Output.AppendText("Failed to parse Count=" + countLC + "Into its values properly for lootContainer: " + containerID + Environment.NewLine);
                        }
                        _lootContainer.Add(containerID, tempContainer);
                    }

                    //Container exists/now exists, parse this Item/Group into the List

                    //Parse Count value if there is one
                    lootGroupContents tempLGContents = new lootGroupContents();
                    countParse        tempValue      = new countParse();
                    string            count          = lg.GetAttribute("count");
                    string            prob           = lg.GetAttribute("prob");
                    string            probTmpl       = lg.GetAttribute("loot_prob_template");

                    if (parseCount(count, tempValue))//Parse Count into its fields, if it fails, assume Count=1 by leaving it default.
                    {
                        tempLGContents.count    = tempValue.count;
                        tempLGContents.minCount = tempValue.minCount;
                        tempLGContents.maxCount = tempValue.maxCount;
                        tempLGContents.minmax   = tempValue.minmax;
                    }
                    //Try to parse Prob to a Decimal, if it fails assume it is a 1 by leaving it default.
                    decimal decValue;
                    if (probTmpl != "")
                    {
                        //This is a Probability Template, not a Straight Probability, find the matching Probability Template and pull the Probability form there
                        foreach (KeyValuePair <int, Decimal> kv in _LootProbTmpl[probTmpl])
                        {
                            if (Int32.Parse(T_Player.Text) < kv.Key)
                            {
                                tempLGContents.prob        = kv.Value;
                                tempLGContents.tempProbMod = kv.Value;
                                break;
                            }
                        }
                        M_Output.AppendText("");
                    }
                    else
                    {
                        if (Decimal.TryParse(prob, out decValue))
                        {
                            tempLGContents.prob        = decValue;
                            tempLGContents.tempProbMod = decValue;
                        }
                    }

                    //Determine if this is an Item, or a Group
                    string name = "";
                    if (lg.GetAttribute("name") != "")
                    {
                        //Element is an Item, use Item Name
                        name = lg.GetAttribute("name");
                        tempLGContents.item    = name;
                        tempLGContents.isGroup = false;
                        _lootContainer[containerID].contents.Add(tempLGContents);
                    }
                    else if (lg.GetAttribute("group") != "")
                    {
                        //Element is a Group, For Containers, we want to parse the Group out to individual Items.
                        name = lg.GetAttribute("group");

                        //Parse through the Group for its Items
                        List <lootGroupContents> explodedContents = new List <lootGroupContents>();
                        explodedContents = explodeGroup(ref name, ref tempLGContents);
                        _lootContainer[containerID].contents.AddRange(explodedContents);
                    }
                    else
                    {
                        //Element is unknown
                        M_Output.AppendText("Unknown Element" + Environment.NewLine);
                    }
                }
            }

            //Save All Found Container Names back to the XML File to allow user to edit it and add names for next run.
            System.Xml.Linq.XElement el = new System.Xml.Linq.XElement("root", _containerNames.Select(kv => new System.Xml.Linq.XElement("ID" + kv.Key.ToString(), kv.Value)));
            string fileName             = "containernames.xml";

            el.Save(fileName);


            M_Output.Clear();
            //Equate Container's Total Probability:
            foreach (KeyValuePair <int, lootContainer> lc in _lootContainer)
            {
                M_Output.AppendText("Loot Container: " + lc.Key + Environment.NewLine);
                M_Output.AppendText(Environment.NewLine);

                decimal tempProb = 0;

                //Added to break code for testing specific Containers
                if (lc.Key == 5)
                {
                    M_Output.AppendText("");
                }

                foreach (lootGroupContents lGC in lc.Value.contents)
                {
                    tempProb += lGC.tempProbMod;
                }

                decimal containerTotProb = tempProb;

                List <lootGroupContents> SortedList = lc.Value.contents.OrderBy(o => o.item).ToList();
                foreach (lootGroupContents item in SortedList)
                {
                    if (containerTotProb == 0)
                    {
                        containerTotProb = 1;
                    }
                    item.prob        = item.tempProbMod / containerTotProb;
                    item.tempProbMod = 0;
                    //M_Output.AppendText("     " + item.item + ", " + Math.Round(item.tempProbMod,3) + ", " + Math.Round(item.tempProbMod/containerTotProb, 3) + Environment.NewLine);

                    //Add to the list of Item Names if not already in it.
                    if (!_itemNames.Contains(item.item))
                    {
                        _itemNames.Add(item.item);
                    }
                }
            }

            V_LContainer.DataSource    = new BindingSource(getContainerBinding(_lootContainer), null);
            V_LContainer.DisplayMember = "Value";
            V_LContainer.ValueMember   = "Key";
            V_LContainer.Refresh();
            V_LContainer.Enabled = true;

            _itemNames.Sort();

            //Parse through the Containers and add Containers Witohut names to import file.
        }