Skip to content

Commit

Permalink
Add !balance command (#935)
Browse files Browse the repository at this point in the history
* Add !balance command

* Fix pull #935

* Fix #2 pull #935 (comment)
  • Loading branch information
MisakaDev authored and JustArchi committed Nov 3, 2018
1 parent 5423596 commit c50b70d
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 169 deletions.
14 changes: 14 additions & 0 deletions ArchiSteamFarm/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ public sealed class Bot : IDisposable {
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));
Expand Down Expand Up @@ -212,6 +215,7 @@ private Bot(string botName, BotConfig botConfig, BotDatabase botDatabase) {
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);
Expand Down Expand Up @@ -2262,6 +2266,16 @@ private void OnVanityURLChangedCallback(ArchiHandler.VanityURLChangedCallback ca
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)) {
Expand Down
42 changes: 42 additions & 0 deletions ArchiSteamFarm/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ internal async Task<string> Response(ulong steamID, string message) {
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":
Expand Down Expand Up @@ -130,6 +132,8 @@ internal async Task<string> Response(ulong steamID, string message) {
}

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":
Expand Down Expand Up @@ -2325,6 +2329,44 @@ private string ResponseVersion(ulong steamID) {
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,
Expand Down
Loading

0 comments on commit c50b70d

Please sign in to comment.