-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from approvers/feature/rinia/reply/002_shuffle
Common: 毎日0時に各種確率が変わるように
- Loading branch information
Showing
18 changed files
with
301 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace Approvers.King.Common; | ||
|
||
public static class EventUtility | ||
{ | ||
public static Task WaitAsync(Action<Func<Task>> register, Action<Func<Task>> unregister, | ||
CancellationToken ct = default) | ||
{ | ||
var tcs = new TaskCompletionSource(); | ||
Func<Task>? h = null; | ||
h = () => | ||
{ | ||
unregister(h!); | ||
tcs.SetResult(); | ||
return Task.CompletedTask; | ||
}; | ||
register(h); | ||
ct.Register(() => | ||
{ | ||
unregister(h); | ||
tcs.SetCanceled(); | ||
}); | ||
|
||
try | ||
{ | ||
return tcs.Task; | ||
} | ||
catch | ||
{ | ||
unregister(h); | ||
throw; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
namespace Approvers.King.Common; | ||
|
||
public class GachaManager | ||
{ | ||
private readonly List<ReplyMessage> _replyMessageTable = new(); | ||
|
||
public static GachaManager Instance { get; } = new(); | ||
|
||
/// <summary> | ||
/// 現在のメッセージに反応する確率 | ||
/// </summary> | ||
public float RareReplyRate { get; private set; } | ||
|
||
/// <summary> | ||
/// 各メッセージの確率 | ||
/// </summary> | ||
public IReadOnlyList<ReplyMessage> ReplyMessageTable => _replyMessageTable; | ||
|
||
public void Initialize() | ||
{ | ||
_replyMessageTable.Clear(); | ||
_replyMessageTable.AddRange(MasterManager.ReplyMessages.Select(x => new ReplyMessage | ||
{ | ||
Rate = 1f, | ||
Message = x, | ||
})); | ||
} | ||
|
||
public string? TryPickRareReplyMessage() | ||
{ | ||
if (RandomUtility.IsHit(RareReplyRate) == false) return null; | ||
return PickMessage(); | ||
} | ||
|
||
public string PickMessage() | ||
{ | ||
var totalRate = _replyMessageTable.Sum(x => x.Rate); | ||
var value = RandomUtility.GetRandomFloat(totalRate); | ||
|
||
foreach (var element in _replyMessageTable) | ||
{ | ||
if (value < element.Rate) return element.Message; | ||
value -= element.Rate; | ||
} | ||
|
||
return _replyMessageTable[^1].Message; | ||
} | ||
|
||
public void ShuffleRareReplyRate() | ||
{ | ||
RareReplyRate = MasterManager.RareReplyRateTable.PickRandom(); | ||
} | ||
|
||
public void ShuffleMessageRates() | ||
{ | ||
var borders = Enumerable.Range(0, _replyMessageTable.Count - 1) | ||
.Select(x => (float)Math.Pow(RandomUtility.GetRandomFloat(1f), 2)) | ||
.Select(x => (int)Math.Floor(x * 100f)) | ||
.OrderBy(x => x) | ||
.ToList(); | ||
borders.Add(100); | ||
var randomIndices = Enumerable.Range(0, _replyMessageTable.Count).Shuffle().ToList(); | ||
|
||
_replyMessageTable[randomIndices[0]].Rate = borders[0] * 0.01f; | ||
for (var i = 1; i < _replyMessageTable.Count; i++) | ||
{ | ||
_replyMessageTable[randomIndices[i]].Rate = (borders[i] - borders[i - 1]) * 0.01f; | ||
} | ||
} | ||
|
||
public class ReplyMessage | ||
{ | ||
public float Rate { get; set; } | ||
public string Message { get; init; } = string.Empty; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace Approvers.King.Common; | ||
|
||
public abstract class SchedulerJobPresenterBase : PresenterBase | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Approvers.King.Common; | ||
|
||
public abstract class SchedulerJobRunner | ||
{ | ||
public Func<DateTime, bool>? Predicate { get; init; } | ||
public abstract void Run(); | ||
} | ||
|
||
public class SchedulerJobRunner<T> : SchedulerJobRunner where T : SchedulerJobPresenterBase, new() | ||
{ | ||
public override void Run() | ||
{ | ||
new T().RunAsync().Run(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Timers; | ||
using Timer = System.Timers.Timer; | ||
|
||
namespace Approvers.King.Common; | ||
|
||
public static class SchedulerManager | ||
{ | ||
private static readonly Timer Timer = new(TimeSpan.FromSeconds(1)); | ||
private static readonly List<SchedulerJobRunner> Runners = new(); | ||
|
||
public static void Initialize() | ||
{ | ||
Timer.Elapsed += OnEverySecond; | ||
Timer.Start(); | ||
} | ||
|
||
private static void OnEverySecond(object? sender, ElapsedEventArgs e) | ||
{ | ||
var now = TimeManager.GetNow(); | ||
foreach (var runner in Runners) | ||
{ | ||
if (runner.Predicate != null && runner.Predicate(now)) runner.Run(); | ||
} | ||
} | ||
|
||
public static void RegisterDaily<T>(TimeSpan time) where T : SchedulerJobPresenterBase, new() | ||
{ | ||
Runners.Add(new SchedulerJobRunner<T> | ||
{ | ||
Predicate = dateTime => dateTime.Hour == time.Hours && | ||
dateTime.Minute == time.Minutes && | ||
dateTime.Second == time.Seconds, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Approvers.King.Common; | ||
|
||
public static class TaskExtensions | ||
{ | ||
public static void Run(this Task source) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Approvers.King.Common; | ||
using Discord; | ||
|
||
namespace Approvers.King.Events; | ||
|
||
public class GachaRateUpdatePresenter : SchedulerJobPresenterBase | ||
{ | ||
private static readonly string IssoSmileStamp = "<:isso_smile:1081501060369236069>"; | ||
|
||
protected override async Task MainAsync() | ||
{ | ||
// 排出確率を変える | ||
GachaManager.Instance.ShuffleRareReplyRate(); | ||
GachaManager.Instance.ShuffleMessageRates(); | ||
|
||
// 名前を更新する | ||
var guild = DiscordManager.Client.GetGuild(SettingManager.DiscordTargetGuildId); | ||
|
||
await guild.CurrentUser.ModifyAsync(x => | ||
x.Nickname = $"{GachaManager.Instance.RareReplyRate:P0}の確率でわかってくれる創造主"); | ||
|
||
// 排出率を投稿する | ||
var records = GachaManager.Instance.ReplyMessageTable | ||
.OrderByDescending(x => x.Rate) | ||
.Select(x => (x.Message, x.Rate.ToString("P0"))); | ||
var embed = new EmbedBuilder() | ||
.WithTitle( | ||
$"{IssoSmileStamp}{IssoSmileStamp}{IssoSmileStamp} 本日のいっそう {IssoSmileStamp}{IssoSmileStamp}{IssoSmileStamp}") | ||
.WithColor(new Color(0xf1, 0xc4, 0x0f)) | ||
.WithDescription($"本日は {Discord.Format.Bold($"{GachaManager.Instance.RareReplyRate:P0}")} の確率で反応します") | ||
.AddField("排出確率", Format.Table(records)); | ||
|
||
await guild.GetTextChannel(SettingManager.DiscordMainChannelId) | ||
.SendMessageAsync(embed: embed.Build()); | ||
} | ||
} |
Oops, something went wrong.