Skip to content

Commit

Permalink
check only boosters when it's due time for them
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudokhvist committed Feb 27, 2019
1 parent 527f115 commit 0c3a1a2
Showing 1 changed file with 56 additions and 55 deletions.
111 changes: 56 additions & 55 deletions BoosterCreator/BoosterHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
namespace BoosterCreator {
internal sealed class BoosterHandler : IDisposable {
private readonly Bot Bot;
private readonly ConcurrentDictionary<uint,DateTime?> GameIDs = new ConcurrentDictionary<uint, DateTime?>();
private readonly ConcurrentDictionary<uint, DateTime?> GameIDs = new ConcurrentDictionary<uint, DateTime?>();
private readonly Timer BoosterTimer;

internal static ConcurrentDictionary<string, BoosterHandler> BoosterHandlers = new ConcurrentDictionary<string, BoosterHandler>();

internal BoosterHandler([NotNull] Bot bot, IReadOnlyCollection<uint> gameIDs) {
Bot = bot ?? throw new ArgumentNullException(nameof(bot));
foreach(var gameID in gameIDs) {
foreach (var gameID in gameIDs) {
GameIDs.TryAdd(gameID, DateTime.Now.AddHours(1));
}

Expand All @@ -44,7 +44,7 @@ private async Task AutoBooster() {
}

string response = await CreateBooster(Bot, GameIDs).ConfigureAwait(false);
ASF.ArchiLogger.LogGenericInfo (response);
ASF.ArchiLogger.LogGenericInfo(response);
}

internal static async Task<string> CreateBooster(Bot bot, ConcurrentDictionary<uint, DateTime?> gameIDs) {
Expand Down Expand Up @@ -78,80 +78,81 @@ internal static async Task<string> CreateBooster(Bot bot, ConcurrentDictionary<u
StringBuilder response = new StringBuilder();

foreach (var gameID in gameIDs) {
await Task.Delay(500).ConfigureAwait(false);
if (!gameID.Value.HasValue || DateTime.Compare(gameID.Value.Value, DateTime.Now) <= 0) {
await Task.Delay(500).ConfigureAwait(false);

if (!boosterInfos.ContainsKey(gameID.Key)) {
response.AppendLine(Commands.FormatBotResponse(bot, string.Format(Strings.BotAddLicense, gameID, "NotEligible")));
//If we are not eligible - wait 8 hours, just in case game will be added to account later
if (gameID.Value.HasValue) { //if source is timer, not command
gameIDs[gameID.Key] = DateTime.Now.AddHours(8);
if (!boosterInfos.ContainsKey(gameID.Key)) {
response.AppendLine(Commands.FormatBotResponse(bot, string.Format(Strings.BotAddLicense, gameID, "NotEligible")));
//If we are not eligible - wait 8 hours, just in case game will be added to account later
if (gameID.Value.HasValue) { //if source is timer, not command
gameIDs[gameID.Key] = DateTime.Now.AddHours(8);
}
continue;
}
continue;
}

Steam.BoosterInfo bi = boosterInfos[gameID.Key];
Steam.BoosterInfo bi = boosterInfos[gameID.Key];

if (gooAmount < bi.Price) {
response.AppendLine(Commands.FormatBotResponse(bot, string.Format(Strings.BotAddLicense, gameID, "NotEnoughGems")));
//If we have not enough gems - wait 8 hours, just in case gems will be added to account later
if (gameID.Value.HasValue) { //if source is timer, not command
gameIDs[gameID.Key] = DateTime.Now.AddHours(8);
if (gooAmount < bi.Price) {
response.AppendLine(Commands.FormatBotResponse(bot, string.Format(Strings.BotAddLicense, gameID, "NotEnoughGems")));
//If we have not enough gems - wait 8 hours, just in case gems will be added to account later
if (gameID.Value.HasValue) { //if source is timer, not command
gameIDs[gameID.Key] = DateTime.Now.AddHours(8);
}
continue;
}
continue;
}

if (bi.Unavailable) {
response.AppendLine(Commands.FormatBotResponse(bot, string.Format(Strings.BotAddLicense, gameID, $"Available at time: {bi.AvailableAtTime}")));
//Wait until specified time
DateTime availableAtTime;

if (DateTime.TryParseExact(bi.AvailableAtTime, "d MMM @ h:mmtt", CultureInfo.CurrentCulture, DateTimeStyles.None, out availableAtTime)) {
DateTime convertedTime = TimeZoneInfo.ConvertTime(availableAtTime, ValveTimeZone.GetTimeZoneInfo(), TimeZoneInfo.Local);
if (gameID.Value.HasValue) { //if source is timer, not command
gameIDs[gameID.Key] = convertedTime;
if (bi.Unavailable) {
response.AppendLine(Commands.FormatBotResponse(bot, string.Format(Strings.BotAddLicense, gameID, $"Available at time: {bi.AvailableAtTime}")));
//Wait until specified time
DateTime availableAtTime;

if (DateTime.TryParseExact(bi.AvailableAtTime, "d MMM @ h:mmtt", CultureInfo.CurrentCulture, DateTimeStyles.None, out availableAtTime)) {
DateTime convertedTime = TimeZoneInfo.ConvertTime(availableAtTime, ValveTimeZone.GetTimeZoneInfo(), TimeZoneInfo.Local);
if (gameID.Value.HasValue) { //if source is timer, not command
gameIDs[gameID.Key] = convertedTime;
}
} else {
ASF.ArchiLogger.LogGenericInfo("Unable to parse time \"" + bi.AvailableAtTime + "\", please report this.");
}
} else {
ASF.ArchiLogger.LogGenericInfo("Unable to parse time \""+ bi.AvailableAtTime+"\", please report this.");
continue;
}
continue;
}

uint nTp;
uint nTp;

if (unTradableGooAmount > 0) {
nTp = tradableGooAmount > bi.Price ? (uint) 1 : 3;
} else {
nTp = 2;
}
if (unTradableGooAmount > 0) {
nTp = tradableGooAmount > bi.Price ? (uint) 1 : 3;
} else {
nTp = 2;
}

Steam.BoostersResponse result = await WebRequest.CreateBooster(bot, bi.AppID, bi.Series, nTp).ConfigureAwait(false);
Steam.BoostersResponse result = await WebRequest.CreateBooster(bot, bi.AppID, bi.Series, nTp).ConfigureAwait(false);

if (result?.Result?.Result != EResult.OK) {
response.AppendLine(Commands.FormatBotResponse(bot, string.Format(Strings.BotAddLicense, bi.AppID, EResult.Fail)));
//Some unhandled error - wait 8 hours before retry
if (result?.Result?.Result != EResult.OK) {
response.AppendLine(Commands.FormatBotResponse(bot, string.Format(Strings.BotAddLicense, bi.AppID, EResult.Fail)));
//Some unhandled error - wait 8 hours before retry
if (gameID.Value.HasValue) { //if source is timer, not command
gameIDs[gameID.Key] = DateTime.Now.AddHours(8);
}
continue;
}

gooAmount = result.GooAmount;
tradableGooAmount = result.TradableGooAmount;
unTradableGooAmount = result.UntradableGooAmount;
response.AppendLine(Commands.FormatBotResponse(bot, string.Format(Strings.BotAddLicenseWithItems, bi.AppID, EResult.OK, bi.Name)));
//Buster was made - next is only available in 24 hours
if (gameID.Value.HasValue) { //if source is timer, not command
gameIDs[gameID.Key] = DateTime.Now.AddHours(8);
gameIDs[gameID.Key] = DateTime.Now.AddHours(24);
}
continue;
}

gooAmount = result.GooAmount;
tradableGooAmount = result.TradableGooAmount;
unTradableGooAmount = result.UntradableGooAmount;
response.AppendLine(Commands.FormatBotResponse(bot, string.Format(Strings.BotAddLicenseWithItems, bi.AppID, EResult.OK, bi.Name)));
//Buster was made - next is only available in 24 hours
if (gameID.Value.HasValue) { //if source is timer, not command
gameIDs[gameID.Key] = DateTime.Now.AddHours(24);
}


}

//Get nearest time when we should try for new booster;
DateTime? nextTry = gameIDs.Values.Min<DateTime?>();

if (nextTry.HasValue) { //if it was not from command
//Add 10 minutes to avoid race conditions
//Add 10 minutes to avoid race conditions
BoosterHandler.BoosterHandlers[bot.BotName].BoosterTimer.Change(nextTry.Value - DateTime.Now + TimeSpan.FromMinutes(10), nextTry.Value - DateTime.Now + TimeSpan.FromMinutes(10));
}

Expand Down

0 comments on commit 0c3a1a2

Please sign in to comment.