diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4d13c54 --- /dev/null +++ b/.gitignore @@ -0,0 +1,334 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ diff --git a/API.cs b/API.cs new file mode 100644 index 0000000..82f6ab0 --- /dev/null +++ b/API.cs @@ -0,0 +1,80 @@ +using Exiled.API.Features; +using MEC; +using System.Collections.Generic; +using System.IO; +using System.Text.Json; +using System.Text.RegularExpressions; + +namespace XPSystem +{ + static public class API + { + static public void AddXP(Player player, int xp) + { + PlayerLog log = Main.players.Find(x => x.UserId == player.UserId); + bool levelUp = false; + log.XP += xp; + while (log.XP >= Main.Instance.Config.XPPerLevel) + { + levelUp = true; + log.LVL += 1; + log.XP -= Main.Instance.Config.XPPerLevel; + Timing.RunCoroutine(EvaluateRank(player)); + if (Main.Instance.Config.ShowAddedLVL) + { + string hint = Regex.Replace(Main.Instance.Config.AddedLVLHint, @"%level%", log.LVL.ToString()); + player.ShowHint(hint); + } + } + if (!levelUp && Main.Instance.Config.ShowAddedXP && xp > 0) + { + player.ShowHint($"+ {xp} XP"); + } + File.WriteAllText(Main.path, JsonSerializer.Serialize(Main.players)); + } + static public IEnumerator EvaluateRank(Player player) + { + yield return Timing.WaitForOneFrame; + + if (!Main.players.Exists(x => x.UserId == player.UserId)) + { + if (player.Group == null) + { + Main.players.Add(new PlayerLog(player.UserId, 0, 0, string.Empty)); + } + else + { + Main.players.Add(new PlayerLog(player.UserId, 0, 0, player.Group.BadgeText)); + } + } + File.WriteAllText(Main.path, JsonSerializer.Serialize(Main.players)); + PlayerLog log = Main.players.Find(x => x.UserId == player.UserId); + string badge = GetLVLBadge(player); + string color = Regex.Match(badge, @"(?<=%).*(?=%)").Value; + badge = Regex.Replace(badge, @"%.*%", string.Empty); + string replaced = Regex.Replace(Main.Instance.Config.BadgeStructure, "%lvl%", log.LVL.ToString()); + replaced = Regex.Replace(replaced, "%badge%", badge.Trim()); + replaced = Regex.Replace(replaced, "%oldbadge%", log.OldBadge.Trim()); + player.RankName = replaced; + player.RankColor = color; + } + static private string GetLVLBadge(Player player) + { + PlayerLog log = Main.players.Find(x => x.UserId == player.UserId); + string biggestLvl = string.Empty; + foreach (KeyValuePair lvl in Main.Instance.Config.LevelsBadge) + { + if (log.LVL >= lvl.Key) + { + biggestLvl = lvl.Value; + } + else + { + + break; + } + } + return biggestLvl; + } + } +} diff --git a/Commands/Get.cs b/Commands/Get.cs new file mode 100644 index 0000000..e3fba6f --- /dev/null +++ b/Commands/Get.cs @@ -0,0 +1,45 @@ +using CommandSystem; +using Exiled.API.Features; +using Exiled.Permissions.Extensions; +using System; + +namespace XPSystem +{ + internal class Get : ICommand + { + public string Command => "get"; + + public static Get Instance { get; } = new Get(); + + public string[] Aliases => new string[] { }; + + public string Description => "Gets the player's XP and LVL values by userid or in-game id"; + + public bool Execute(ArraySegment arguments, ICommandSender sender, out string response) + { + if (!sender.CheckPermission("xps.get")) + { + response = "You don't have permission (xps.get) to use this command."; + return false; + } + if (arguments.Count == 0) + { + response = "Usage : XPSystem get (userid)"; + return false; + } + PlayerLog player = Main.players.Find(x => x.UserId == arguments.At(0)); + if (player == default(PlayerLog)) + { + var byId = Player.Get(arguments.At(0)); + if (byId == null) + { + response = "Invalid UserId or the player hasn't joined the server yet."; + return false; + } + player = Main.players.Find(x => x.UserId == byId.UserId); + } + response = $"UserId: {player.UserId} | LVL: {player.LVL} | XP: {player.XP}"; + return true; + } + } +} diff --git a/Commands/Leaderboard.cs b/Commands/Leaderboard.cs new file mode 100644 index 0000000..174b436 --- /dev/null +++ b/Commands/Leaderboard.cs @@ -0,0 +1,59 @@ +using CommandSystem; +using Exiled.Permissions.Extensions; +using System; +using System.Linq; + +namespace XPSystem +{ + public class Leaderboard : ICommand + { + public string Command => "leaderboard"; + + public static Leaderboard Instance { get; } = new Leaderboard(); + + public string[] Aliases => new string[] { "lb" }; + + public string Description => "Players, sorted by their LV (Level of Violence). Use: XPSystem leaderboard (amount)"; + + public bool Execute(ArraySegment arguments, ICommandSender sender, out string response) + { + if (!sender.CheckPermission("xps.get")) + { + response = "You don't have permission (xps.get) to use this command."; + return false; + } + if (arguments.Count == 0) + { + response = GetTopPlayers(10); + return true; + } + else + { + if (int.TryParse(arguments.At(0), out var amount) && amount > 0) + { + response = GetTopPlayers(amount); + return true; + } + else + { + response = "Invalid players amount."; + return false; + } + } + } + + private string GetTopPlayers(int amount) + { + var sorted = Main.players.OrderByDescending(o => o.LVL); + var players = sorted.Take(amount); + string str = ""; + int index = 1; + foreach (PlayerLog player in players) + { + str += $"{index}. ({player.UserId}) : LVL{player.LVL}, XP: {player.XP}\n"; + index++; + } + return str; + } + } +} diff --git a/Commands/Set.cs b/Commands/Set.cs new file mode 100644 index 0000000..23b9442 --- /dev/null +++ b/Commands/Set.cs @@ -0,0 +1,59 @@ +using CommandSystem; +using Exiled.API.Features; +using MEC; +using Exiled.Permissions.Extensions; +using System; + +namespace XPSystem +{ + + internal class Set : ICommand + { + public string Command => "set"; + + public string[] Aliases => new string[] { }; + + public static Set Instance { get; } = new Set(); + + private readonly string usage = "Usage : XPSystem set (UserId | in-game id) (int amount)"; + + public string Description => $"set a certain value in player's lvl variable. {usage}"; + + public bool Execute(ArraySegment arguments, ICommandSender sender, out string response) + { + if (!sender.CheckPermission("xps.set")) + { + response = "You don't have permission (xps.set) to use this command."; + return false; + } + if (arguments.Count != 2) + { + response = usage; + return false; + } + PlayerLog player = Main.players.Find(x => x.UserId == arguments.At(0)); + if (player == default(PlayerLog)) + { + var byId = Player.Get(arguments.At(0)); + if (byId == null) + { + response = "Invalid UserId or the player hasn't joined the server yet."; + return false; + } + player = Main.players.Find(x => x.UserId == byId.UserId); + } + if (int.TryParse(arguments.At(1), out int lvl) && lvl >= 0) + { + player.LVL = lvl; + response = $"{player.UserId}'s LVL is now {player.LVL}"; + Timing.RunCoroutine(API.EvaluateRank(Player.Get(player.UserId))); + return true; + } + else + { + response = $"Invalid amount of LVLs : {lvl}"; + return false; + } + } + } +} diff --git a/Commands/XPSystem.cs b/Commands/XPSystem.cs new file mode 100644 index 0000000..71ec7d6 --- /dev/null +++ b/Commands/XPSystem.cs @@ -0,0 +1,30 @@ +using CommandSystem; +using System; + +namespace XPSystem +{ + [CommandHandler(typeof(RemoteAdminCommandHandler))] + [CommandHandler(typeof(GameConsoleCommandHandler))] + internal class XPSystem : ParentCommand + { + public XPSystem() => LoadGeneratedCommands(); + public override string Command => "XPSystem"; + + public override string[] Aliases => new string[] { "xps" }; + + public override string Description => "Manipulates with players' XP and LVL values."; + + public override void LoadGeneratedCommands() + { + RegisterCommand(Leaderboard.Instance); + RegisterCommand(Set.Instance); + RegisterCommand(Get.Instance); + } + + protected override bool ExecuteParent(ArraySegment arguments, ICommandSender sender, out string response) + { + response = "Use: .xps (leaderboard | set | get)"; + return false; + } + } +} diff --git a/Config.cs b/Config.cs new file mode 100644 index 0000000..69455a4 --- /dev/null +++ b/Config.cs @@ -0,0 +1,84 @@ +using Exiled.API.Interfaces; +using System.Collections.Generic; +using System.ComponentModel; + +namespace XPSystem +{ + public class Config : IConfig + { + public bool IsEnabled { get; set; } = true; + + [Description("Hint shown to the players if they have DNT enabled.")] + public string DNTHint { get; set; } = "We can't track your stats while you have DNT enabled in your game options!"; + + [Description("(You may add your own entries) Role1: Role2: XP player with Role1 gets for killing a person with Role2 ")] + public Dictionary> KillXP { get; set; } = new Dictionary>() + { + [RoleType.ClassD] = new Dictionary() + { + [RoleType.Scientist] = 200, + [RoleType.FacilityGuard] = 150, + [RoleType.NtfPrivate] = 200, + [RoleType.NtfSergeant] = 250, + [RoleType.NtfCaptain] = 300, + [RoleType.Scp049] = 500, + [RoleType.Scp0492] = 100, + [RoleType.Scp106] = 500, + [RoleType.Scp173] = 500, + [RoleType.Scp096] = 500, + [RoleType.Scp93953] = 500, + [RoleType.Scp93989] = 500, + }, + [RoleType.Scientist] = new Dictionary() + { + [RoleType.ClassD] = 50, + [RoleType.ChaosConscript] = 200, + [RoleType.ChaosRifleman] = 200, + [RoleType.ChaosRepressor] = 250, + [RoleType.ChaosMarauder] = 300, + [RoleType.Scp049] = 500, + [RoleType.Scp0492] = 100, + [RoleType.Scp106] = 500, + [RoleType.Scp173] = 500, + [RoleType.Scp096] = 500, + [RoleType.Scp93953] = 500, + [RoleType.Scp93989] = 500, + } + }; + + [Description("How many XP should a player get if their team wins.")] + public int TeamWinXP { get; set; } = 250; + + [Description("How many XP is required to advance a level.")] + public int XPPerLevel { get; set; } = 1000; + + [Description("Show a mini-hint if a player gets XP")] + public bool ShowAddedXP { get; set; } = true; + + [Description("Show a hint to the player if he advances a level.")] + public bool ShowAddedLVL { get; set; } = true; + + [Description("What hint to show if player advances a level. (if ShowAddedLVL = false, this is irrelevant)")] + public string AddedLVLHint { get; set; } = "NEW LEVEL: %level%"; + + [Description("(You may add your own entries) How many XP a player gets for escaping")] + public Dictionary EscapeXP { get; set; } = new Dictionary() + { + [RoleType.ClassD] = 500, + [RoleType.Scientist] = 300 + }; + + [Description("(You may add your own entries) Level threshold and a badge. %color%. if you get a TAG FAIL in your console, either change your color, or remove special characters like brackets.")] + public Dictionary LevelsBadge { get; set; } = new Dictionary() + { + [0] = "Visitor %cyan%", + [1] = "Junior %orange%", + [5] = "Senior %yellow%", + [10] = "Veteran %red%", + [50] = "Nerd %black%" + }; + + [Description("The structure of the badge displayed in-game. Variables: %lvl% - the level. %badge% earned badge in specified in LevelsBadge. %oldbadge% - base-game badge, like ones specified in config-remoteadmin, or a global badge. can be null.")] + public string BadgeStructure { get; set; } = "(LVL %lvl% | %badge%) %oldbadge%"; + } +} diff --git a/EventHandlers.cs b/EventHandlers.cs new file mode 100644 index 0000000..0416548 --- /dev/null +++ b/EventHandlers.cs @@ -0,0 +1,57 @@ +using Exiled.API.Features; +using Exiled.Events.EventArgs; +using MEC; +using System.Collections.Generic; + +namespace XPSystem +{ + class EventHandlers + { + public void OnJoined(VerifiedEventArgs ev) + { + if (ev.Player.DoNotTrack) + { + ev.Player.OpenReportWindow(Main.Instance.Config.DNTHint); + return; + } + Timing.RunCoroutine(API.EvaluateRank(ev.Player)); + } + + public void OnKill(DyingEventArgs ev) + { + if (ev.IsAllowed && ev.Killer != null && ev.Target != null && !ev.Killer.DoNotTrack) + { + Dictionary role; + if (Main.Instance.Config.KillXP.TryGetValue(ev.Killer.Role, out role)) + { + int xp; + if (role.TryGetValue(ev.Target.Role, out xp)) + { + API.AddXP(ev.Killer, xp); + } + } + } + } + + public void OnEscape(EscapingEventArgs ev) + { + if (ev.IsAllowed) + { + API.AddXP(ev.Player, Main.Instance.Config.EscapeXP[ev.Player.Role]); + } + } + + + + public void OnRoundEnd(EndingRoundEventArgs ev) + { + foreach (Player player in Player.List) + { + if (player.LeadingTeam == ev.LeadingTeam && ev.IsAllowed && !Round.IsLocked) + { + API.AddXP(player, Main.Instance.Config.TeamWinXP); + } + } + } + } +} diff --git a/Main.cs b/Main.cs new file mode 100644 index 0000000..a690a38 --- /dev/null +++ b/Main.cs @@ -0,0 +1,50 @@ +using Exiled.API.Features; +using System; +using System.Collections.Generic; +using System.IO; +using System.Text.Json; +using Player = Exiled.Events.Handlers.Player; +using Server = Exiled.Events.Handlers.Server; + +namespace XPSystem +{ + public class Main : Plugin + { + public static Main Instance { get; set; } + private EventHandlers handlers = new(); + public static readonly string path = Environment.ExpandEnvironmentVariables(@"%AppData%\EXILED\Players.json"); + public static List players = new List(); + + private void GetOrCreateJson() + { + if (!File.Exists(path)) + { + File.WriteAllText(path, JsonSerializer.Serialize(players)); + return; + }; + players = JsonSerializer.Deserialize>(File.ReadAllText(path)); + if (players == null) { players = new List(); } + } + public override void OnEnabled() + { + handlers = new(); + Player.Verified += handlers.OnJoined; + Player.Dying += handlers.OnKill; + Server.EndingRound += handlers.OnRoundEnd; + Player.Escaping += handlers.OnEscape; + Instance = this; + GetOrCreateJson(); + base.OnEnabled(); + } + + public override void OnDisabled() + { + handlers = null; + Player.Verified -= handlers.OnJoined; + Player.Dying -= handlers.OnKill; + Server.EndingRound -= handlers.OnRoundEnd; + Player.Escaping -= handlers.OnEscape; + base.OnDisabled(); + } + } +} \ No newline at end of file diff --git a/PlayerLog.cs b/PlayerLog.cs new file mode 100644 index 0000000..67e256f --- /dev/null +++ b/PlayerLog.cs @@ -0,0 +1,17 @@ +namespace XPSystem +{ + public class PlayerLog + { + public PlayerLog(string USERID, int lvl, int xp, string OLDBADGE) + { + UserId = USERID; + LVL = lvl; + XP = xp; + OldBadge = OLDBADGE; + } + public string UserId { get; set; } + public int LVL { get; set; } + public int XP { get; set; } + public string OldBadge { get; set; } + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..171b219 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# XPSystem + A basic, customisable XP and LVL system for SCP: SL. diff --git a/XPSystem.csproj b/XPSystem.csproj new file mode 100644 index 0000000..7a00280 --- /dev/null +++ b/XPSystem.csproj @@ -0,0 +1,36 @@ + + + + net48 + 10.0 + disable + enable + true + AnyCPU + + + + + + + + + + + + + + D:\SteamLibrary\steamapps\common\SCP Secret Laboratory Dedicated Server\SCPSL_Data\Managed\Assembly-CSharp.dll + + + D:\SteamLibrary\steamapps\common\SCP Secret Laboratory Dedicated Server\SCPSL_Data\Managed\Assembly-CSharp-firstpass.dll + + + D:\SteamLibrary\steamapps\common\SCP Secret Laboratory Dedicated Server\SCPSL_Data\Managed\CommandSystem.Core.dll + + + D:\SteamLibrary\steamapps\common\SCP Secret Laboratory Dedicated Server\SCPSL_Data\Managed\UnityEngine.CoreModule.dll + + + +