Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slot: 1時間ごとに確率に揺らぎが発生するように #40

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Common/Log/LogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ private static string CreateTimestamp()
return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}

public static void Log(object message)
{
Console.WriteLine($"{CreateTimestamp()}: [INFO] {message}");
}

public static void Log(string message)
{
Console.WriteLine($"{CreateTimestamp()}: [INFO] {message}");
Expand Down
4 changes: 3 additions & 1 deletion Common/Master/SettingMaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ private int GetInt(string key)
public int PricePerGachaTenTimes => GetInt(nameof(PricePerGachaTenTimes));
public int PricePerGachaOnceCertain => GetInt(nameof(PricePerGachaOnceCertain));
public int PurchaseInfoRankingViewUserCount => GetInt(nameof(PurchaseInfoRankingViewUserCount));
public string SilentReplyMessage => GetString(nameof(SilentReplyMessage));
public int PricePerSlotOnce => GetInt(nameof(PricePerSlotOnce));
public string SlotReelRollingFormat => GetString(nameof(SlotReelRollingFormat));
public string SlotLeverFormat => GetString(nameof(SlotLeverFormat));
public int UserSlotExecuteLimitPerDay => GetInt(nameof(UserSlotExecuteLimitPerDay));
public int SlotMaxConditionOffsetPermillage => GetInt(nameof(SlotMaxConditionOffsetPermillage));
public int SlotMinConditionOffsetPermillage => GetInt(nameof(SlotMinConditionOffsetPermillage));
public int SlotRepeatPermillageUpperBound => GetInt(nameof(SlotRepeatPermillageUpperBound));
}

[SuppressMessage("ReSharper", "UnassignedGetOnlyAutoProperty")]
Expand Down
1 change: 1 addition & 0 deletions Common/Models/AppState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ public class AppState
public enum AppStateType
{
RareReplyProbabilityPermillage,
SlotConditionOffsetPermillage,
}
24 changes: 23 additions & 1 deletion Common/Models/SlotManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace Approvers.King.Common;
public class SlotManager : Singleton<SlotManager>
{
private readonly List<SlotItem> _items = [];
private int _conditionOffsetPermillage;
private const int ReelCount = 3;

public void LoadMaster()
Expand All @@ -12,6 +13,26 @@ public void LoadMaster()
_items.AddRange(items);
}

public async Task LoadAsync()
{
await using var app = AppService.CreateSession();
_conditionOffsetPermillage = await app.AppStates.GetIntAsync(AppStateType.SlotConditionOffsetPermillage) ?? 0;
}

public async Task SaveAsync()
{
await using var app = AppService.CreateSession();
await app.AppStates.SetIntAsync(AppStateType.SlotConditionOffsetPermillage, _conditionOffsetPermillage);
await app.SaveChangesAsync();
}

public void RefreshCondition()
{
var max = MasterManager.SettingMaster.SlotMaxConditionOffsetPermillage;
var min = MasterManager.SettingMaster.SlotMinConditionOffsetPermillage;
_conditionOffsetPermillage = RandomManager.GetRandomInt(min, max + 1);
}

public SlotExecuteResult Execute()
{
var itemCount = _items.Count;
Expand All @@ -26,7 +47,8 @@ public SlotExecuteResult Execute()
}

// 一定確率で直前と同じ出目が出る
var repeatProbability = NumberUtility.GetProbabilityFromPermillage(reelItems[i - 1].RepeatPermillage);
var repeatPermillage = Math.Clamp(reelItems[i - 1].RepeatPermillage + _conditionOffsetPermillage, 0, MasterManager.SettingMaster.SlotRepeatPermillageUpperBound);
var repeatProbability = NumberUtility.GetProbabilityFromPermillage(repeatPermillage);
var isRepeat = RandomManager.IsHit(repeatProbability);
if (isRepeat)
{
Expand Down
2 changes: 2 additions & 0 deletions Common/Scheduler/SchedulerJobRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public abstract class SchedulerJobRunner
{
public bool OnRiseOnly { get; set; }
public bool? PreviousCondition = null;
public Predicate<DateTime>? Predicate { get; init; }
public abstract void Run();
}
Expand Down
31 changes: 29 additions & 2 deletions Common/Scheduler/SchedulerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,26 @@ 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();
{
var condition = runner.Predicate != null && runner.Predicate(now);

if (runner.OnRiseOnly == false)
{
if (condition)
{
runner.Run();
}
}
else
{
if (runner.PreviousCondition is false && condition)
{
runner.Run();
}
}

runner.PreviousCondition = condition;
}
}

public static void RegisterDaily<T>(TimeSpan time) where T : SchedulerJobPresenterBase, new()
Expand Down Expand Up @@ -54,4 +72,13 @@ private static void OnEverySecond(object? sender, ElapsedEventArgs e)
x.Second == datetime.Second
});
}

public static void RegisterOn<T>(Predicate<DateTime> predicate) where T : SchedulerJobPresenterBase, new()
{
Runners.Add(new SchedulerJobRunner<T>
{
Predicate = predicate,
OnRiseOnly = true
});
}
}
12 changes: 12 additions & 0 deletions Events/Slot/SlotConditionRefreshPresenter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Approvers.King.Common;

namespace Approvers.King.Events;

public class SlotConditionRefreshPresenter : SchedulerJobPresenterBase
{
protected override async Task MainAsync()
{
SlotManager.Instance.RefreshCondition();
await SlotManager.Instance.SaveAsync();
}
}
5 changes: 5 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ private static async Task BuildAsync(string[] args)
await MasterManager.FetchAsync();

await GachaManager.Instance.LoadAsync();

SlotManager.Instance.LoadMaster();
await SlotManager.Instance.LoadAsync();

SchedulerManager.Initialize();

await DiscordManager.InitializeAsync();

if (GachaManager.Instance.IsTableEmpty)
Expand All @@ -38,6 +42,7 @@ private static async Task BuildAsync(string[] args)
TimeSpan.FromSeconds(1));
SchedulerManager.RegisterMonthly<MonthlyResetPresenter>(TimeManager.MonthlyResetDay,
TimeManager.DailyResetTime);
SchedulerManager.RegisterOn<SlotConditionRefreshPresenter>(x => x.Minute is 0);

// 永久に待つ
await Task.Delay(-1);
Expand Down