internal async Task StartFarming() {
if (NowFarming || Paused || !Bot.IsPlayingPossible) {
return;
}
if (Bot.IsLimitedUser) {
await Bot.OnFarmingFinished(false).ConfigureAwait(false);
return;
}
await FarmingSemaphore.WaitAsync().ConfigureAwait(false);
try {
if (NowFarming || Paused || !Bot.IsPlayingPossible) {
return;
}
if (!await IsAnythingToFarm().ConfigureAwait(false)) {
Bot.ArchiLogger.LogGenericInfo("We don't have anything to farm on this account!");
await Bot.OnFarmingFinished(false).ConfigureAwait(false);
return;
}
Bot.ArchiLogger.LogGenericInfo("We have a total of " + GamesToFarm.Count + " games (" + GamesToFarm.Sum(game => game.CardsRemaining) + " cards) to farm on this account...");
// This is the last moment for final check if we can farm
if (!Bot.IsPlayingPossible) {
Bot.ArchiLogger.LogGenericInfo("But farming is currently unavailable, we'll try later!");
return;
}
KeepFarming = NowFarming = true;
} finally {
FarmingSemaphore.Release();
}
do {
// Now the algorithm used for farming depends on whether account is restricted or not
if (Bot.BotConfig.CardDropsRestricted) { // If we have restricted card drops, we use complex algorithm
Bot.ArchiLogger.LogGenericInfo("Chosen farming algorithm: Complex");
while (GamesToFarm.Count > 0) {
HashSet<Game> gamesToFarmSolo = GamesToFarm.Count > 1 ? new HashSet<Game>(GamesToFarm.Where(game => game.HoursPlayed >= 2)) : new HashSet<Game>(GamesToFarm);
if (gamesToFarmSolo.Count > 0) {
while (gamesToFarmSolo.Count > 0) {
Game game = gamesToFarmSolo.First();
if (await FarmSolo(game).ConfigureAwait(false)) {
gamesToFarmSolo.Remove(game);
} else {
NowFarming = false;
return;
}
}
} else {
if (FarmMultiple(GamesToFarm.OrderByDescending(game => game.HoursPlayed).Take(MaxGamesPlayedConcurrently))) {
Bot.ArchiLogger.LogGenericInfo("Done farming: " + string.Join(", ", GamesToFarm.Select(game => game.AppID)));
} else {
NowFarming = false;
return;
}
}
}
} else { // If we have unrestricted card drops, we use simple algorithm
Bot.ArchiLogger.LogGenericInfo("Chosen farming algorithm: Simple");
while (GamesToFarm.Count > 0) {
Game game = GamesToFarm.First();
if (await FarmSolo(game).ConfigureAwait(false)) {
continue;
}
NowFarming = false;
return;
}
}
} while (await IsAnythingToFarm().ConfigureAwait(false));
CurrentGamesFarming.ClearAndTrim();
NowFarming = false;
Bot.ArchiLogger.LogGenericInfo("Farming finished!");
await Bot.OnFarmingFinished(true).ConfigureAwait(false);
}