Skip to content

Commit

Permalink
修复: Servertools 添加数据库错误
Browse files Browse the repository at this point in the history
  • Loading branch information
Controllerdestiny committed May 4, 2024
1 parent daf3ea7 commit 3402eac
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
16 changes: 11 additions & 5 deletions ServerTools/DB/PlayerDeath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace ServerTools.DB;

public class PlayerDeath : Dictionary<string, int>
{
public new int this[string key]
private new int this[string key]
{
get
{
Expand Down Expand Up @@ -47,9 +47,15 @@ private void ReadAll()

public void Add(string name)
{
this[name] += 1;
if (database.Query("UPDATE Death SET Count = @0 WHERE Name = @1", this[name], name) != 1)
database.Query("INSERT INTO `Death` (`Name`, `Count`) VALUES (@0, @1)", this[name], 1);

if (ContainsKey(name))
{
this[name] += 1;
database.Query("UPDATE Death SET Count = @0 WHERE Name = @1", this[name], name);
}
else
{
this[name] = 1;
database.Query("INSERT INTO `Death` (`Name`, `Count`) VALUES (@0, @1)", name, 1);
}
}
}
7 changes: 6 additions & 1 deletion ServerTools/DB/PlayerOnline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace ServerTools.DB;

public class PlayerOnline : Dictionary<string, int>
{
private HashSet<string> _players = new();
public new int this[string key]
{
get
Expand Down Expand Up @@ -42,6 +43,7 @@ public void ReadAll()
string username = reader.Get<string>("username");
int duration = reader.Get<int>("duration");
this[username] = duration;
_players.Add(username);
}
}

Expand All @@ -67,6 +69,7 @@ public bool Update(string Name, int duration)
}
public bool Insert(string Name, int duration)
{
_players.Add(Name);
return 1 == database.Query("INSERT INTO `OnlineDuration` (`username`, `duration`) VALUES (@0, @1)", Name, duration);
}

Expand All @@ -75,7 +78,9 @@ public bool Insert(string Name, int duration)

public void AddOrUpdate(string name, int duration)
{
if (!Update(name, duration))
if (_players.Contains(name))
Update(name, duration);
else
Insert(name, duration);
}

Expand Down
10 changes: 7 additions & 3 deletions ServerTools/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,17 @@ private void OnUpdate(EventArgs e)
if (TimerCount % 60 == 0)
{
Timer?.Invoke(e);
foreach (var ply in Deads)
if (Config.DeadTimer)
{
if (ply != null && ply.Active && ply.Dead)
foreach (var ply in Deads)
{
ply.SendInfoMessage(Config.DeadFormat, ply.RespawnTimer);
if (ply != null && ply.Active && ply.Dead)
{
ply.SendInfoMessage(Config.DeadFormat, ply.RespawnTimer);
}
}
}

}
}

Expand Down

0 comments on commit 3402eac

Please sign in to comment.