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

fix: ServerTools restApi request error #626

Merged
merged 5 commits into from
Dec 28, 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
84 changes: 56 additions & 28 deletions src/ServerTools/Config.cs
Original file line number Diff line number Diff line change
@@ -1,89 +1,117 @@
using Newtonsoft.Json;
using LazyAPI.ConfigFiles;
using Newtonsoft.Json;

namespace ServerTools;


public class Config
public class Config : JsonConfigBase<Config>
{
[JsonProperty("死亡延续")]
[LocalizedPropertyName(CultureType.Chinese, "死亡延续")]
[LocalizedPropertyName(CultureType.English, "DeathLast")]
public bool DeathLast = true;

[JsonProperty("限制哨兵数量")]
[LocalizedPropertyName(CultureType.Chinese, "限制哨兵数量")]
[LocalizedPropertyName(CultureType.English, "SentryLimit")]
public int sentryLimit = 20;

[JsonProperty("限制召唤物数量")]
[LocalizedPropertyName(CultureType.Chinese, "限制召唤物数量")]
[LocalizedPropertyName(CultureType.English, "SummonLimit")]
public int summonLimit = 11;

[JsonProperty("仅允许软核进入")]
[LocalizedPropertyName(CultureType.Chinese, "仅允许软核进入")]
[LocalizedPropertyName(CultureType.English, "OnlySoftCoresAreAllowed")]
public bool OnlySoftCoresAreAllowed = false;

[JsonProperty("是否设置世界模式")]
[LocalizedPropertyName(CultureType.Chinese, "是否设置世界模式")]
[LocalizedPropertyName(CultureType.English, "SetWorldMode")]
public bool SetWorldMode = true;

[JsonProperty("世界模式")]
[LocalizedPropertyName(CultureType.Chinese, "世界模式")]
[LocalizedPropertyName(CultureType.English, "WorldMode")]
public int WorldMode = 2;

[JsonProperty("限制发言长度")]
[LocalizedPropertyName(CultureType.Chinese, "限制发言长度")]
[LocalizedPropertyName(CultureType.English, "ChatLength")]
public int ChatLength = 50;

[JsonProperty("设置旅途模式难度")]
[LocalizedPropertyName(CultureType.Chinese, "设置旅途模式难度")]
[LocalizedPropertyName(CultureType.English, "SetJourneyDifficult")]
public bool SetJourneyDifficult = false;

[JsonProperty("旅途模式难度")]
[LocalizedPropertyName(CultureType.Chinese, "禁用Emoji消息")]
[LocalizedPropertyName(CultureType.English, "DisableEmojiMessage")]
public bool DisableEmojiMessage = false;

[LocalizedPropertyName(CultureType.Chinese, "旅途模式难度")]
[LocalizedPropertyName(CultureType.English, "JourneyDifficult")]
public string JourneyMode = "master";

[JsonProperty("阻止未注册进入")]
[LocalizedPropertyName(CultureType.Chinese, "阻止未注册进入")]
[LocalizedPropertyName(CultureType.English, "BlockUnregisteredEntry")]
public bool BlockUnregisteredEntry = false;

[JsonProperty("禁止怪物捡钱")]
[LocalizedPropertyName(CultureType.Chinese, "禁止怪物捡钱")]
[LocalizedPropertyName(CultureType.English, "MonsterPickUpMoney")]
public bool PickUpMoney = true;

[JsonProperty("清理掉落物")]
[LocalizedPropertyName(CultureType.Chinese, "清理掉落物")]
[LocalizedPropertyName(CultureType.English, "ClearDrop")]
public bool ClearDrop = false;

[JsonProperty("死亡倒计时")]
[LocalizedPropertyName(CultureType.Chinese, "死亡倒计时")]
[LocalizedPropertyName(CultureType.English, "DeadTimer")]
public bool DeadTimer = false;

[JsonProperty("阻止死亡角色进入")]
[LocalizedPropertyName(CultureType.Chinese, "阻止死亡角色进入")]
[LocalizedPropertyName(CultureType.English, "PreventsDeath")]
public bool PreventsDeathStateJoin = true;

[JsonProperty("禁止双箱")]
[LocalizedPropertyName(CultureType.Chinese, "禁止双箱")]
[LocalizedPropertyName(CultureType.English, "KeepOpenChest")]
public bool KeepOpenChest = true;

[JsonProperty("禁止双饰品")]
[LocalizedPropertyName(CultureType.Chinese, "禁止双饰品")]
[LocalizedPropertyName(CultureType.English, "KeepArmor")]
public bool KeepArmor = true;

[JsonProperty("禁止肉前第七格饰品")]
[LocalizedPropertyName(CultureType.Chinese, "禁止肉前第七格饰品")]
[LocalizedPropertyName(CultureType.English, "KeepArmor2")]
public bool KeepArmor2 = true;

[JsonProperty("死亡倒计时格式")]
[LocalizedPropertyName(CultureType.Chinese, "死亡倒计时格式")]
[LocalizedPropertyName(CultureType.English, "DeadFormat")]
public string DeadFormat = "你还有{0}秒复活!";

[JsonProperty("未注册阻止语句")]
[LocalizedPropertyName(CultureType.Chinese, "未注册阻止语句")]
[LocalizedPropertyName(CultureType.English, "BlockEntryStatement")]
public string BlockEntryStatement = "未注册不能进入服务器";

[JsonProperty("未注册启动服务器执行命令")]
[LocalizedPropertyName(CultureType.Chinese, "未注册启动服务器执行命令")]
[LocalizedPropertyName(CultureType.English, "BlockEntryExecCommands")]
public string[] ResetExecCommands = Array.Empty<string>();

[JsonProperty("开启NPC保护", Order = 7)]
[LocalizedPropertyName(CultureType.English, "EnableNpcProtect")]
public bool NpcProtect = false;

[JsonProperty("NPC保护表", Order = 7)]
[LocalizedPropertyName(CultureType.English, "NpcProtects")]
public List<int> NpcProtectList = new();

[JsonProperty("禁止多鱼线", Order = 8)]
[LocalizedPropertyName(CultureType.English, "MultipleFishingRodsAreProhibited")]
public bool MultipleFishingRodsAreProhibited = true;

[JsonProperty("浮漂列表", Order = 8)]
[LocalizedPropertyName(CultureType.English, "ForbiddenBuoys")]
public List<short> ForbiddenBuoys = new();

public static Config Read(string PATH)
{
return File.Exists(PATH) ? JsonConvert.DeserializeObject<Config>(File.ReadAllText(PATH)) ?? new() : new();
}
protected override string Filename => "ServerTools";

public void Write(string PATH)
protected override void SetDefault()
{
File.WriteAllText(PATH, JsonConvert.SerializeObject(this, Formatting.Indented));
this.ForbiddenBuoys = new List<short>() { 360, 361, 362, 363, 364, 365, 366, 381, 382, 760, 775, 986, 987, 988, 989, 990, 991, 992, 993 };
this.NpcProtectList = new List<int>() { 17, 18, 19, 20, 38, 105, 106, 107, 108, 160, 123, 124, 142, 207, 208, 227, 228, 229, 353, 354, 376, 441, 453, 550, 579, 588, 589, 633, 663, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 375, 442, 443, 539, 444, 445, 446, 447, 448, 605, 627, 601, 613 };

}
}
61 changes: 29 additions & 32 deletions src/ServerTools/DB/PlayerDeath.cs
Original file line number Diff line number Diff line change
@@ -1,53 +1,50 @@
using MySql.Data.MySqlClient;
using LazyAPI.Database;
using LinqToDB;
using LinqToDB.Mapping;
using MySql.Data.MySqlClient;
using System.Data;
using TShockAPI;
using TShockAPI.DB;

