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