Example #1
0
        public async Task <AvatarLayer> AddLayerAsync(AvatarLayer layer)
        {
            VerifyManagementPermission();
            layer.SiteId = GetCurrentSiteId();
            var currentLayer = await _avatarLayerRepository.AddSaveAsync(
                GetClaimId(ClaimType.UserId), layer);

            foreach (var text in layer.Texts)
            {
                var languageId = await _languageService.GetLanguageIdAsync(text.Language);

                if (languageId != default)
                {
                    await _avatarLayerRepository.AddAvatarLayerTextAsync(currentLayer.Id,
                                                                         languageId,
                                                                         text);
                }
            }
            await _avatarLayerRepository.SaveAsync();

            var layerData = _avatarLayerRepository.GetNameAndLabelByLanguageId(currentLayer.Id,
                                                                               await _languageService.GetDefaultLanguageIdAsync());

            currentLayer.Name = layerData["Name"];
            return(currentLayer);
        }
Example #2
0
        public async Task <ICollection <AvatarLayer> > GetUserWardrobeAsync()
        {
            var activeUserId       = GetActiveUserId();
            var siteId             = GetCurrentSiteId();
            var currentCultureName = _userContextProvider.GetCurrentCulture()?.Name;
            var currentLanguageId  = currentCultureName != null ?
                                     await _languageService.GetLanguageIdAsync(currentCultureName) :
                                     await _languageService.GetDefaultLanguageIdAsync();

            var layers = await _avatarLayerRepository.GetAllWithColorsAsync(siteId);

            if (layers.Count > 0)
            {
                var userAvatar = await _avatarElementRepository.GetUserAvatarAsync(activeUserId);

                var bundleItems = new List <AvatarItem>();
                if (userAvatar.Count == 0)
                {
                    bundleItems = (await _avatarBundleRepository.GetRandomDefaultBundleAsync(siteId)).ToList();
                }
                var filePath = _pathResolver.ResolveContentPath($"site{siteId}/avatars/");
                foreach (var layer in layers)
                {
                    var layerText = _avatarLayerRepository
                                    .GetNameAndLabelByLanguageId(layer.Id, currentLanguageId);
                    layer.Name        = layerText["Name"];
                    layer.RemoveLabel = layerText["RemoveLabel"];
                    layer.AvatarItems = await _avatarItemRepository
                                        .GetUserItemsByLayerAsync(activeUserId, layer.Id);

                    layer.Icon = _pathResolver.ResolveContentPath(layer.Icon);

                    if (userAvatar.Count > 0)
                    {
                        var layerSelection = userAvatar.SingleOrDefault(_ =>
                                                                        _.AvatarItem.AvatarLayerId == layer.Id);
                        if (layerSelection != null)
                        {
                            layer.SelectedItem  = layerSelection.AvatarItemId;
                            layer.SelectedColor = layerSelection.AvatarColorId;
                            layer.FilePath      = _pathResolver.ResolveContentPath(layerSelection.Filename);
                        }
                        else if (layer.AvatarColors.Count > 0)
                        {
                            layer.SelectedColor = layer.AvatarColors
                                                  .ElementAt(new Random().Next(0, layer.AvatarColors.Count)).Id;
                        }
                    }
                    else
                    {
                        if (layer.AvatarColors.Count > 0)
                        {
                            layer.SelectedColor = layer.AvatarColors
                                                  .ElementAt(new Random().Next(0, layer.AvatarColors.Count)).Id;
                        }

                        if (bundleItems.Count > 0)
                        {
                            var layerSelection = bundleItems.SingleOrDefault(_ =>
                                                                             _.AvatarLayerId == layer.Id);
                            if (layerSelection != null)
                            {
                                layer.SelectedItem = layerSelection.Id;
                            }
                        }

                        if (!layer.SelectedItem.HasValue && !layer.CanBeEmpty)
                        {
                            layer.SelectedItem = layer.AvatarItems.First().Id;
                        }

                        if (layer.SelectedItem.HasValue)
                        {
                            var fileName = "item";
                            if (layer.SelectedColor.HasValue)
                            {
                                fileName += $"_{layer.SelectedColor}";
                            }
                            fileName      += ".png";
                            layer.FilePath = Path.Combine(filePath, $"layer{layer.Id}", $"item{layer.SelectedItem}", fileName);
                        }
                    }
                }
            }
            return(layers.Where(_ => _.AvatarItems.Count > 0).ToList());
        }