Esempio n. 1
0
        internal static TournamentXPandedModel GetTournamentInfo(Town town)
        {
            var t = Tournaments.Where(x => x.SettlementStringId == town.Settlement.StringId).FirstOrDefault();

            if (t == null)
            {
                t = new TournamentXPandedModel()
                {
                    SettlementStringId = town.Settlement.StringId
                };
                if (town.Settlement.HasTournament)
                {
                    var g = Campaign.Current.TournamentManager.GetTournamentGame(town);
                    t.PrizePool.Add(new PrizeItem()
                    {
                        ItemStringId = g.Prize.StringId, ItemModifierStringId = ""
                    });
                    t.SelectedPrizeStringId = g.Prize.StringId;
                }
                Tournaments.Add(t);
            }
            return(t);
        }
        public static ItemObject GenerateTournamentPrize(TournamentGame tournamentGame, TournamentXPandedModel tournamentInfo, bool keepTownPrize = false, bool regenAllPrizes = true)
        {
            if (tournamentGame == null)
            {
                return(null);
            }

            var numItemsToGet = TournamentXPSettings.Instance.NumberOfPrizeOptions;
            var allitems      = GetItemStringsRevised(tournamentGame, TournamentXPSettings.Instance.GetActivePrizeTypes());

            if (allitems.Count == 0)
            {
                MessageBox.Show("TournamentsXPanded Error:\nAlert, your prize generation filters have resulted in no valid prizes.  Consider widening your prize value range and if you are using a custom list make sure it's loaded correctly.");
                return(null);
            }

            //Add any existing items if we are filling in missing ones from an already generated pool
            if (regenAllPrizes)
            {
                tournamentInfo.SelectedPrizeStringId = "";
                tournamentInfo.PrizePool             = new List <PrizeItem>();
            }
            var pickeditems = new List <PrizeItem>();

            if (keepTownPrize && tournamentGame != null && !string.IsNullOrWhiteSpace((tournamentGame.Prize.StringId)))
            {
                pickeditems.Add(new PrizeItem()
                {
                    ItemStringId = tournamentGame.Prize.StringId, ItemModifierStringId = ""
                });
                tournamentInfo.SelectedPrizeStringId = tournamentGame.Prize.StringId;
            }
            try
            {
                if (!regenAllPrizes)
                {
                    foreach (PrizeItem existingPrize in tournamentInfo.PrizePool)
                    {
                        if (pickeditems.Where(x => x.ItemStringId == existingPrize.ItemStringId).Count() == 0)
                        {
                            pickeditems.Add(existingPrize);
                        }
                        if (allitems.Contains(existingPrize.ItemStringId))
                        {
                            allitems.Remove(existingPrize.ItemStringId);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorLog.Log("ERROR: GetTournamentPrize existingprizes\n" + ex.ToStringFull());
            }

            //If the totoal pool of unique items is less than our desired number, reduce our pool size.
            if (allitems.Count() < numItemsToGet)
            {
                numItemsToGet = allitems.Count();
            }

            while (pickeditems.Count < numItemsToGet && allitems.Count() > 0)
            {
                var randomId = allitems.GetRandomElement <string>();
                if (pickeditems.Where(x => x.ItemStringId == randomId).Count() == 0)
                {
                    pickeditems.Add(new PrizeItem()
                    {
                        ItemStringId = randomId, ItemModifierStringId = ""
                    });
                }
                allitems.Remove(randomId);
            }
            tournamentInfo.PrizePool = new List <PrizeItem>();

            foreach (var pickedItem in pickeditems)
            {
                ItemModifier itemModifier         = null;
                ItemObject   pickedPrize          = null;
                string       itemModifierStringId = "";
                try
                {
                    pickedPrize = Game.Current.ObjectManager.GetObject <ItemObject>(pickedItem.ItemStringId);
                }
                catch (Exception ex)
                {
                    ErrorLog.Log("Error getting object StringId: " + pickedItem.ItemStringId + "\n" + ex.ToStringFull());
                }

                if (pickedPrize != null)
                {
                    if (TournamentXPSettings.Instance.EnableItemModifiersForPrizes && pickedPrize.ItemType != ItemObject.ItemTypeEnum.Thrown && pickedPrize.ItemType != ItemObject.ItemTypeEnum.Arrows)
                    {
                        try
                        {
                            if (MBRandom.RandomFloatRanged(100f) < 50f)
                            {
                                var ee = GetEquipmentWithModifier(pickedPrize, GetProsperityModifier(tournamentGame.Town.Settlement));
                                itemModifier = ee.ItemModifier;
                                if (itemModifier != null)
                                {
                                    itemModifierStringId = itemModifier.StringId;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            ErrorLog.Log("Error in GetEquipmentWithModifier\nItem:" + pickedPrize.StringId + "\n" + ex.ToStringFull());
                        }
                    }
                    try
                    {
                        tournamentInfo.PrizePool.Add(new PrizeItem()
                        {
                            ItemStringId = pickedPrize.StringId, ItemModifierStringId = itemModifierStringId
                        });
                    }
                    catch (Exception ex)
                    {
                        ErrorLog.Log("Error adding equipment to prizepool.\n" + ex.ToStringFull());
                    }
                }
                else
                {
                    MessageBox.Show("Invalid Item detected.  Please remove: " + pickedItem.ItemStringId + " from your list of custom items. Ignoring this item and continuing.");
                }
            }

            if (!keepTownPrize)
            {
                if (tournamentInfo.PrizePool.Count > 0)
                {
                    tournamentInfo.SelectedPrizeStringId = tournamentInfo.PrizePool.First().ItemStringId;
                    //    SetTournamentSelectedPrize(tournamentGame, tournamentInfo.SelectPrizeItemRosterElement?.EquipmentElement.Item);
                }
            }
            try
            {
                return(tournamentInfo.SelectedPrizeItem.ToItemRosterElement().EquipmentElement.Item);
            }
            catch
            {
                //something went wrong generating a valid prize somewhere up above. hopefully earlier try catches will provide info, this is just a final failsafe to let it revert to default behavior.
            }
            return(null);
        }