From 6c9e6c23258e663fe876aecd81f68ea748167631 Mon Sep 17 00:00:00 2001 From: Ivan Rubinov Date: Mon, 3 Jun 2024 23:19:57 +0300 Subject: [PATCH 1/3] =?UTF-8?q?=D0=9D=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BC=D0=B0=D0=BD=D0=B4=D1=8B=20setallbalance=20=D0=B8?= =?UTF-8?q?=20setbalance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/CorvaxSetBalanceCommand.cs | 93 +++++++++++++++++++ Content.Server/Database/ServerDbBase.cs | 7 ++ Content.Server/Database/ServerDbManager.cs | 11 +++ .../commands/corvaxsetbalance.ftl | 5 + 4 files changed, 116 insertions(+) create mode 100644 Content.Server/Administration/Commands/CorvaxSetBalanceCommand.cs create mode 100644 Resources/Locale/ru-RU/administration/commands/corvaxsetbalance.ftl diff --git a/Content.Server/Administration/Commands/CorvaxSetBalanceCommand.cs b/Content.Server/Administration/Commands/CorvaxSetBalanceCommand.cs new file mode 100644 index 00000000000..cfe7a5cc30e --- /dev/null +++ b/Content.Server/Administration/Commands/CorvaxSetBalanceCommand.cs @@ -0,0 +1,93 @@ +using Content.Server.RoundEnd; +using Content.Shared.Administration; +using Robust.Shared.Console; +using Content.Server.Database; +using System.Threading; +using Content.Shared.Preferences; +using Content.Shared.Preferences.Loadouts; + +namespace Content.Server.Administration.Commands +{ + + [AdminCommand(AdminFlags.Admin)] + public sealed class SetAllBalanceCommand : IConsoleCommand + { + [Dependency] private readonly IServerDbManager _dbManager = default!; + public string Command => "setallbalance"; + public string Description => Loc.GetString("set-all-balance-command-description"); + public string Help => Loc.GetString("set-all-balance-command-help-text", ("command",Command)); + + public void Execute(IConsoleShell shell, string argStr, string[] args) + { + var loc = IoCManager.Resolve(); + + if (args.Length == 1 && int.TryParse(args[0], out var set_balance)) + { + _dbManager.SetAllBalance(set_balance); + } + else if (args.Length == 1) + { + shell.WriteLine(Loc.GetString("shell-argument-number-invalid", ("index", "1"))); + } + else + { + _dbManager.SetAllBalance(25000); + } + } + } + + [AdminCommand(AdminFlags.Admin)] + public sealed class SetBalanceCommand : IConsoleCommand + { + [Dependency] private readonly IServerDbManager _dbManager = default!; + public string Command => "setbalance"; + public string Description => Loc.GetString("set-balance-command-description"); + public string Help => Loc.GetString("set-balance-command-help-text", ("command", Command)); + + public async void Execute(IConsoleShell shell, string argStr, string[] args) + { + var loc = IoCManager.Resolve(); + var set_balance = 0; + int.TryParse(args[1], out set_balance); + if (args.Length == 1 || args.Length == 2) + { + var _userId = await _dbManager.GetPlayerRecordByUserName(args[0], new CancellationToken()); + if (_userId is not null) { + var userId = _userId.UserId; + var _profile = await _dbManager.GetPlayerPreferencesAsync(userId, new CancellationToken()); + if (_profile is not null) { + foreach (var item in _profile.Characters) + { + if (item.Value is HumanoidCharacterProfile profile) + { + var newProfile = new HumanoidCharacterProfile( + profile.Name, + profile.FlavorText, + profile.Species, + profile.Age, + profile.Sex, + profile.Gender, + set_balance, + profile.Appearance, + profile.SpawnPriority, + profile.JobPriorities, + profile.PreferenceUnavailable, + profile.AntagPreferences, + profile.TraitPreferences, + new Dictionary(profile.Loadouts)); + await _dbManager.SaveCharacterSlotAsync(userId, newProfile, item.Key); + } + } + } + } + } + else + { + shell.WriteLine(Loc.GetString("shell-need-between-arguments", ("lower", "1"), ("upper", "2"))); + } + + } + } + + +} diff --git a/Content.Server/Database/ServerDbBase.cs b/Content.Server/Database/ServerDbBase.cs index a35c3847fc0..8f4959b0525 100644 --- a/Content.Server/Database/ServerDbBase.cs +++ b/Content.Server/Database/ServerDbBase.cs @@ -262,6 +262,13 @@ private static HumanoidCharacterProfile ConvertProfiles(Profile profile) ); } + public async Task SetAllBalance(int set_balance) + { + await using var db = await GetDb(); + await db.DbContext.Database.ExecuteSqlAsync($"""UPDATE profile SET bank_balance = {set_balance}"""); + await db.DbContext.SaveChangesAsync(); + } + private static Profile ConvertProfiles(HumanoidCharacterProfile humanoid, int slot, Profile? profile = null) { profile ??= new Profile(); diff --git a/Content.Server/Database/ServerDbManager.cs b/Content.Server/Database/ServerDbManager.cs index 01d15267274..11b0013907f 100644 --- a/Content.Server/Database/ServerDbManager.cs +++ b/Content.Server/Database/ServerDbManager.cs @@ -171,6 +171,12 @@ public Task EditServerRoleBan( /// The list of all updates to apply to the database. Task UpdatePlayTimes(IReadOnlyCollection updates); + /// + /// Change balance in db + /// + /// The list of all updates to apply to the database. + Task SetAllBalance(int set_balance); + #endregion #region Player Records @@ -519,6 +525,11 @@ public Task UpdatePlayerRecordAsync( return RunDbCommand(() => _db.UpdatePlayerRecord(userId, userName, address, hwId)); } + public Task SetAllBalance(int set_balance) { + DbWriteOpsMetric.Inc(); + return RunDbCommand(() => _db.SetAllBalance(set_balance)); + } + public Task GetPlayerRecordByUserName(string userName, CancellationToken cancel = default) { DbReadOpsMetric.Inc(); diff --git a/Resources/Locale/ru-RU/administration/commands/corvaxsetbalance.ftl b/Resources/Locale/ru-RU/administration/commands/corvaxsetbalance.ftl new file mode 100644 index 00000000000..7e1c34a7268 --- /dev/null +++ b/Resources/Locale/ru-RU/administration/commands/corvaxsetbalance.ftl @@ -0,0 +1,5 @@ +set-all-balance-command-description = Комманда которая ставит баланс у всех. +set-all-balance-command-help-text = Записывает данные в базу данных напрямую. ИСПОЛЬЗОВАТЬ ТОЛЬКО В ЛОББИ И ПОСЛЕ ЧЕГО ПЕРЕЗАПУСТИТЬ СЕРВЕР. + +set-balance-command-description = Команда которая устанавливает баланс по ckey/username. +set-balance-command-help-text = Записывает данные в базу данных напрямую. Первый аргумент - username, Второй не обязательный - значение баланса, если ничего нет ставит 0. ИСПОЛЬЗОВАТЬ ТОЛЬКО В ЛОББИ И ПОСЛЕ ЧЕГО ПЕРЕЗАПУСТИТЬ СЕРВЕР. From 038c9877e6d2cc5e96393b61e291a335bca0f62d Mon Sep 17 00:00:00 2001 From: Ivan Rubinov Date: Tue, 4 Jun 2024 16:00:39 +0300 Subject: [PATCH 2/3] =?UTF-8?q?=D0=A4=D0=B8=D0=BA=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/CorvaxSetBalanceCommand.cs | 137 +++++++++--------- 1 file changed, 69 insertions(+), 68 deletions(-) diff --git a/Content.Server/Administration/Commands/CorvaxSetBalanceCommand.cs b/Content.Server/Administration/Commands/CorvaxSetBalanceCommand.cs index cfe7a5cc30e..a01e36d1684 100644 --- a/Content.Server/Administration/Commands/CorvaxSetBalanceCommand.cs +++ b/Content.Server/Administration/Commands/CorvaxSetBalanceCommand.cs @@ -1,4 +1,3 @@ -using Content.Server.RoundEnd; using Content.Shared.Administration; using Robust.Shared.Console; using Content.Server.Database; @@ -6,88 +5,90 @@ using Content.Shared.Preferences; using Content.Shared.Preferences.Loadouts; -namespace Content.Server.Administration.Commands +namespace Content.Server.Administration.Commands; + +[AdminCommand(AdminFlags.Host)] +public sealed class SetAllBalanceCommand : IConsoleCommand { + [Dependency] private readonly IServerDbManager _dbManager = default!; + + public string Command => "setallbalance"; + public string Description => Loc.GetString("set-all-balance-command-description"); + public string Help => Loc.GetString("set-all-balance-command-help-text", ("command",Command)); - [AdminCommand(AdminFlags.Admin)] - public sealed class SetAllBalanceCommand : IConsoleCommand + public void Execute(IConsoleShell shell, string argStr, string[] args) { - [Dependency] private readonly IServerDbManager _dbManager = default!; - public string Command => "setallbalance"; - public string Description => Loc.GetString("set-all-balance-command-description"); - public string Help => Loc.GetString("set-all-balance-command-help-text", ("command",Command)); + var loc = IoCManager.Resolve(); - public void Execute(IConsoleShell shell, string argStr, string[] args) + if (args.Length == 1 && int.TryParse(args[0], out var set_balance)) { - var loc = IoCManager.Resolve(); - - if (args.Length == 1 && int.TryParse(args[0], out var set_balance)) - { - _dbManager.SetAllBalance(set_balance); - } - else if (args.Length == 1) - { - shell.WriteLine(Loc.GetString("shell-argument-number-invalid", ("index", "1"))); - } - else - { - _dbManager.SetAllBalance(25000); - } + _dbManager.SetAllBalance(set_balance); + return; + } + else if (args.Length == 1) + { + shell.WriteLine(Loc.GetString("shell-argument-number-invalid", ("index", "1"))); + return; + } + else + { + _dbManager.SetAllBalance(25000); + return; } } +} - [AdminCommand(AdminFlags.Admin)] - public sealed class SetBalanceCommand : IConsoleCommand - { - [Dependency] private readonly IServerDbManager _dbManager = default!; - public string Command => "setbalance"; - public string Description => Loc.GetString("set-balance-command-description"); - public string Help => Loc.GetString("set-balance-command-help-text", ("command", Command)); +[AdminCommand(AdminFlags.Host)] +public sealed class SetBalanceCommand : IConsoleCommand +{ + [Dependency] private readonly IServerDbManager _dbManager = default!; + + public string Command => "setbalance"; + public string Description => Loc.GetString("set-balance-command-description"); + public string Help => Loc.GetString("set-balance-command-help-text", ("command", Command)); - public async void Execute(IConsoleShell shell, string argStr, string[] args) + public async void Execute(IConsoleShell shell, string argStr, string[] args) + { + var loc = IoCManager.Resolve(); + var set_balance = 0; + int.TryParse(args[1], out set_balance); + if (args.Length == 1 || args.Length == 2) { - var loc = IoCManager.Resolve(); - var set_balance = 0; - int.TryParse(args[1], out set_balance); - if (args.Length == 1 || args.Length == 2) - { - var _userId = await _dbManager.GetPlayerRecordByUserName(args[0], new CancellationToken()); - if (_userId is not null) { - var userId = _userId.UserId; - var _profile = await _dbManager.GetPlayerPreferencesAsync(userId, new CancellationToken()); - if (_profile is not null) { - foreach (var item in _profile.Characters) + var _userId = await _dbManager.GetPlayerRecordByUserName(args[0], new CancellationToken()); + if (_userId is not null) { + var userId = _userId.UserId; + var _profile = await _dbManager.GetPlayerPreferencesAsync(userId, new CancellationToken()); + if (_profile is not null) { + foreach (var item in _profile.Characters) + { + if (item.Value is HumanoidCharacterProfile profile) { - if (item.Value is HumanoidCharacterProfile profile) - { - var newProfile = new HumanoidCharacterProfile( - profile.Name, - profile.FlavorText, - profile.Species, - profile.Age, - profile.Sex, - profile.Gender, - set_balance, - profile.Appearance, - profile.SpawnPriority, - profile.JobPriorities, - profile.PreferenceUnavailable, - profile.AntagPreferences, - profile.TraitPreferences, - new Dictionary(profile.Loadouts)); - await _dbManager.SaveCharacterSlotAsync(userId, newProfile, item.Key); - } + var newProfile = new HumanoidCharacterProfile( + profile.Name, + profile.FlavorText, + profile.Species, + profile.Age, + profile.Sex, + profile.Gender, + set_balance, + profile.Appearance, + profile.SpawnPriority, + profile.JobPriorities, + profile.PreferenceUnavailable, + profile.AntagPreferences, + profile.TraitPreferences, + new Dictionary(profile.Loadouts)); + await _dbManager.SaveCharacterSlotAsync(userId, newProfile, item.Key); } } } } - else - { - shell.WriteLine(Loc.GetString("shell-need-between-arguments", ("lower", "1"), ("upper", "2"))); - } - + return; + } + else + { + shell.WriteLine(Loc.GetString("shell-need-between-arguments", ("lower", "1"), ("upper", "2"))); + return; } } - - } From f0ccb01915b3f669c10dec05a45c18619286f638 Mon Sep 17 00:00:00 2001 From: Ivan Rubinov Date: Sat, 8 Jun 2024 19:57:07 +0300 Subject: [PATCH 3/3] =?UTF-8?q?=D1=84=D0=B8=D0=BA=D1=81=20=D1=85=D1=83?= =?UTF-8?q?=D0=B9=D0=BD=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/CorvaxSetBalanceCommand.cs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Content.Server/Administration/Commands/CorvaxSetBalanceCommand.cs b/Content.Server/Administration/Commands/CorvaxSetBalanceCommand.cs index a01e36d1684..edb7fb27c8f 100644 --- a/Content.Server/Administration/Commands/CorvaxSetBalanceCommand.cs +++ b/Content.Server/Administration/Commands/CorvaxSetBalanceCommand.cs @@ -39,20 +39,18 @@ public void Execute(IConsoleShell shell, string argStr, string[] args) } [AdminCommand(AdminFlags.Host)] -public sealed class SetBalanceCommand : IConsoleCommand +public sealed class SetZeroBalanceCommand : IConsoleCommand { [Dependency] private readonly IServerDbManager _dbManager = default!; - public string Command => "setbalance"; + public string Command => "setzerobalance"; public string Description => Loc.GetString("set-balance-command-description"); public string Help => Loc.GetString("set-balance-command-help-text", ("command", Command)); public async void Execute(IConsoleShell shell, string argStr, string[] args) { var loc = IoCManager.Resolve(); - var set_balance = 0; - int.TryParse(args[1], out set_balance); - if (args.Length == 1 || args.Length == 2) + if (args.Length == 1) { var _userId = await _dbManager.GetPlayerRecordByUserName(args[0], new CancellationToken()); if (_userId is not null) { @@ -70,7 +68,7 @@ public async void Execute(IConsoleShell shell, string argStr, string[] args) profile.Age, profile.Sex, profile.Gender, - set_balance, + 0, profile.Appearance, profile.SpawnPriority, profile.JobPriorities, @@ -82,12 +80,15 @@ public async void Execute(IConsoleShell shell, string argStr, string[] args) } } } + return; + } else { + shell.WriteLine(Loc.GetString("shell-target-player-does-not-exist ")); + return; } - return; } else { - shell.WriteLine(Loc.GetString("shell-need-between-arguments", ("lower", "1"), ("upper", "2"))); + shell.WriteLine(Loc.GetString("shell-need-exactly-one-argument")); return; } }