Add !pause + misc

This commit is contained in:
JustArchi
2016-03-24 14:18:07 +01:00
parent 3311f2a703
commit 84857e060b
3 changed files with 63 additions and 16 deletions

View File

@@ -344,6 +344,8 @@ namespace ArchiSteamFarm {
return ResponseFarm(steamID);
case "!loot":
return await ResponseSendTrade(steamID).ConfigureAwait(false);
case "!pause":
return await ResponsePause(steamID).ConfigureAwait(false);
case "!rejoinchat":
return ResponseRejoinChat(steamID);
case "!restart":
@@ -384,6 +386,8 @@ namespace ArchiSteamFarm {
} else {
return await ResponseOwns(steamID, BotName, args[1]).ConfigureAwait(false);
}
case "!pause":
return await ResponsePause(steamID, args[1]).ConfigureAwait(false);
case "!play":
if (args.Length > 2) {
return await ResponsePlay(steamID, args[1], args[2]).ConfigureAwait(false);
@@ -490,6 +494,37 @@ namespace ArchiSteamFarm {
Logging.LogGenericInfo("Successfully finished importing mobile authenticator!", BotName);
}
private async Task<string> ResponsePause(ulong steamID) {
if (steamID == 0) {
return null;
}
if (!IsMaster(steamID)) {
return null;
}
if (CardsFarmer.ManualMode) {
await CardsFarmer.SwitchToManualMode(false).ConfigureAwait(false);
return "Automatic farming is enabled again!";
} else {
await CardsFarmer.SwitchToManualMode(true).ConfigureAwait(false);
return "Automatic farming is now stopped!";
}
}
private static async Task<string> ResponsePause(ulong steamID, string botName) {
if (steamID == 0 || string.IsNullOrEmpty(botName)) {
return null;
}
Bot bot;
if (!Bots.TryGetValue(botName, out bot)) {
return "Couldn't find any bot named " + botName + "!";
}
return await bot.ResponsePause(steamID).ConfigureAwait(false);
}
private string ResponseStatus(ulong steamID) {
if (steamID == 0) {
return null;
@@ -1029,11 +1064,14 @@ namespace ArchiSteamFarm {
}
if (gameIDs.Contains(0)) {
if (await CardsFarmer.SwitchToManualMode(false).ConfigureAwait(false)) {
if (CardsFarmer.ManualMode) {
ResetGamesPlayed();
await CardsFarmer.SwitchToManualMode(false).ConfigureAwait(false);
}
} else {
await CardsFarmer.SwitchToManualMode(true).ConfigureAwait(false);
if (!CardsFarmer.ManualMode) {
await CardsFarmer.SwitchToManualMode(true).ConfigureAwait(false);
}
ArchiHandler.PlayGames(gameIDs);
}
@@ -1657,7 +1695,7 @@ namespace ArchiSteamFarm {
}
if (checkTrades) {
Trading.CheckTrades();
Trading.CheckTrades().Forget();
}
if (markInventory && BotConfig.DismissInventoryNotifications) {

View File

@@ -42,7 +42,9 @@ namespace ArchiSteamFarm {
private readonly Bot Bot;
private readonly Timer Timer;
private volatile bool ManualMode, NowFarming;
internal bool ManualMode { get; private set; }
private bool NowFarming;
internal CardsFarmer(Bot bot) {
if (bot == null) {
@@ -61,9 +63,9 @@ namespace ArchiSteamFarm {
}
}
internal async Task<bool> SwitchToManualMode(bool manualMode) {
internal async Task SwitchToManualMode(bool manualMode) {
if (ManualMode == manualMode) {
return false;
return;
}
ManualMode = manualMode;
@@ -75,8 +77,6 @@ namespace ArchiSteamFarm {
Logging.LogGenericInfo("Now running in Automatic Farming mode", Bot.BotName);
StartFarming().Forget();
}
return true;
}
internal async Task StartFarming() {

View File

@@ -35,9 +35,9 @@ namespace ArchiSteamFarm {
private static readonly SemaphoreSlim InventorySemaphore = new SemaphoreSlim(1);
private readonly Bot Bot;
private readonly SemaphoreSlim Semaphore = new SemaphoreSlim(1);
private readonly SemaphoreSlim TradesSemaphore = new SemaphoreSlim(1);
private volatile byte ParsingTasks = 0;
private byte ParsingTasks = 0;
internal static async Task LimitInventoryRequestsAsync() {
await InventorySemaphore.WaitAsync().ConfigureAwait(false);
@@ -55,18 +55,27 @@ namespace ArchiSteamFarm {
Bot = bot;
}
internal async void CheckTrades() {
if (ParsingTasks >= 2) {
internal async Task CheckTrades() {
bool shouldRun = false;
lock (TradesSemaphore) {
if (ParsingTasks < 2) {
ParsingTasks++;
shouldRun = true;
}
}
if (!shouldRun) {
return;
}
ParsingTasks++;
await TradesSemaphore.WaitAsync().ConfigureAwait(false);
await Semaphore.WaitAsync().ConfigureAwait(false);
await ParseActiveTrades().ConfigureAwait(false);
Semaphore.Release();
lock (TradesSemaphore) {
ParsingTasks--;
}
ParsingTasks--;
TradesSemaphore.Release();
}
private async Task ParseActiveTrades() {