/// <inheritdoc /> public async Task AddToUserAsync (List <IUserBase> userList, int points = 1, int minutes = 0, bool subCheck = true) { if (userList.Count != 0) { userList = RemoveFilteredUsers(userList); var pointsMultiplier = _configuration.PointsMultiplier; var channelSubscribers = new List <IUserBase>(); if (subCheck) { for (var attempt = 0; attempt < 5; attempt++) { try { channelSubscribers = await _apiRetriever .GetChannelSubscribersAsync(_apiRetriever.TwitchChannelId) .ConfigureAwait(false); break; } catch (GatewayTimeoutException ex) { if (attempt == 4) { throw; } Log.Warning(ex, "Failed. Gateway Timed Out. Retrying in 20 Seconds"); } catch (Exception ex) { Log.Error(ex, "Failed to GetSubscribers or ChannelId"); throw; } await Task.Delay(20000).ConfigureAwait(false); } } var addPointsTasks = new List <Task>(); foreach (var user in userList) { var pointAdderValue = points; if (channelSubscribers.Any(x => x.UserId == user.UserId)) { pointAdderValue = (int)(pointAdderValue * pointsMultiplier); } addPointsTasks.Add(_dataAccess.ModifierUserIdAsync(user.UserId, pointAdderValue, minutes)); } await Task.WhenAll(addPointsTasks).ConfigureAwait(false); await _rankManager.UpdateRankAsync(userList).ConfigureAwait(false); } }
public async Task <string> ProcessorAsync(OnChatCommandReceivedArgs e) { if (string.IsNullOrEmpty(e.Command.ArgumentsAsString)) { return(GambleFormat); } if (!int.TryParse(e.Command.ArgumentsAsList[0], out var points)) { return(StandardMessages.ErrorMessages.NotNumber); } if (points <= 0) { return("Trebuie sa joci minim 1XP"); } var databaseUser = await _dataAccess.RetrieveUserFromTableAsync(DatabaseTables.UserPoints, e.Command.ChatMessage.UserId).ConfigureAwait(false); if (databaseUser == null) { return(string.Format(StandardMessages.ErrorMessages.NotInDatabase, e.Command.ChatMessage.DisplayName)); } if (!int.TryParse(databaseUser.Points, out var userPoints)) { Log.Error("COULDN'T parse databse points {points}", databaseUser.Points); return(StandardMessages.ErrorMessages.BigError); } if (userPoints < points) { return(StandardMessages.ErrorMessages.NotEnoughPoints); } if (!_lostGambles.TryGetValue(e.Command.ChatMessage.UserId, out var tries)) { _lostGambles[e.Command.ChatMessage.UserId] = 0; tries = 0; } if (_random.Next(0, 100) <= (tries < 2 ? 40 : 60)) { _lostGambles[e.Command.ChatMessage.UserId] = 0; await _dataAccess.ModifierUserIdAsync(e.Command.ChatMessage.UserId, points).ConfigureAwait(false); await _rankManager .UpdateRankAsync(new UserBase(e.Command.ChatMessage.DisplayName, e.Command.ChatMessage.UserId)) .ConfigureAwait(false); return($"/me {e.Command.ChatMessage.DisplayName} ai castigat : {points * 2}XP"); } _lostGambles[e.Command.ChatMessage.UserId] = tries + 1; await _dataAccess.ModifierUserIdAsync(e.Command.ChatMessage.UserId, -1 *points).ConfigureAwait(false); await _rankManager .UpdateRankAsync(new UserBase(e.Command.ChatMessage.DisplayName, e.Command.ChatMessage.UserId)) .ConfigureAwait(false); return($"/me {e.Command.ChatMessage.DisplayName} ai pierdut : {points}XP"); }