namespace ServerTools.DB;

public class PlayerDeath : Dictionary<string, int>
[Table("Death")]
public class PlayerDeath : RecordBase<PlayerDeath>
{
private new int this[string key]
{
get => this.TryGetValue(key, out var result) ? result : 0;
[PrimaryKey, Identity]
[Column("Name")]
public string Name { get; set; } = string.Empty;

set => base[key] = value;
}
private readonly IDbConnection database;
public PlayerDeath()
{
this.database = TShock.DB;
var Skeleton = new SqlTable("Death",
new SqlColumn("Count", MySqlDbType.Int32) { Length = 255 },
new SqlColumn("Name", MySqlDbType.VarChar) { Length = 255, Unique = true }
);
var List = new SqlTableCreator(this.database, this.database.GetSqlType() == SqlType.Sqlite ? new SqliteQueryCreator() : new MysqlQueryCreator());
List.EnsureTableStructure(Skeleton);
this.ReadAll();
[Column("Count")]
public int Count { get; set; }


private static Context? _context;

public static Context Instance => _context ??= Db.Context<PlayerDeath>("Death");

public static PlayerDeath? GetPlayerDeath(string name)
{
return Instance.Records.FirstOrDefault(x => x.Name == name);
}

private void ReadAll()
public static List<PlayerDeath> GetDeathRank()
{
using var reader = this.database.QueryReader("SELECT * FROM Death");
while (reader.Read())
{
var Name = reader.Get<string>("Name");
var Count = reader.Get<int>("Count");
this[Name] = Count;
}
return Instance.Records.OrderByDescending(x => x.Count).ToList();
}

public void Add(string name)
public static void Add(string name)
{
if (this.ContainsKey(name))
var record = GetPlayerDeath(name);
if (record == null)
{
this[name] += 1;
this.database.Query("UPDATE Death SET Count = @0 WHERE Name = @1", this[name], name);
record = new PlayerDeath { Name = name, Count = 1 };
Instance.Insert(record);
}
else
{
this[name] = 1;
this.database.Query("INSERT INTO `Death` (`Name`, `Count`) VALUES (@0, @1)", name, 1);
record.Count++;
Instance.Update(record);
}
}
}
93 changes: 28 additions & 65 deletions src/ServerTools/DB/PlayerOnline.cs
Original file line number Diff line number Diff line change
@@ -1,87 +1,50 @@
using MySql.Data.MySqlClient;
using LazyAPI.Database;
using LinqToDB;
using LinqToDB.Mapping;
using System.Data;
using TShockAPI;
using TShockAPI.DB;

namespace ServerTools.DB;

public class PlayerOnline : Dictionary<string, int>
[Table("OnlineDuration")]
public class PlayerOnline : RecordBase<PlayerOnline>
{
private readonly HashSet<string> _players = new();
public new int this[string key]
{
get => this.TryGetValue(key, out var result) ? result : 0;
[PrimaryKey, Identity]
[Column("username")]
public string Name { get; set; } = string.Empty;

set => base[key] = value;
}
private readonly IDbConnection database;
public PlayerOnline()
{
this.database = TShock.DB;
var Skeleton = new SqlTable("OnlineDuration",
new SqlColumn("username", MySqlDbType.Text) { Length = 500 },
new SqlColumn("duration", MySqlDbType.Int32) { Length = 255 }
);
var List = new SqlTableCreator(this.database, this.database.GetSqlType() == SqlType.Sqlite ? new SqliteQueryCreator() : new MysqlQueryCreator());
List.EnsureTableStructure(Skeleton);
this.ReadAll();
}
[Column("duration")]
public int Duration { get; set; }

public void ReadAll()
{
using var reader = this.database.QueryReader("SELECT * FROM OnlineDuration");
while (reader.Read())
{
var username = reader.Get<string>("username");
var duration = reader.Get<int>("duration");
this[username] = duration;
this._players.Add(username);
}
}

public bool Read(string name, out int duration)
{
using var reader = this.database.QueryReader("SELECT * FROM `OnlineDuration` WHERE `username` LIKE @0", name);
if (reader.Read())
{
duration = reader.Get<int>("duration");
return true;
}
else
{
duration = 0;
return false;
}
}
private static Context? _context;

public bool Update(string Name, int duration)
{
return 1 == this.database.Query("UPDATE `OnlineDuration` SET `duration` = @0 WHERE `OnlineDuration`.`username` = @1", duration, Name);
public static Context Instance => _context ??= Db.Context<PlayerOnline>("Death");

}
public bool Insert(string Name, int duration)
public static PlayerOnline? GetPlayerOnline(string name)
{
this._players.Add(Name);
return 1 == this.database.Query("INSERT INTO `OnlineDuration` (`username`, `duration`) VALUES (@0, @1)", Name, duration);
return Instance.Records.FirstOrDefault(x => x.Name == name);
}

public static List<PlayerOnline> GetOnlineRank()
{
return Instance.Records.OrderByDescending(x => x.Duration).ToList();
}



public void AddOrUpdate(string name, int duration)
public static bool Add(string Name, int duration)
{
if (this._players.Contains(name))
var online = GetPlayerOnline(Name);
if (online == null)
{
this.Update(name, duration);
online = new PlayerOnline { Name = Name, Duration = duration };
Instance.Insert(online);
return true;
}
else
{
this.Insert(name, duration);
online.Duration += duration;
Instance.Update(online);
return false;
}
}

public void UpdateAll()
{
this.ForEach(x => this.AddOrUpdate(x.Key, x.Value));
}

}
8 changes: 3 additions & 5 deletions src/ServerTools/Plugin.Commamd.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Xna.Framework;
using ServerTools.DB;
using Terraria;
using TShockAPI;

Expand Down Expand Up @@ -28,10 +29,7 @@ void ShowOnline(List<string> line)
}
);
}
var OnlineInfo = from online in PlayerOnlines
orderby online.Value descending
where online.Value > 0
select GetString($"{online.Key} 在线时长: {Math.Ceiling(Convert.ToDouble(online.Value * 1.0f / 60))}分钟").Color(TShockAPI.Utils.GreenHighlight);
var OnlineInfo = PlayerOnline.GetOnlineRank().Select(online => GetString($"{online.Name} 在线时长: {Math.Ceiling(Convert.ToDouble(online.Duration * 1.0f / 60))}分钟").Color(TShockAPI.Utils.GreenHighlight)).ToList();
ShowOnline(OnlineInfo.ToList());
}

Expand All @@ -58,7 +56,7 @@ void Show(List<string> line)
}
);
}
var line = PlayerDeathRank.OrderByDescending(x => x.Value).Select(x => GetString($"[{x.Key}] => 死亡{x.Value}次")).ToList();
var line = DB.PlayerDeath.GetDeathRank().Select(x => GetString($"[{x.Name}] => 死亡{x.Count}次")).ToList();
Show(line);
}

Expand Down
Loading
Loading