Skip to content

Commit

Permalink
Merge pull request #125 from ca45382/feature/command-interpret
Browse files Browse the repository at this point in the history
汎用コマンド解釈処理部分の作成
  • Loading branch information
ca45382 committed Aug 15, 2021
2 parents dc189ea + ed4d26a commit aa91f5e
Show file tree
Hide file tree
Showing 37 changed files with 1,426 additions and 1,255 deletions.
72 changes: 72 additions & 0 deletions Attribute/CommandAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using PriconneBotConsoleApp.DataType;
using PriconneBotConsoleApp.Extension;

namespace PriconneBotConsoleApp.Attribute
{
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class CommandAttribute : System.Attribute
{
public CommandAttribute(
string[] names = null,
int minArgumentLength = 0,
int maxArgumentLength = int.MaxValue,
params ChannelFeatureType[] compatibleChannels)
{
Names = names ?? new[] { string.Empty };
MinArgumentLength = minArgumentLength;
MaxArgumentLength = maxArgumentLength;
CompatibleChannels = compatibleChannels.Length == 0 ? new[] { ChannelFeatureType.All } : compatibleChannels;
}

public CommandAttribute(
string name,
int minArgumentLength = 0,
int maxArgumentLength = int.MaxValue,
params ChannelFeatureType[] compatibleChannels)
: this(
name == null ? null : new[] { name },
minArgumentLength,
maxArgumentLength,
compatibleChannels)
{
}

public CommandAttribute(
AttackType attackType,
int minArgumentLength = 0,
int maxArgumentLength = int.MaxValue,
params ChannelFeatureType[] compatibleChannels)
: this(
attackType.GetMultiDescription().Names,
minArgumentLength,
maxArgumentLength,
compatibleChannels)
{
}

/// <summary>
/// 受け取ったコマンドを格納する
/// </summary>
public IReadOnlyList<string> Names { get; }

/// <summary>
/// 引数の長さの最小値
/// </summary>
public int MinArgumentLength { get; }

/// <summary>
/// 引数の長さの最大値
/// </summary>
public int MaxArgumentLength { get; }

/// <summary>
/// コマンドが対応するチャンネル
/// </summary>
public IReadOnlyList<ChannelFeatureType> CompatibleChannels { get; }

public bool IsCompatibleArgumentLength(int argLength)
=> MinArgumentLength <= argLength && argLength <= MaxArgumentLength;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace PriconneBotConsoleApp.Attribute
{
public struct MultiDescriptionData
{
public string LongDescription;
public string ShortDescription;
public string[] Aliases;
public string LongDescription { get; init; }
public string ShortDescription { get; init; }
public IReadOnlyList<string> Aliases { get; init; }

private string[] m_Names;

public string[] Names
=> m_Names ??= Aliases.Append(LongDescription).Append(ShortDescription).ToArray();
}

[AttributeUsage(AttributeTargets.Field)]
public class MultiDescriptionAttribute : System.Attribute
{
public MultiDescriptionAttribute(
string longDescription,
string shortDescription = null,
string longDescription,
string shortDescription = null,
params string[] aliases)
{
Data = new MultiDescriptionData
Expand All @@ -26,5 +33,5 @@ public class MultiDescriptionAttribute : System.Attribute
}

public MultiDescriptionData Data { get; }
}
}
}
52 changes: 52 additions & 0 deletions DataModel/CommandEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Discord.WebSocket;
using PriconneBotConsoleApp.Database;
using PriconneBotConsoleApp.DataType;
using PriconneBotConsoleApp.Extension;

namespace PriconneBotConsoleApp.DataModel
{
public class CommandEventArgs : EventArgs
{
public CommandEventArgs(SocketUserMessage socketUserMessage)
{
SocketUserMessage = socketUserMessage;

if (SocketUserMessage.Content.Length == 0)
{
throw new ArgumentException("Content.Length が 0");
}

var splitContents = SocketUserMessage.Content.ZenToHan().Split(' ', StringSplitOptions.RemoveEmptyEntries);
Name = splitContents[0];
Arguments = splitContents.Length > 1 ? splitContents.Skip(1).ToList() : Array.Empty<string>();
User = SocketUserMessage.Author as SocketGuildUser;
Channel = SocketUserMessage.Channel as SocketTextChannel;
PlayerData = DatabasePlayerDataController.LoadPlayerData(Channel.Guild.Id, SocketUserMessage.Author.Id);
Role = Channel.Guild.GetRole(PlayerData?.ClanData.ClanRoleID ?? 0);

if (Role == null)
{
return;
}

ClanData = DatabaseClanDataController.LoadClanData(Role);

ChannelFeatureType =
(ChannelFeatureType?)ClanData.ChannelData.FirstOrDefault(x => x.ChannelID == Channel.Id)?.FeatureID
?? ChannelFeatureType.All;
}

public SocketUserMessage SocketUserMessage { get; }
public string Name { get; }
public IReadOnlyList<string> Arguments { get; }
public SocketGuildUser User { get; }
public SocketTextChannel Channel { get; }
public SocketRole Role { get; }
public ClanData ClanData { get; }
public PlayerData PlayerData { get; }
public ChannelFeatureType ChannelFeatureType { get; }
}
}
70 changes: 18 additions & 52 deletions DataModel/Shiori/ClanData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using PriconneBotConsoleApp.DataType;
using PriconneBotConsoleApp.Extension;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
Expand Down Expand Up @@ -82,68 +83,33 @@ public void SetBossLap(int bossNum, int bossLap)
};
}

