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

汎用コマンド解釈処理部分の作成 #125

Merged
merged 50 commits into from
Aug 15, 2021
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
3316a22
汎用コマンド解釈処理の作成
ca45382 Jul 26, 2021
3d8a1fb
コマンド処理部分の作成
ca45382 Jul 26, 2021
2e98ab9
持ち越し部分をコマンドに対応
ca45382 Jul 28, 2021
f5e6dc5
CommandMapper周辺をリファクタリング
cllightz Jul 28, 2021
82a0473
コンパイルエラーを修正
ca45382 Jul 30, 2021
c64873c
キャルの綴り変更
ca45382 Jul 30, 2021
0e97437
AttackTypeを入れたときにString配列にするように対応
ca45382 Jul 30, 2021
755555d
持ち越し処理をコマンドに適用
ca45382 Jul 30, 2021
66e7b59
GetProgressDataにNullチェックを追加
ca45382 Aug 1, 2021
7ebe7af
チャンネル・メッセージのIDを返すメソッドを追加
ca45382 Aug 1, 2021
6d8b735
BossNumberにUnknownを追加
ca45382 Aug 1, 2021
2212119
PlayerDataを追加
ca45382 Aug 1, 2021
d18b929
コマンドに対応
ca45382 Aug 1, 2021
1b6aa2b
不要なusingを削除
ca45382 Aug 1, 2021
ad784f2
凸報告機能に適用
ca45382 Aug 8, 2021
9da05b1
予約をコマンドに追加
ca45382 Aug 9, 2021
21f8267
変数名の変更
ca45382 Aug 9, 2021
3606b64
TaskKillの部分を仮で追加
ca45382 Aug 10, 2021
1834602
Invokeにtry_catchを設定
ca45382 Aug 13, 2021
8fb67d3
ファイル名の変更
ca45382 Aug 14, 2021
49c441e
Enumの名称変更
ca45382 Aug 14, 2021
acc7c55
冗長な変数代入を修正
ca45382 Aug 14, 2021
d0cc363
リファクタリング
ca45382 Aug 14, 2021
888ff4a
配列にしなくていい箇所を修正
ca45382 Aug 14, 2021
96ddb7d
リファクタリング
ca45382 Aug 14, 2021
260ba7c
使用しなくなった関数を削除
ca45382 Aug 14, 2021
a0da883
リファクタリング
ca45382 Aug 14, 2021
e7d6658
エラーメッセージの修正+リファクタリング
ca45382 Aug 14, 2021
9abd6f7
6ボス以上を登録できてしまうバグを修正
ca45382 Aug 14, 2021
52850f8
予約が削除できないバグを修正
ca45382 Aug 14, 2021
d9e4b0c
さいころとTL変換器を追加
ca45382 Aug 14, 2021
eec9174
イベント表示機能を追加
ca45382 Aug 14, 2021
e3d9948
ボス番号と時間の判定の可読性を向上
ca45382 Aug 15, 2021
fc0ba36
コメントを修正
ca45382 Aug 15, 2021
90bd8d3
コード整形
ca45382 Aug 15, 2021
0d71a3e
不要な空白を削除
ca45382 Aug 15, 2021
69105b6
誤字修正
ca45382 Aug 15, 2021
1ed4c87
周回数を判定する箇所の可読性を向上
ca45382 Aug 15, 2021
da27ac2
不要なusingとリファクタリング
ca45382 Aug 15, 2021
f84ed20
return前の空行を追加
ca45382 Aug 15, 2021
43ebe30
null判定を追加
ca45382 Aug 15, 2021
f33dd54
TODOの追加 #131
ca45382 Aug 15, 2021
c306bcb
Exceptionのコメントを追加
ca45382 Aug 15, 2021
20e3633
リファクタリング
ca45382 Aug 15, 2021
b3fd9bd
IsValidDamageを追加
ca45382 Aug 15, 2021
3dd9edc
OrderByしてLastしてたのを関数化
ca45382 Aug 15, 2021
12d47a3
TODOを追加
ca45382 Aug 15, 2021
13d4d69
コード整形
ca45382 Aug 15, 2021
fb4255e
shortをushortに修正
ca45382 Aug 15, 2021
ed4d26a
タイポ修正
ca45382 Aug 15, 2021
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
8 changes: 4 additions & 4 deletions DataType/ErrorType.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.ComponentModel;

