Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
加入了决斗系统插件
  • Loading branch information
1242509682 committed Apr 15, 2024
commit 2dcff6a9c9c55a51441493f61c07540eaed5d591
10 changes: 10 additions & 0 deletions Plugin.sln
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DeathDrop", "DeathDrop\Deat
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PerPlayerLoot", "PerPlayerLoot\PerPlayerLoot.csproj", "{E1E2954F-20ED-4AA1-9922-3120D543270A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PvPer", "PvPer\PvPer.csproj", "{757ACBB0-6CDC-40C8-9A79-4C52B3479939}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -376,6 +378,14 @@ Global
{E1E2954F-20ED-4AA1-9922-3120D543270A}.Release|Any CPU.Build.0 = Release|Any CPU
{E1E2954F-20ED-4AA1-9922-3120D543270A}.Release|x64.ActiveCfg = Release|x64
{E1E2954F-20ED-4AA1-9922-3120D543270A}.Release|x64.Build.0 = Release|x64
{757ACBB0-6CDC-40C8-9A79-4C52B3479939}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{757ACBB0-6CDC-40C8-9A79-4C52B3479939}.Debug|Any CPU.Build.0 = Debug|Any CPU
{757ACBB0-6CDC-40C8-9A79-4C52B3479939}.Debug|x64.ActiveCfg = Debug|Any CPU
{757ACBB0-6CDC-40C8-9A79-4C52B3479939}.Debug|x64.Build.0 = Debug|Any CPU
{757ACBB0-6CDC-40C8-9A79-4C52B3479939}.Release|Any CPU.ActiveCfg = Release|Any CPU
{757ACBB0-6CDC-40C8-9A79-4C52B3479939}.Release|Any CPU.Build.0 = Release|Any CPU
{757ACBB0-6CDC-40C8-9A79-4C52B3479939}.Release|x64.ActiveCfg = Release|Any CPU
{757ACBB0-6CDC-40C8-9A79-4C52B3479939}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
205 changes: 205 additions & 0 deletions PvPer/Commands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
using Steamworks;
using TShockAPI;

namespace PvPer
{
public class Commands
{
public static void Duel(CommandArgs args)
{
if (args.Parameters.Count < 1)
{
args.Player.SendErrorMessage("����ϵͳָ��˵���\n " +
"[c/74D3E8:/pvp add �����] - [c/7EE874:������ҲμӾ���] \n " +
"[c/74D3E8:/pvp yes] - [c/7EE874:���ܾ���] \n " +
"[c/74D3E8:/pvp no] - [c/7EE874:�ܾ�����] \n " +
"[c/74D3E8:/pvp start] - [c/7EE874:��ʼ��ս]\n " +
"[c/FFFE80:/pvp list] - [c/7EE874:����]\n ");
return;
}

switch (args.Parameters[0].ToLower())
{
case "add":
case "����":
if (args.Parameters.Count < 2)
{
args.Player.SendErrorMessage("��ָ��Ŀ����ҵ����ơ�");
}
else
{
InviteCmd(args);
}
return;
case "yes":
case "����":
AcceptCmd(args);
return;
case "no":
case "�ܾ�":
RejectCommand(args);
return;
case "start":
case "��ʼ":
StatsCommand(args);
return;
case "l":
case "list":
case "����":
LeaderboardCommand(args);
return;
default:
args.Player.SendErrorMessage("����ϵͳָ��˵���\n " +
"[c/74D3E8:/pvp add �����] - [c/7EE874:������ҲμӾ���] \n " +
"[c/74D3E8:/pvp yes] - [c/7EE874:���ܾ���] \n " +
"[c/74D3E8:/pvp no] - [c/7EE874:�ܾ�����] \n " +
"[c/74D3E8:/pvp start] - [c/7EE874:��ʼ��ս]\n " +
"[c/FFFE80:/pvp list] - [c/7EE874:����]\n ");
return;
}
}

private static void InviteCmd(CommandArgs args)
{
List<TSPlayer> plrList = TSPlayer.FindByNameOrID(string.Join(" ", args.Parameters.GetRange(1, args.Parameters.Count - 1)));


if (plrList.Count == 0)
{
args.Player.SendErrorMessage("δ�ҵ�ָ����ҡ�");
return;
}

if (Utils.IsPlayerInADuel(args.Player.Index))
{
args.Player.SendErrorMessage("�������Ѿ��ھ������ˡ�");
return;
}

TSPlayer targetPlr = plrList[0];

if (targetPlr.Index == args.Player.Index)
{
args.Player.SendErrorMessage("���������Լ�������");
return;
}

if (Utils.IsPlayerInADuel(targetPlr.Index))
{
args.Player.SendErrorMessage($"{targetPlr.Name} ���ڽ���һ��������");
return;
}

PvPer.Invitations.Add(new Pair(args.Player.Index, targetPlr.Index));
args.Player.SendSuccessMessage($"�ɹ����� {targetPlr.Name} ���о�����");
targetPlr.SendMessage($"{args.Player.Name} [c/FE7F81:���������;�������] \n������ [c/CCFFCC:/pvp yes ����] �� [c/FFE6CC:/pvp no�ܾ�] ", 255, 204, 255);
}

private static void AcceptCmd(CommandArgs args)
{
Pair? invitation = Utils.GetInvitationFromReceiverIndex(args.Player.Index);

if (invitation == null)
{
args.Player.SendErrorMessage("[c/FE7F81:����ǰû���յ��κξ�������]");
return;
}

invitation.StartDuel();
}

private static void RejectCommand(CommandArgs args)
{
Pair? invitation = Utils.GetInvitationFromReceiverIndex(args.Player.Index);

if (invitation == null)
{
args.Player.SendErrorMessage("[c/FE7F81:����ǰû���յ��κξ�������]");
return;
}

TShock.Players[invitation.Player1].SendErrorMessage("[c/FFCB80:�Է�����Ѿܾ����ľ�������]��");
PvPer.Invitations.Remove(invitation);
}

private static void StatsCommand(CommandArgs args)
{
if (args.Parameters.Count < 2)
{
try
{
DPlayer plr = PvPer.DbManager.GetDPlayer(args.Player.Account.ID);
args.Player.SendInfoMessage("[c/FFCB80:����ս��:]\n" +
$"[c/63DC5A:��ɱ: ]{plr.Kills}\n" +
$"[c/F56469:����:] {plr.Deaths}\n" +
$"��ɱ/���� [c/5993DB:ʤ��ֵ: ]{plr.GetKillDeathRatio()}");
}
catch (NullReferenceException)
{
args.Player.SendErrorMessage("���δ�ҵ���");
}
}
else
{
try
{
string name = string.Join(" ", args.Parameters.GetRange(1, args.Parameters.Count - 1));
List<TShockAPI.DB.UserAccount> matchedAccounts = TShock.UserAccounts.GetUserAccountsByName(name);

if (matchedAccounts.Count == 0)
{
args.Player.SendErrorMessage("���δ�ҵ���");
return;
}

DPlayer plr = PvPer.DbManager.GetDPlayer(matchedAccounts[0].ID);
args.Player.SendInfoMessage("[c/FFCB80:����ս��:]\n" +
$"[c/63DC5A:��ɱ: ]{plr.Kills}\n" +
$"[c/F56469:����:] {plr.Deaths}\n" +
$"��ɱ/���� [c/5993DB:ʤ��ֵ: ]{plr.GetKillDeathRatio()}");
}
catch (NullReferenceException)
{
args.Player.SendErrorMessage("���δ�ҵ���");
}
}
}

private static void LeaderboardCommand(CommandArgs args)
{
Task.Run(() =>
{
string message = "";
List<DPlayer> list = PvPer.DbManager.GetAllDPlayers();

list.Sort((p1, p2) =>
{
if (p1.GetKillDeathRatio() >= p2.GetKillDeathRatio())
{
return -1;
}
else
{
return 1;
}
});

DPlayer p;

if (list.TryGetValue(0, out p))
{
message += $"{1}. {TShock.UserAccounts.GetUserAccountByID(p.AccountID).Name} : {p.GetKillDeathRatio():F2}";
}

for (int i = 1; i < 5; i++)
{
if (list.TryGetValue(i, out p))
{
message += $"\n{i + 1}. {TShock.UserAccounts.GetUserAccountByID(p.AccountID).Name} : {p.GetKillDeathRatio():F2}";
}
}
args.Player.SendInfoMessage(message);
});
}
}
}
29 changes: 29 additions & 0 deletions PvPer/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Newtonsoft.Json;

namespace PvPer
{
public class Config
{
public int Player1PositionX, Player1PositionY, Player2PositionX, Player2PositionY, ArenaPosX1, ArenaPosY1, ArenaPosX2, ArenaPosY2;
public void Write()
{
File.WriteAllText(PvPer.ConfigPath, JsonConvert.SerializeObject(this, Formatting.Indented));
}
public static Config Read()
{
Config? newConfig = null;
if (File.Exists(PvPer.ConfigPath))
{
newConfig = JsonConvert.DeserializeObject<Config>(File.ReadAllText(PvPer.ConfigPath));
}

if (newConfig == null)
{
newConfig = new Config();
}

newConfig.Write();
return newConfig;
}
}
}
19 changes: 19 additions & 0 deletions PvPer/DBManager/DPlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace PvPer
{
public class DPlayer
{
public int AccountID, Kills, Deaths;

public DPlayer(int accountID, int kills, int deaths)
{
AccountID = accountID;
Kills = kills;
Deaths = deaths;
}

public double GetKillDeathRatio()
{
return Deaths == 0 ? Kills : (double)Kills / Deaths;
}
}
}
62 changes: 62 additions & 0 deletions PvPer/DBManager/DbManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using MySql.Data.MySqlClient;
using System.Data;
using TShockAPI.DB;

namespace PvPer
{
public class DbManager
{
private IDbConnection _db;

public DbManager(IDbConnection db)
{
_db = db;

var sqlCreator = new SqlTableCreator(db, new SqliteQueryCreator());

sqlCreator.EnsureTableStructure(new SqlTable("Players",
new SqlColumn("AccountID", MySqlDbType.Int32) { Primary = true, Unique = true },
new SqlColumn("Kills", MySqlDbType.Int32),
new SqlColumn("Deaths", MySqlDbType.Int32)
));
}

public bool InsertPlayer(int accountID, int kills = 0, int deaths = 0)
{
return _db.Query("INSERT INTO Players (AccountID, Kills, Deaths) VALUES (@0, @1, @2)", accountID, kills, deaths) != 0;
}

public bool SavePlayer(DPlayer player)
{
return _db.Query("UPDATE Players SET Kills = @1, Deaths = @2 WHERE AccountID = @0", player.AccountID, player.Kills, player.Deaths) != 0;
}

public DPlayer GetDPlayer(int accountID)
{
using var reader = _db.QueryReader("SELECT * FROM Players WHERE AccountID = @0", accountID);
while (reader.Read())
{
return new DPlayer(reader.Get<int>("AccountID"), reader.Get<int>("Kills"), reader.Get<int>("Deaths"));
}
throw new NullReferenceException();
}

public List<DPlayer> GetAllDPlayers()
{
List<DPlayer> list = new List<DPlayer>();
using var reader = _db.QueryReader("SELECT * FROM Players");
while (reader.Read())
{
list.Add(new DPlayer(reader.Get<int>("AccountID"), reader.Get<int>("Kills"), reader.Get<int>("Deaths")));
}
return list;
}

// ���������ݱ��е����м�¼����ѧ�ӣ�
public bool ClearData()
{
// ɾ��Players���е����м�¼
return _db.Query("DELETE FROM Players") != 0;
}
}
}
Loading