private void SoftTimerElapsed(string id, Cooldown.SoftCooldown softCooldown) { Cooldown cooldown = cooldownDict[id]; softCooldown.CooldownTimer.Dispose(); cooldown.softCooldowns.Remove(softCooldown); if (cooldown.softCooldowns.Count == 0) { cooldownDict.Remove(id); } }
/// <summary> /// Adjust a user's balance and make a record of it /// </summary> /// <param name="userId">id of the user whose account is being affected</param> /// <param name="change">the amount to adjust the balance by.</param> /// <returns> /// records of the transaction including: /// the user id /// the amount changed by /// the timestamp of the change /// the old balance /// the new balance /// </returns> public async Task <Dictionary <string, object> > MakeTransaction(int userId, int change, Dictionary <string, object> extra) { if (extra.ContainsKey("cooldown") && (bool)extra["cooldown"]) { if (extra.ContainsKey("cooldown_id")) { if (cooldownDict.ContainsKey((string)extra["cooldown_id"])) { Cooldown cooldown = cooldownDict[(string)extra["cooldown_id"]]; if (cooldown.softCooldowns.Any(x => x.CooldownActive)) { int absChange = Math.Abs(change); int minBid = cooldown.softCooldowns.Where(x => x.CooldownActive) .Sum(x => x.CooldownMinimumBid); if (absChange < minBid) { throw new BidTooLowError( $"The minimum bid for this item is currently {minBid}, you cannot bid {absChange}"); } } } if (cooldownActive) { Cooldown cooldown = cooldownDict.ContainsKey((string)extra["cooldown_id"]) ? cooldownDict[(string)extra["cooldown_id"]] : new Cooldown(); Cooldown.SoftCooldown softCooldown = new Cooldown.SoftCooldown { CooldownActive = true, CooldownLength = (int)extra["cooldown_length"], CooldownMinimumBid = (int)extra["cooldown_minimum_bid"] }; softCooldown.DueTime = DateTime.UtcNow.AddMilliseconds(softCooldown.CooldownLength); softCooldown.CooldownTimer = new Timer((softCooldown.DueTime - DateTime.UtcNow).TotalMilliseconds); softCooldown.CooldownTimer.Elapsed += delegate { SoftTimerElapsed((string)extra["id"], softCooldown); }; softCooldown.CooldownTimer.Start(); cooldown.softCooldowns.Add(softCooldown); if (!cooldownDict.ContainsKey((string)extra["cooldown_id"])) { cooldownDict.Add((string)extra["cooldown_id"], cooldown); } } } } Logger(new ApiLogMessage($"Adjusting {userId}'s balance by {change}", ApiLogLevel.Debug)); int oldBalance = await GetStoredMoneyValue(userId); await AdjustStoredMoneyValue(userId, change); int newBalance = await GetStoredMoneyValue(userId); Dictionary <string, object> transaction = new Dictionary <string, object> { { "user", userId }, { "change", change }, { "timestamp", DateTime.UtcNow }, { "old_balance", oldBalance }, { "new_balance", newBalance } }; foreach (KeyValuePair <string, object> pair in extra.Where(x => !x.Key.StartsWith("cooldown"))) { transaction.Add(pair.Key, pair.Value); } Logger(new ApiLogMessage($"Recording transaction: {{{string.Join(",", transaction.Select(kv => kv.Key + " - " + kv.Value.ToString()).ToArray())}}}", ApiLogLevel.Debug)); await RecordTransaction(transaction); return(transaction); }