Skip to content

Commit

Permalink
Crazy Frog #152
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyl18 committed Sep 10, 2022
1 parent 4cde539 commit 278675d
Show file tree
Hide file tree
Showing 9 changed files with 542 additions and 164 deletions.
22 changes: 13 additions & 9 deletions WFBot/Features/Other/CustomCommandMatcherHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
Expand Down Expand Up @@ -49,17 +50,17 @@ void PrintCustomCommands()
foreach (var info in _commandInfos.Value)
{
var commandID = info.Method.Name;
Console.WriteLine($"ID: {commandID}");
Trace.WriteLine($"ID: {commandID}");
if (ContainsCustomCommand(commandID))
{
Console.WriteLine($" 自定义匹配器: [{GetCustomCommandList(commandID).Connect()}]\n");
Trace.WriteLine($" 自定义匹配器: [{GetCustomCommandList(commandID).Connect()}]\n");
found = true;
}
}

if (!found)
{
Console.WriteLine("没有一个命令有自定义匹配器.");
Trace.WriteLine("没有一个命令有自定义匹配器.");
}
}

Expand All @@ -69,19 +70,19 @@ void PrintCommands()
foreach (var info in _commandInfos.Value)
{
var commandID = info.Method.Name;
Console.WriteLine($"ID: {commandID}");
Trace.WriteLine($"ID: {commandID}");
if (info.Method.IsAttributeDefined<MatchersAttribute>())
{
Console.WriteLine($" 预定义匹配器: [{info.Method.GetCustomAttribute<MatchersAttribute>().Matchers.Connect()}]");
Trace.WriteLine($" 预定义匹配器: [{info.Method.GetCustomAttribute<MatchersAttribute>().Matchers.Connect()}]");
}
if (info.Method.IsAttributeDefined<MatchersIgnoreCaseAttribute>())
{
Console.WriteLine($" 预定义匹配器(不区分大小写): [{info.Method.GetCustomAttribute<MatchersIgnoreCaseAttribute>().Matchers.Connect()}]");
Trace.WriteLine($" 预定义匹配器(不区分大小写): [{info.Method.GetCustomAttribute<MatchersIgnoreCaseAttribute>().Matchers.Connect()}]");
}

if (ContainsCustomCommand(commandID))
{
Console.WriteLine($" 自定义匹配器: [{GetCustomCommandList(commandID).Connect()}]\n");
Trace.WriteLine($" 自定义匹配器: [{GetCustomCommandList(commandID).Connect()}]\n");
}
}
}
Expand Down Expand Up @@ -133,8 +134,11 @@ string RemoveCustomCommandMatcher(string commandID, string matcher)

void SaveConfig() => CustomCommandConfig.Save();

public Action<Message> MessageSender { get; } = (s) => Console.WriteLine(s);
public Action<Message> ErrorMessageSender { get; } = s => Console.WriteLine(s);
public Action<Message> MessageSender { get; } = (s) =>
{
Trace.WriteLine(s);
};
public Action<Message> ErrorMessageSender { get; } = s => Trace.WriteLine(s);
public OrichaltContext O { get; }
public string Message { get; }

Expand Down
8 changes: 6 additions & 2 deletions WFBot/Features/Resource/WFResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal static async Task InitWFResource()
WFChineseApi = new WFChineseAPI();
ThreadPool.SetMinThreads(64, 64);

await Task.WhenAll(
var task = new List<Task> {
Task.Run(SetWFCDResources),
Task.Run(SetWFContentApi),
// Task.Run(() => { WFAApi = new WFAApi(); }),
Expand All @@ -47,7 +47,11 @@ await Task.WhenAll(
Task.Run(async () => WFBotTranslateData = await GetWFBotTranslateApi()),
Task.Run(async () => RWildcardAndSlang = await GetWildcardAndSlang()),
Task.Run(() => WildCardSearcher = WildCardSearcher.Create())
);
};
foreach (var task1 in task)
{
await task1;
}
WFTranslator = new WFTranslator();
Weaponinfos = GetWeaponInfos();

Expand Down
60 changes: 38 additions & 22 deletions WFBot/WFBotCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using WFBot.Utils;
using WFBot.WebUI;
using WFBot.Windows;
using static System.Net.Mime.MediaTypeNames;

#pragma warning disable 164

Expand Down Expand Up @@ -80,18 +81,28 @@ public static async Task Main(string[] args)
}
catch (Exception e)
{
Console.WriteLine("WFBot 在初始化中遇到了问题.");
Trace.WriteLine("WFBot 在初始化中遇到了问题.");
Trace.WriteLine($"你现在可以在 WebUI http://localhost:{WFBotWebUIServer.GetServerPort()} 中修改设置.");
Trace.WriteLine(e);
var sw = Stopwatch.StartNew();
if (!skipPressKey)
{
Console.WriteLine("按任意键继续.");
Console.ReadKey();
}

if (sw.ElapsedMilliseconds < 200)
{
Console.WriteLine("触发 Console.ReadKey 的时间较短, 可能是在 docker 下运行, 为了保证 WebUI 的正常运行, 将不结束程序.");
Thread.CurrentThread.Join();
}
return;
}

