< Summary

Class:AvatarReporterController
Assembly:AvatarReporterController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarReporterController/Implementation/AvatarReporterController.cs
Covered lines:40
Uncovered lines:2
Coverable lines:42
Total lines:99
Line coverage:95.2% (40 of 42)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AvatarReporterController(...)0%110100%
get_reporter()0%110100%
set_reporter(...)0%110100%
SetUp(...)0%220100%
ReportAvatarPosition(...)0%8.018094.44%
ReportAvatarRemoved()0%220100%
CanReport()0%220100%
HasMoved(...)0%110100%
WasInLoadedScene()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Avatar/AvatarReporterController/Implementation/AvatarReporterController.cs

#LineLine coverage
 1using System.Linq;
 2using DCL;
 3using DCL.Configuration;
 4using DCL.Helpers;
 5using UnityEngine;
 6
 7public class AvatarReporterController : IAvatarReporterController
 8{
 9    private string entityId;
 10    private string avatarId;
 11    private string lastSceneId;
 12    private Vector2Int lastCoords;
 13    private Vector3 lastPositionChecked;
 69614    private bool isInitialReport = true;
 15
 16    private readonly IWorldState worldState;
 17
 73518    IReporter IAvatarReporterController.reporter { get; set; } = new Reporter();
 19
 69620    public AvatarReporterController(IWorldState worldState)
 21    {
 69622        this.worldState = worldState;
 69623    }
 24
 25    void IAvatarReporterController.SetUp(string sceneId, string entityId, string avatarId)
 26    {
 27        // NOTE: do not report avatars that doesn't belong to the global scene
 928        if (sceneId != EnvironmentSettings.AVATAR_GLOBAL_SCENE_ID)
 129            return;
 30
 831        this.entityId = entityId;
 832        this.avatarId = avatarId;
 833        isInitialReport = true;
 834        lastSceneId = null;
 835    }
 36
 37    void IAvatarReporterController.ReportAvatarPosition(Vector3 position)
 38    {
 1639        if (!CanReport())
 340            return;
 41
 1342        bool wasInLoadedScene = WasInLoadedScene();
 43
 1344        if (wasInLoadedScene && !HasMoved(position))
 145            return;
 46
 1247        Vector2Int coords = Utils.WorldToGridPosition(CommonScriptableObjects.worldOffset + position);
 48
 1249        if (wasInLoadedScene && lastCoords == coords)
 50        {
 051            return;
 52        }
 53
 1254        var scenePair = worldState.loadedScenes
 1455                                  .FirstOrDefault(pair => pair.Value.sceneData.parcels != null && pair.Value.sceneData.p
 56
 1257        string currentSceneId = scenePair.Key;
 58
 1259        if (currentSceneId == lastSceneId && !isInitialReport)
 60        {
 161            return;
 62        }
 63
 1164        ((IAvatarReporterController)this).reporter.ReportAvatarSceneChange(entityId, avatarId, currentSceneId);
 65
 1166        lastSceneId = currentSceneId;
 1167        lastCoords = coords;
 1168        lastPositionChecked = position;
 1169        isInitialReport = false;
 1170    }
 71
 72    void IAvatarReporterController.ReportAvatarRemoved()
 73    {
 69074        if (!CanReport())
 68975            return;
 76
 177        ((IAvatarReporterController)this).reporter.ReportAvatarRemoved(entityId, avatarId);
 78
 179        entityId = null;
 180        avatarId = null;
 181        lastSceneId = null;
 182        isInitialReport = true;
 183    }
 84
 85    private bool CanReport()
 86    {
 70687        return !string.IsNullOrEmpty(entityId) && !string.IsNullOrEmpty(avatarId);
 88    }
 89
 90    private bool HasMoved(Vector3 currentPosition)
 91    {
 392        return Vector3.SqrMagnitude(currentPosition - lastPositionChecked) > 0.0001f;
 93    }
 94
 95    private bool WasInLoadedScene()
 96    {
 097        return !string.IsNullOrEmpty(lastSceneId);
 98    }
 99}