using System; using System.Collections.Generic; using System.IO; using Server; using Server.Items; using Server.Mobiles; using Server.Commands; namespace Server.Engines.UOStore { public static class PromoCodeSystem { private static readonly string FilePath = Path.Combine(Core.BaseDirectory, "Data/PromoCodes.json"); // Category → List of codes public static Dictionary> Codes = new Dictionary>(); public static void Initialize() { Load(); CommandSystem.Register("PromoReload", AccessLevel.Administrator, e => { Load(); e.Mobile.SendMessage("Promo codes reloaded."); }); CommandSystem.Register("PromoSave", AccessLevel.Administrator, e => { Save(); e.Mobile.SendMessage("Promo codes saved."); }); } #region JSON LOAD/SAVE public static void Load() { try { if (!File.Exists(FilePath)) { Console.WriteLine("[PromoCodes] No JSON file found, creating empty file."); Save(); return; } string json = File.ReadAllText(FilePath); var root = MiniJson.Parse(json) as Dictionary; Codes.Clear(); if (root != null) { foreach (var kvp in root) { var list = new List(); foreach (var code in (List)kvp.Value) list.Add(code.ToString()); Codes[kvp.Key] = list; } } Console.WriteLine("[PromoCodes] Loaded " + Codes.Count + " categories."); } catch (Exception ex) { Console.WriteLine("[PromoCodes] ERROR loading JSON: " + ex); } } public static void Save() { try { using (var sw = new StreamWriter(FilePath)) { sw.WriteLine("{"); int ci = 0; foreach (var kvp in Codes) { sw.Write($" \"{kvp.Key}\": ["); for (int i = 0; i < kvp.Value.Count; i++) { sw.Write($"\"{kvp.Value[i]}\""); if (i < kvp.Value.Count - 1) sw.Write(", "); } sw.Write("]"); if (ci < Codes.Count - 1) sw.Write(","); sw.WriteLine(); ci++; } sw.WriteLine("}"); } Console.WriteLine("[PromoCodes] Saved promo code JSON."); } catch (Exception ex) { Console.WriteLine("[PromoCodes] ERROR saving JSON: " + ex); } } #endregion #region Redemption public static bool TryRedeem(PlayerMobile pm, string code) { foreach (var kvp in Codes) { string category = kvp.Key; List list = kvp.Value; if (list.Contains(code)) { list.Remove(code); Save(); GiveReward(pm, category); return true; } } return false; } private static void GiveReward(PlayerMobile pm, string category) { switch (category) { case "MountTokens": pm.AddToBackpack(new EtherealHorse()); pm.SendMessage(0x55, "You received an Ethereal Mount!"); break; case "StarterPack": pm.AddToBackpack(new BagOfReagents()); pm.AddToBackpack(new Gold(5000)); pm.SendMessage(0x55, "You received a Starter Pack!"); break; case "HolidayGifts": pm.AddToBackpack(new GiftBox()); pm.SendMessage(0x55, "Happy Holidays!"); break; case "SoulStoneTokens": pm.AddToBackpack(new SoulstoneToken()); pm.SendMessage(0x55, "You received a Soul Stone Token!"); break; default: pm.SendMessage(0x22, "Unknown promo reward category: " + category); break; } } #endregion } #region Mini JSON Parser (No Dependencies) public static class MiniJson { public static object Parse(string json) { return new JsonParser(json).ParseValue(); } private class JsonParser { private readonly string _json; private int _i; public JsonParser(string json) { _json = json; } public object ParseValue() { Skip(); if (_i >= _json.Length) return null; char c = _json[_i]; if (c == '{') return ParseObject(); if (c == '[') return ParseArray(); if (c == '"') return ParseString(); if (char.IsDigit(c) || c == '-') return ParseNumber(); if (Match("true")) return true; if (Match("false")) return false; if (Match("null")) return null; throw new Exception("Invalid JSON at " + _i); } private Dictionary ParseObject() { var dict = new Dictionary(); _i++; // skip { while (true) { Skip(); if (_json[_i] == '}') { _i++; break; } string key = ParseString(); Skip(); _i++; // skip : object val = ParseValue(); dict[key] = val; Skip(); if (_json[_i] == ',') { _i++; continue; } if (_json[_i] == '}') { _i++; break; } } return dict; } private List ParseArray() { var list = new List(); _i++; // skip [ while (true) { Skip(); if (_json[_i] == ']') { _i++; break; } list.Add(ParseValue()); Skip(); if (_json[_i] == ',') { _i++; continue; } if (_json[_i] == ']') { _i++; break; } } return list; } private string ParseString() { _i++; // skip " int start = _i; while (_json[_i] != '"') _i++; string s = _json.Substring(start, _i - start); _i++; // skip closing " return s; } private double ParseNumber() { int start = _i; while (_i < _json.Length && (char.IsDigit(_json[_i]) || _json[_i] == '.' || _json[_i] == '-')) { _i++; } return double.Parse(_json.Substring(start, _i - start)); } private void Skip() { while (_i < _json.Length && char.IsWhiteSpace(_json[_i])) _i++; } private bool Match(string s) { if (_json.Substring(_i).StartsWith(s)) { _i += s.Length; return true; } return false; } } } #endregion }