public int GetBossLap(BossNumberType bossNumberType)
=> GetBossLap((int)bossNumberType);

public void SetBossLap(BossNumberType bossNumberType, int bossLap)
=> SetBossLap((int)bossNumberType, bossLap);

/// <summary>
/// 最も周回数を返す
/// クラン内のチャンネルIDを返す。
/// </summary>
/// <param name="channelFeatureType"></param>
/// <returns></returns>
public int GetMinBossLap()
=> Enumerable.Min(new int[] { Boss1Lap, Boss2Lap, Boss3Lap, Boss4Lap, Boss5Lap });
public ulong GetChannelID(ChannelFeatureType channelFeatureType)
=> ChannelData?.GetChannelID(ClanID, channelFeatureType) ?? 0;

/// <summary>
/// 5つのボスデータから今のボスに変換。来月削除
/// クラン内のメッセージIDを返す
/// </summary>
/// <param name="clanData"></param>
/// <param name="messageFeatureType"></param>
/// <returns></returns>
[Obsolete]
public byte GetNowBoss()
{
if (Boss1Lap == Boss2Lap
&& Boss2Lap == Boss3Lap
&& Boss3Lap == Boss4Lap
&& Boss4Lap == Boss5Lap)
{
return (byte)BossNumberType.Boss5Number;
}
if (Boss1Lap == Boss2Lap + 1 )
{
return (byte)BossNumberType.Boss1Number;
}
else if (Boss2Lap == Boss3Lap + 1)
{
return (byte)BossNumberType.Boss2Number;
}
else if (Boss3Lap == Boss4Lap + 1)
{
return (byte)BossNumberType.Boss3Number;
}
else if (Boss4Lap == Boss5Lap + 1)
{
return (byte)BossNumberType.Boss4Number;
}

return 0;
}
public ulong GetMessageID(MessageFeatureType messageFeatureType)
=> MessageData?.GetMessageID(ClanID, messageFeatureType) ?? 0;

/// <summary>
/// 5つのボスデータから今のLapに変換。来月削除。
/// 最も小さい周回数を返す
/// </summary>
/// <param name="clanData"></param>
/// <returns></returns>
[Obsolete]
public ushort GetNowLap()
{
if (Boss1Lap == Boss2Lap
&& Boss2Lap == Boss3Lap
&& Boss3Lap == Boss4Lap
&& Boss4Lap == Boss5Lap)
{
return Boss5Lap;
}
else
{
return Boss1Lap;
}
}
public int GetMinBossLap()
=> Enumerable.Min(new int[] { Boss1Lap, Boss2Lap, Boss3Lap, Boss4Lap, Boss5Lap });
}

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

[MultiDescription("ニャル", "", "n", "N")]
NewYearKyaru,
NewYearKaryl,

[MultiDescription("持ち越し", "", "-", "持越し", "持越")]
CarryOver,
Expand Down
1 change: 1 addition & 0 deletions DataType/BossNumberType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
public enum BossNumberType
{
Unknown = 0,
Boss1Number = 1,
Boss2Number = 2,
Boss3Number = 3,
Expand Down
10 changes: 5 additions & 5 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}~{1}です。")]

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

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

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

Expand Down
8 changes: 6 additions & 2 deletions DataType/InfomationType.cs → DataType/InformationType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@

namespace PriconneBotConsoleApp.DataType
{
public enum InfomationType
public enum InformationType
{
Unknown,

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

//持ち越し関連
[Description("持ち越しをすべて削除しました。\nこのメッセージは{0}秒後削除されます。")]
DeleteAllCarryOverData,
}
}
9 changes: 2 additions & 7 deletions DataType/ProgressStatus.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;

namespace PriconneBotConsoleApp.DataType
{
Expand All @@ -25,6 +20,6 @@ public enum ProgressStatus : byte
SOS,

[Description("🏃")]
CarryOver,
SubdueBoss,
}
}
3 changes: 3 additions & 0 deletions DataType/ShioriFeature/ChannelFeatureType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ public enum ChannelFeatureType : uint
{
Unknown = 0,

/// <summary>すべてのチャンネルで利用できるコマンドにつけられる.</summary>
All = 9999,

DeclareID = 1001,
ReserveID = 1002,
ReserveResultID = 1003,
Expand Down
15 changes: 15 additions & 0 deletions DataType/WarningType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PriconneBotConsoleApp.DataType
{
public enum WarningType
{
[Description("コメントが長いので切り取られました。\n 問題がある場合は予約削除をして再度予約してください。") ]
TooLongComment,
}
}
3 changes: 2 additions & 1 deletion Database/Shiori/DatabaseProgressController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ public static IEnumerable<ProgressData> GetProgressData(ClanData clanData, BossN

public static IEnumerable<ProgressData> GetProgressData(PlayerData playerData, BossNumberType bossNumber)
{
var playerID = playerData?.PlayerID ?? 0;
using var databaseConnector = new DatabaseConnector();

return databaseConnector.ProgressData.AsQueryable()
.Where(x => x.PlayerID == playerData.PlayerID && x.BossNumber == (byte)bossNumber && !x.DeleteFlag)
.Where(x => x.PlayerID == playerID && x.BossNumber == (byte)bossNumber && !x.DeleteFlag)
.ToArray();
}

Expand Down
Loading

0 comments on commit aa91f5e

Please sign in to comment.