namespace PriconneBotConsoleApp.DataType
{
Expand All @@ -12,10 +9,13 @@ public enum ErrorType
//予約関連
[Description("予約に失敗しました。")]
FailedReservation,

[Description("予約できません。予約可能時間は{0}:00~{1}:00です。")]
OutOfReservationTime,

[Description("コメントが長いので切り取られました。\n 問題がある場合は予約削除をして再度予約してください。")]
TooLongComment,

[Description("予約できません。予約は{0}周目まで可能です。")]
OutOfReservationBossLaps,

Expand Down
2 changes: 1 addition & 1 deletion DataType/InfomationType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public enum InfomationType

// 凸報告関連
[Description("<@{0}>の凸報告を代理削除しました。\nこのメッセージは{1}秒後削除されます。")]
DeleteInsted,
DeleteInstead,

//持ち越し関連
[Description("持ち越しをすべて削除しました。\nこのメッセージは{0}秒後削除されます。")]
Expand Down
4 changes: 2 additions & 2 deletions Database/Shiori/DatabaseReservationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static List<ReservationData> LoadReservationData(PlayerData playerData)

public static IEnumerable<ReservationData> LoadReservationData(ClanData clanData, int bossNumber)
{
if (bossNumber < CommonDefine.MinBossNumber || bossNumber > CommonDefine.MaxBossNumber)
if (!CommonDefine.IsValidBossNumber(bossNumber))
{
return null;
}
Expand All @@ -66,7 +66,7 @@ public static IEnumerable<ReservationData> LoadReservationData(ClanData clanData

public static List<ReservationData> LoadBossLapReservationData(ClanData clanData, int bossNumber)
{
if (bossNumber < CommonDefine.MinBossNumber || bossNumber > CommonDefine.MaxBossNumber)
if (!CommonDefine.IsValidBossNumber(bossNumber))
{
return null;
}
Expand Down
3 changes: 3 additions & 0 deletions Define/ClanBattleDefine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
public static class ClanBattleDefine
{
public const short MaxLapNumber = 250;

public static bool IsValidLapNumber(int value)
=> 0 <= value && value <= MaxLapNumber;
}
}
9 changes: 9 additions & 0 deletions Define/CommonDefine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,14 @@ public static class CommonDefine
public const int MaxDamageValue = 999999;

public const int DisplayDamageUnit = 10000;

public static bool IsValidBossNumber(int value)
=> MinBossNumber <= value && value <= MaxBossNumber;

public static bool IsValidBattleTime(int value)
=> MinBattleTime <= value && value <= MaxBattleTime;

public static bool IsValidDamageValue(int value)
=> 0 <= value && value <= MaxDamageValue;
}
}
22 changes: 22 additions & 0 deletions Extension/EnumerableExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,27 @@ public static void ForEach<T>([NotNull] this IEnumerable<T> source, [NotNull] Ac
action(element);
}
}

public static TSource MaxBy<TSource, TValue>([NotNull] this IEnumerable<TSource> source, [NotNull] Func<TSource, TValue> func)
where TValue : IComparable<TValue>
{
var firstFlag = true;
TValue maxValue = default;
TSource maxSource = default;

foreach (var element in source)
{
var value = func(element);

if (firstFlag || value.CompareTo(maxValue) > 0)
{
maxValue = value;
maxSource = element;
firstFlag = false;
}
}

return maxSource;
}
}
}
10 changes: 5 additions & 5 deletions Script/ClanBattle/BattleCarryOver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public void UpdateCarryOverData()

