Skip to content

Commit

Permalink
协程bug修复, session分类为内存session和储存session, cmd Waiter新增多个构造器
Browse files Browse the repository at this point in the history
  • Loading branch information
Saplonily committed Jan 6, 2023
1 parent c65ce75 commit d8499a8
Show file tree
Hide file tree
Showing 14 changed files with 607 additions and 29 deletions.
32 changes: 31 additions & 1 deletion SaladimQBot.Extensions/-Service/CommandWaiter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class CommandWaiter : EventWaiter

public override EventWaiterChecker Checker { get; }

public CommandWaiter(SimCommandExecuter simCommandExecuter, Func<MethodBasedCommand, CommandContent, object[]?, bool> checker)
public CommandWaiter(SimCommandExecuter simCommandExecuter, Func<MethodBasedCommand, CommandContent, object[], bool> checker)
{
simCommandExecuter.OnCommandExecuted += this.SimCommandExecutor_OnCommandExecuted;
this.checker = checker;
Expand Down Expand Up @@ -40,6 +40,36 @@ public CommandWaiter(SimCommandExecuter simCommandExecuter, IUser executor, stri

}

public CommandWaiter(SimCommandExecuter simCommandExecuter, IUser executor, string cmdName, Action<object[]> argsReporter)
{
simCommandExecuter.OnCommandExecuted += this.SimCommandExecutor_OnCommandExecuted;
this.checker = (cmd, content, checkerArgs) =>
{
if (content.Executor.IsSameUser(executor) && cmd.Name == cmdName)
{
argsReporter(checkerArgs);
return true;
}
return false;
};
Checker = DefaultChecker;
}

public CommandWaiter(SimCommandExecuter simCommandExecuter, IGroupUser executor, string cmdName, Action<object[]> argsReporter)
{
simCommandExecuter.OnCommandExecuted += this.SimCommandExecutor_OnCommandExecuted;
this.checker = (cmd, content, checkerArgs) =>
{
if (content.Executor is IGroupUser groupUser && groupUser.IsSameGroupUser(executor) && cmd.Name == cmdName)
{
argsReporter(checkerArgs);
return true;
}
return false;
};
Checker = DefaultChecker;
}

protected bool DefaultChecker(IIClientEvent clientEvent)
{
if (curState)
Expand Down
11 changes: 10 additions & 1 deletion SaladimQBot.Extensions/-Service/CoroutineService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ public void AddNewCoroutine(IEnumerator<EventWaiter> enumerator)
}
}

/// <summary>
/// 移除一个协程
/// </summary>
/// <param name="enumerator"></param>
public void RemoveCoroutine(IEnumerator<EventWaiter> enumerator)
{
coroutines.Remove(enumerator);
}

