< 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:41
Uncovered lines:3
Coverable lines:44
Total lines:110
Line coverage:93.1% (41 of 44)
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%
GetcurrentSceneIdNonAlloc(...)0%440100%
ReportAvatarPosition(...)0%8.018094.12%
ReportAvatarRemoved()0%220100%
CanReport()0%2100%
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.Collections.Generic;
 2using System.Linq;
 3using DCL;
 4using DCL.Configuration;
 5using DCL.Controllers;
 6using DCL.Helpers;
 7using UnityEngine;
 8
 9public class AvatarReporterController : IAvatarReporterController
 10{
 11    private string avatarId;
 12    private string lastSceneId;
 13    private Vector2Int lastCoords;
 14    private Vector3 lastPositionChecked;
 40415    private bool isInitialReport = true;
 16
 17    private readonly IWorldState worldState;
 18
 44319    IReporter IAvatarReporterController.reporter { get; set; } = new Reporter();
 20
 40421    public AvatarReporterController(IWorldState worldState)
 22    {
 40423        this.worldState = worldState;
 40424    }
 25
 26    void IAvatarReporterController.SetUp(string sceneId, string avatarId)
 27    {
 28        // NOTE: do not report avatars that doesn't belong to the global scene
 929        if (sceneId != EnvironmentSettings.AVATAR_GLOBAL_SCENE_ID)
 130            return;
 31
 832        this.avatarId = avatarId;
 833        isInitialReport = true;
 834        lastSceneId = null;
 835    }
 36
 37    string GetcurrentSceneIdNonAlloc(Vector2Int coords)
 38    {
 4539        foreach (KeyValuePair<string, IParcelScene> parcelScene in worldState.loadedScenes)
 40        {
 1441            var parcels = parcelScene.Value.sceneData.parcels;
 42
 1443            if (parcels != null && parcels.Contains(coords))
 44            {
 745                return parcelScene.Key;
 46            }
 47        }
 48
 549        return null;
 750    }
 51
 52    void IAvatarReporterController.ReportAvatarPosition(Vector3 position)
 53    {
 1654        if (!CanReport())
 355            return;
 56
 1357        bool wasInLoadedScene = WasInLoadedScene();
 58
 1359        if (wasInLoadedScene && !HasMoved(position))
 160            return;
 61
 1262        Vector2Int coords = Utils.WorldToGridPosition(CommonScriptableObjects.worldOffset + position);
 63
 1264        if (wasInLoadedScene && lastCoords == coords)
 65        {
 066            return;
 67        }
 68
 1269        string currentSceneId = GetcurrentSceneIdNonAlloc(coords);
 70
 1271        if (currentSceneId == lastSceneId && !isInitialReport)
 72        {
 173            return;
 74        }
 75
 1176        ((IAvatarReporterController)this).reporter.ReportAvatarSceneChange(avatarId, currentSceneId);
 77
 1178        lastSceneId = currentSceneId;
 1179        lastCoords = coords;
 1180        lastPositionChecked = position;
 1181        isInitialReport = false;
 1182    }
 83
 84    void IAvatarReporterController.ReportAvatarRemoved()
 85    {
 40586        if (!CanReport())
 40487            return;
 88
 189        ((IAvatarReporterController)this).reporter.ReportAvatarRemoved(avatarId);
 90
 191        avatarId = null;
 192        lastSceneId = null;
 193        isInitialReport = true;
 194    }
 95
 96    private bool CanReport()
 97    {
 098        return !string.IsNullOrEmpty(avatarId);
 99    }
 100
 101    private bool HasMoved(Vector3 currentPosition)
 102    {
 3103        return Vector3.SqrMagnitude(currentPosition - lastPositionChecked) > 0.0001f;
 104    }
 105
 106    private bool WasInLoadedScene()
 107    {
 0108        return !string.IsNullOrEmpty(lastSceneId);
 109    }
 110}