From c32c3bb62bb663cf2c2a4149e79417fc599cc69b Mon Sep 17 00:00:00 2001 From: Kukks Date: Wed, 9 Jan 2019 12:22:36 +0100 Subject: [PATCH] add contribution ranking --- .../Controllers/AppsController.Crowdfund.cs | 12 ++++++++--- .../Crowdfund/CrowdfundHubStreamer.cs | 17 ++++++++++++++-- .../AppViewModels/UpdateCrowdfundViewModel.cs | 4 ++++ .../AppViewModels/ViewCrowdfundViewModel.cs | 2 +- .../Views/Apps/UpdateCrowdfund.cshtml | 10 ++++++++++ .../AppsPublic/Crowdfund/VueCrowdfund.cshtml | 20 ++++++++++++++----- BTCPayServer/wwwroot/crowdfund/app.js | 6 +++--- 7 files changed, 57 insertions(+), 14 deletions(-) diff --git a/BTCPayServer/Controllers/AppsController.Crowdfund.cs b/BTCPayServer/Controllers/AppsController.Crowdfund.cs index 390be5824..919684893 100644 --- a/BTCPayServer/Controllers/AppsController.Crowdfund.cs +++ b/BTCPayServer/Controllers/AppsController.Crowdfund.cs @@ -43,7 +43,9 @@ namespace BTCPayServer.Controllers public bool UseInvoiceAmount { get; set; } = true; public int ResetEveryAmount { get; set; } = 1; public CrowdfundResetEvery ResetEvery { get; set; } = CrowdfundResetEvery.Never; - public bool UseAllStoreInvoices { get; set; } = false; + public bool UseAllStoreInvoices { get; set; } + public bool DisplayPerksRanking { get; set; } + public bool SortPerksByPopularity { get; set; } } @@ -79,7 +81,9 @@ namespace BTCPayServer.Controllers ResetEveryAmount = settings.ResetEveryAmount, ResetEvery = Enum.GetName(typeof(CrowdfundResetEvery), settings.ResetEvery), UseAllStoreInvoices = settings.UseAllStoreInvoices, - AppId = appId + AppId = appId, + DisplayPerksRanking = settings.DisplayPerksRanking, + SortPerksByPopularity = settings.SortPerksByPopularity }; return View(vm); } @@ -142,7 +146,9 @@ namespace BTCPayServer.Controllers ResetEveryAmount = vm.ResetEveryAmount, ResetEvery = Enum.Parse(vm.ResetEvery), UseInvoiceAmount = vm.UseInvoiceAmount, - UseAllStoreInvoices = vm.UseAllStoreInvoices + UseAllStoreInvoices = vm.UseAllStoreInvoices, + DisplayPerksRanking = vm.DisplayPerksRanking, + SortPerksByPopularity = vm.SortPerksByPopularity }; app.SetSettings(newSettings); diff --git a/BTCPayServer/Crowdfund/CrowdfundHubStreamer.cs b/BTCPayServer/Crowdfund/CrowdfundHubStreamer.cs index 6ef878588..7e1f69105 100644 --- a/BTCPayServer/Crowdfund/CrowdfundHubStreamer.cs +++ b/BTCPayServer/Crowdfund/CrowdfundHubStreamer.cs @@ -293,7 +293,19 @@ namespace BTCPayServer.Hubs .Where(entity => !string.IsNullOrEmpty( entity.ProductInformation.ItemCode)) .GroupBy(entity => entity.ProductInformation.ItemCode) .ToDictionary(entities => entities.Key, entities => entities.Count()); - + + var perks = _AppsHelper.Parse(settings.PerksTemplate, settings.TargetCurrency); + if (settings.SortPerksByPopularity) + { + var ordered = perkCount.OrderByDescending(pair => pair.Value); + var newPerksOrder = ordered + .Select(keyValuePair => perks.SingleOrDefault(item => item.Id == keyValuePair.Key)) + .Where(matchingPerk => matchingPerk != null) + .ToList(); + var remainingPerks = perks.Where(item => !newPerksOrder.Contains(item)); + newPerksOrder.AddRange(remainingPerks); + perks = newPerksOrder.ToArray(); + } return new ViewCrowdfundViewModel() { Title = settings.Title, @@ -310,12 +322,13 @@ namespace BTCPayServer.Hubs TargetCurrency = settings.TargetCurrency, EnforceTargetAmount = settings.EnforceTargetAmount, StatusMessage = statusMessage, - Perks = _AppsHelper.Parse(settings.PerksTemplate, settings.TargetCurrency), + Perks = perks, DisqusEnabled = settings.DisqusEnabled, SoundsEnabled = settings.SoundsEnabled, DisqusShortname = settings.DisqusShortname, AnimationsEnabled = settings.AnimationsEnabled, ResetEveryAmount = settings.ResetEveryAmount, + DisplayPerksRanking = settings.DisplayPerksRanking, PerkCount = perkCount, ResetEvery = Enum.GetName(typeof(CrowdfundResetEvery),settings.ResetEvery), CurrencyData = _AppsHelper.GetCurrencyData(settings.TargetCurrency, true), diff --git a/BTCPayServer/Models/AppViewModels/UpdateCrowdfundViewModel.cs b/BTCPayServer/Models/AppViewModels/UpdateCrowdfundViewModel.cs index 132d0391f..f0239b037 100644 --- a/BTCPayServer/Models/AppViewModels/UpdateCrowdfundViewModel.cs +++ b/BTCPayServer/Models/AppViewModels/UpdateCrowdfundViewModel.cs @@ -74,6 +74,10 @@ namespace BTCPayServer.Models.AppViewModels public bool UseAllStoreInvoices { get; set; } public string AppId { get; set; } + [Display(Name = "Sort contribution perks by popularity")] + public bool SortPerksByPopularity { get; set; } + [Display(Name = "Display contribution ranking")] + public bool DisplayPerksRanking { get; set; } } public enum CrowdfundResetEvery diff --git a/BTCPayServer/Models/AppViewModels/ViewCrowdfundViewModel.cs b/BTCPayServer/Models/AppViewModels/ViewCrowdfundViewModel.cs index 1de719a82..14a6f4610 100644 --- a/BTCPayServer/Models/AppViewModels/ViewCrowdfundViewModel.cs +++ b/BTCPayServer/Models/AppViewModels/ViewCrowdfundViewModel.cs @@ -54,7 +54,7 @@ namespace BTCPayServer.Models.AppViewModels public bool Started => !StartDate.HasValue || DateTime.Now.ToUniversalTime() > StartDate; public bool Ended => !EndDate.HasValue || DateTime.Now.ToUniversalTime() > EndDate; - + public bool DisplayPerksRanking { get; set; } } public class ContributeToCrowdfund diff --git a/BTCPayServer/Views/Apps/UpdateCrowdfund.cshtml b/BTCPayServer/Views/Apps/UpdateCrowdfund.cshtml index c92bd6c3c..c1cb071a6 100644 --- a/BTCPayServer/Views/Apps/UpdateCrowdfund.cshtml +++ b/BTCPayServer/Views/Apps/UpdateCrowdfund.cshtml @@ -129,6 +129,16 @@ +
+ + + +
+
+ + + +
diff --git a/BTCPayServer/Views/AppsPublic/Crowdfund/VueCrowdfund.cshtml b/BTCPayServer/Views/AppsPublic/Crowdfund/VueCrowdfund.cshtml index 59966f6ce..b5b0a9dbd 100644 --- a/BTCPayServer/Views/AppsPublic/Crowdfund/VueCrowdfund.cshtml +++ b/BTCPayServer/Views/AppsPublic/Crowdfund/VueCrowdfund.cshtml @@ -130,6 +130,7 @@
@@ -152,6 +153,7 @@
@@ -192,11 +194,11 @@