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

添加插件:智能区域 #380

Merged
merged 7 commits into from
Aug 9, 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: 10 additions & 0 deletions Plugin.sln
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Misc", "Yaaiomni\Misc\Misc.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SourceGen", "Yaaiomni\SourceGen\SourceGen.csproj", "{0CD78DBF-DC60-4B91-B5EF-4910172547CE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmartRegions", "SmartRegions\SmartRegions.csproj", "{CBB56AB5-84D6-460A-8D0C-DA85A03BA811}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1086,6 +1088,14 @@ Global
{0CD78DBF-DC60-4B91-B5EF-4910172547CE}.Release|Any CPU.Build.0 = Release|Any CPU
{0CD78DBF-DC60-4B91-B5EF-4910172547CE}.Release|x64.ActiveCfg = Release|Any CPU
{0CD78DBF-DC60-4B91-B5EF-4910172547CE}.Release|x64.Build.0 = Release|Any CPU
{CBB56AB5-84D6-460A-8D0C-DA85A03BA811}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CBB56AB5-84D6-460A-8D0C-DA85A03BA811}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CBB56AB5-84D6-460A-8D0C-DA85A03BA811}.Debug|x64.ActiveCfg = Debug|Any CPU
{CBB56AB5-84D6-460A-8D0C-DA85A03BA811}.Debug|x64.Build.0 = Debug|Any CPU
{CBB56AB5-84D6-460A-8D0C-DA85A03BA811}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CBB56AB5-84D6-460A-8D0C-DA85A03BA811}.Release|Any CPU.Build.0 = Release|Any CPU
{CBB56AB5-84D6-460A-8D0C-DA85A03BA811}.Release|x64.ActiveCfg = Release|Any CPU
{CBB56AB5-84D6-460A-8D0C-DA85A03BA811}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
| [CaiCustomEmojiCommand](CaiCustomEmojiCommand/README.md) | 自定义表情命令 | 无 |
| [BetterWhitelist](BetterWhitelist/README.md) | 白名单插件 | 无 |
| [AutoReset](AutoReset/README.md) | 完全自动重置 | 无 |
| [SmartRegions](SmartRegions/README.md) | 智能区域 | 无 |

</Details>

Expand Down
72 changes: 72 additions & 0 deletions SmartRegions/DBConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Data.Sqlite;
using TShockAPI;
using System.IO;

namespace SmartRegions
{
sealed class DBConnection
{
SqliteConnection Connection;

public void Initialize()
{
string folderPath = Path.Combine(TShock.SavePath, "SmartRegions");
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
string path = Path.Combine(folderPath, "SmartRegions.sqlite");
Connection = new SqliteConnection($"Data Source='{path}'");
Connection.Open();
var command = new SqliteCommand(
"CREATE TABLE IF NOT EXISTS Regions (" +
" Name TEXT PRIMARY KEY," +
" Command TEXT," +
" Cooldown REAL)",
Connection);
command.ExecuteNonQuery();
}

public List<SmartRegion> GetRegions()
{
var result = new List<SmartRegion>();
var command = new SqliteCommand("SELECT Name, Command, Cooldown FROM Regions", Connection);
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
string name = reader.GetString(0);
string cmd = reader.GetString(1);
double cooldown = reader.GetDouble(2);
result.Add(new SmartRegion { name = name, command = cmd, cooldown = cooldown });
}
}
return result;
}

public async Task SaveRegion(SmartRegion region)
{
var command = new SqliteCommand(
"INSERT OR REPLACE INTO Regions (Name, Command, Cooldown) VALUES (@nm, @cmd, @cool)",
Connection);
command.Parameters.Add(new SqliteParameter("@nm", region.name));
command.Parameters.Add(new SqliteParameter("@cmd", region.command));
command.Parameters.Add(new SqliteParameter("@cool", region.cooldown));
await command.ExecuteNonQueryAsync();
}

public async Task RemoveRegion(string region)
{
var command = new SqliteCommand("DELETE FROM Regions WHERE Name = @nm", Connection);
command.Parameters.Add(new SqliteParameter("@nm", region));
await command.ExecuteNonQueryAsync();
}

public void Close()
{
Connection?.Close();
}
}
}
Loading
Loading