wfbot.Run();
}

public static bool Panic { get; private set; }
}
public sealed class WFBotCore
{
Expand Down Expand Up @@ -157,25 +168,31 @@ public void Run()
Thread.CurrentThread.Join();
}

switch (text.ToLower())
{
case "ui":
OpenWFBotSettingsWindow();
break;
case "webui":
OpenWebUI();
break;
case "exit":
case "stop":
Shutdown();
return;
default:
if (!(new CustomCommandMatcherHandler(text.TrimStart('/'))).ProcessCommandInput().Result.matched)
{
// todo ConnectorManager.Connector.OnCommandLineInput(text);
}
break;
}
OnConsoleCommand(text);

}
}

public void OnConsoleCommand(string s)
{
switch (s.ToLower())
{
case "ui":
OpenWFBotSettingsWindow();
break;
case "webui":
OpenWebUI();
break;
case "exit":
case "stop":
Shutdown();
return;
default:
if (!(new CustomCommandMatcherHandler(s.TrimStart('/'))).ProcessCommandInput().Result.matched)
{
// todo ConnectorManager.Connector.OnCommandLineInput(text);
}
break;
}
}

Expand Down Expand Up @@ -322,8 +339,6 @@ public async Task Init()
{UseShellExecute = true});
}
Thread.CurrentThread.Join();

Shutdown();
}
/*while (Config.Instance.Miguel_Platform == MessagePlatform.Unknown && !IsTest)
{
Expand Down Expand Up @@ -430,6 +445,7 @@ private void InitLogger()
{ TraceOutputOptions = TraceOptions.Timestamp };
Trace.Listeners.Add(fileListener);
}

Trace.Listeners.Add(new ConsoleTraceListener());
Trace.Listeners.Add(new WebLogTraceListener());
Trace.AutoFlush = true;
Expand Down
25 changes: 23 additions & 2 deletions WFBot/WebUI/Pages/Features.razor
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
@page "/feature"
@using WFBot.Orichalt
@using System.Diagnostics
<PageTitle>一些功能</PageTitle>
<h1>功能</h1>
<div>
<h2>超级广播</h2>
<input type="text" class="form-control my-3" @bind="broadcastContent"/>
<p>向所有开启了通知功能的群发送一条广播</p>
<input type="text" class="form-control my-3" @bind="broadcastContent" />
<button class="btn btn-danger" @onclick="() => ModalShow()">发送!</button>
<h2 class="mt-4">删除所有缓存</h2>
<p>如果因为缓存原因不能载入机器人, 可以在这里删除</p>
<button class="btn btn-danger" @onclick="() => DeleteCache()">删除</button>

</div>

@if (showModal)
Expand Down Expand Up @@ -38,7 +44,7 @@

@code {
// 抄代码 https://gist.github.com/conficient/ba98d1662c659e170ec16650acea05c8
string broadcastContent;
string broadcastContent;
bool showModal = false;

void ModalShow() => showModal = true;
Expand All @@ -48,4 +54,19 @@
MiguelNetwork.Broadcast(broadcastContent);
showModal = false;
}

void DeleteCache()
{
foreach (var file in Directory.GetFiles("WFCaches"))
{
try
{
File.Delete(file);
}
catch (Exception e)
{
Trace.WriteLine($"缓存 {file} 删除失败: {e}");
}
}
}
}
21 changes: 21 additions & 0 deletions WFBot/WebUI/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,21 @@
</div>
</div>
</div>
<h2 class="my-2">执行控制台命令</h2>
<div class="row mb-5">
<div class="col-sm-10">
<input type="text" @bind="command" class="form-control"/>
</div>
<div class="col-sm-2">
<button class="btn btn-primary" @onclick="ExecuteCommand">执行</button>
</div>
</div>



@code
{
string command;
Timer updateTimer;
protected override void OnInitialized()
{
Expand All @@ -51,4 +62,14 @@
{
InvokeAsync(StateHasChanged);
}

void ExecuteCommand()
{
var c = command;
Task.Run(() =>
{
WFBotCore.Instance.OnConsoleCommand(c);
});
command = "";
}
}
7 changes: 0 additions & 7 deletions WFBot/WebUI/Pages/InvasionSettings.razor

This file was deleted.

Loading

0 comments on commit 278675d

Please sign in to comment.