Skip to content

Commit

Permalink
Bug fixes pertaining to extension logic
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyTech6 committed Dec 9, 2024
1 parent e273f0c commit 5d8d9c4
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 13 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
`latest`
- ShadowBan False by default
- banned.json renamed to banned.json
- banned loaduntracked removed reason
- Removed early escape for already banned when banning
- Unban while SQL down check and recover

`1.1.0`
- Noted JSON Rising in README
- Local can ban extend
- Local can ban extended
- Output changes

`1.0.12`
Expand Down
18 changes: 12 additions & 6 deletions Commands/BanCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.Entities;
using UnityEngine;
using VampireCommandFramework;
Expand Down Expand Up @@ -106,8 +107,13 @@ private static (bool Success, PlayerInfo PlayerInfo) HandleBanOperation(ChatComm

if (banList.Exists(x => x.PlayerID == playerInfo.User.PlatformId))
{
ctx.Reply($"{name} is already {banType}.");
return (false, null);
Ban oldBan = banList.First(x => x.PlayerID == playerInfo.User.PlatformId);

if (oldBan.TimeUntil > bannedTime)
{
ctx.Reply($"{name} is already {banType}.");
return (false, null);
}
}

Ban ban = new Ban(playerInfo.User.CharacterName.ToString(), playerInfo.User.PlatformId, bannedTime.ToUniversalTime(), reason, ctx.User.CharacterName.ToString());
Expand All @@ -124,25 +130,25 @@ private static (bool Success, PlayerInfo PlayerInfo) HandleBanOperation(ChatComm
{
int i = SQLlink.ResolveConflict(ban, banList);

if(i == 0)
if (i == 0)
{
ctx.Reply($"{name} is already permanently {banType}.");
return (false, null);
}

if(i == 1)
if (i == 1)
{
ctx.Reply($"{name} already has an active ban with a longer length.");
return (true, playerInfo);
}

if(i == 2)
if (i == 2)
{
ctx.Reply($"{name} has been {banType} {(length == 0 ? "permanent" : $"for {TimeUtility.FormatRemainder(timeSpan)}")}");
return (true, playerInfo);
}

if(i == 4)
if (i == 4)
{
ctx.Reply($"{name} is already {banType}.");
}
Expand Down
2 changes: 1 addition & 1 deletion Commands/BannedCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static void Backlog(ChatCommandContext ctx)
{
if (Database.Banned.Exists(x => x.PlayerID == Convert.ToUInt64(line))) continue;

Ban ban = new Ban(string.Empty, Convert.ToUInt64(line), DateTime.MinValue, "Synced from local banlist.txt file.", "banlist.txt");
Ban ban = new Ban(string.Empty, Convert.ToUInt64(line), DateTime.MinValue, "", "banlist.txt");
ban.LocalBan = true;
ban.DatabaseId = -1;
Database.AddBan(ban, Database.Banned);
Expand Down
53 changes: 49 additions & 4 deletions Structs/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ internal class Database
WriteIndented = true,
IncludeFields = true,
};
public static string BannedFile = Path.Combine(Plugin.ConfigFiles, "banned.json");
public static string BannedFile = Path.Combine(Plugin.ConfigFiles, "bans_server.json");
public static string ChatBanFile = Path.Combine(Plugin.ConfigFiles, "bans_chat.json");
public static string VoiceBanFile = Path.Combine(Plugin.ConfigFiles, "bans_voice.json");
public static string MessageFile = Path.Combine(Plugin.ConfigFiles, "messages.json");
public static string DeleteFile = Path.Combine(Plugin.ConfigFiles, "ignore.json");

public static List<Ban> Banned;
public static List<Ban> ChatBans;
public static List<Ban> VoiceBans;
public static List<MessagePair> Messages;
public static List<DeleteLater> Deletes;

public static dynamic SQL => IL2CPPChainloader.Instance.Plugins.TryGetValue("CrimsonSQL", out var pluginInfo)
? pluginInfo.Instance.GetType().GetProperty("SQLService").GetValue(pluginInfo.Instance)
Expand Down Expand Up @@ -74,6 +76,16 @@ private static void LoadDatabases()
Banned = new List<Ban>();
}

if(File.Exists(DeleteFile))
{
string json = File.ReadAllText(DeleteFile);
Deletes = JsonSerializer.Deserialize<List<DeleteLater>>(json, prettyJsonOptions);
}
else
{
Deletes = new List<DeleteLater>();
}

if(File.Exists(MessageFile))
{
string json = File.ReadAllText(MessageFile);
Expand Down Expand Up @@ -187,7 +199,14 @@ public static void DeleteBan(Ban ban, List<Ban> list, bool fromResolve = false)

if (SQL != null && Settings.UseSQL.Value && fromResolve && SQL.Connect())
{
SQLlink.DeleteBan(ban, list);
if(SQL.Connect())
{
SQLlink.DeleteBan(ban, list);
}
else
{
Deletes.Add(new DeleteLater(ban.DatabaseId, $"{type}Bans"));
}
}

SaveDatabases();
Expand All @@ -212,19 +231,33 @@ private static void SaveDatabases()
string json = JsonSerializer.Serialize(Banned, prettyJsonOptions);
File.WriteAllText(BannedFile, json);
}

if(Deletes.Count > 0 || File.Exists(DeleteFile))
{
string json = JsonSerializer.Serialize(Deletes, prettyJsonOptions);
File.WriteAllText(DeleteFile, json);
}
}

public static void SyncDB()
{
if(!SQL.Connect()) return;

foreach(var delete in Deletes)
{
SQLlink.DeleteBan(delete.ID, delete.TableName);
}

Deletes.Clear();
if(File.Exists(DeleteFile)) File.Delete(DeleteFile);

SyncTable(ChatBans, "ChatBans");
SyncTable(VoiceBans, "VoiceBans");
SyncTable(Banned, "ServerBans");
}

private static void SyncTable(List<Ban> list, string tableName)
{
if(!SQL.Connect()) return;

DataTable table = SQL.Select(tableName);

// Create a set of PlayerIDs from the database for efficient lookup
Expand Down Expand Up @@ -332,4 +365,16 @@ static IEnumerator Clean()
}
}
}
}

public struct DeleteLater
{
public int ID { get; set; }
public string TableName { get; set; }

public DeleteLater(int id, string tableName)
{
ID = id;
TableName = tableName;
}
}
10 changes: 10 additions & 0 deletions Structs/SQLlink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ public static void DeleteBan(Ban ban, List<Ban> list)
Database.SQL.Delete(tableName, whereConditions);
}

public static void DeleteBan(int id, string tableName)
{
var whereConditions = new Dictionary<string, object>
{
["Id"] = id
};

Database.SQL.Delete(tableName, whereConditions);
}

public static Ban GetBan(ulong playerID, List<Ban> list)
{
var whereConditions = new Dictionary<string, object>
Expand Down
2 changes: 1 addition & 1 deletion Structs/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void InitConfig()
CreateDirectories(path);
}

ShadowBan = InitConfigEntry(ConfigHeader, "ShadowBan", true,
ShadowBan = InitConfigEntry(ConfigHeader, "ShadowBan", false,
"If this is set to true, the player will never be notified when chat or voice banned.");
BanFilePath = InitConfigEntry(ConfigHeader, "BanFilePath", "save-data/Settings/banlist.txt",
"The path from root to the banlist.txt file");
Expand Down

0 comments on commit 5d8d9c4

Please sign in to comment.