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

Purchase: 利益と課金額を分けて記録する #33

Merged
merged 1 commit into from
Nov 24, 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
10 changes: 7 additions & 3 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 MonthlySlotReward { get; set; }
public int TodaySlotExecuteCount { get; set; }

public User DeepCopy()
Expand All @@ -14,12 +15,13 @@ public User DeepCopy()
return user;
}

public void ResetMonthlyPurchase()
public void ResetMonthlyState()
{
MonthlyPurchase = 0;
MonthlySlotReward = 0;
}

public void ResetDailySlotExecuteCount()
public void ResetDailyState()
{
TodaySlotExecuteCount = 0;
}
Expand Down Expand Up @@ -52,7 +54,9 @@ public SlotExecuteResult ExecuteSlot()

var price = MasterManager.SettingMaster.PricePerSlotOnce;
var result = SlotManager.Instance.Execute();
MonthlyPurchase += price - (int)(NumberUtility.GetPercentFromPermillage(result.ResultRatePermillage) * price);
var reward = (int)(NumberUtility.GetPercentFromPermillage(result.ResultRatePermillage) * price);
MonthlyPurchase += price;
MonthlySlotReward += reward;
TodaySlotExecuteCount++;
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion Events/DailyResetPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class DailyResetPresenter : SchedulerJobPresenterBase
protected override async Task MainAsync()
{
await using var app = AppService.CreateSession();
await app.Users.ForEachAsync(user => user.ResetDailySlotExecuteCount());
await app.Users.ForEachAsync(user => user.ResetDailyState());
await app.SaveChangesAsync();

// 排出確率を変える
Expand Down
16 changes: 11 additions & 5 deletions Events/MonthlyResetPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,31 @@ protected override async Task MainAsync()
{
await using var app = AppService.CreateSession();

var summaryUsers = await app.Users
var purchaseRankingUsers = await app.Users
.OrderByDescending(user => user.MonthlyPurchase)
.Take(MasterManager.SettingMaster.PurchaseInfoRankingViewUserCount)
.Select(x => x.DeepCopy())
.ToListAsync();
var slotRewardRankingUsers = await app.Users
.OrderByDescending(user => user.MonthlySlotReward)
.Take(MasterManager.SettingMaster.PurchaseInfoRankingViewUserCount)
.Select(x => x.DeepCopy())
.ToListAsync();

await app.Users.ForEachAsync(user => user.ResetMonthlyPurchase());
await app.Users.ForEachAsync(user => user.ResetMonthlyState());
await app.SaveChangesAsync();

await SendSummaryAsync(summaryUsers);
await SendSummaryAsync(purchaseRankingUsers, slotRewardRankingUsers);
}

private async Task SendSummaryAsync(IReadOnlyList<User> rankingUsers)
private async Task SendSummaryAsync(IReadOnlyList<User> purchaseRankingUsers, IReadOnlyList<User> slotRewardRankingUsers)
{
var embed = new EmbedBuilder()
.WithColor(Color.LightOrange)
.WithTitle(Format.Bold($"{IssoUtility.SmileStamp} †今月も貢げカス† {IssoUtility.SmileStamp}"))
.WithDescription("月が変わったから課金額をリセットした")
.AddField("先月のランキング", PurchaseUtility.CreateRankingView(rankingUsers))
.AddField("先月の課金額ランキング", RankingUtility.CreatePurchaseView(purchaseRankingUsers))
.AddField("先月の利益ランキング", RankingUtility.CreateSlotRewardView(slotRewardRankingUsers))
.WithCurrentTimestamp()
.Build();

Expand Down
13 changes: 9 additions & 4 deletions Events/PurchaseInfoCommandPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,25 @@ protected override async Task MainAsync()
await using var app = AppService.CreateSession();

var selfUser = await app.FindOrCreateUserAsync(Message.Author.Id);
var rankingUsers = await app.Users
var purchaseRankingUsers = await app.Users
.OrderByDescending(user => user.MonthlyPurchase)
.Take(MasterManager.SettingMaster.PurchaseInfoRankingViewUserCount)
.ToListAsync();
var slotRewardRankingUsers = await app.Users
.OrderByDescending(user => user.MonthlySlotReward)
.Take(MasterManager.SettingMaster.PurchaseInfoRankingViewUserCount)
.ToListAsync();

await SendReplyAsync(selfUser, rankingUsers);
await SendReplyAsync(selfUser, purchaseRankingUsers, slotRewardRankingUsers);
}

private async Task SendReplyAsync(User selfUser, IReadOnlyList<User> rankingUsers)
private async Task SendReplyAsync(User selfUser, IReadOnlyList<User> purchaseRankingUsers, IReadOnlyList<User> slotRewardRankingUsers)
{
var embed = new EmbedBuilder()
.WithColor(Color.LightOrange)
.AddField("おまえの今月の課金額", $"{selfUser.MonthlyPurchase:N0}†カス†(税込)", inline: true)
.AddField("ランキング", PurchaseUtility.CreateRankingView(rankingUsers))
.AddField("課金額ランキング", RankingUtility.CreatePurchaseView(purchaseRankingUsers))
.AddField("利益ランキング", RankingUtility.CreateSlotRewardView(slotRewardRankingUsers))
.WithCurrentTimestamp()
.Build();

Expand Down
26 changes: 0 additions & 26 deletions Events/PurchaseUtility.cs

This file was deleted.

44 changes: 44 additions & 0 deletions Events/RankingUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Text;
using Approvers.King.Common;
using Discord;

namespace Approvers.King.Events;

public static class RankingUtility
{
public static string CreatePurchaseView(IReadOnlyList<User> rankingUsers)
{
var embedBuilder = new StringBuilder();
var order = 1;
foreach (var user in rankingUsers)
{
var scoreText = Math.Min(999_999_999, user.MonthlyPurchase).ToString("N0");
var whiteSpace = Math.Max(0, 11 - scoreText.Length);
var line =
Format.Code($"#{order:D2} - {"".PadLeft(whiteSpace, ' ')}{scoreText}†カス†(税込)") + " " +
MentionUtils.MentionUser(user.DiscordID);
embedBuilder.AppendLine(line);
order++;
}

return embedBuilder.ToString();
}

public static string CreateSlotRewardView(IReadOnlyList<User> rankingUsers)
{
var embedBuilder = new StringBuilder();
var order = 1;
foreach (var user in rankingUsers)
{
var scoreText = Math.Min(999_999_999, user.MonthlySlotReward).ToString("N0");
var whiteSpace = Math.Max(0, 11 - scoreText.Length);
var line =
Format.Code($"#{order:D2} - {"".PadLeft(whiteSpace, ' ')}{scoreText}†カス†(税込)") + " " +
MentionUtils.MentionUser(user.DiscordID);
embedBuilder.AppendLine(line);
order++;
}

return embedBuilder.ToString();
}
}
10 changes: 5 additions & 5 deletions Events/SlotExecutePresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected override async Task MainAsync()

var slotMessage = await Message.ReplyAsync(CreateSlotMessage(result, 0));
var purchaseMessage =
await slotMessage.Channel.SendMessageAsync(CreatePurchaseMessage(result, user.MonthlyPurchase, false));
await slotMessage.Channel.SendMessageAsync(CreatePurchaseMessage(result, user, false));

await Task.Delay(TimeSpan.FromSeconds(1));
var reelCount = result.ReelItems.Length;
Expand All @@ -31,7 +31,7 @@ protected override async Task MainAsync()
}

await purchaseMessage.ModifyAsync(prop =>
prop.Content = CreatePurchaseMessage(result, user.MonthlyPurchase, true));
prop.Content = CreatePurchaseMessage(result, user, true));
}

