< Summary

Class:ExploreV2ComponentRealmsController
Assembly:ExploreV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/MainMenu/ExploreV2Menu/ExploreV2ComponentRealmsController.cs
Covered lines:45
Uncovered lines:5
Coverable lines:50
Total lines:113
Line coverage:90% (45 of 50)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ExploreV2ComponentRealmsController(...)0%110100%
Initialize()0%110100%
Dispose()0%110100%
UpdateRealmInfo(...)0%770100%
UpdateAvailableRealmsInfo(...)0%660100%
NeedToRefreshRealms(...)0%9.797061.54%
ServerNameForCurrentRealm()0%3.073080%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/MainMenu/ExploreV2Menu/ExploreV2ComponentRealmsController.cs

#LineLine coverage
 1// unset:none
 2using DCL;
 3using System;
 4using System.Collections.Generic;
 5using System.Linq;
 6using Variables.RealmsInfo;
 7
 8public class ExploreV2ComponentRealmsController: IDisposable
 9{
 10    private readonly DataStore_Realm realmModel;
 11    private readonly IExploreV2MenuComponentView view;
 12
 5313    internal readonly List<RealmRowComponentModel> currentAvailableRealms = new ();
 14
 5315    public ExploreV2ComponentRealmsController(DataStore_Realm realmModel, IExploreV2MenuComponentView view)
 16    {
 5317        this.realmModel = realmModel;
 5318        this.view = view;
 5319    }
 20
 21    public void Initialize()
 22    {
 5123        realmModel.realmName.OnChange += UpdateRealmInfo;
 5124        realmModel.realmsInfo.OnSet += UpdateAvailableRealmsInfo;
 25
 5126        UpdateRealmInfo(realmModel.realmName.Get());
 5127        UpdateAvailableRealmsInfo(realmModel.realmsInfo.Get());
 5128    }
 29
 30    public void Dispose()
 31    {
 5132        realmModel.realmName.OnChange -= UpdateRealmInfo;
 5133        realmModel.realmsInfo.OnSet -= UpdateAvailableRealmsInfo;
 5134    }
 35
 36    internal void UpdateRealmInfo(string realmName, string _ = "")
 37    {
 5238        if (string.IsNullOrEmpty(realmName))
 5139            return;
 40
 41        // Get the name of the current realm
 142        view.currentRealmViewer.SetRealm(realmName);
 143        view.currentRealmSelectorModal.SetCurrentRealm(realmName);
 44
 45        // Calculate number of users in the current realm
 146        var realmList = DataStore.i.realm.realmsInfo.Get()?.ToList();
 247        RealmModel currentRealmModel = realmList?.FirstOrDefault(r => r.serverName == realmName);
 148        var realmUsers = 0;
 49
 150        if (currentRealmModel != null)
 151            realmUsers = currentRealmModel.usersCount;
 52
 153        view.currentRealmViewer.SetNumberOfUsers(realmUsers);
 154    }
 55
 56    internal void UpdateAvailableRealmsInfo(IEnumerable<RealmModel> currentRealmList)
 57    {
 5358        var realmList = currentRealmList?.ToList();
 59
 5360        if (!NeedToRefreshRealms(realmList))
 5161            return;
 62
 263        currentAvailableRealms.Clear();
 64
 265        if (realmList != null)
 66        {
 267            string serverName = ServerNameForCurrentRealm();
 68
 1269            foreach (RealmModel realm in realmList)
 470                currentAvailableRealms.Add(new RealmRowComponentModel
 71                {
 72                    name = realm.serverName,
 73                    players = realm.usersCount,
 74                    isConnected = realm.serverName == serverName,
 75                });
 76        }
 77
 278        view.currentRealmSelectorModal.SetAvailableRealms(currentAvailableRealms);
 279    }
 80
 81    private bool NeedToRefreshRealms(IEnumerable<RealmModel> newRealmList)
 82    {
 5383        if (newRealmList == null)
 084            return true;
 85
 5386        var needToRefresh = false;
 87
 5388        IEnumerable<RealmModel> realmModels = newRealmList as RealmModel[] ?? newRealmList.ToArray();
 89
 5390        if (realmModels.Count() != currentAvailableRealms.Count)
 291            needToRefresh = true;
 92        else
 10293            foreach (RealmModel realm in realmModels)
 094                if (!currentAvailableRealms.Exists(x => x.name == realm.serverName && x.players == realm.usersCount))
 95                {
 096                    needToRefresh = true;
 097                    break;
 98                }
 99
 53100        return needToRefresh;
 101    }
 102
 103    private static string ServerNameForCurrentRealm()
 104    {
 2105        if (DataStore.i.realm.playerRealm.Get() != null)
 1106            return DataStore.i.realm.playerRealm.Get().serverName;
 107
 1108        if (DataStore.i.realm.playerRealmAboutConfiguration.Get() != null)
 0109            return DataStore.i.realm.playerRealmAboutConfiguration.Get().RealmName;
 110
 1111        return "";
 112    }
 113}