using System; using System.Collections.Generic; using System.IO; using System.Linq; using Server.Commands; using Server.Mobiles; using Server.Items; using Server.Engines.VendorSearching; using Server.Gumps; using Server.Network; using Server.Engines.Points; using Server.Multis; namespace Server.Engines.UOStore { public enum StoreCategory { None, Featured, Character, Equipment, Decorations, Mounts, Misc, Cart } public enum SortBy { Name, PriceLower, PriceHigher, Newest, Oldest } public static class UltimaStore { public static readonly string FilePath = Path.Combine("Saves/Misc", "UltimaStore.bin"); public static bool Enabled { get { return Configuration.Enabled; } set { Configuration.Enabled = value; } } public static List Entries { get; private set; } public static Dictionary> PendingItems { get; private set; } private static UltimaStoreContainer _UltimaStoreContainer; public static UltimaStoreContainer UltimaStoreContainer { get { if (_UltimaStoreContainer != null && _UltimaStoreContainer.Deleted) { _UltimaStoreContainer = null; } return _UltimaStoreContainer ?? (_UltimaStoreContainer = new UltimaStoreContainer()); } } static UltimaStore() { Entries = new List(); PendingItems = new Dictionary>(); PlayerProfiles = new Dictionary(); } public static void Configure() { PacketHandlers.Register(0xFA, 1, true, UOStoreRequest); CommandSystem.Register("Store", AccessLevel.Player, e => OpenStore(e.Mobile as PlayerMobile)); LoadJson(); // ⭐ REQUIRED EventSink.WorldSave += OnSave; EventSink.WorldLoad += OnLoad; PromoCodeSystem.Initialize(); } public static void Initialize() { } public static void LoadJson() { string path = Path.Combine(Core.BaseDirectory, "Data/UltimaStore.json"); if (!File.Exists(path)) { Console.WriteLine("UltimaStore: JSON file not found: " + path); return; } try { string json = File.ReadAllText(path); var root = MiniJson.Parse(json) as Dictionary; if (root == null) { Console.WriteLine("UltimaStore: JSON root is null or invalid."); return; } Entries.Clear(); foreach (var kvp in root) { string categoryName = kvp.Key; StoreCategory category; if (!Enum.TryParse(categoryName, true, out category)) category = StoreCategory.Misc; var list = kvp.Value as List; if (list == null) continue; foreach (var obj in list) { var dict = obj as Dictionary; if (dict == null) continue; // // 1. Resolve Item Type // string typeName = dict["Type"].ToString(); // Remove namespace if present if (typeName.Contains(".")) typeName = typeName.Split('.').Last(); Type itemType = ScriptCompiler.FindTypeByName(typeName); if (itemType == null) { Console.WriteLine($"UltimaStore JSON: Unknown item type '{typeName}'"); continue; } // // 2. Build Name (TextDefinition[]) // TextDefinition[] nameDefs = new TextDefinition[0]; if (dict.ContainsKey("NameLines")) { var arr = dict["NameLines"] as List; if (arr != null) { nameDefs = new TextDefinition[arr.Count]; for (int i = 0; i < arr.Count; i++) { string raw = arr[i].ToString().Trim(); if (int.TryParse(raw, out int cliloc)) nameDefs[i] = new TextDefinition(cliloc); else nameDefs[i] = new TextDefinition(raw); } } } // // 3. Tooltip // int tooltip = dict.ContainsKey("Tooltip") ? Convert.ToInt32(dict["Tooltip"]) : 0; string tooltipText = dict.ContainsKey("TooltipText") ? dict["TooltipText"].ToString() : null; // // 4. Other fields // int itemID = dict.ContainsKey("ItemID") ? Convert.ToInt32(dict["ItemID"]) : 0; int gumpID = dict.ContainsKey("GumpID") ? Convert.ToInt32(dict["GumpID"]) : 0; int hue = dict.ContainsKey("Hue") ? Convert.ToInt32(dict["Hue"]) : 0; int price = dict.ContainsKey("Price") ? Convert.ToInt32(dict["Price"]) : 0; // // 5. Create StoreEntry using the correct constructor // StoreEntry entry; if (!string.IsNullOrEmpty(tooltipText)) { entry = new StoreEntry(itemType, nameDefs, tooltipText, itemID, gumpID, hue, price, category); } else { entry = new StoreEntry(itemType, nameDefs, tooltip, itemID, gumpID, hue, price, category); } // // 6. Add to store // Entries.Add(entry); } } Console.WriteLine($"UltimaStore: Loaded {Entries.Count} entries from JSON."); } catch (Exception e) { Console.WriteLine("UltimaStore JSON Load Error: " + e); } } public static void Register(TextDefinition name, int tooltip, int itemID, int gumpID, int hue, int cost, StoreCategory cat, Func constructor = null) where T : Item { Register(typeof(T), name, tooltip, itemID, gumpID, hue, cost, cat, constructor); } public static void Register(Type itemType, TextDefinition name, int tooltip, int itemID, int gumpID, int hue, int cost, StoreCategory cat, Func constructor = null) { Register(new StoreEntry(itemType, name, tooltip, itemID, gumpID, hue, cost, cat, constructor)); } public static void Register(TextDefinition[] name, int tooltip, int itemID, int gumpID, int hue, int cost, StoreCategory cat, Func constructor = null) where T : Item { Register(typeof(T), name, tooltip, itemID, gumpID, hue, cost, cat, constructor); } public static void Register(Type itemType, TextDefinition[] name, int tooltip, int itemID, int gumpID, int hue, int cost, StoreCategory cat, Func constructor = null) { Register(new StoreEntry(itemType, name, tooltip, itemID, gumpID, hue, cost, cat, constructor)); } public static void Register(TextDefinition name, string tooltipText, int itemID, int gumpID, int hue, int cost, StoreCategory cat, Func constructor = null) where T : Item { Register(typeof(T), name, tooltipText, itemID, gumpID, hue, cost, cat, constructor); } public static void Register(Type itemType, TextDefinition name, string tooltipText, int itemID, int gumpID, int hue, int cost, StoreCategory cat, Func constructor = null) { Register(new StoreEntry(itemType, name, tooltipText, itemID, gumpID, hue, cost, cat, constructor)); } public static void Register(TextDefinition[] name, string tooltipText, int itemID, int gumpID, int hue, int cost, StoreCategory cat, Func constructor = null) where T : Item { Register(typeof(T), name, tooltipText, itemID, gumpID, hue, cost, cat, constructor); } public static void Register(Type itemType, TextDefinition[] name, string tooltipText, int itemID, int gumpID, int hue, int cost, StoreCategory cat, Func constructor = null) { Register(new StoreEntry(itemType, name, tooltipText, itemID, gumpID, hue, cost, cat, constructor)); } public static void Register(StoreEntry entry) { Entries.Add(entry); } public static bool CanSearch(Mobile m) { return m != null && m.Region.GetLogoutDelay(m) <= TimeSpan.Zero; } public static void UOStoreRequest(NetState state, PacketReader pvSrc) { OpenStore(state.Mobile as PlayerMobile); } public static void OpenStore(PlayerMobile user) { if (user == null || user.NetState == null) { return; } if (!Enabled || (Configuration.Expansion != Expansion.None && Core.Expansion < Configuration.Expansion)) { // The promo code redemption system is currently unavailable. Please try again later. user.SendLocalizedMessage(1062904); return; } if (Configuration.CurrencyImpl == CurrencyType.None) { // The promo code redemption system is currently unavailable. Please try again later. user.SendLocalizedMessage(1062904); return; } if (!user.NetState.UltimaStore) { user.SendMessage("You must update Ultima Online in order to use the in game store."); return; } if (user.AccessLevel < AccessLevel.Counselor && !CanSearch(user)) { // Before using the in game store, you must be in a safe log-out location // such as an inn or a house which has you on its Owner, Co-owner, or Friends list. user.SendLocalizedMessage(1156586); return; } if (!user.HasGump(typeof(UltimaStoreGump))) { BaseGump.SendGump(new UltimaStoreGump(user)); } } #region Constructors public static Item ConstructHairDye(Mobile m, StoreEntry entry) { var info = NaturalHairDye.Table.FirstOrDefault(x => x.Localization == entry.Name[1].Number); if (info != null) { return new NaturalHairDye(info.Type); } return null; } public static Item ConstructHaochisPigment(Mobile m, StoreEntry entry) { var info = HaochisPigment.Table.FirstOrDefault(x => x.Localization == entry.Name[1].Number); if (info != null) { return new HaochisPigment(info.Type, 50); } return null; } public static Item ConstructPigments(Mobile m, StoreEntry entry) { PigmentType type = PigmentType.None; for (int i = 0; i < PigmentsOfTokuno.Table.Length; i++) { if (PigmentsOfTokuno.Table[i][1] == entry.Name[1].Number) { type = (PigmentType)i; break; } } if (type != PigmentType.None) { return new PigmentsOfTokuno(type, 50); } return null; } public static Item ConstructEarrings(Mobile m, StoreEntry entry) { AosElementAttribute ele = AosElementAttribute.Physical; switch (entry.Name[0].Number) { case 1071092: ele = AosElementAttribute.Fire; break; case 1071093: ele = AosElementAttribute.Cold; break; case 1071094: ele = AosElementAttribute.Poison; break; case 1071095: ele = AosElementAttribute.Energy; break; } return new EarringsOfProtection(ele); } public static Item ConstructRobe(Mobile m, StoreEntry entry) { return new HoodedBritanniaRobe(entry.ItemID); } public static Item ConstructMiniHouseDeed(Mobile m, StoreEntry entry) { int label = entry.Name[1].Number; switch (label) { default: for (int i = 0; i < MiniHouseInfo.Info.Length; i++) { if (MiniHouseInfo.Info[i].LabelNumber == entry.Name[1].Number) { var type = (MiniHouseType)i; return new MiniHouseDeed(type); } } return null; case 1157015: return new MiniHouseDeed(MiniHouseType.TwoStoryWoodAndPlaster); case 1157014: return new MiniHouseDeed(MiniHouseType.TwoStoryStoneAndPlaster); } } public static Item ConstructRaisedGarden(Mobile m, StoreEntry entry) { var bag = new Bag(); bag.DropItem(new RaisedGardenDeed()); bag.DropItem(new RaisedGardenDeed()); bag.DropItem(new RaisedGardenDeed()); return bag; } public static Item ConstructLampPost(Mobile m, StoreEntry entry) { var item = new LampPost2 { Movable = true, LootType = LootType.Blessed }; return item; } public static Item ConstructForgedMetal(Mobile m, StoreEntry entry) { switch (entry.Name[1].Number) { case 1156686: return new ForgedMetalOfArtifacts(10); case 1156687: return new ForgedMetalOfArtifacts(5); } return null; } public static Item ConstructSoulstone(Mobile m, StoreEntry entry) { switch (entry.Name[0].Number) { case 1078835: return new SoulstoneToken(SoulstoneType.Blue); case 1078834: return new SoulstoneToken(SoulstoneType.Green); case 1158404: return new SoulstoneToken(SoulstoneType.Violet); } return null; } public static Item ConstructMerchantsTrinket(Mobile m, StoreEntry entry) { switch (entry.Name[0].Number) { case 1156827: return new MerchantsTrinket(false); case 1156828: return new MerchantsTrinket(true); } return null; } public static Item ConstructBOBCoverOne(Mobile m, StoreEntry entry) { return new BagOfBulkOrderCovers(12, 25); } public static Item ConstructBOBCoverTwo(Mobile m, StoreEntry entry) { return new BagOfBulkOrderCovers(1, 11); } public static Item ConstructHitchingPost(Mobile m, StoreEntry entry) { return new HitchingPost(false); } #endregion public static void AddPendingItem(Mobile m, Item item) { if (!PendingItems.TryGetValue(m, out List list)) { PendingItems[m] = list = new List(); } if (!list.Contains(item)) { list.Add(item); } UltimaStoreContainer.DropItem(item); } public static bool HasPendingItem(PlayerMobile pm) { return PendingItems.ContainsKey(pm); } public static void CheckPendingItem(Mobile m) { if (PendingItems.TryGetValue(m, out List list)) { var index = list.Count; while (--index >= 0) { if (index >= list.Count) { continue; } var item = list[index]; if (item != null) { if (m.Backpack != null && m.Alive && m.Backpack.TryDropItem(m, item, false)) { if (item is IPromotionalToken && ((IPromotionalToken)item).ItemName != null) { // A token has been placed in your backpack. Double-click it to redeem your ~1_PROMO~. m.SendLocalizedMessage(1075248, ((IPromotionalToken)item).ItemName.ToString()); } else if (item.LabelNumber > 0 || item.Name != null) { var name = item.LabelNumber > 0 ? ("#" + item.LabelNumber) : item.Name; // Your purchase of ~1_ITEM~ has been placed in your backpack. m.SendLocalizedMessage(1156844, name); } else { // Your purchased item has been placed in your backpack. m.SendLocalizedMessage(1156843); } list.RemoveAt(index); } } else { list.RemoveAt(index); } } if (list.Count == 0 && PendingItems.Remove(m)) { list.TrimExcess(); } } } public static List GetSortedList(string searchString) { var list = new List(); list.AddRange(Entries.Where(e => Insensitive.Contains(GetStringName(e.Name), searchString))); return list; } public static string GetStringName(TextDefinition[] text) { var str = string.Empty; foreach (var td in text) { if (td.Number > 0 && VendorSearch.StringList != null) { str += String.Format("{0} ", VendorSearch.StringList.GetString(td.Number)); } else if (!String.IsNullOrWhiteSpace(td.String)) { str += String.Format("{0} ", td.String); } } return str; } public static string GetStringName(TextDefinition text) { var str = text.String; if (text.Number > 0 && VendorSearch.StringList != null) { str = VendorSearch.StringList.GetString(text.Number); } return str ?? String.Empty; } public static List GetList(StoreCategory cat) { return Entries.Where(e => e.Category == cat).ToList(); } public static void SortList(List list, SortBy sort) { switch (sort) { case SortBy.Name: list.Sort((a, b) => String.CompareOrdinal(GetStringName(a.Name), GetStringName(b.Name))); break; case SortBy.PriceLower: list.Sort((a, b) => a.Price.CompareTo(b.Price)); break; case SortBy.PriceHigher: list.Sort((a, b) => b.Price.CompareTo(a.Price)); break; case SortBy.Newest: break; case SortBy.Oldest: list.Reverse(); break; } } public static int CartCount(Mobile m) { var profile = GetProfile(m, false); if (profile != null) { return profile.Cart.Count; } return 0; } public static int GetSubTotal(Dictionary cart) { if (cart == null || cart.Count == 0) { return 0; } var sub = 0.0; foreach (var kvp in cart) { sub += kvp.Key.Cost * kvp.Value; } return (int)sub; } public static int GetCurrency(Mobile m, bool sendMessage = false) { switch (Configuration.CurrencyImpl) { case CurrencyType.Sovereigns: { if (m is PlayerMobile) { return ((PlayerMobile)m).AccountSovereigns; } } break; case CurrencyType.Gold: return Banker.GetBalance(m); case CurrencyType.PointsSystem: { var sys = PointsSystem.GetSystemInstance(Configuration.PointsImpl); if (sys != null) { return (int)Math.Min(Int32.MaxValue, sys.GetPoints(m)); } } break; case CurrencyType.Custom: return Configuration.GetCustomCurrency(m); } return 0; } public static void TryPurchase(Mobile m) { var cart = GetCart(m); var total = GetSubTotal(cart); if (cart == null || cart.Count == 0 || total == 0) { // Purchase failed due to your cart being empty. m.SendLocalizedMessage(1156842); } else if (total > GetCurrency(m, true)) { if (m is PlayerMobile) { BaseGump.SendGump(new NoFundsGump((PlayerMobile)m)); } } else { var subtotal = 0; var fail = false; var remove = new List(); foreach (var entry in cart) { for (var i = 0; i < entry.Value; i++) { if (!entry.Key.Construct(m)) { fail = true; try { using (var op = File.AppendText("UltimaStoreError.log")) { op.WriteLine("Bad Constructor: {0}", entry.Key.ItemType.Name); Utility.WriteConsoleColor(ConsoleColor.Red, "[Ultima Store]: Bad Constructor: {0}", entry.Key.ItemType.Name); } } catch { } } else { remove.Add(entry.Key); subtotal += entry.Key.Cost; } } } if (subtotal > 0) { DeductCurrency(m, subtotal); } var profile = GetProfile(m); foreach (var entry in remove) { profile.RemoveFromCart(entry); } if (fail) { // Failed to process one of your items. Please check your cart and try again. m.SendLocalizedMessage(1156853); } } } /// /// Should have already passed GetCurrency /// /// /// public static int DeductCurrency(Mobile m, int amount) { switch (Configuration.CurrencyImpl) { case CurrencyType.Sovereigns: { if (m is PlayerMobile && ((PlayerMobile)m).WithdrawSovereigns(amount)) { return amount; } } break; case CurrencyType.Gold: { if (Banker.Withdraw(m, amount, true)) { return amount; } } break; case CurrencyType.PointsSystem: { var sys = PointsSystem.GetSystemInstance(Configuration.PointsImpl); if (sys != null && sys.DeductPoints(m, amount, true)) { return amount; } } break; case CurrencyType.Custom: return Configuration.DeductCustomCurrecy(m, amount); } return 0; } #region Player Persistence public static Dictionary PlayerProfiles { get; private set; } public static PlayerProfile GetProfile(Mobile m, bool create = true) { PlayerProfile profile; if ((!PlayerProfiles.TryGetValue(m, out profile) || profile == null) && create) { PlayerProfiles[m] = profile = new PlayerProfile(m); } return profile; } public static Dictionary GetCart(Mobile m) { var profile = GetProfile(m, false); if (profile != null) { return profile.Cart; } return null; } public static void OnSave(WorldSaveEventArgs e) { Persistence.Serialize(FilePath, Serialize); } public static void OnLoad() { Persistence.Deserialize(FilePath, Deserialize); } private static void Serialize(GenericWriter writer) { writer.Write(0); writer.Write(_UltimaStoreContainer); writer.Write(PendingItems.Count); foreach (var kvp in PendingItems) { writer.Write(kvp.Key); writer.WriteItemList(kvp.Value, true); } writer.Write(PlayerProfiles.Count); foreach (var pe in PlayerProfiles) { pe.Value.Serialize(writer); } } private static void Deserialize(GenericReader reader) { reader.ReadInt(); _UltimaStoreContainer = reader.ReadItem(); var count = reader.ReadInt(); for (var i = 0; i < count; i++) { var m = reader.ReadMobile(); var list = reader.ReadStrongItemList(); if (m != null && list.Count > 0) { PendingItems[m] = list; } } count = reader.ReadInt(); for (var i = 0; i < count; i++) { var pe = new PlayerProfile(reader); if (pe.Player != null) { PlayerProfiles[pe.Player] = pe; } } } #endregion } [DeleteConfirm("This is the Ultima Store item display container. You should not delete this.")] public sealed class UltimaStoreContainer : Container { private static readonly List _DisplayItems = new List(); public override bool Decays { get { return false; } } public override string DefaultName { get { return "Ultima Store Display Container"; } } public UltimaStoreContainer() : base(0) // No Draw { Movable = false; Visible = false; Internalize(); } public UltimaStoreContainer(Serial serial) : base(serial) { } public void AddDisplayItem(Item item) { if (item == null) { return; } if (!_DisplayItems.Contains(item)) { _DisplayItems.Add(item); } DropItem(item); } public Item FindDisplayItem(Type t) { var item = GetDisplayItem(t); if (item == null) { item = Loot.Construct(t); if (item != null) { AddDisplayItem(item); } } return item; } public Item GetDisplayItem(Type t) { return _DisplayItems.FirstOrDefault(x => x.GetType() == t); } public override void Serialize(GenericWriter writer) { base.Serialize(writer); writer.Write(0); writer.WriteItemList(_DisplayItems, true); } public override void Deserialize(GenericReader reader) { base.Deserialize(reader); reader.ReadInt(); var list = reader.ReadStrongItemList(); if (list.Count > 0) { Timer.DelayCall(o => o.ForEach(AddDisplayItem), list); } } } }