Add !balance command (#935)

* Add !balance command

* Fix pull https://github.com/JustArchiNET/ArchiSteamFarm/pull/935

* Fix #2 pull https://github.com/JustArchiNET/ArchiSteamFarm/pull/935#partial-pull-merging
This commit is contained in:
MisakaDev
2018-11-03 19:42:40 +02:00
committed by Łukasz Domeradzki
parent 5423596f1f
commit c50b70d3e3
4 changed files with 250 additions and 169 deletions

View File

@@ -150,6 +150,9 @@ namespace ArchiSteamFarm {
private string TwoFactorCode;
private byte TwoFactorCodeFailures;
internal int WalletBalance { get; private set; }
internal ECurrencyCode WalletCurrency { get; private set; }
private Bot(string botName, BotConfig botConfig, BotDatabase botDatabase) {
if (string.IsNullOrEmpty(botName) || (botConfig == null) || (botDatabase == null)) {
throw new ArgumentNullException(nameof(botName) + " || " + nameof(botConfig) + " || " + nameof(botDatabase));
@@ -212,6 +215,7 @@ namespace ArchiSteamFarm {
CallbackManager.Subscribe<SteamUser.LoggedOnCallback>(OnLoggedOn);
CallbackManager.Subscribe<SteamUser.LoginKeyCallback>(OnLoginKey);
CallbackManager.Subscribe<SteamUser.UpdateMachineAuthCallback>(OnMachineAuth);
CallbackManager.Subscribe<SteamUser.WalletInfoCallback>(OnWalletUpdate);
CallbackManager.Subscribe<ArchiHandler.PlayingSessionStateCallback>(OnPlayingSessionState);
CallbackManager.Subscribe<ArchiHandler.SharedLibraryLockStatusCallback>(OnSharedLibraryLockStatus);
@@ -2262,6 +2266,16 @@ namespace ArchiSteamFarm {
ArchiWebHandler.OnVanityURLChanged(callback.VanityURL);
}
private void OnWalletUpdate(SteamUser.WalletInfoCallback callback) {
if (callback == null) {
ArchiLogger.LogNullError(nameof(callback));
return;
}
WalletBalance = callback.Balance;
WalletCurrency = callback.Currency;
}
[SuppressMessage("ReSharper", "FunctionComplexityOverflow")]
private async Task RedeemGamesInBackground() {
if (!await GamesRedeemerInBackgroundSemaphore.WaitAsync(0).ConfigureAwait(false)) {

View File

@@ -71,6 +71,8 @@ namespace ArchiSteamFarm {
return await Response2FAConfirm(steamID, false).ConfigureAwait(false);
case "2FAOK":
return await Response2FAConfirm(steamID, true).ConfigureAwait(false);
case "BALANCE":
return ResponseWalletBalance(steamID);
case "BL":
return ResponseBlacklist(steamID);
case "EXIT":
@@ -130,6 +132,8 @@ namespace ArchiSteamFarm {
}
return await ResponseAddLicense(steamID, args[1]).ConfigureAwait(false);
case "BALANCE":
return await ResponseWalletBalance(steamID, Utilities.GetArgsAsText(args, 1, ",")).ConfigureAwait(false);
case "BL":
return await ResponseBlacklist(steamID, Utilities.GetArgsAsText(args, 1, ",")).ConfigureAwait(false);
case "BLADD":
@@ -2325,6 +2329,44 @@ namespace ArchiSteamFarm {
return IsOperator(steamID) ? FormatBotResponse(string.Format(Strings.BotVersion, SharedInfo.ASF, SharedInfo.Version)) : null;
}
private string ResponseWalletBalance(ulong steamID) {
if (steamID == 0) {
Bot.ArchiLogger.LogNullError(nameof(steamID));
return null;
}
if (!Bot.IsMaster(steamID)) {
return null;
}
if (!Bot.IsConnectedAndLoggedOn) {
return FormatBotResponse(Strings.BotNotConnected);
}
if (Bot.WalletCurrency != ECurrencyCode.Invalid) {
return FormatBotResponse(string.Format(Strings.BotWalletBalance, Bot.WalletBalance / 100.0, Bot.WalletCurrency.ToString()));
}
return FormatBotResponse(string.Format(Strings.BotHasNoWallet));
}
private static async Task<string> ResponseWalletBalance(ulong steamID, string botNames) {
if ((steamID == 0) || string.IsNullOrEmpty(botNames)) {
ASF.ArchiLogger.LogNullError(nameof(steamID) + " || " + nameof(botNames));
return null;
}
HashSet<Bot> bots = Bot.GetBots(botNames);
if ((bots == null) || (bots.Count == 0)) {
return ASF.IsOwner(steamID) ? FormatStaticResponse(string.Format(Strings.BotNotFound, botNames)) : null;
}
IList<string> results = await Utilities.InParallel(bots.Select(bot => Task.Run(() => bot.Commands.ResponseWalletBalance(steamID)))).ConfigureAwait(false);
List<string> responses = new List<string>(results.Where(result => !string.IsNullOrEmpty(result)));
return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}
[Flags]
private enum ERedeemFlags : byte {
None = 0,

File diff suppressed because it is too large Load Diff

View File

@@ -661,4 +661,11 @@ StackTrace:
<data name="TargetBotNotConnected" xml:space="preserve">
<value>Target bot instance is not connected!</value>
</data>
</root>
<data name="BotWalletBalance" xml:space="preserve">
<value>Wallet balance: {0} {1}</value>
<comment>{0} will be replaced by wallet balance value, {1} will be replaced by currency name</comment>
</data>
<data name="BotHasNoWallet" xml:space="preserve">
<value>Bot has no wallet.</value>
</data>
</root>