if (!byte.TryParse(m_CommandEventArgs.Arguments[0], out var bossNumber)
|| !byte.TryParse(m_CommandEventArgs.Arguments[1], out var remainTime)
|| bossNumber < CommonDefine.MinBossNumber || bossNumber > CommonDefine.MaxBossNumber
|| remainTime < CommonDefine.MinBattleTime || remainTime > CommonDefine.MaxBattleTime
|| !CommonDefine.IsValidBossNumber(bossNumber)
|| !CommonDefine.IsValidBattleTime(remainTime)
|| playerData == null)
{
return;
Expand Down Expand Up @@ -131,14 +131,14 @@ public void DeleteOtherPlayerData()
var targetUser = MentionUtils.TryParseUser(m_CommandEventArgs.Arguments[0], out var userID)
|| ulong.TryParse(m_CommandEventArgs.Arguments[0], out userID)
? m_CommandEventArgs.Role.Guild.GetUser(userID)
: throw new ArgumentNullException();
: throw new ArgumentNullException("ユーザー情報がありません。");

// コマンドは `!rm @削除対象のユーザー 古い方から何番目か` としている。
if (!byte.TryParse(m_CommandEventArgs.Arguments[1], out var number)
|| number <= 0
|| CommonDefine.MaxReportNumber < number)
{
throw new ArgumentOutOfRangeException();
throw new ArgumentOutOfRangeException("持ち越しがありません。");
}

var playerData = DatabasePlayerDataController.LoadPlayerData(m_CommandEventArgs.Role, targetUser.Id);
Expand Down Expand Up @@ -172,7 +172,7 @@ private void DeletePlayerCarryOverData(PlayerData playerData, byte deleteNumber)

if (carryOverList.Length == 0)
{
throw new ArgumentOutOfRangeException();
throw new ArgumentOutOfRangeException("持ち越しがありません。");
}

DatabaseCarryOverController.DeleteCarryOverData(carryOverList[(deleteNumber > 0 && deleteNumber <= carryOverList.Length) ? deleteNumber - 1 : 0]);
Expand Down
34 changes: 19 additions & 15 deletions Script/ClanBattle/BattleProgress.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -38,7 +37,7 @@ public string GetNameWithData()

return string.Join(
halfSizeWhitespace,
((ProgressStatus) ProgressData.Status).ToLabel(),
((ProgressStatus)ProgressData.Status).ToLabel(),
ProgressData.CarryOverFlag ? "持" : fullSizeWhitespace,
$"{ProgressData.Damage,6}@{ProgressData.RemainTime:D2}",
((AttackType)ProgressData.AttackType).ToShortLabel(),
Expand All @@ -62,7 +61,7 @@ public BattleProgress(CommandEventArgs commandEventArgs)
public async Task Start()
{
if (!ushort.TryParse(m_CommandEventArgs.Arguments[0], out var lap)
|| lap > ClanBattleDefine.MaxLapNumber || lap <= 0)
|| !ClanBattleDefine.IsValidLapNumber(lap))
{
return;
}
Expand Down Expand Up @@ -112,29 +111,33 @@ public async Task UpdateAttackData(AttackType attackType)
/// <returns></returns>
public async Task UpdateDamageData()
{
// TODO : progressDataFlagでやりくりしているのが非常に見にくいので修正したい
var progressDataFlag = false;
ca45382 marked this conversation as resolved.
Show resolved Hide resolved
uint damageNumber = 0;
int damageNumber = 0;
byte remainTimeNumber = 0;

// TODO : 冗長なRegexの高速化 #131
if (Regex.IsMatch(m_CommandEventArgs.Name, @"\d+万$"))
{
if (!uint.TryParse(Regex.Match(m_CommandEventArgs.Name, @"\d+").ToString(), out damageNumber)
|| damageNumber > CommonDefine.MaxDamageValue)
if (!int.TryParse(Regex.Match(m_CommandEventArgs.Name, @"\d+").ToString(), out damageNumber)
|| CommonDefine.IsValidDamageValue(damageNumber))
{
return;
}

progressDataFlag = true;
}

// TODO : 冗長なRegexの高速化 #131
if (Regex.IsMatch(m_CommandEventArgs.Name, @"\d+@\d+"))
{
var damageText = Regex.Match(m_CommandEventArgs.Name, @"\d+@").ToString();
var remainTimeText = Regex.Match(m_CommandEventArgs.Name, @"@\d+").ToString();

if (!uint.TryParse(Regex.Match(damageText, @"\d+").ToString(), out damageNumber)
if (!int.TryParse(Regex.Match(damageText, @"\d+").ToString(), out damageNumber)
|| !byte.TryParse(Regex.Match(remainTimeText, @"\d+").ToString(), out remainTimeNumber)
|| damageNumber > CommonDefine.MaxDamageValue || remainTimeNumber > CommonDefine.MaxBattleTime)
|| CommonDefine.IsValidDamageValue(damageNumber)
|| remainTimeNumber > CommonDefine.MaxBattleTime)
{
return;
}
Expand Down Expand Up @@ -162,7 +165,7 @@ public async Task UpdateDamageData()
userProgressData.Status = (byte)ProgressStatus.SOS;
}

userProgressData.Damage = damageNumber;
userProgressData.Damage = (uint)damageNumber;
userProgressData.RemainTime = remainTimeNumber;
userProgressData.CommentData = string.Join(" ", m_CommandEventArgs.Arguments);
await UpdateProgressData(userProgressData, m_CommandEventArgs.PlayerData);
Expand Down Expand Up @@ -283,7 +286,7 @@ private async Task ChangeLap(ushort lap = 0)
/// <returns></returns>
private void InitializeProgressData()
{
if(DatabaseProgressController.GetProgressData(m_CommandEventArgs.ClanData, m_BossNumberType) is { } deleteData)
if (DatabaseProgressController.GetProgressData(m_CommandEventArgs.ClanData, m_BossNumberType) is { } deleteData)
{
DatabaseProgressController.DeleteProgressData(deleteData);
}
Expand Down Expand Up @@ -330,15 +333,16 @@ private async Task SendClanProgressList(bool removeLastMessage = true)
private Embed CreateProgressList()
{
var clanPlayerDataList = DatabasePlayerDataController.LoadPlayerData(m_CommandEventArgs.ClanData);
var clanProgressData = DatabaseProgressController.GetProgressData(m_CommandEventArgs.ClanData, m_BossNumberType)
.OrderBy(x => x.Status).ThenByDescending(x => x.Damage).ThenBy(x => x.CreateDateTime)
.ToArray();

var progressPlayer = clanProgressData.Select(x =>
new PlayerInfo(clanPlayerDataList.FirstOrDefault(y => y.PlayerID == x.PlayerID),x))
var progressPlayer = DatabaseProgressController.GetProgressData(m_CommandEventArgs.ClanData, m_BossNumberType)
.OrderBy(x => x.Status)
.ThenByDescending(x => x.Damage)
.ThenBy(x => x.CreateDateTime)
.Select(x => new PlayerInfo(clanPlayerDataList.FirstOrDefault(y => y.PlayerID == x.PlayerID), x))
.ToArray();

var bossLap = m_CommandEventArgs.ClanData.GetBossLap(m_BossNumberType);

var bossData = RediveClanBattleData.BossDataList
.FirstOrDefault(x => x.BossNumber == (byte)m_BossNumberType && x.LapNumberFrom <= bossLap && (x.LapNumberTo == -1 || x.LapNumberTo >= bossLap));

Expand Down
11 changes: 6 additions & 5 deletions Script/ClanBattle/BattleReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void RegisterReportData()
{
var reportData = new ReportData();

// TODO : 冗長なRegexの高速化 #131
if (Regex.IsMatch(m_CommandEventArgs.Name, @"\d\D{1,3}"))
{
var bossNumber = int.Parse(Regex.Match(m_CommandEventArgs.Name, @"\d").Value);
Expand All @@ -69,7 +70,7 @@ public void RegisterReportData()
reportData.BossNumber = (byte)bossNumber;
}

if (reportData.BossNumber < CommonDefine.MinBossNumber || reportData.BossNumber > CommonDefine.MaxBossNumber)
if (!CommonDefine.IsValidBossNumber(reportData.BossNumber))
{
return;
}
Expand Down Expand Up @@ -120,7 +121,7 @@ public void DeleteReportData()
}

var removeData = DatabaseReportDataController.GetReportData(playerData)
.OrderBy(x => x.DateTime).Last();
.MaxBy(x => x.DateTime);

if (DatabaseReportDataController.DeleteReportData(removeData))
{
Expand All @@ -129,7 +130,7 @@ public void DeleteReportData()
if (playerData.UserID != m_CommandEventArgs.User.Id)
{
_ = m_CommandEventArgs.SocketUserMessage.Channel.SendTimedMessageAsync(TimeDefine.SuccessMessageDisplayTime,
string.Format(InfomationType.DeleteInsted.ToLabel(), playerData.UserID, TimeDefine.SuccessMessageDisplayTime));
string.Format(InfomationType.DeleteInstead.ToLabel(), playerData.UserID, TimeDefine.SuccessMessageDisplayTime));
}
}
}
Expand All @@ -140,9 +141,9 @@ public void DeleteReportData()
public void RegisterOtherUserReportData()
{
if ((!ulong.TryParse(m_CommandEventArgs.Arguments[0], out var registerUserID)
&& !MentionUtils.TryParseUser(m_CommandEventArgs.Arguments[0], out registerUserID))
&& !MentionUtils.TryParseUser(m_CommandEventArgs.Arguments[0], out registerUserID))
|| !byte.TryParse(m_CommandEventArgs.Arguments[1], out var bossNumber)
|| bossNumber > CommonDefine.MaxBossNumber || bossNumber < CommonDefine.MinBossNumber
|| !CommonDefine.IsValidBossNumber(bossNumber)
|| !EnumMapper.TryParse<AttackType>(m_CommandEventArgs.Arguments[2], out var attackType))
{
return;
Expand Down
24 changes: 13 additions & 11 deletions Script/ClanBattle/BattleReservation.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
using PriconneBotConsoleApp.Database;
using PriconneBotConsoleApp.DataModel;
using PriconneBotConsoleApp.DataType;
Expand Down Expand Up @@ -36,8 +32,10 @@ public void RegisterReserveData()
TimeDefine.ErrorMessageDisplayTime,
string.Format(ErrorType.OutOfReservationTime.ToLabel(),
$"{m_CommandEventArgs.ClanData.ReservationStartTime.Hours}",
$"{m_CommandEventArgs.ClanData.ReservationEndTime.Hours}")
);
$"{m_CommandEventArgs.ClanData.ReservationEndTime.Hours}"
)
);

return;
}

