Skip to content

Commit

Permalink
Slot: スロットを回せる回数に制限をつける (#32)
Browse files Browse the repository at this point in the history
* Slot: スロットを回せる回数に制限をつける

* 修正
  • Loading branch information
2RiniaR authored Nov 23, 2024
1 parent 6c7a294 commit 260a32f
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 31 deletions.
18 changes: 16 additions & 2 deletions Common/DiscordManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

namespace Approvers.King.Common;

public static class DiscordManager
public class DiscordManager : Singleton<DiscordManager>
{
public static readonly DiscordSocketClient Client = new(new DiscordSocketConfig
private readonly DiscordSocketClient _client = new(new DiscordSocketConfig
{
GatewayIntents = GatewayIntents.MessageContent | GatewayIntents.Guilds | GatewayIntents.GuildMessages,
});

public static DiscordSocketClient Client => Instance._client;

public static async Task InitializeAsync()
{
Client.Log += OnLog;
Expand All @@ -24,6 +26,18 @@ private static Task OnLog(LogMessage content)
return Task.CompletedTask;
}

public static SocketGuildUser GetClientUser()
{
var guild = Client.GetGuild(EnvironmentManager.DiscordTargetGuildId);
return guild.CurrentUser;
}

public static SocketTextChannel GetMainChannel()
{
var guild = Client.GetGuild(EnvironmentManager.DiscordTargetGuildId);
return guild.GetTextChannel(EnvironmentManager.DiscordMainChannelId);
}

public static async Task ExecuteAsync<T>(SocketUserMessage message, Func<T, Task>? onInitializeAsync = null)
where T : DiscordMessagePresenterBase, new()
{
Expand Down
8 changes: 7 additions & 1 deletion Common/DiscordMessagePresenterBase.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
using Discord.WebSocket;
using Discord;
using Discord.WebSocket;

namespace Approvers.King.Common;

public abstract class DiscordMessagePresenterBase : PresenterBase
{
public SocketUserMessage Message { get; init; } = null!;

protected override async Task SendAppError(AppException e)
{
await Message.ReplyAsync(e.Message);
}
}
1 change: 1 addition & 0 deletions Common/Master/SettingMaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ private int GetInt(string key)
public int PricePerSlotOnce => GetInt(nameof(PricePerSlotOnce));
public string SlotReelRollingFormat => GetString(nameof(SlotReelRollingFormat));
public string SlotLeverFormat => GetString(nameof(SlotLeverFormat));
public int UserSlotExecuteLimitPerDay => GetInt(nameof(UserSlotExecuteLimitPerDay));
}

[SuppressMessage("ReSharper", "UnassignedGetOnlyAutoProperty")]
Expand Down
3 changes: 3 additions & 0 deletions Common/Models/AppException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Approvers.King.Common;

public class AppException(string message) : Exception(message);
12 changes: 12 additions & 0 deletions Common/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class User
{
[Key] public ulong DiscordID { get; set; }
public int MonthlyPurchase { get; set; }
public int TodaySlotExecuteCount { get; set; }

public User DeepCopy()
{
Expand All @@ -18,6 +19,11 @@ public void ResetMonthlyPurchase()
MonthlyPurchase = 0;
}

public void ResetDailySlotExecuteCount()
{
TodaySlotExecuteCount = 0;
}

public GachaProbability? RollGachaOnce()
{
MonthlyPurchase += MasterManager.SettingMaster.PricePerGachaOnce;
Expand All @@ -39,9 +45,15 @@ public GachaProbability RollGachaOnceCertain()

public SlotExecuteResult ExecuteSlot()
{
if (TodaySlotExecuteCount >= MasterManager.SettingMaster.UserSlotExecuteLimitPerDay)
{
throw new AppException("今日はもう回せないぞカス");
}

var price = MasterManager.SettingMaster.PricePerSlotOnce;
var result = SlotManager.Instance.Execute();
MonthlyPurchase += price - (int)(NumberUtility.GetPercentFromPermillage(result.ResultRatePermillage) * price);
TodaySlotExecuteCount++;
return result;
}
}
6 changes: 6 additions & 0 deletions Common/PresenterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ public async Task RunAsync()
{
await MainAsync();
}
catch (AppException e)
{
await SendAppError(e);
}
catch (Exception e)
{
await Console.Error.WriteLineAsync("========================================\n");
Console.Error.Write(e);
}
}

protected abstract Task SendAppError(AppException e);

protected abstract Task MainAsync();

public static Embed ErrorEmbed(string message)
Expand Down
4 changes: 4 additions & 0 deletions Common/Scheduler/SchedulerJobPresenterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

public abstract class SchedulerJobPresenterBase : PresenterBase
{
protected override async Task SendAppError(AppException e)
{
await DiscordManager.GetMainChannel().SendMessageAsync(e.Message);
}
}
26 changes: 26 additions & 0 deletions Events/DailyResetPresenter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Approvers.King.Common;
using Microsoft.EntityFrameworkCore;

namespace Approvers.King.Events;

public class DailyResetPresenter : SchedulerJobPresenterBase
{
protected override async Task MainAsync()
{
await using var app = AppService.CreateSession();
await app.Users.ForEachAsync(user => user.ResetDailySlotExecuteCount());
await app.SaveChangesAsync();

// 排出確率を変える
GachaManager.Instance.RefreshMessageTable();
GachaManager.Instance.ShuffleRareReplyRate();
GachaManager.Instance.ShuffleMessageRates();
await GachaManager.Instance.SaveAsync();

// 名前を更新する
await DiscordManager.GetClientUser().ModifyAsync(x => x.Nickname = $"{GachaManager.Instance.RareReplyRate:P0}の確率でわかってくれる創造主");

// 排出率を投稿する
await DiscordManager.GetMainChannel().SendMessageAsync(embed: GachaUtility.GetInfoEmbedBuilder().Build());
}
}
24 changes: 0 additions & 24 deletions Events/GachaRateUpdatePresenter.cs

This file was deleted.

3 changes: 1 addition & 2 deletions Events/MonthlyResetPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ private async Task SendSummaryAsync(IReadOnlyList<User> rankingUsers)
.WithCurrentTimestamp()
.Build();

var guild = DiscordManager.Client.GetGuild(EnvironmentManager.DiscordTargetGuildId);
await guild.GetTextChannel(EnvironmentManager.DiscordMainChannelId).SendMessageAsync(embed: embed);
await DiscordManager.GetMainChannel().SendMessageAsync(embed: embed);
}
}
68 changes: 68 additions & 0 deletions Migrations/20241123205651_AddSlotDaily.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions Migrations/20241123205651_AddSlotDaily.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Approvers.King.Migrations
{
/// <inheritdoc />
public partial class AddSlotDaily : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "TodaySlotExecuteCount",
table: "Users",
type: "INTEGER",
nullable: false,
defaultValue: 0);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "TodaySlotExecuteCount",
table: "Users");
}
}
}
3 changes: 3 additions & 0 deletions Migrations/AppServiceModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<int>("MonthlyPurchase")
.HasColumnType("INTEGER");

b.Property<int>("TodaySlotExecuteCount")
.HasColumnType("INTEGER");

b.HasKey("DiscordID");

b.ToTable("Users");
Expand Down
4 changes: 2 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private static async Task BuildAsync(string[] args)
if (GachaManager.Instance.IsTableEmpty)
{
// 起動時にデータがない場合、ガチャ確率を初期化する
await new GachaRateUpdatePresenter().RunAsync();
await new DailyResetPresenter().RunAsync();
}

DiscordManager.Client.MessageReceived += message =>
Expand All @@ -32,7 +32,7 @@ private static async Task BuildAsync(string[] args)
return Task.CompletedTask;
};

SchedulerManager.RegisterDaily<GachaRateUpdatePresenter>(TimeManager.DailyResetTime);
SchedulerManager.RegisterDaily<DailyResetPresenter>(TimeManager.DailyResetTime);
SchedulerManager.RegisterYearly<BirthPresenter>(TimeManager.Birthday + TimeManager.DailyResetTime +
TimeSpan.FromSeconds(1));
SchedulerManager.RegisterMonthly<MonthlyResetPresenter>(TimeManager.MonthlyResetDay,
Expand Down

0 comments on commit 260a32f

Please sign in to comment.