Kotz's editorconfig styles slightly modified. Target typed new usage. Brackets in expressions used for clarity.

This commit is contained in:
Kwoth
2021-12-26 02:52:09 +01:00
parent 68741ec484
commit d18f9429c6
172 changed files with 921 additions and 494 deletions

View File

@@ -20,10 +20,10 @@ public sealed class AnimalRace : IDisposable
public event Func<AnimalRace, Task> OnEnded = delegate { return Task.CompletedTask; };
public IReadOnlyCollection<AnimalRacingUser> Users => _users.ToList();
public List<AnimalRacingUser> FinishedUsers { get; } = new List<AnimalRacingUser>();
public List<AnimalRacingUser> FinishedUsers { get; } = new();
private readonly SemaphoreSlim _locker = new SemaphoreSlim(1, 1);
private readonly HashSet<AnimalRacingUser> _users = new HashSet<AnimalRacingUser>();
private readonly SemaphoreSlim _locker = new(1, 1);
private readonly HashSet<AnimalRacingUser> _users = new();
private readonly ICurrencyService _currency;
private readonly RaceOptions _options;
private readonly Queue<RaceAnimal> _animalsQueue;

View File

@@ -13,7 +13,7 @@ public class Blackjack
public Dealer Dealer { get; set; }
public List<User> Players { get; set; } = new List<User>();
public List<User> Players { get; set; } = new();
public GameState State { get; set; } = GameState.Starting;
public User CurrentUser { get; private set; }
@@ -24,7 +24,7 @@ public class Blackjack
public event Func<Blackjack, Task> StateUpdated;
public event Func<Blackjack, Task> GameEnded;
private readonly SemaphoreSlim locker = new SemaphoreSlim(1, 1);
private readonly SemaphoreSlim locker = new(1, 1);
public Blackjack(ICurrencyService cs, DbService db)
{
@@ -174,7 +174,7 @@ public class Blackjack
{
var hw = Dealer.GetHandValue();
while (hw < 17
|| hw == 17 && Dealer.Cards.Count(x => x.Number == 1) > (Dealer.GetRawHandValue() - 17) / 10)// hit on soft 17
|| (hw == 17 && Dealer.Cards.Count(x => x.Number == 1) > (Dealer.GetRawHandValue() - 17) / 10))// hit on soft 17
{
/* Dealer has
A 6

View File

@@ -2,7 +2,7 @@
public abstract class Player
{
public List<Deck.Card> Cards { get; } = new List<Deck.Card>();
public List<Deck.Card> Cards { get; } = new();
public int GetHandValue()
{

View File

@@ -25,7 +25,7 @@ public class CurrencyRaffleGame
}
}
private readonly HashSet<User> _users = new HashSet<User>();
private readonly HashSet<User> _users = new();
public IEnumerable<User> Users => _users;
public Type GameType { get; }

View File

@@ -20,7 +20,7 @@ public class QuadDeck : Deck
public class Deck
{
private static readonly Dictionary<int, string> cardNames = new Dictionary<int, string>() {
private static readonly Dictionary<int, string> _cardNames = new() {
{ 1, "Ace" },
{ 2, "Two" },
{ 3, "Three" },
@@ -35,7 +35,7 @@ public class Deck
{ 12, "Queen" },
{ 13, "King" }
};
private static Dictionary<string, Func<List<Card>, bool>> handValues;
private static Dictionary<string, Func<List<Card>, bool>> _handValues;
public enum CardSuit
@@ -75,15 +75,14 @@ public class Deck
this.Number = cardNum;
}
public string GetValueText() => cardNames[Number];
public string GetValueText() => _cardNames[Number];
public override string ToString() => cardNames[Number] + " Of " + Suit;
public override string ToString() => _cardNames[Number] + " Of " + Suit;
public int CompareTo(object obj)
{
if (!(obj is Card)) return 0;
var c = (Card)obj;
return this.Number - c.Number;
if (obj is not Card card) return 0;
return this.Number - card.Number;
}
public static Card Parse(string input)
@@ -133,7 +132,7 @@ public class Deck
{CardSuit.Spades, "♠"},
{CardSuit.Hearts, "♥"},
};
private static IReadOnlyDictionary<string, CardSuit> _suitCharToSuit = new Dictionary<string, CardSuit>
private static readonly IReadOnlyDictionary<string, CardSuit> _suitCharToSuit = new Dictionary<string, CardSuit>
{
{"♦", CardSuit.Diamonds },
{"d", CardSuit.Diamonds },
@@ -144,7 +143,7 @@ public class Deck
{"♥", CardSuit.Hearts },
{"h", CardSuit.Hearts },
};
private static IReadOnlyDictionary<char, int> _numberCharToNumber = new Dictionary<char, int>()
private static readonly IReadOnlyDictionary<char, int> _numberCharToNumber = new Dictionary<char, int>()
{
{'a', 1 },
{'2', 2 },
@@ -200,7 +199,7 @@ public class Deck
}
}
}
private Random r = new NadekoRandom();
private readonly Random _r = new NadekoRandom();
/// <summary>
/// Take a card from the pool, you either take it from the top if the deck is shuffled, or from a random place if the deck is in the default order.
/// </summary>
@@ -211,7 +210,7 @@ public class Deck
Restart();
//you can either do this if your deck is not shuffled
var num = r.Next(0, CardPool.Count);
var num = _r.Next(0, CardPool.Count);
var c = CardPool[num];
CardPool.RemoveAt(num);
return c;
@@ -230,73 +229,73 @@ public class Deck
{
if (CardPool.Count <= 1) return;
var orderedPool = CardPool.Shuffle();
CardPool = CardPool as List<Card> ?? orderedPool.ToList();
CardPool ??= orderedPool.ToList();
}
public override string ToString() => string.Concat(CardPool.Select(c => c.ToString())) + Environment.NewLine;
private static void InitHandValues()
{
bool hasPair(List<Card> cards) => cards.GroupBy(card => card.Number)
bool HasPair(List<Card> cards) => cards.GroupBy(card => card.Number)
.Count(group => group.Count() == 2) == 1;
bool isPair(List<Card> cards) => cards.GroupBy(card => card.Number)
bool IsPair(List<Card> cards) => cards.GroupBy(card => card.Number)
.Count(group => group.Count() == 3) == 0
&& hasPair(cards);
&& HasPair(cards);
bool isTwoPair(List<Card> cards) => cards.GroupBy(card => card.Number)
bool IsTwoPair(List<Card> cards) => cards.GroupBy(card => card.Number)
.Count(group => group.Count() == 2) == 2;
bool isStraight(List<Card> cards)
bool IsStraight(List<Card> cards)
{
if (cards.GroupBy(card => card.Number).Count() != cards.Count())
return false;
var toReturn = cards.Max(card => (int)card.Number)
- cards.Min(card => (int)card.Number) == 4;
var toReturn = cards.Max(card => card.Number)
- cards.Min(card => card.Number) == 4;
if (toReturn || cards.All(c => c.Number != 1)) return toReturn;
var newCards = cards.Select(c => c.Number == 1 ? new(c.Suit, 14) : c);
return newCards.Max(card => (int)card.Number)
- newCards.Min(card => (int)card.Number) == 4;
return newCards.Max(card => card.Number)
- newCards.Min(card => card.Number) == 4;
}
bool hasThreeOfKind(List<Card> cards) => cards.GroupBy(card => card.Number)
bool HasThreeOfKind(List<Card> cards) => cards.GroupBy(card => card.Number)
.Any(group => group.Count() == 3);
bool isThreeOfKind(List<Card> cards) => hasThreeOfKind(cards) && !hasPair(cards);
bool IsThreeOfKind(List<Card> cards) => HasThreeOfKind(cards) && !HasPair(cards);
bool isFlush(List<Card> cards) => cards.GroupBy(card => card.Suit).Count() == 1;
bool IsFlush(List<Card> cards) => cards.GroupBy(card => card.Suit).Count() == 1;
bool isFourOfKind(List<Card> cards) => cards.GroupBy(card => card.Number)
bool IsFourOfKind(List<Card> cards) => cards.GroupBy(card => card.Number)
.Any(group => group.Count() == 4);
bool isFullHouse(List<Card> cards) => hasPair(cards) && hasThreeOfKind(cards);
bool IsFullHouse(List<Card> cards) => HasPair(cards) && HasThreeOfKind(cards);
bool hasStraightFlush(List<Card> cards) => isFlush(cards) && isStraight(cards);
bool HasStraightFlush(List<Card> cards) => IsFlush(cards) && IsStraight(cards);
bool isRoyalFlush(List<Card> cards) => cards.Min(card => card.Number) == 1 &&
bool IsRoyalFlush(List<Card> cards) => cards.Min(card => card.Number) == 1 &&
cards.Max(card => card.Number) == 13
&& hasStraightFlush(cards);
&& HasStraightFlush(cards);
bool isStraightFlush(List<Card> cards) => hasStraightFlush(cards) && !isRoyalFlush(cards);
bool IsStraightFlush(List<Card> cards) => HasStraightFlush(cards) && !IsRoyalFlush(cards);
handValues = new()
_handValues = new()
{
{ "Royal Flush", isRoyalFlush },
{ "Straight Flush", isStraightFlush },
{ "Four Of A Kind", isFourOfKind },
{ "Full House", isFullHouse },
{ "Flush", isFlush },
{ "Straight", isStraight },
{ "Three Of A Kind", isThreeOfKind },
{ "Two Pairs", isTwoPair },
{ "A Pair", isPair }
{ "Royal Flush", IsRoyalFlush },
{ "Straight Flush", IsStraightFlush },
{ "Four Of A Kind", IsFourOfKind },
{ "Full House", IsFullHouse },
{ "Flush", IsFlush },
{ "Straight", IsStraight },
{ "Three Of A Kind", IsThreeOfKind },
{ "Two Pairs", IsTwoPair },
{ "A Pair", IsPair }
};
}
public static string GetHandValue(List<Card> cards)
{
if (handValues is null)
if (_handValues is null)
InitHandValues();
foreach (var kvp in handValues.Where(x => x.Value(cards)))
foreach (var kvp in _handValues.Where(x => x.Value(cards)))
{
return kvp.Key;
}

View File

@@ -18,8 +18,8 @@ public class GameStatusEvent : ICurrencyEvent
private readonly Func<CurrencyEvent.Type, EventOptions, long, IEmbedBuilder> _embedFunc;
private readonly bool _isPotLimited;
private readonly ITextChannel _channel;
private readonly ConcurrentHashSet<ulong> _awardedUsers = new ConcurrentHashSet<ulong>();
private readonly ConcurrentQueue<ulong> _toAward = new ConcurrentQueue<ulong>();
private readonly ConcurrentHashSet<ulong> _awardedUsers = new();
private readonly ConcurrentQueue<ulong> _toAward = new();
private readonly Timer _t;
private readonly Timer _timeout = null;
private readonly EventOptions _opts;
@@ -127,7 +127,7 @@ public class GameStatusEvent : ICurrencyEvent
}
}
private readonly object stopLock = new object();
private readonly object stopLock = new();
public async Task StopEvent()
{
await Task.Yield();
@@ -150,7 +150,7 @@ public class GameStatusEvent : ICurrencyEvent
{
var _ = Task.Run(async () =>
{
if (!(msg.Author is IGuildUser gu) // no unknown users, as they could be bots, or alts
if (msg.Author is not IGuildUser gu // no unknown users, as they could be bots, or alts
|| gu.IsBot // no bots
|| msg.Content != _code // code has to be the same
|| (DateTime.UtcNow - gu.CreatedAt).TotalDays <= 5) // no recently created accounts
@@ -178,7 +178,7 @@ public class GameStatusEvent : ICurrencyEvent
return Task.CompletedTask;
}
private readonly object potLock = new object();
private readonly object potLock = new();
private bool TryTakeFromPot()
{
if (_isPotLimited)

View File

@@ -19,8 +19,8 @@ public class ReactionEvent : ICurrencyEvent
private readonly Func<CurrencyEvent.Type, EventOptions, long, IEmbedBuilder> _embedFunc;
private readonly bool _isPotLimited;
private readonly ITextChannel _channel;
private readonly ConcurrentHashSet<ulong> _awardedUsers = new ConcurrentHashSet<ulong>();
private readonly ConcurrentQueue<ulong> _toAward = new ConcurrentQueue<ulong>();
private readonly ConcurrentHashSet<ulong> _awardedUsers = new();
private readonly ConcurrentQueue<ulong> _toAward = new();
private readonly Timer _t;
private readonly Timer _timeout = null;
private readonly bool _noRecentlyJoinedServer;
@@ -130,7 +130,7 @@ public class ReactionEvent : ICurrencyEvent
}
}
private readonly object stopLock = new object();
private readonly object stopLock = new();
public async Task StopEvent()
{
await Task.Yield();
@@ -160,8 +160,8 @@ public class ReactionEvent : ICurrencyEvent
|| msg.Id != _msg.Id // same message
|| gu.IsBot // no bots
|| (DateTime.UtcNow - gu.CreatedAt).TotalDays <= 5 // no recently created accounts
|| _noRecentlyJoinedServer && // if specified, no users who joined the server in the last 24h
(gu.JoinedAt is null || (DateTime.UtcNow - gu.JoinedAt.Value).TotalDays < 1)) // and no users for who we don't know when they joined
|| (_noRecentlyJoinedServer && // if specified, no users who joined the server in the last 24h
(gu.JoinedAt is null || (DateTime.UtcNow - gu.JoinedAt.Value).TotalDays < 1))) // and no users for who we don't know when they joined
{
return;
}
@@ -177,7 +177,7 @@ public class ReactionEvent : ICurrencyEvent
return Task.CompletedTask;
}
private readonly object potLock = new object();
private readonly object potLock = new();
private bool TryTakeFromPot()
{
if (_isPotLimited)

View File

@@ -108,9 +108,9 @@ Doesn't have to be ordered.")]
{
Pairs = new BetRollPair[]
{
new BetRollPair { WhenAbove = 99, MultiplyBy = 10 },
new BetRollPair { WhenAbove = 90, MultiplyBy = 4 },
new BetRollPair { WhenAbove = 66, MultiplyBy = 2 }
new() { WhenAbove = 99, MultiplyBy = 10 },
new() { WhenAbove = 90, MultiplyBy = 4 },
new() { WhenAbove = 66, MultiplyBy = 2 }
};
}
}
@@ -184,11 +184,11 @@ public sealed partial class WaifuConfig
[Comment(@"Minimum price a waifu can have")]
public int MinPrice { get; set; } = 50;
public MultipliersData Multipliers { get; set; } = new MultipliersData();
public MultipliersData Multipliers { get; set; } = new();
[Comment(@"List of items available for gifting.
If negative is true, gift will instead reduce waifu value.")]
public List<WaifuItemModel> Items { get; set; } = new List<WaifuItemModel>();
public List<WaifuItemModel> Items { get; set; } = new();
public WaifuConfig()
{

View File

@@ -26,13 +26,13 @@ public class RollDuelGame
}
private readonly Timer _timeoutTimer;
private readonly NadekoRandom _rng = new NadekoRandom();
private readonly SemaphoreSlim _locker = new SemaphoreSlim(1, 1);
private readonly NadekoRandom _rng = new();
private readonly SemaphoreSlim _locker = new(1, 1);
public event Func<RollDuelGame, Task> OnGameTick;
public event Func<RollDuelGame, Reason, Task> OnEnded;
public List<(int, int)> Rolls { get; } = new List<(int, int)>();
public List<(int, int)> Rolls { get; } = new();
public State CurrentState { get; private set; }
public ulong Winner { get; private set; }
@@ -111,7 +111,7 @@ public class RollDuelGame
await _cs.AddAsync(Winner, "Roll Duel win", won)
.ConfigureAwait(false);
await _cs.AddAsync(_botId, "Roll Duel fee", Amount * 2 - won)
await _cs.AddAsync(_botId, "Roll Duel fee", (Amount * 2) - won)
.ConfigureAwait(false);
}
try { await (OnGameTick?.Invoke(this)).ConfigureAwait(false); } catch { }

View File

@@ -4,6 +4,6 @@ public class SlotResponse
{
public float Multiplier { get; set; }
public long Won { get; set; }
public List<int> Rolls { get; set; } = new List<int>();
public List<int> Rolls { get; set; } = new();
public GamblingError Error { get; set; }
}