< Summary

Class:AvatarModel
Assembly:AvatarModel
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Models/AvatarModel/AvatarModel.cs
Covered lines:32
Uncovered lines:47
Coverable lines:79
Total lines:196
Line coverage:40.5% (32 of 79)
Covered branches:0
Total branches:0
Covered methods:7
Total methods:9
Method coverage:77.7% (7 of 9)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AvatarModel()0%110100%
FallbackModel(...)0%110100%
GetDataFromPb(...)0%3801900%
IsListContainedIn(...)0%4.683042.86%
IsHashSetContainedIn(...)0%4.683042.86%
HaveSameWearablesAndColors(...)0%142.7922037.04%
Equals(...)0%1821300%
CopyFrom(...)0%33092.86%
GetDataFromJSON(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Models/AvatarModel/AvatarModel.cs

#LineLine coverage
 1using DCL.Helpers;
 2using Decentraland.Sdk.Ecs6;
 3using MainScripts.DCL.Components;
 4using System;
 5using System.Collections.Generic;
 6using System.Linq;
 7using UnityEngine;
 8
 9[Serializable]
 10public class AvatarModel : BaseModel
 11{
 12    [Serializable]
 13    public class AvatarEmoteEntry
 14    {
 15        public int slot;
 16        public string urn;
 17    }
 18
 19    public string id;
 20    public string name;
 21    public string bodyShape;
 22
 23    public Color skinColor;
 24    public Color hairColor;
 25    public Color eyeColor;
 26
 309027    public List<string> wearables = new ();
 309028    public HashSet<string> forceRender = new ();
 309029    public List<AvatarEmoteEntry> emotes = new ();
 30
 31    public string expressionTriggerId = null;
 309032    public long expressionTriggerTimestamp = -1;
 33    public bool talking = false;
 34
 35    public static AvatarModel FallbackModel(string name, int id) =>
 58736        new ()
 37        {
 38            id = $"{name}_{id}",
 39            name = name,
 40            bodyShape = "urn:decentraland:off-chain:base-avatars:BaseMale",
 41
 42            skinColor = new Color(0.800f, 0.608f, 0.467f),
 43            hairColor = new Color(0.596f, 0.373f, 0.216f),
 44            eyeColor = new Color(0.373f, 0.224f, 0.196f),
 45        };
 46
 47    public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel)
 48    {
 049        if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.AvatarShape)
 050            return Utils.SafeUnimplemented<AvatarModel, AvatarModel>(expected: ComponentBodyPayload.PayloadOneofCase.Ava
 51
 052        var model = new AvatarModel();
 053        if (pbModel.AvatarShape.HasId) model.id = pbModel.AvatarShape.Id;
 054        if (pbModel.AvatarShape.HasName) model.name = pbModel.AvatarShape.Name;
 055        if (pbModel.AvatarShape.HasTalking) model.talking = pbModel.AvatarShape.Talking;
 056        if (pbModel.AvatarShape.HasBodyShape) model.bodyShape = pbModel.AvatarShape.BodyShape;
 057        if (pbModel.AvatarShape.EyeColor != null) model.eyeColor = pbModel.AvatarShape.EyeColor.AsUnityColor();
 058        if (pbModel.AvatarShape.HairColor != null) model.hairColor = pbModel.AvatarShape.HairColor.AsUnityColor();
 059        if (pbModel.AvatarShape.SkinColor != null) model.skinColor = pbModel.AvatarShape.SkinColor.AsUnityColor();
 060        if (pbModel.AvatarShape.HasExpressionTriggerId) model.expressionTriggerId = pbModel.AvatarShape.ExpressionTrigge
 061        if (pbModel.AvatarShape.HasExpressionTriggerTimestamp) model.expressionTriggerTimestamp = pbModel.AvatarShape.Ex
 062        if (pbModel.AvatarShape.Wearables is { Count: > 0 }) model.wearables = pbModel.AvatarShape.Wearables.ToList();
 63
 064        if (pbModel.AvatarShape.Emotes is { Count: > 0 })
 65        {
 066            model.emotes = new List<AvatarEmoteEntry>(pbModel.AvatarShape.Emotes.Count);
 67
 068            for (var i = 0; i < pbModel.AvatarShape.Emotes.Count; i++)
 69            {
 070                if (pbModel.AvatarShape.Emotes[i] == null) continue;
 071                AvatarEmoteEntry emote = new AvatarEmoteEntry();
 72
 073                if (pbModel.AvatarShape.Emotes[i].HasSlot) emote.slot = pbModel.AvatarShape.Emotes[i].Slot;
 074                if (pbModel.AvatarShape.Emotes[i].HasUrn) emote.urn = pbModel.AvatarShape.Emotes[i].Urn;
 075                model.emotes.Add(emote);
 76            }
 77        }
 78
 079        return model;
 80    }
 81
 82    private bool IsListContainedIn(List<string> first, List<string> second)
 83    {
 484        foreach (string item in first)
 85
 86            // This contains is slow
 087            if (!second.Contains(item))
 088                return false;
 89
 290        return true;
 091    }
 92
 93    private bool IsHashSetContainedIn(HashSet<string> first, HashSet<string> second)
 94    {
 495        foreach (string item in first)
 096            if (!second.Contains(item))
 097                return false;
 98
 299        return true;
 0100    }
 101
 102    public bool HaveSameWearablesAndColors(AvatarModel other)
 103    {
 1104        if (other == null)
 0105            return false;
 106
 107        //wearables are the same
 1108        if (!(wearables.Count == other.wearables.Count
 109              && IsListContainedIn(wearables, other.wearables)
 110              && IsListContainedIn(other.wearables, wearables)))
 0111            return false;
 112
 1113        if (!(forceRender.Count == other.forceRender.Count
 114              && IsHashSetContainedIn(forceRender, other.forceRender)
 115              && IsHashSetContainedIn(other.forceRender, forceRender)))
 0116            return false;
 117
 118        //emotes are the same
 1119        if (emotes == null && other.emotes != null)
 0120            return false;
 121
 1122        if (emotes != null && other.emotes == null)
 0123            return false;
 124
 1125        if (emotes != null && other.emotes != null)
 126        {
 1127            if (emotes.Count != other.emotes.Count)
 0128                return false;
 129
 2130            foreach (AvatarEmoteEntry emote in emotes)
 131            {
 0132                var found = false;
 133
 0134                foreach (AvatarEmoteEntry t in other.emotes)
 0135                    if (t.urn == emote.urn)
 136                    {
 0137                        found = true;
 0138                        break;
 139                    }
 140
 0141                if (!found)
 0142                    return false;
 143            }
 144        }
 145
 1146        return bodyShape == other.bodyShape &&
 147               skinColor == other.skinColor &&
 148               hairColor == other.hairColor &&
 149               eyeColor == other.eyeColor;
 0150    }
 151
 152    public bool Equals(AvatarModel other)
 153    {
 0154        if (other == null) return false;
 155
 0156        bool wearablesAreEqual = wearables.All(other.wearables.Contains)
 157                                 && other.wearables.All(wearables.Contains)
 158                                 && wearables.Count == other.wearables.Count;
 159
 0160        bool forceRenderAreEqual = forceRender.All(other.forceRender.Contains)
 161                                   && other.forceRender.All(forceRender.Contains)
 162                                   && forceRender.Count == other.forceRender.Count;
 163
 0164        return id == other.id
 165               && name == other.name
 166               && bodyShape == other.bodyShape
 167               && skinColor == other.skinColor
 168               && hairColor == other.hairColor
 169               && eyeColor == other.eyeColor
 170               && expressionTriggerId == other.expressionTriggerId
 171               && expressionTriggerTimestamp == other.expressionTriggerTimestamp
 172               && wearablesAreEqual
 173               && forceRenderAreEqual;
 174    }
 175
 176    public void CopyFrom(AvatarModel other)
 177    {
 727178        if (other == null)
 0179            return;
 180
 727181        id = other.id;
 727182        name = other.name;
 727183        bodyShape = other.bodyShape;
 727184        skinColor = other.skinColor;
 727185        hairColor = other.hairColor;
 727186        eyeColor = other.eyeColor;
 727187        expressionTriggerId = other.expressionTriggerId;
 727188        expressionTriggerTimestamp = other.expressionTriggerTimestamp;
 727189        wearables = new List<string>(other.wearables);
 727190        emotes = other.emotes.Select(x => new AvatarEmoteEntry() { slot = x.slot, urn = x.urn }).ToList();
 727191        forceRender = new HashSet<string>(other.forceRender);
 727192    }
 193
 194    public override BaseModel GetDataFromJSON(string json) =>
 1195        Utils.SafeFromJson<AvatarModel>(json);
 196}