From a47d271a33d0aeffef7a66f50ffe67560111d5bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Mon, 28 Oct 2024 22:40:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84=EF=BC=8C=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E7=9F=BF=E7=89=A9=E5=88=B7=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/CollectiblesService.cs | 37 +++++++++++++++-- .../Etos/SuccessMiningEto.cs | 2 + .../DigitalCollectiblesSettingProvider.cs | 19 ++++++--- .../SuccessMiningEventHandler.cs | 13 +++++- .../Managers/MiningPoolManager.cs | 41 +++++++++++-------- 5 files changed, 85 insertions(+), 27 deletions(-) diff --git a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/Services/CollectiblesService.cs b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/Services/CollectiblesService.cs index e7593217..bfa7ad44 100644 --- a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/Services/CollectiblesService.cs +++ b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/Services/CollectiblesService.cs @@ -33,9 +33,40 @@ public class CollectiblesService : ApplicationService public async Task GetAccountInfoAsync() { var userId = CurrentUser.GetId(); - var totalValue = await _collectiblesUserStoreRepository._DbQueryable.Where(store => store.UserId == userId) + var collectiblesList = await _collectiblesUserStoreRepository._DbQueryable + .Where(store => store.UserId == userId) .LeftJoin((store, c) => store.CollectiblesId == c.Id) - .SumAsync((store, c) => c.ValueNumber); + .Select((store, c) => + new + { + c.Id, + c.ValueNumber + } + ).ToListAsync(); + var groupBy = collectiblesList.GroupBy(x => x.Id); + decimal totalValue = 0; + + //首个价值百分之百,后续每个只有百分之40,最大10个 + foreach (var groupByItem in groupBy) + { + foreach (var item in groupByItem.Select((value, index) => new { value, index })) + { + + if (item.index == 0) + { + totalValue += item.value.ValueNumber; + } + else if (item.index == 10) + { + //到第11个,直接跳出循环 + break; + } + else + { + totalValue += item.value.ValueNumber * 0.4m; + } + } + } return new CollectiblesAccountInfoDto { TotalValue = totalValue @@ -56,7 +87,7 @@ public class CollectiblesService : ApplicationService var userId = CurrentUser.GetId(); RefAsync total = 0; var output = await _collectiblesUserStoreRepository._DbQueryable - .Where(x=>x.UserId==userId) + .Where(x => x.UserId == userId) .WhereIF( input.StartTime is not null && input.EndTime is not null, u => u.CreationTime >= input.StartTime && u.CreationTime <= input.EndTime) diff --git a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain.Shared/Etos/SuccessMiningEto.cs b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain.Shared/Etos/SuccessMiningEto.cs index 3ccba77c..ebdc2248 100644 --- a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain.Shared/Etos/SuccessMiningEto.cs +++ b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain.Shared/Etos/SuccessMiningEto.cs @@ -5,4 +5,6 @@ namespace Yi.Framework.DigitalCollectibles.Domain.Shared.Etos; public class SuccessMiningEto { public Guid CollectiblesId { get; set; } + + public Guid UserId{ get; set; } } \ No newline at end of file diff --git a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain.Shared/Settings/DigitalCollectiblesSettingProvider.cs b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain.Shared/Settings/DigitalCollectiblesSettingProvider.cs index 1537942e..8b046e72 100644 --- a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain.Shared/Settings/DigitalCollectiblesSettingProvider.cs +++ b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain.Shared/Settings/DigitalCollectiblesSettingProvider.cs @@ -19,16 +19,25 @@ namespace Yi.Abp.Domain.Shared.Settings new SettingDefinition("MaxPoolLimit", "100"), //每日挖矿最大上限--控制无限挖矿 - new SettingDefinition("MiningMaxLimit", "36"), - + // new SettingDefinition("MiningMaxLimit", "36"), + new SettingDefinition("MiningMaxLimit", "999"), + //每次挖矿最小间隔(秒)--控制暴力挖矿 new SettingDefinition("MiningMinIntervalSeconds", "3"), //每次挖到矿的概率--控制爆率 - new SettingDefinition("MiningMinProbability", "0.06"), - + // new SettingDefinition("MiningMinProbability", "0.06"), + new SettingDefinition("MiningMinProbability", "0.6"), + //交易税率--控制频繁交易 - new SettingDefinition("MarketTaxRate", "0.2") + new SettingDefinition("MarketTaxRate", "0.02"), + + + + //矿池刷新内容 + new SettingDefinition("PoolData", "60,24,10,3,1") + + ); } } diff --git a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain/EventHandlers/SuccessMiningEventHandler.cs b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain/EventHandlers/SuccessMiningEventHandler.cs index 345f10fc..94ae9956 100644 --- a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain/EventHandlers/SuccessMiningEventHandler.cs +++ b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain/EventHandlers/SuccessMiningEventHandler.cs @@ -15,12 +15,13 @@ public class SuccessMiningEventHandler : ILocalEventHandler, I { private MiningPoolManager _miningPoolManager; private ISqlSugarRepository _repository; - + private readonly ISqlSugarRepository _userStoreRepository; public SuccessMiningEventHandler(MiningPoolManager miningPoolManager, - ISqlSugarRepository repository) + ISqlSugarRepository repository, ISqlSugarRepository userStoreRepository) { _miningPoolManager = miningPoolManager; _repository = repository; + _userStoreRepository = userStoreRepository; } public async Task HandleEventAsync(SuccessMiningEto eventData) @@ -32,5 +33,13 @@ public class SuccessMiningEventHandler : ILocalEventHandler, I //新增全世界发现 currentCollectibles.FindTotal += 1; await _repository.UpdateAsync(currentCollectibles); + + //使用结果新增给对应的用户 + await _userStoreRepository.InsertAsync(new CollectiblesUserStoreAggregateRoot + { + UserId = eventData.UserId, + CollectiblesId = eventData.CollectiblesId, + IsRead = false + }); } } \ No newline at end of file diff --git a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain/Managers/MiningPoolManager.cs b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain/Managers/MiningPoolManager.cs index 18cb659a..0b71d4fe 100644 --- a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain/Managers/MiningPoolManager.cs +++ b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain/Managers/MiningPoolManager.cs @@ -2,12 +2,14 @@ using Microsoft.Extensions.Caching.Distributed; using Volo.Abp.Caching; using Volo.Abp.Domain.Services; +using Volo.Abp.EventBus.Local; using Volo.Abp.Settings; using Volo.Abp.Threading; using Yi.Framework.DigitalCollectibles.Domain.Dtos; using Yi.Framework.DigitalCollectibles.Domain.Entities; using Yi.Framework.DigitalCollectibles.Domain.Shared.Consts; using Yi.Framework.DigitalCollectibles.Domain.Shared.Enums; +using Yi.Framework.DigitalCollectibles.Domain.Shared.Etos; using Yi.Framework.SettingManagement.Domain; using Yi.Framework.SqlSugarCore.Abstractions; @@ -26,6 +28,7 @@ public class MiningPoolManager : DomainService private readonly IDistributedCache _userMiningLimitCache; private readonly ISqlSugarRepository _userStoreRepository; private IRedisClient RedisClient => LazyServiceProvider.LazyGetRequiredService(); + private ILocalEventBus LocalEventBus => LazyServiceProvider.LazyGetRequiredService(); public MiningPoolManager(ISettingProvider settingProvider, IDistributedCache miningPoolCache, ISqlSugarRepository collectiblesRepository, @@ -66,6 +69,7 @@ public class MiningPoolManager : DomainService pool.I4_LegendNumber -= 1; break; } + //重新设置 await SetMiningPoolAsync(pool); } @@ -89,7 +93,7 @@ public class MiningPoolManager : DomainService { var onHook = await _onHookRepository._DbQueryable.Where(x => x.UserId == userId) .Where(x => x.IsActive == true) - .Where(x => x.EndTime <= DateTime.Now) + .Where(x => x.EndTime > DateTime.Now) .FirstAsync(); if (onHook is not null) @@ -198,51 +202,54 @@ public class MiningPoolManager : DomainService { poolState = false; } + break; case RarityEnum.Senior: if (pool.I1_SeniorNumber <= 0) { poolState = false; } + break; case RarityEnum.Rare: if (pool.I2_RareNumber <= 0) { poolState = false; } + break; case RarityEnum.Gem: if (pool.I3_GemNumber <= 0) { poolState = false; } + break; case RarityEnum.Legend: if (pool.I4_LegendNumber <= 0) { poolState = false; } + break; } - if (poolState==false) + if (poolState == false) { throw new UserFriendlyException($"超级可惜!真的真的只差最后一点就挖到了"); } - + int randomIndex = new Random().Next(collectiblesList.Count); var currentCollectibles = collectiblesList[randomIndex]; result.Collectibles = currentCollectibles; - //使用结果新增给对应的用户 - await _userStoreRepository.InsertAsync(new CollectiblesUserStoreAggregateRoot + await LocalEventBus.PublishAsync(new SuccessMiningEto { - UserId = userId, - CollectiblesId = result.Collectibles.Id, - IsRead = false - }); + CollectiblesId = currentCollectibles.Id, + UserId = userId + }, false); return result; } @@ -316,22 +323,22 @@ public class MiningPoolManager : DomainService { //获取当前最大的限制 var maximumPoolLimit = int.Parse(await _settingProvider.GetOrNullAsync("MaxPoolLimit")); - + var poolData = (await _settingProvider.GetOrNullAsync("PoolData")).Split(',').Select(x=>int.Parse(x)).ToList(); DateTime startTime = DateTime.Today.AddHours(10); DateTime endTime = startTime.AddDays(1); - var probabilityValues = RarityEnumExtensions.GetProbabilityArray(); - var result = GenerateDistribution(maximumPoolLimit, probabilityValues); + // var probabilityValues = RarityEnumExtensions.GetProbabilityArray(); + // var result = GenerateDistribution(maximumPoolLimit, probabilityValues); //根据配置,将不同比例的矿,塞入矿池, //矿池,交给redis await SetMiningPoolAsync(new MiningPoolContent(startTime, endTime) { - I0_OrdinaryNumber = result[0], - I1_SeniorNumber = result[1], - I2_RareNumber = result[2], - I3_GemNumber = result[3], - I4_LegendNumber = result[4] + I0_OrdinaryNumber = poolData[0], + I1_SeniorNumber = poolData[1], + I2_RareNumber = poolData[2], + I3_GemNumber = poolData[3], + I4_LegendNumber = poolData[4] }); }