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

Gacha: 10連ガチャ機能の実装 #4

Merged
merged 1 commit into from
Jan 14, 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
7 changes: 7 additions & 0 deletions Common/MasterManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,11 @@ public static class MasterManager
};

public static TimeSpan SilentTimeSpan => TimeSpan.FromMinutes(5);

public static IReadOnlyList<string> GachaTriggerMessages => new List<string>
{
"10連",
"ガチャ",
"おみくじ",
};
}
5 changes: 5 additions & 0 deletions Common/RandomUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ public static float GetRandomFloat(float min, float max)
{
return (float)(min + (max - min) * Random.NextDouble());
}

public static bool IsHit(float probability)
{
return GetRandomFloat(1f) <= probability;
}
}
30 changes: 30 additions & 0 deletions Events/GachaCommandPresenter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Text;
using Approvers.King.Common;
using Discord;

namespace Approvers.King.Events;

public class GachaCommandPresenter : DiscordMessagePresenterBase
{
private const int PickCount = 10;

protected override async Task MainAsync()
{
var results = Enumerable.Range(0, PickCount).Select(_ => Pick());

var builder = new StringBuilder();
builder.AppendLine($"↓↓↓ いっそう{PickCount}連おみくじ ↓↓↓");
foreach (var result in results)
{
builder.AppendLine(result != null ? Discord.Format.Bold($"・{result}") : Discord.Format.Code("x"));
}

await Message.ReplyAsync(builder.ToString());
}

private static string? Pick()
{
if (RandomUtility.IsHit(MasterManager.ReplyRate) == false) return null;
return MessageUtility.PickRandomMessage();
}
}
2 changes: 1 addition & 1 deletion Events/RareReplyPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class RareReplyPresenter : DiscordMessagePresenterBase
protected override async Task MainAsync()
{
if (SilentManager.IsSilent(Message.Author.Id) ||
RandomUtility.GetRandomFloat(1f) > MasterManager.ReplyRate) return;
RandomUtility.IsHit(MasterManager.ReplyRate) == false) return;

await Task.Delay(TimeSpan.FromSeconds(RandomUtility.GetRandomFloat(MasterManager.ReplyMaxDelay)));
using (Message.Channel.EnterTypingState())
Expand Down
7 changes: 7 additions & 0 deletions Triggers/DiscordTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ private static async Task OnMessageReceived(SocketMessage message)
return;
}

if (MasterManager.GachaTriggerMessages.Any(userMessage.Content.Contains))
{
// 10連ガチャ
await DiscordManager.ExecuteAsync<GachaCommandPresenter>(userMessage);
return;
}

// 返信
await DiscordManager.ExecuteAsync<InteractReplyPresenter>(userMessage);
return;
Expand Down