using System; using System.Collections.Generic; using LightJSON; using UnityEngine; namespace Databases { public sealed class CustomizableItemDatabase { public List Database { get; private set; } private static readonly CustomizableItemDatabase _instance = new CustomizableItemDatabase(); //This enum is coming from the console code and is used in server requests. Do not change the order or values! //Add only with caution. //This enum is referenced here: main\src\JD\gameplay\Managers\JD_CustomizableItemDatabase.h public enum CustomizableItemSortBy { Id = 0, Mojo, None }; private CustomizableItemDatabase() { Database = new List(); } public static CustomizableItemDatabase Instance { get { return _instance; } } public void Setup(JSON database) { if (database != null) { JSON avatars = database.GetFieldAsObject("avatars"); LoadCustomizableItems(avatars.Children, CustomizableItem.CustomizableItemType.Avatar); JSON skins = database.GetFieldAsObject("skins"); LoadCustomizableItems(skins.Children, CustomizableItem.CustomizableItemType.Skin); } } public List queryDatabase(CustomizableItem.CustomizableItemType type, int unlockType, int jDVersion) { var subset = Database.FindAll(x => x.ItemType == type && (x.JDVersion == jDVersion || jDVersion == -1) //-1 comes from the server and is used for INVALID_JDVERSION && (x.UnlockType == unlockType)); //if the jdVersion query parameter is invalid, do not test for matches on this parameter return subset; } public string getImageURI(CustomizableItem.CustomizableItemType type, int itemId) { string imageURI = getDefaultImageURI(type); CustomizableItem item = Database.Find(x => x.ItemType == type && x.CustomizableItemID == itemId); if (item != null) { imageURI = item.ImageURI; } return imageURI; } private void LoadCustomizableItems(Dictionary customizableItemMap, CustomizableItem.CustomizableItemType type) { foreach (KeyValuePair mapItem in customizableItemMap) { int customizableId = Int32.Parse(mapItem.Key); if (Database.Exists(x => x.ItemType == type && x.CustomizableItemID == customizableId)) { Debug.LogErrorFormat("The item of type {1} and id {2} already exists in the Database ", type, customizableId); } else { CustomizableItem newItem = new CustomizableItem(); newItem.ItemType = type; newItem.CustomizableItemID = customizableId; newItem.ItemStatus = (CustomizableItem.CustomizableItemStatus)mapItem.Value.GetFieldAsNumber("status"); newItem.UnlockType = (int)mapItem.Value.GetFieldAsNumber("unlockType"); newItem.JDVersion = (int)mapItem.Value.GetFieldAsNumber("jdVersion"); newItem.MojoValue = (int)mapItem.Value.GetFieldAsNumber("mojoPrice"); newItem.ImageURI = mapItem.Value.GetFieldAsString("url"); Database.Add(newItem); } } } public string getDefaultImageURI(CustomizableItem.CustomizableItemType itemType) { string imageURI = string.Empty; switch (itemType) { case CustomizableItem.CustomizableItemType.Avatar: imageURI = "local://DancerProfile/avatar_loading"; break; case CustomizableItem.CustomizableItemType.Skin: imageURI = ""; break; default: Debug.AssertFormat(false, "[CustomizableItemDatabase.getDefaultImageURI] The current itemType {1} is not handled in the switch case.", itemType); break; } return imageURI; } } }