< 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:38
Uncovered lines:3
Coverable lines:41
Total lines:106
Line coverage:92.6% (38 of 41)
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%220100%
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
 9/// <summary>
 10/// This controller checks if an avatar entered or exited a scene by checking its position every frame
 11/// </summary>
 12public class AvatarReporterController : IAvatarReporterController
 13{
 14    private string avatarId;
 15    private string lastSceneId;
 16    private Vector2Int lastCoords;
 17    private Vector3 lastPositionChecked;
 41018    private bool isInitialReport = true;
 19
 20    private readonly IWorldState worldState;
 21
 44922    IReporter IAvatarReporterController.reporter { get; set; } = new Reporter();
 23
 41024    public AvatarReporterController(IWorldState worldState)
 25    {
 41026        this.worldState = worldState;
 41027    }
 28
 29    void IAvatarReporterController.SetUp(string sceneId, string avatarId)
 30    {
 31        // NOTE: do not report avatars that doesn't belong to the global scene
 932        if (sceneId != EnvironmentSettings.AVATAR_GLOBAL_SCENE_ID)
 133            return;
 34
 835        this.avatarId = avatarId;
 836        isInitialReport = true;
 837        lastSceneId = null;
 838    }
 39
 40    string GetcurrentSceneIdNonAlloc(Vector2Int coords)
 41    {
 1242        if (worldState.loadedScenesByCoordinate.ContainsKey(coords))
 743            return worldState.loadedScenesByCoordinate[coords];
 44
 545        return null;
 46    }
 47
 48    void IAvatarReporterController.ReportAvatarPosition(Vector3 position)
 49    {
 1650        if (!CanReport())
 351            return;
 52
 1353        bool wasInLoadedScene = WasInLoadedScene();
 54
 1355        if (wasInLoadedScene && !HasMoved(position))
 156            return;
 57
 1258        Vector2Int coords = Utils.WorldToGridPosition(CommonScriptableObjects.worldOffset + position);
 59
 1260        if (wasInLoadedScene && lastCoords == coords)
 61        {
 062            return;
 63        }
 64
 1265        string currentSceneId = GetcurrentSceneIdNonAlloc(coords);
 66
 1267        if (currentSceneId == lastSceneId && !isInitialReport)
 68        {
 169            return;
 70        }
 71
 1172        ((IAvatarReporterController)this).reporter.ReportAvatarSceneChange(avatarId, currentSceneId);
 73
 1174        lastSceneId = currentSceneId;
 1175        lastCoords = coords;
 1176        lastPositionChecked = position;
 1177        isInitialReport = false;
 1178    }
 79
 80    void IAvatarReporterController.ReportAvatarRemoved()
 81    {
 41182        if (!CanReport())
 41083            return;
 84
 185        ((IAvatarReporterController)this).reporter.ReportAvatarRemoved(avatarId);
 86
 187        avatarId = null;
 188        lastSceneId = null;
 189        isInitialReport = true;
 190    }
 91
 92    private bool CanReport()
 93    {
 094        return !string.IsNullOrEmpty(avatarId);
 95    }
 96
 97    private bool HasMoved(Vector3 currentPosition)
 98    {
 399        return Vector3.SqrMagnitude(currentPosition - lastPositionChecked) > 0.0001f;
 100    }
 101
 102    private bool WasInLoadedScene()
 103    {
 0104        return !string.IsNullOrEmpty(lastSceneId);
 105    }
 106}