private static string CreateSlotMessage(SlotExecuteResult result, int openReelCount)
Expand All @@ -56,7 +56,7 @@ private static string CreateSlotMessage(SlotExecuteResult result, int openReelCo
return sb.ToString();
}

private static string CreatePurchaseMessage(SlotExecuteResult result, int monthlyPurchase, bool isOpen)
private static string CreatePurchaseMessage(SlotExecuteResult result, User user, bool isOpen)
{
var sb = new StringBuilder();

Expand All @@ -79,11 +79,11 @@ private static string CreatePurchaseMessage(SlotExecuteResult result, int monthl

if (isOpen == false)
{
sb.AppendLine("おまえの今月の課金額 → ???");
sb.AppendLine("おまえの今月の利益 → ???");
}
else
{
sb.AppendLine($"おまえの今月の課金額 → {monthlyPurchase:N0}†カス†(税込)");
sb.AppendLine($"おまえの今月の利益 → {user.MonthlySlotReward:N0}†カス†(税込)");
}

return sb.ToString();
Expand Down
71 changes: 71 additions & 0 deletions Migrations/20241124155902_AddSlotReward.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/20241124155902_AddSlotReward.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 AddSlotReward : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "MonthlySlotReward",
table: "Users",
type: "INTEGER",
nullable: false,
defaultValue: 0);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "MonthlySlotReward",
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>("MonthlySlotReward")
.HasColumnType("INTEGER");

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

Expand Down
Loading