From 0f54e9216764aaf8617baacd54f3875036cc7b68 Mon Sep 17 00:00:00 2001 From: John Graves <40397054+Cepheid-UK@users.noreply.github.com> Date: Tue, 12 Jul 2022 23:21:31 +0300 Subject: [PATCH] [W3C-93] New race win icons (#178) * [W3C-93] New race win icons * use test db --- .../CommonValueObjects/AvatarCategory.cs | 4 +- .../AvatarCategoryToMaxPictureId.cs | 16 ++ .../PersonalSettings/PersonalSetting.cs | 138 ++++++++++++++---- .../PersonalSettings/ProfilePicture.cs | 8 +- .../PersonalSettings/RaceToMaxPicture.cs | 16 -- .../PlayerProfiles/PlayerOverallStats.cs | 5 + .../PersonalSettings/PersonalSettingsTests.cs | 8 +- 7 files changed, 143 insertions(+), 52 deletions(-) create mode 100644 W3ChampionsStatisticService/PersonalSettings/AvatarCategoryToMaxPictureId.cs delete mode 100644 W3ChampionsStatisticService/PersonalSettings/RaceToMaxPicture.cs diff --git a/W3C.Domain/CommonValueObjects/AvatarCategory.cs b/W3C.Domain/CommonValueObjects/AvatarCategory.cs index 7d35620b..41eaaf64 100644 --- a/W3C.Domain/CommonValueObjects/AvatarCategory.cs +++ b/W3C.Domain/CommonValueObjects/AvatarCategory.cs @@ -8,6 +8,8 @@ public enum AvatarCategory NE = 4, UD = 8, Total = 16, - Special = 32 + Special = 32, + Starter = 64, + Tournament = 128, } } diff --git a/W3ChampionsStatisticService/PersonalSettings/AvatarCategoryToMaxPictureId.cs b/W3ChampionsStatisticService/PersonalSettings/AvatarCategoryToMaxPictureId.cs new file mode 100644 index 00000000..1d6a5789 --- /dev/null +++ b/W3ChampionsStatisticService/PersonalSettings/AvatarCategoryToMaxPictureId.cs @@ -0,0 +1,16 @@ +using W3C.Domain.CommonValueObjects; + +namespace W3ChampionsStatisticService.PersonalSettings +{ + public class AvatarCategoryToMaxPictureId + { + public AvatarCategory AvatarType { get; } + public long Max { get; } + + public AvatarCategoryToMaxPictureId(AvatarCategory avatarType, long max) + { + AvatarType = avatarType; + Max = max; + } + } +} \ No newline at end of file diff --git a/W3ChampionsStatisticService/PersonalSettings/PersonalSetting.cs b/W3ChampionsStatisticService/PersonalSettings/PersonalSetting.cs index db515556..9593c6a0 100644 --- a/W3ChampionsStatisticService/PersonalSettings/PersonalSetting.cs +++ b/W3ChampionsStatisticService/PersonalSettings/PersonalSetting.cs @@ -59,14 +59,25 @@ public bool SetProfilePicture(SetPictureCommand cmd) { bool isValid = false; - if (cmd.avatarCategory == AvatarCategory.Special) + switch (cmd.avatarCategory) { - isValid = SpecialPictures == null ? false : SpecialPictures.Any(x => x.PictureId == cmd.pictureId); - } - else - { - var winsPerRace = RaceWins?.GetWinsPerRace((Race)cmd.avatarCategory); - isValid = winsPerRace >= PictureRange.FirstOrDefault(p => p.PictureId == cmd.pictureId)?.NeededWins; + case AvatarCategory.Starter: + isValid = true; + break; + + case AvatarCategory.Special: + isValid = SpecialPictures == null ? false : SpecialPictures.Any(x => x.PictureId == cmd.pictureId); + break; + + case AvatarCategory.Total: + var totalWins = RaceWins?.GetTotalWins(); + isValid = totalWins >= TotalPicturerange.FirstOrDefault(p => p.PictureId == cmd.pictureId)?.NeededWins; + break; + + default: + var winsPerRace = RaceWins?.GetWinsPerRace((Race)cmd.avatarCategory); + isValid = winsPerRace >= RacePictureRange.FirstOrDefault(p => p.PictureId == cmd.pictureId)?.NeededWins; + break; } if (isValid) @@ -87,35 +98,34 @@ public void UpdateSpecialPictures(SpecialPicture[] specialPictures) SpecialPictures = specialPictures; } - public List PickablePictures => new List + public List PickablePictures => new List { - new RaceToMaxPicture(Race.HU, GetMaxOf(RaceWins.GetWinsPerRace(Race.HU)) ), - new RaceToMaxPicture(Race.OC, GetMaxOf(RaceWins.GetWinsPerRace(Race.OC)) ), - new RaceToMaxPicture(Race.NE, GetMaxOf(RaceWins.GetWinsPerRace(Race.NE)) ), - new RaceToMaxPicture(Race.UD, GetMaxOf(RaceWins.GetWinsPerRace(Race.UD)) ), - new RaceToMaxPicture(Race.RnD, GetMaxOf(RaceWins.GetWinsPerRace(Race.RnD)) ) + new AvatarCategoryToMaxPictureId(AvatarCategory.HU, GetMaxPictureIdForRace(Race.HU)), + new AvatarCategoryToMaxPictureId(AvatarCategory.OC, GetMaxPictureIdForRace(Race.OC)), + new AvatarCategoryToMaxPictureId(AvatarCategory.NE, GetMaxPictureIdForRace(Race.NE)), + new AvatarCategoryToMaxPictureId(AvatarCategory.UD, GetMaxPictureIdForRace(Race.UD)), + new AvatarCategoryToMaxPictureId(AvatarCategory.RnD, GetMaxPictureIdForRace(Race.RnD)), + new AvatarCategoryToMaxPictureId(AvatarCategory.Total, GetMaxPictureIdForAllWins()), }; - private long GetMaxOf(long getWinsPerRace) + private long GetMaxPictureIdForRace(Race race) { - return PictureRange.Where(r => r.NeededWins <= getWinsPerRace).Max(r => r.PictureId); + var minimumWinsNeededForRaceIcon = RacePictureRange.First().NeededWins; + var raceWinsForRace = RaceWins.GetWinsPerRace(race); + + if (raceWinsForRace < minimumWinsNeededForRaceIcon) return 0; + + return RacePictureRange + .Where(r => r.NeededWins <= raceWinsForRace) + .Max(r => r.PictureId); } - [BsonIgnore] - public List PictureRange => new List + private long GetMaxPictureIdForAllWins() { - new WinsToPictureId(0, 0), - new WinsToPictureId(1, 5), - new WinsToPictureId(2, 20), - new WinsToPictureId(3, 70), - new WinsToPictureId(4, 150), - new WinsToPictureId(5, 250), - new WinsToPictureId(6, 400), - new WinsToPictureId(7, 600), - new WinsToPictureId(8, 900), - new WinsToPictureId(9, 1200), - new WinsToPictureId(10, 1500) - }; + return TotalPicturerange + .Where(r => r.NeededWins <= RaceWins.GetTotalWins()) + .Max(r => r.PictureId); + } public void Update(PersonalSettingsDTO dto) { @@ -132,5 +142,75 @@ public void Update(PersonalSettingsDTO dto) } public DateTimeOffset LastUpdated { get; set; } + + [BsonIgnore] + public List RacePictureRange => new List + { + new WinsToPictureId(1, 5), + new WinsToPictureId(2, 10), + new WinsToPictureId(3, 25), + new WinsToPictureId(4, 50), + new WinsToPictureId(5, 100), + new WinsToPictureId(6, 150), + new WinsToPictureId(7, 250), + new WinsToPictureId(8, 350), + new WinsToPictureId(9, 500), + new WinsToPictureId(10, 750), + new WinsToPictureId(11, 1000), + new WinsToPictureId(12, 1250), + new WinsToPictureId(13, 1500), + new WinsToPictureId(14, 1750), + new WinsToPictureId(15, 2000), + new WinsToPictureId(16, 2500), + new WinsToPictureId(17, 3500), + new WinsToPictureId(18, 5000) + }; + + [BsonIgnore] + public List TotalPicturerange => new List + { + new WinsToPictureId(1, 15), + new WinsToPictureId(2, 30), + new WinsToPictureId(3, 75), + new WinsToPictureId(4, 150), + new WinsToPictureId(5, 300), + new WinsToPictureId(6, 450), + new WinsToPictureId(7, 750), + new WinsToPictureId(8, 1000), + new WinsToPictureId(9, 1500), + new WinsToPictureId(10, 2250), + new WinsToPictureId(11, 3000), + new WinsToPictureId(12, 3750), + new WinsToPictureId(13, 4500), + new WinsToPictureId(14, 5250), + new WinsToPictureId(15, 6000), + new WinsToPictureId(16, 7500), + new WinsToPictureId(17, 10000), + new WinsToPictureId(18, 15000) + }; + + [BsonIgnore] + public List TournamentPicturerange => new List + // for future use with autotours + { + new WinsToPictureId(1, 1), + new WinsToPictureId(2, 5), + new WinsToPictureId(3, 10), + new WinsToPictureId(4, 20), + new WinsToPictureId(5, 30), + new WinsToPictureId(6, 50), + new WinsToPictureId(7, 75), + new WinsToPictureId(8, 100), + new WinsToPictureId(9, 125), + new WinsToPictureId(10, 150), + new WinsToPictureId(11, 200), + new WinsToPictureId(12, 300), + new WinsToPictureId(13, 400), + new WinsToPictureId(14, 500), + new WinsToPictureId(15, 750), + new WinsToPictureId(16, 1000), + new WinsToPictureId(17, 1250), + new WinsToPictureId(18, 1500) + }; } } \ No newline at end of file diff --git a/W3ChampionsStatisticService/PersonalSettings/ProfilePicture.cs b/W3ChampionsStatisticService/PersonalSettings/ProfilePicture.cs index df75823d..9c00977c 100644 --- a/W3ChampionsStatisticService/PersonalSettings/ProfilePicture.cs +++ b/W3ChampionsStatisticService/PersonalSettings/ProfilePicture.cs @@ -1,4 +1,5 @@ -using W3C.Domain.CommonValueObjects; +using System; +using W3C.Domain.CommonValueObjects; namespace W3ChampionsStatisticService.PersonalSettings { @@ -6,10 +7,11 @@ public class ProfilePicture { public static ProfilePicture Default() { + var random = new Random(); return new ProfilePicture() { - Race = AvatarCategory.Total, - PictureId = 0 + Race = AvatarCategory.Starter, + PictureId = random.Next(1,5), }; } diff --git a/W3ChampionsStatisticService/PersonalSettings/RaceToMaxPicture.cs b/W3ChampionsStatisticService/PersonalSettings/RaceToMaxPicture.cs deleted file mode 100644 index 01b03b98..00000000 --- a/W3ChampionsStatisticService/PersonalSettings/RaceToMaxPicture.cs +++ /dev/null @@ -1,16 +0,0 @@ -using W3C.Domain.CommonValueObjects; - -namespace W3ChampionsStatisticService.PersonalSettings -{ - public class RaceToMaxPicture - { - public Race Race { get; } - public long Max { get; } - - public RaceToMaxPicture(Race race, long max) - { - Race = race; - Max = max; - } - } -} \ No newline at end of file diff --git a/W3ChampionsStatisticService/PlayerProfiles/PlayerOverallStats.cs b/W3ChampionsStatisticService/PlayerProfiles/PlayerOverallStats.cs index 6c0ecae3..3f462f6a 100644 --- a/W3ChampionsStatisticService/PlayerProfiles/PlayerOverallStats.cs +++ b/W3ChampionsStatisticService/PlayerProfiles/PlayerOverallStats.cs @@ -50,5 +50,10 @@ public int GetWinsPerRace(Race race) { return WinLosses.Single(w => w.Race == race).Wins; } + + public int GetTotalWins() + { + return WinLosses.Sum(w => w.Wins); + } } } \ No newline at end of file diff --git a/WC3ChampionsStatisticService.UnitTests/PersonalSettings/PersonalSettingsTests.cs b/WC3ChampionsStatisticService.UnitTests/PersonalSettings/PersonalSettingsTests.cs index aab90ba9..258f93e0 100644 --- a/WC3ChampionsStatisticService.UnitTests/PersonalSettings/PersonalSettingsTests.cs +++ b/WC3ChampionsStatisticService.UnitTests/PersonalSettings/PersonalSettingsTests.cs @@ -59,7 +59,7 @@ public void SetProfilePicture_TooFewWins() SetPictureCommand cmd2 = new SetPictureCommand() { avatarCategory = AvatarCategory.HU, - pictureId = 2 + pictureId = 3 }; var profilePicture = personalSetting.SetProfilePicture(cmd2); @@ -80,7 +80,7 @@ public void SetProfilePicture_AllowedPictures() } personalSetting.Players = new List { player }; - Assert.AreEqual(2, personalSetting.PickablePictures.Single(r => r.Race == Race.HU).Max); + Assert.AreEqual(2, personalSetting.PickablePictures.Single(r => r.AvatarType == AvatarCategory.HU).Max); } [Test] @@ -98,7 +98,9 @@ public void SetProfilePicture_SpecialAvatar_ButSpecialPicturesNull_DoesNotThrow( }; Assert.DoesNotThrow(() => personalSetting.SetProfilePicture(cmd)); - Assert.AreEqual(expectedProfilePic.PictureId, personalSetting.ProfilePicture.PictureId); + Assert.IsTrue( + personalSetting.ProfilePicture.PictureId >= 1 && + personalSetting.ProfilePicture.PictureId <= 5); Assert.AreEqual(expectedProfilePic.Race, personalSetting.ProfilePicture.Race); }