/// <summary>
/// 使用一个事件推动该协程
/// 在遇到EventWaiter时会停止并且等待该事件
Expand All @@ -51,8 +60,8 @@ public void PushCoroutines(IIClientEvent clientEvent)
{
if (markedRemoves.Contains(coroutines[i]))
{
coroutines.RemoveAt(i);
markedRemoves.Remove(coroutines[i]);
coroutines.RemoveAt(i);
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions SaladimQBot.Extensions/-Service/ISession.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace SaladimQBot.Extensions;

public interface ISession
{
SessionId SessionId { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SaladimQBot.Extensions;

public interface ISessionService
public interface IStoreSessionService
{
TSession GetSession<TSession>(SessionId sessionId) where TSession : class, ISession, new();

Expand All @@ -11,20 +11,15 @@ public interface ISessionService

public static class ISessionServiceExtensions
{
public static TSession GetUserSession<TSession>(this ISessionService service, long userId)
public static TSession GetUserSession<TSession>(this IStoreSessionService service, long userId)
where TSession : class, ISession, new()
=> service.GetSession<TSession>(new SessionId(userId));

public static TSession GetGroupSession<TSession>(this ISessionService service, long groupId)
public static TSession GetGroupSession<TSession>(this IStoreSessionService service, long groupId)
where TSession : class, ISession, new()
=> service.GetSession<TSession>(new SessionId(0, groupId));

public static TSession GetGroupUserSession<TSession>(this ISessionService service, long groupId, long userId)
public static TSession GetGroupUserSession<TSession>(this IStoreSessionService service, long groupId, long userId)
where TSession : class, ISession, new()
=> service.GetSession<TSession>(new SessionId(userId, groupId));
}

public interface ISession
{
SessionId SessionId { get; set; }
}
71 changes: 71 additions & 0 deletions SaladimQBot.Extensions/-Service/MemorySessionService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;

namespace SaladimQBot.Extensions;

public class MemorySessionService
{
protected Dictionary<Type, Dictionary<SessionId, ISession>> allSessions;

public MemorySessionService()
{
allSessions = new();
}

public TSession GetSession<TSession>(SessionId sessionId) where TSession : class, ISession, new()
{
Type typeSession = typeof(TSession);
if (allSessions.TryGetValue(typeSession, out var sessions))
{
if (sessions.TryGetValue(sessionId, out var session))
{
return (TSession)session;
}
else
{
TSession newSession = new()
{
SessionId = sessionId
};
sessions.Add(sessionId, newSession);
return newSession;
}
}
else
{
allSessions.Add(typeSession, new());
return GetSession<TSession>(sessionId);
}
}

public bool TryRemoveSession<TSession>(SessionId sessionId) where TSession : class, ISession, new()
{
Type typeSession = typeof(TSession);
if (allSessions.TryGetValue(typeSession, out var sessions))
{
if (sessions.TryGetValue(sessionId, out var session))
{
sessions.Remove(session.SessionId);
return true;
}
}
return false;
}

public TSession GetUserSession<TSession>(long userId) where TSession : class, ISession, new()
=> GetSession<TSession>(new SessionId(userId));

public TSession GetGroupSession<TSession>(long groupId) where TSession : class, ISession, new()
=> GetSession<TSession>(new SessionId(0, groupId));

public TSession GetGroupUserSession<TSession>(long groupId, long userId) where TSession : class, ISession, new()
=> GetSession<TSession>(new SessionId(userId, groupId));

public bool TryRemoveUserSession<TSession>(long userId) where TSession : class, ISession, new()
=> TryRemoveSession<TSession>(new SessionId(userId));

public bool TryRemoveGroupSession<TSession>(long groupId) where TSession : class, ISession, new()
=> TryRemoveSession<TSession>(new SessionId(0, groupId));

public bool TryRemoveGroupUserSession<TSession>(long groupId, long userId) where TSession : class, ISession, new()
=> TryRemoveSession<TSession>(new SessionId(userId, groupId));
}
10 changes: 5 additions & 5 deletions SaladimQBot.Extensions/-Service/SessionSqliteService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace SaladimQBot.Extensions;

public class SessionSqliteService : ISessionService
public class SessionSqliteService : IStoreSessionService
{
protected SQLiteConnection sqliteConnection;

Expand All @@ -16,7 +16,7 @@ public TSession GetSession<TSession>(SessionId sessionId) where TSession : ISess
{
lock (sqliteConnection)
{
if (!typeof(SqliteSession).IsAssignableFrom(typeof(TSession)))
if (!typeof(SqliteStoreSession).IsAssignableFrom(typeof(TSession)))
throw new InvalidOperationException("The session type must subclass SqliteSession.");
try
{
Expand Down Expand Up @@ -45,14 +45,14 @@ public void SaveSession<TSession>(TSession session)
sqliteConnection.InsertOrReplace(session);
}

TSession ISessionService.GetSession<TSession>(SessionId sessionId)
TSession IStoreSessionService.GetSession<TSession>(SessionId sessionId)
=> this.GetSession<TSession>(sessionId);

void ISessionService.SaveSession<TSession>(TSession session)
void IStoreSessionService.SaveSession<TSession>(TSession session)
=> this.SaveSession(session);
}

public abstract class SqliteSession : ISession
public abstract class SqliteStoreSession : ISession
{
[PrimaryKey, Column("session_string")]
public string SessionString
Expand Down
10 changes: 5 additions & 5 deletions SaladimQBot.Extensions/CommonTypeParsers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static class CommonTypeParsers
public static Vector2 Vector2(string s)
{
//以逗号分隔
string[] ps = s.Split(',');
string[] ps = s.Split(',', ',');
if (ps.Length != 2) throw new CommonTypeParseFailedException();
//允许括号
if (ps[0].StartsWith("(") && ps[1].EndsWith(")"))
Expand All @@ -32,7 +32,7 @@ public static Vector2 Vector2(string s)
public static Vector3 Vector3(string s)
{
//以逗号分隔
string[] ps = s.Split(',');
string[] ps = s.Split(',', ',');
if (ps.Length != 3) throw new CommonTypeParseFailedException();
//允许括号
if (ps[0].StartsWith("(") && ps[2].EndsWith(")"))
Expand Down Expand Up @@ -78,12 +78,12 @@ public static Color Color(string s)
}
}
//假设是以逗号分隔的形式
else if (s.Contains(','))
else if (s.Contains(',') || s.Contains(','))
{
//不是小数的形式
if (!s.Contains('.'))
{
string[] nums = s.Split(',');
string[] nums = s.Split(',', ',');
if (nums.Length == 3)
{
int r = int.Parse(nums[0]);
Expand All @@ -107,7 +107,7 @@ public static Color Color(string s)
//含小数点, 是小数形式
else
{
string[] nums = s.Split(',');
string[] nums = s.Split(',', ',');
if (nums.Length == 3)
{
float r = float.Parse(nums[0]);
Expand Down
4 changes: 3 additions & 1 deletion SaladimWpf/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ public App()
coll.AddSingleton<JavaScriptService>();
coll.AddSingleton<IntegralCalculatorService>();
coll.AddSingleton<Auto1A2BService>();
coll.AddSingleton<ISessionService, SessionSqliteService>();
coll.AddSingleton<SessionSqliteService>();
#if DEBUG
coll.AddSingleton(new SessionSqliteServiceConfig(new(@"D:\User\Desktop\SaladimWPF\data\debug.db")));
#elif !DEBUG
coll.AddSingleton(new SessionSqliteServiceConfig(new(@"D:\User\Desktop\SaladimWPF\data\release.db")));
#endif
coll.AddSingleton<CoroutineService>();
coll.AddSingleton<FiveInARowService>();
coll.AddSingleton<MemorySessionService>();
})
.Build();
serviceProvider = AppHost.Services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<History>True|2023-01-05T08:55:14.3920225Z;True|2023-01-05T15:08:31.6491096+08:00;False|2023-01-05T15:08:19.9781972+08:00;False|2023-01-05T15:05:41.3082089+08:00;True|2023-01-05T11:17:02.6841461+08:00;True|2023-01-05T11:01:56.5159682+08:00;True|2023-01-05T11:01:16.6067244+08:00;True|2023-01-05T11:00:41.0894016+08:00;True|2023-01-04T21:55:42.5301452+08:00;True|2023-01-04T21:54:10.5989202+08:00;True|2023-01-03T21:30:00.6144582+08:00;True|2023-01-02T20:23:10.2277035+08:00;True|2023-01-02T20:15:03.4479581+08:00;True|2023-01-02T20:14:30.6578219+08:00;True|2023-01-02T20:13:35.9810001+08:00;True|2023-01-02T20:12:49.5190240+08:00;True|2023-01-02T18:41:02.6217807+08:00;True|2023-01-02T18:32:44.8093539+08:00;True|2023-01-02T18:11:08.6941887+08:00;True|2023-01-02T16:33:42.1850536+08:00;True|2023-01-02T16:32:26.4861060+08:00;True|2023-01-02T14:29:37.5313187+08:00;True|2023-01-02T14:29:05.0817055+08:00;True|2023-01-01T11:56:47.6765386+08:00;True|2022-12-31T20:47:29.3442163+08:00;True|2022-12-31T19:53:31.4897608+08:00;True|2022-12-31T19:09:27.5849193+08:00;True|2022-12-31T18:34:51.6835134+08:00;True|2022-12-31T18:33:07.6356083+08:00;True|2022-12-31T18:31:44.3594272+08:00;True|2022-12-31T16:30:33.5907953+08:00;False|2022-12-31T16:29:57.1706009+08:00;True|2022-12-30T22:23:38.8870651+08:00;True|2022-12-30T20:13:20.9119842+08:00;True|2022-12-30T20:08:30.3908541+08:00;True|2022-12-30T16:07:17.2492927+08:00;True|2022-12-30T11:02:36.4689010+08:00;True|2022-12-30T10:05:08.1779874+08:00;True|2022-12-30T10:03:14.7093706+08:00;True|2022-12-29T23:30:06.6475000+08:00;True|2022-12-29T23:26:41.2422368+08:00;True|2022-12-29T23:26:02.2951705+08:00;True|2022-12-29T22:36:46.4297516+08:00;True|2022-12-29T17:08:57.3370321+08:00;True|2022-12-29T17:00:01.7295036+08:00;True|2022-12-28T22:08:08.7670608+08:00;True|2022-12-28T17:27:29.8124543+08:00;True|2022-12-28T16:24:51.9861635+08:00;True|2022-12-28T16:23:44.1004953+08:00;True|2022-12-28T16:21:08.2353403+08:00;True|2022-12-28T16:20:56.6429409+08:00;True|2022-12-23T19:22:48.4392831+08:00;True|2022-12-21T22:23:35.9045939+08:00;True|2022-12-21T22:18:16.0962966+08:00;True|2022-12-21T20:54:02.6827032+08:00;True|2022-12-21T20:52:52.1827338+08:00;True|2022-12-21T20:52:06.8220342+08:00;True|2022-12-21T20:51:17.2555457+08:00;True|2022-12-18T21:48:23.3182037+08:00;False|2022-12-18T21:46:45.4066089+08:00;False|2022-12-18T21:46:32.1453050+08:00;True|2022-12-18T21:45:56.8222951+08:00;True|2022-12-18T19:21:18.8207927+08:00;True|2022-12-17T22:46:09.3784351+08:00;True|2022-12-17T21:59:08.8587742+08:00;True|2022-12-17T20:48:28.8245676+08:00;True|2022-12-17T20:34:01.3071499+08:00;True|2022-12-17T20:32:23.5134227+08:00;True|2022-12-17T18:31:00.3332961+08:00;True|2022-12-17T12:29:27.2257800+08:00;True|2022-12-17T11:06:37.8176899+08:00;True|2022-12-15T08:00:13.6842411+08:00;True|2022-12-13T12:10:12.2530920+08:00;True|2022-12-13T10:10:50.0512184+08:00;True|2022-12-13T10:08:30.0309694+08:00;True|2022-12-13T10:04:52.6876847+08:00;True|2022-12-12T20:14:30.0623921+08:00;True|2022-12-12T20:13:38.1238116+08:00;True|2022-12-12T20:12:07.0936964+08:00;True|2022-12-11T13:10:18.2140245+08:00;True|2022-12-11T13:08:51.2427844+08:00;True|2022-12-10T13:00:16.6605746+08:00;True|2022-12-09T23:07:24.8079525+08:00;True|2022-12-09T19:58:04.0989594+08:00;True|2022-12-09T19:56:42.7195927+08:00;True|2022-12-09T19:53:31.8804030+08:00;True|2022-12-09T19:45:39.8557456+08:00;True|2022-11-29T10:02:12.9644217+08:00;True|2022-11-28T20:07:05.4948835+08:00;True|2022-11-28T20:06:52.1724049+08:00;True|2022-11-28T12:58:30.0455236+08:00;True|2022-11-28T12:56:29.6927989+08:00;True|2022-11-28T12:55:41.4295095+08:00;True|2022-11-28T12:53:49.0933314+08:00;True|2022-11-28T12:51:28.8301278+08:00;True|2022-11-28T12:50:46.0058585+08:00;True|2022-11-28T12:50:01.8716847+08:00;True|2022-11-28T12:49:25.7150025+08:00;</History>
<History>True|2023-01-06T08:37:40.8079568Z;True|2023-01-06T16:31:24.6067714+08:00;True|2023-01-06T16:26:29.1946179+08:00;True|2023-01-06T14:14:35.1902279+08:00;True|2023-01-05T17:03:22.1394638+08:00;True|2023-01-05T16:55:14.3920225+08:00;True|2023-01-05T15:08:31.6491096+08:00;False|2023-01-05T15:08:19.9781972+08:00;False|2023-01-05T15:05:41.3082089+08:00;True|2023-01-05T11:17:02.6841461+08:00;True|2023-01-05T11:01:56.5159682+08:00;True|2023-01-05T11:01:16.6067244+08:00;True|2023-01-05T11:00:41.0894016+08:00;True|2023-01-04T21:55:42.5301452+08:00;True|2023-01-04T21:54:10.5989202+08:00;True|2023-01-03T21:30:00.6144582+08:00;True|2023-01-02T20:23:10.2277035+08:00;True|2023-01-02T20:15:03.4479581+08:00;True|2023-01-02T20:14:30.6578219+08:00;True|2023-01-02T20:13:35.9810001+08:00;True|2023-01-02T20:12:49.5190240+08:00;True|2023-01-02T18:41:02.6217807+08:00;True|2023-01-02T18:32:44.8093539+08:00;True|2023-01-02T18:11:08.6941887+08:00;True|2023-01-02T16:33:42.1850536+08:00;True|2023-01-02T16:32:26.4861060+08:00;True|2023-01-02T14:29:37.5313187+08:00;True|2023-01-02T14:29:05.0817055+08:00;True|2023-01-01T11:56:47.6765386+08:00;True|2022-12-31T20:47:29.3442163+08:00;True|2022-12-31T19:53:31.4897608+08:00;True|2022-12-31T19:09:27.5849193+08:00;True|2022-12-31T18:34:51.6835134+08:00;True|2022-12-31T18:33:07.6356083+08:00;True|2022-12-31T18:31:44.3594272+08:00;True|2022-12-31T16:30:33.5907953+08:00;False|2022-12-31T16:29:57.1706009+08:00;True|2022-12-30T22:23:38.8870651+08:00;True|2022-12-30T20:13:20.9119842+08:00;True|2022-12-30T20:08:30.3908541+08:00;True|2022-12-30T16:07:17.2492927+08:00;True|2022-12-30T11:02:36.4689010+08:00;True|2022-12-30T10:05:08.1779874+08:00;True|2022-12-30T10:03:14.7093706+08:00;True|2022-12-29T23:30:06.6475000+08:00;True|2022-12-29T23:26:41.2422368+08:00;True|2022-12-29T23:26:02.2951705+08:00;True|2022-12-29T22:36:46.4297516+08:00;True|2022-12-29T17:08:57.3370321+08:00;True|2022-12-29T17:00:01.7295036+08:00;True|2022-12-28T22:08:08.7670608+08:00;True|2022-12-28T17:27:29.8124543+08:00;True|2022-12-28T16:24:51.9861635+08:00;True|2022-12-28T16:23:44.1004953+08:00;True|2022-12-28T16:21:08.2353403+08:00;True|2022-12-28T16:20:56.6429409+08:00;True|2022-12-23T19:22:48.4392831+08:00;True|2022-12-21T22:23:35.9045939+08:00;True|2022-12-21T22:18:16.0962966+08:00;True|2022-12-21T20:54:02.6827032+08:00;True|2022-12-21T20:52:52.1827338+08:00;True|2022-12-21T20:52:06.8220342+08:00;True|2022-12-21T20:51:17.2555457+08:00;True|2022-12-18T21:48:23.3182037+08:00;False|2022-12-18T21:46:45.4066089+08:00;False|2022-12-18T21:46:32.1453050+08:00;True|2022-12-18T21:45:56.8222951+08:00;True|2022-12-18T19:21:18.8207927+08:00;True|2022-12-17T22:46:09.3784351+08:00;True|2022-12-17T21:59:08.8587742+08:00;True|2022-12-17T20:48:28.8245676+08:00;True|2022-12-17T20:34:01.3071499+08:00;True|2022-12-17T20:32:23.5134227+08:00;True|2022-12-17T18:31:00.3332961+08:00;True|2022-12-17T12:29:27.2257800+08:00;True|2022-12-17T11:06:37.8176899+08:00;True|2022-12-15T08:00:13.6842411+08:00;True|2022-12-13T12:10:12.2530920+08:00;True|2022-12-13T10:10:50.0512184+08:00;True|2022-12-13T10:08:30.0309694+08:00;True|2022-12-13T10:04:52.6876847+08:00;True|2022-12-12T20:14:30.0623921+08:00;True|2022-12-12T20:13:38.1238116+08:00;True|2022-12-12T20:12:07.0936964+08:00;True|2022-12-11T13:10:18.2140245+08:00;True|2022-12-11T13:08:51.2427844+08:00;True|2022-12-10T13:00:16.6605746+08:00;True|2022-12-09T23:07:24.8079525+08:00;True|2022-12-09T19:58:04.0989594+08:00;True|2022-12-09T19:56:42.7195927+08:00;True|2022-12-09T19:53:31.8804030+08:00;True|2022-12-09T19:45:39.8557456+08:00;True|2022-11-29T10:02:12.9644217+08:00;True|2022-11-28T20:07:05.4948835+08:00;True|2022-11-28T20:06:52.1724049+08:00;True|2022-11-28T12:58:30.0455236+08:00;True|2022-11-28T12:56:29.6927989+08:00;True|2022-11-28T12:55:41.4295095+08:00;True|2022-11-28T12:53:49.0933314+08:00;True|2022-11-28T12:51:28.8301278+08:00;</History>
<LastFailureDetails />
</PropertyGroup>
</Project>
Loading

0 comments on commit d8499a8

Please sign in to comment.