public ChoiceViewModel(PlayerModel model, String uniqueID, int amount, Feature feature, IEnumerable <IXML> options, bool multiple = false) { Model = model; UniqueID = uniqueID; Amount = amount; Feature = feature; Options = options; Multiple = multiple; Name = Feature?.Name; OnSelect = new Command((par) => { if (par is ChoiceOption co) { Model.MakeHistory(); int offset = Offset; if (co.Selected) { bool NeedsUpdate = Taken == Amount; string val = co.NameWithSource; for (int c = 0; c < Amount; c++) { String counter = ""; if (c + offset > 0) { counter = "_" + (c + offset).ToString(); } Choice cho = Model.Context.Player.GetChoice(UniqueID + counter); if (cho != null && ConfigManager.SourceInvariantComparer.Equals(cho.Value, val)) { co.Selected = false; Model.Context.Player.RemoveChoice(cho.UniqueID); Model.Save(); Model.FirePlayerChanged(); break; } } } else { for (int c = 0; c < Amount; c++) { String counter = ""; if (c + offset > 0) { counter = "_" + (c + offset).ToString(); } Choice cho = Model.Context.Player.GetChoice(UniqueID + counter); if (cho == null) { co.Selected = true; Model.Context.Player.SetChoice(UniqueID + counter, co.NameWithSource); Model.Save(); Model.FirePlayerChanged(); break; } } } } }); UpdateOptions(); }
public ShopViewModel(PlayerModel playerViewModel) { Model = playerViewModel; Select = new Command(async(par) => { if (par is Item i) { await Model.ShopNavigation.PushAsync(new BuyAddPage(this, i)); } else if (par is MagicProperty mp) { if (mp.Base == null || mp.Base == "") { Model.MakeHistory(); Model.Context.Player.Possessions.Add(new Possession(Model.Context, (Item)null, mp)); Model.Save(); Model.FirePlayerChanged(); await Model.ShopNavigation.PopAsync(); } else { await Model.ShopNavigation.PushAsync(new AddToItemPage(this, mp)); } } else if (par is Feature f) { if (!Model.Context.Player.Boons.Exists(b => ConfigManager.SourceInvariantComparer.Equals(b, f.Name + " " + ConfigManager.SourceSeperator + " " + f.Source))) { Model.MakeHistory(); Model.Context.Player.Boons.Add(f.Name + " " + ConfigManager.SourceSeperator + " " + f.Source); Model.Save(); Model.FirePlayerChanged(); Select.ChangeCanExecute(); await Model.ShopNavigation.PopAsync(); } } else if (par is Spell s) { if (Name == "Spell Scrolls") { Model.MakeHistory(); Model.Context.Player.Items.Add(s.Name + " " + ConfigManager.SourceSeperator + " " + s.Source); Model.Save(); Model.RefreshItems.Execute(null); await Model.ShopNavigation.PopAsync(); } else { await Model.ShopNavigation.PushAsync(new AddToSpellbookPage(this, s)); } } }, (par) => { if (par is Feature f) { return(!Model.Context.Player.Boons.Exists(b => ConfigManager.SourceInvariantComparer.Equals(b, f.Name + " " + ConfigManager.SourceSeperator + " " + f.Source))); } else if (par is Spell s) { if (Name != "Spell Scrolls") { return((from sc in Model.Context.Player.GetFeatures() where sc is SpellcastingFeature && ((SpellcastingFeature)sc).Preparation == PreparationMode.Spellbook && Utils.Matches(Model.Context, s, ((SpellcastingFeature)sc).PrepareableSpells, ((SpellcastingFeature)sc).SpellcastingID) && !Model.Context.Player.GetSpellcasting(((SpellcastingFeature)sc).SpellcastingID).GetSpellbook(Model.Context.Player, Model.Context).Contains(s) select((SpellcastingFeature)sc)).Count() > 0); } } return(true); }); }