| | 1 | | using System.Linq; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Configuration; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | public class AvatarReporterController : IAvatarReporterController |
| | 8 | | { |
| | 9 | | private string entityId; |
| | 10 | | private string avatarId; |
| | 11 | | private string lastSceneId; |
| | 12 | | private Vector2Int lastCoords; |
| | 13 | | private Vector3 lastPositionChecked; |
| 696 | 14 | | private bool isInitialReport = true; |
| | 15 | |
|
| | 16 | | private readonly IWorldState worldState; |
| | 17 | |
|
| 735 | 18 | | IReporter IAvatarReporterController.reporter { get; set; } = new Reporter(); |
| | 19 | |
|
| 696 | 20 | | public AvatarReporterController(IWorldState worldState) |
| | 21 | | { |
| 696 | 22 | | this.worldState = worldState; |
| 696 | 23 | | } |
| | 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 |
| 9 | 28 | | if (sceneId != EnvironmentSettings.AVATAR_GLOBAL_SCENE_ID) |
| 1 | 29 | | return; |
| | 30 | |
|
| 8 | 31 | | this.entityId = entityId; |
| 8 | 32 | | this.avatarId = avatarId; |
| 8 | 33 | | isInitialReport = true; |
| 8 | 34 | | lastSceneId = null; |
| 8 | 35 | | } |
| | 36 | |
|
| | 37 | | void IAvatarReporterController.ReportAvatarPosition(Vector3 position) |
| | 38 | | { |
| 16 | 39 | | if (!CanReport()) |
| 3 | 40 | | return; |
| | 41 | |
|
| 13 | 42 | | bool wasInLoadedScene = WasInLoadedScene(); |
| | 43 | |
|
| 13 | 44 | | if (wasInLoadedScene && !HasMoved(position)) |
| 1 | 45 | | return; |
| | 46 | |
|
| 12 | 47 | | Vector2Int coords = Utils.WorldToGridPosition(CommonScriptableObjects.worldOffset + position); |
| | 48 | |
|
| 12 | 49 | | if (wasInLoadedScene && lastCoords == coords) |
| | 50 | | { |
| 0 | 51 | | return; |
| | 52 | | } |
| | 53 | |
|
| 12 | 54 | | var scenePair = worldState.loadedScenes |
| 14 | 55 | | .FirstOrDefault(pair => pair.Value.sceneData.parcels != null && pair.Value.sceneData.p |
| | 56 | |
|
| 12 | 57 | | string currentSceneId = scenePair.Key; |
| | 58 | |
|
| 12 | 59 | | if (currentSceneId == lastSceneId && !isInitialReport) |
| | 60 | | { |
| 1 | 61 | | return; |
| | 62 | | } |
| | 63 | |
|
| 11 | 64 | | ((IAvatarReporterController)this).reporter.ReportAvatarSceneChange(entityId, avatarId, currentSceneId); |
| | 65 | |
|
| 11 | 66 | | lastSceneId = currentSceneId; |
| 11 | 67 | | lastCoords = coords; |
| 11 | 68 | | lastPositionChecked = position; |
| 11 | 69 | | isInitialReport = false; |
| 11 | 70 | | } |
| | 71 | |
|
| | 72 | | void IAvatarReporterController.ReportAvatarRemoved() |
| | 73 | | { |
| 690 | 74 | | if (!CanReport()) |
| 689 | 75 | | return; |
| | 76 | |
|
| 1 | 77 | | ((IAvatarReporterController)this).reporter.ReportAvatarRemoved(entityId, avatarId); |
| | 78 | |
|
| 1 | 79 | | entityId = null; |
| 1 | 80 | | avatarId = null; |
| 1 | 81 | | lastSceneId = null; |
| 1 | 82 | | isInitialReport = true; |
| 1 | 83 | | } |
| | 84 | |
|
| | 85 | | private bool CanReport() |
| | 86 | | { |
| 706 | 87 | | return !string.IsNullOrEmpty(entityId) && !string.IsNullOrEmpty(avatarId); |
| | 88 | | } |
| | 89 | |
|
| | 90 | | private bool HasMoved(Vector3 currentPosition) |
| | 91 | | { |
| 3 | 92 | | return Vector3.SqrMagnitude(currentPosition - lastPositionChecked) > 0.0001f; |
| | 93 | | } |
| | 94 | |
|
| | 95 | | private bool WasInLoadedScene() |
| | 96 | | { |
| 0 | 97 | | return !string.IsNullOrEmpty(lastSceneId); |
| | 98 | | } |
| | 99 | | } |