| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | public class UserProfileController : MonoBehaviour |
| | 5 | | { |
| 572 | 6 | | public static UserProfileController i { get; private set; } |
| | 7 | |
|
| | 8 | | public event Action OnBaseWereablesFail; |
| | 9 | |
|
| | 10 | | private static UserProfileDictionary userProfilesCatalogValue; |
| | 11 | | private bool baseWearablesAlreadyRequested = false; |
| | 12 | |
|
| | 13 | | public static UserProfileDictionary userProfilesCatalog |
| | 14 | | { |
| | 15 | | get |
| | 16 | | { |
| 204 | 17 | | if (userProfilesCatalogValue == null) |
| | 18 | | { |
| 1 | 19 | | userProfilesCatalogValue = Resources.Load<UserProfileDictionary>("UserProfilesCatalog"); |
| | 20 | | } |
| | 21 | |
|
| 204 | 22 | | return userProfilesCatalogValue; |
| | 23 | | } |
| | 24 | | } |
| | 25 | |
|
| | 26 | | [NonSerialized] public UserProfile ownUserProfile; |
| | 27 | |
|
| | 28 | | public void Awake() |
| | 29 | | { |
| 39 | 30 | | i = this; |
| 39 | 31 | | ownUserProfile = UserProfile.GetOwnUserProfile(); |
| 39 | 32 | | } |
| | 33 | |
|
| | 34 | | public void LoadProfile(string payload) |
| | 35 | | { |
| 0 | 36 | | if (!baseWearablesAlreadyRequested) |
| | 37 | | { |
| 0 | 38 | | baseWearablesAlreadyRequested = true; |
| 0 | 39 | | CatalogController.RequestBaseWearables() |
| | 40 | | .Catch((error) => |
| | 41 | | { |
| 0 | 42 | | OnBaseWereablesFail?.Invoke(); |
| 0 | 43 | | Debug.LogError(error); |
| 0 | 44 | | }); |
| | 45 | | } |
| | 46 | |
|
| 0 | 47 | | if (payload == null) |
| 0 | 48 | | return; |
| | 49 | |
|
| 0 | 50 | | var model = JsonUtility.FromJson<UserProfileModel>(payload); |
| 0 | 51 | | ownUserProfile.UpdateData(model); |
| 0 | 52 | | userProfilesCatalog.Add(model.userId, ownUserProfile); |
| 0 | 53 | | } |
| | 54 | |
|
| 0 | 55 | | public void AddUserProfileToCatalog(string payload) { AddUserProfileToCatalog(JsonUtility.FromJson<UserProfileModel> |
| | 56 | |
|
| | 57 | | public void AddUserProfilesToCatalog(string payload) |
| | 58 | | { |
| 0 | 59 | | UserProfileModel[] items = JsonUtility.FromJson<UserProfileModel[]>(payload); |
| 0 | 60 | | int count = items.Length; |
| 0 | 61 | | for (int i = 0; i < count; ++i) |
| | 62 | | { |
| 0 | 63 | | AddUserProfileToCatalog(items[i]); |
| | 64 | | } |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | public void AddUserProfileToCatalog(UserProfileModel model) |
| | 68 | | { |
| 59 | 69 | | var userProfile = ScriptableObject.CreateInstance<UserProfile>(); |
| 59 | 70 | | userProfile.UpdateData(model); |
| 59 | 71 | | userProfilesCatalog.Add(model.userId, userProfile); |
| 59 | 72 | | } |
| | 73 | |
|
| | 74 | | public static UserProfile GetProfileByName(string targetUserName) |
| | 75 | | { |
| 10 | 76 | | foreach (var userProfile in userProfilesCatalogValue) |
| | 77 | | { |
| 4 | 78 | | if (userProfile.Value.userName.ToLower() == targetUserName.ToLower()) |
| 0 | 79 | | return userProfile.Value; |
| | 80 | | } |
| | 81 | |
|
| 1 | 82 | | return null; |
| 0 | 83 | | } |
| | 84 | |
|
| 0 | 85 | | public static UserProfile GetProfileByUserId(string targetUserId) { return userProfilesCatalogValue.Get(targetUserId |
| | 86 | |
|
| | 87 | | public void RemoveUserProfilesFromCatalog(string payload) |
| | 88 | | { |
| 0 | 89 | | string[] usernames = JsonUtility.FromJson<string[]>(payload); |
| 0 | 90 | | for (int index = 0; index < usernames.Length; index++) |
| | 91 | | { |
| 0 | 92 | | RemoveUserProfileFromCatalog(userProfilesCatalog.Get(usernames[index])); |
| | 93 | | } |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | public void RemoveUserProfileFromCatalog(UserProfile userProfile) |
| | 97 | | { |
| 0 | 98 | | if (userProfile == null) |
| 0 | 99 | | return; |
| | 100 | |
|
| 0 | 101 | | userProfilesCatalog.Remove(userProfile.userId); |
| 0 | 102 | | Destroy(userProfile); |
| 0 | 103 | | } |
| | 104 | |
|
| 14 | 105 | | public void ClearProfilesCatalog() { userProfilesCatalog?.Clear(); } |
| | 106 | | } |