Expand All @@ -49,6 +47,7 @@ public void RegisterReserveData()
TimeDefine.ErrorMessageDisplayTime,
ErrorType.FailedReservation.ToLabel()
);

return;
}

Expand All @@ -62,6 +61,7 @@ public void RegisterReserveData()
TimeDefine.ErrorMessageDisplayTime,
string.Format(ErrorType.OutOfReservationBossLaps.ToLabel(), allowReservationLap.ToString())
);

return;
}

Expand All @@ -79,7 +79,7 @@ public void DeleteReserveData()
{
var deleteReservationData = MessageToReservationData();

if (deleteReservationData is null)
if (deleteReservationData == null)
{
return;
}
Expand All @@ -104,8 +104,10 @@ public void DeleteReserveData()
/// <returns></returns>
private ReservationData MessageToReservationData()
{
if (!Enum.TryParse<BossNumberType>(m_CommandEventArgs.Arguments[1], out var bossNumber)
|| !(byte.TryParse(m_CommandEventArgs.Arguments[0], out var battleLap) && battleLap > m_CommandEventArgs.ClanData.GetBossLap(bossNumber)))
if (!byte.TryParse(m_CommandEventArgs.Arguments[1], out var bossNumber)
|| !byte.TryParse(m_CommandEventArgs.Arguments[0], out var battleLap)
|| !CommonDefine.IsValidBossNumber(bossNumber)
|| !CommonDefine.IsValidBattleTime(battleLap))
{
return null;
}
Expand All @@ -122,7 +124,7 @@ private ReservationData MessageToReservationData()
{
PlayerID = m_CommandEventArgs.PlayerData.PlayerID,
BattleLap = battleLap,
BossNumber = (byte)bossNumber,
BossNumber = bossNumber,
CommentData = commentData,
};
}
Expand All @@ -147,7 +149,7 @@ private void RegisterReservationData(ReservationData reservationData)

private bool DeleteUserReservationData(ReservationData reservationData)
{
var userReservationDataList = DatabaseReservationController.LoadReservationData(reservationData.PlayerData);
var userReservationDataList = DatabaseReservationController.LoadReservationData(m_CommandEventArgs.PlayerData);

var sqlReservationData = userReservationDataList
.Where(x => x.BossNumber == reservationData.BossNumber && x.BattleLap == reservationData.BattleLap)
Expand Down
5 changes: 3 additions & 2 deletions Script/ClanBattle/BattleReservationSummary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task RunInteraction(SocketMessageComponent messageComponent)
return;
}

if(buttonType == ButtonType.Reload)
if (buttonType == ButtonType.Reload)
{
await UpdateMessage();
}
Expand Down Expand Up @@ -105,6 +105,7 @@ private Embed CreateEmbed()
var reservationDataSet = DatabaseReservationController.LoadReservationData(m_ClanData);
List<List<ReservationData>> reservationDataList = new();

// TODO : Linqで行けそうな気がする
for (var i = 0; i < CommonDefine.MaxBossNumber; i++)
{
reservationDataList.Add(new List<ReservationData>());
Expand All @@ -119,7 +120,7 @@ private Embed CreateEmbed()

if (!reservationDataList[i].Any())
{
// 何かの空白代入して空行を生成している
// 空行を保つためのゼロ幅空白(\u200b)を挿入している
fieldBuilder.Value = "\n\u200b";
}
else
Expand Down
Loading