mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 09:48:26 -04:00
Fixed around 140 wrong namings and other refactorings which were marked as warnings
This commit is contained in:
@@ -44,8 +44,7 @@ public sealed class AnimalRace : IDisposable
|
||||
}
|
||||
|
||||
public void Initialize() //lame name
|
||||
{
|
||||
var _t = Task.Run(async () =>
|
||||
=> _ = Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(_options.StartTime * 1000);
|
||||
|
||||
@@ -59,7 +58,6 @@ public sealed class AnimalRace : IDisposable
|
||||
}
|
||||
finally { _locker.Release(); }
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<AnimalRacingUser> JoinRace(ulong userId, string userName, long bet = 0)
|
||||
{
|
||||
@@ -104,13 +102,13 @@ public sealed class AnimalRace : IDisposable
|
||||
if (user.Bet > 0)
|
||||
await _currency.AddAsync(user.UserId, "Race refund", user.Bet);
|
||||
|
||||
var _sf = OnStartingFailed?.Invoke(this);
|
||||
_ = OnStartingFailed?.Invoke(this);
|
||||
CurrentPhase = Phase.Ended;
|
||||
return;
|
||||
}
|
||||
|
||||
var _ = OnStarted?.Invoke(this);
|
||||
var _t = Task.Run(async () =>
|
||||
_ = OnStarted?.Invoke(this);
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
var rng = new NadekoRandom();
|
||||
while (!_users.All(x => x.Progress >= 60))
|
||||
@@ -126,7 +124,7 @@ public sealed class AnimalRace : IDisposable
|
||||
|
||||
FinishedUsers.AddRange(finished);
|
||||
|
||||
var _ignore = OnStateUpdate?.Invoke(this);
|
||||
_ = OnStateUpdate?.Invoke(this);
|
||||
await Task.Delay(2500);
|
||||
}
|
||||
|
||||
@@ -135,7 +133,7 @@ public sealed class AnimalRace : IDisposable
|
||||
"Won a Race",
|
||||
FinishedUsers[0].Bet * (_users.Count - 1));
|
||||
|
||||
var _ended = OnEnded?.Invoke(this);
|
||||
_ = OnEnded?.Invoke(this);
|
||||
});
|
||||
}
|
||||
|
||||
|
@@ -48,7 +48,7 @@ public partial class Gambling
|
||||
|
||||
Task _client_MessageReceived(SocketMessage arg)
|
||||
{
|
||||
var _ = Task.Run(() =>
|
||||
_= Task.Run(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
|
@@ -18,7 +18,7 @@ public partial class Gambling
|
||||
|
||||
private readonly ICurrencyService _cs;
|
||||
private readonly DbService _db;
|
||||
private IUserMessage _msg;
|
||||
private IUserMessage msg;
|
||||
|
||||
public BlackJackCommands(ICurrencyService cs, DbService db, GamblingConfigService gamblingConf)
|
||||
: base(gamblingConf)
|
||||
@@ -74,12 +74,13 @@ public partial class Gambling
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_msg is not null)
|
||||
if (msg is not null)
|
||||
{
|
||||
var _ = _msg.DeleteAsync();
|
||||
_= msg.DeleteAsync();
|
||||
}
|
||||
|
||||
var c = bj.Dealer.Cards.Select(x => x.GetEmojiString());
|
||||
var c = bj.Dealer.Cards.Select(x => x.GetEmojiString())
|
||||
.ToList();
|
||||
var dealerIcon = "❔ ";
|
||||
if (bj.State == Blackjack.GameState.Ended)
|
||||
{
|
||||
@@ -102,7 +103,7 @@ public partial class Gambling
|
||||
|
||||
foreach (var p in bj.Players)
|
||||
{
|
||||
c = p.Cards.Select(x => x.GetEmojiString());
|
||||
c = p.Cards.Select(x => x.GetEmojiString()).ToList();
|
||||
cStr = "-\t" + string.Concat(c.Select(x => x[..^1] + " "));
|
||||
cStr += "\n-\t" + string.Concat(c.Select(x => x.Last() + " "));
|
||||
var full = $"{p.DiscordUser.ToString().TrimTo(20)} | Bet: {p.Bet} | Value: {p.GetHandValue()}";
|
||||
@@ -133,7 +134,7 @@ public partial class Gambling
|
||||
embed.AddField(full, cStr);
|
||||
}
|
||||
|
||||
_msg = await ctx.Channel.EmbedAsync(embed);
|
||||
msg = await ctx.Channel.EmbedAsync(embed);
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -146,7 +147,7 @@ public partial class Gambling
|
||||
? Format.Strikethrough(x.DiscordUser.ToString().TrimTo(30))
|
||||
: x.DiscordUser.ToString();
|
||||
|
||||
var hand = $"{string.Concat(x.Cards.Select(y => "〖" + y.GetEmojiString() + "〗"))}";
|
||||
// var hand = $"{string.Concat(x.Cards.Select(y => "〖" + y.GetEmojiString() + "〗"))}";
|
||||
|
||||
|
||||
return $"{playerName} | Bet: {x.Bet}\n";
|
||||
|
@@ -35,9 +35,7 @@ public class Blackjack
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
var _ = GameLoop();
|
||||
}
|
||||
=> _= GameLoop();
|
||||
|
||||
public async Task GameLoop()
|
||||
{
|
||||
@@ -89,13 +87,13 @@ public class Blackjack
|
||||
Log.Information("Dealer moves");
|
||||
await DealerMoves();
|
||||
await PrintState();
|
||||
var _ = GameEnded?.Invoke(this);
|
||||
_= GameEnded?.Invoke(this);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "REPORT THE MESSAGE BELOW IN #NadekoLog SERVER PLEASE");
|
||||
State = GameState.Ended;
|
||||
var _ = GameEnded?.Invoke(this);
|
||||
_= GameEnded?.Invoke(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,7 +125,7 @@ public class Blackjack
|
||||
if (!await _cs.RemoveAsync(user, "BlackJack-gamble", bet, gamble: true)) return false;
|
||||
|
||||
Players.Add(new(user, bet));
|
||||
var _ = PrintState();
|
||||
_= PrintState();
|
||||
return true;
|
||||
}
|
||||
finally
|
||||
|
@@ -28,8 +28,8 @@ public sealed class Connect4Game : IDisposable
|
||||
OtherPlayerWon
|
||||
}
|
||||
|
||||
public const int NumberOfColumns = 7;
|
||||
public const int NumberOfRows = 6;
|
||||
public const int NUMBER_OF_COLUMNS = 7;
|
||||
public const int NUMBER_OF_ROWS = 6;
|
||||
|
||||
//public event Func<Connect4Game, Task> OnGameStarted;
|
||||
public event Func<Connect4Game, Task> OnGameStateUpdated;
|
||||
@@ -51,7 +51,7 @@ public sealed class Connect4Game : IDisposable
|
||||
=> CurrentPhase == Phase.P2Move ? _players[0].Value : _players[1].Value;
|
||||
|
||||
//state is bottom to top, left to right
|
||||
private readonly Field[] _gameState = new Field[NumberOfRows * NumberOfColumns];
|
||||
private readonly Field[] _gameState = new Field[NUMBER_OF_ROWS * NUMBER_OF_COLUMNS];
|
||||
private readonly (ulong UserId, string Username)?[] _players = new (ulong, string)?[2];
|
||||
|
||||
private readonly SemaphoreSlim _locker = new(1, 1);
|
||||
@@ -81,14 +81,14 @@ public sealed class Connect4Game : IDisposable
|
||||
_cs = cs;
|
||||
|
||||
_rng = new();
|
||||
for (var i = 0; i < NumberOfColumns * NumberOfRows; i++) _gameState[i] = Field.Empty;
|
||||
for (var i = 0; i < NUMBER_OF_COLUMNS * NUMBER_OF_ROWS; i++) _gameState[i] = Field.Empty;
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
if (CurrentPhase != Phase.Joining)
|
||||
return;
|
||||
var _ = Task.Run(async () =>
|
||||
_= Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(15000);
|
||||
await _locker.WaitAsync();
|
||||
@@ -165,14 +165,14 @@ public sealed class Connect4Game : IDisposable
|
||||
|| (_players[1].Value.UserId == userId && CurrentPhase == Phase.P2Move)))
|
||||
return false;
|
||||
|
||||
if (inputCol is < 0 or > NumberOfColumns) //invalid input
|
||||
if (inputCol is < 0 or > NUMBER_OF_COLUMNS) //invalid input
|
||||
return false;
|
||||
|
||||
if (IsColumnFull(inputCol)) //can't play there event?
|
||||
return false;
|
||||
|
||||
var start = NumberOfRows * inputCol;
|
||||
for (var i = start; i < start + NumberOfRows; i++)
|
||||
var start = NUMBER_OF_ROWS * inputCol;
|
||||
for (var i = start; i < start + NUMBER_OF_ROWS; i++)
|
||||
if (_gameState[i] == Field.Empty)
|
||||
{
|
||||
_gameState[i] = GetPlayerPiece(userId);
|
||||
@@ -182,21 +182,21 @@ public sealed class Connect4Game : IDisposable
|
||||
//check winnning condition
|
||||
// ok, i'll go from [0-2] in rows (and through all columns) and check upward if 4 are connected
|
||||
|
||||
for (var i = 0; i < NumberOfRows - 3; i++)
|
||||
for (var i = 0; i < NUMBER_OF_ROWS - 3; i++)
|
||||
{
|
||||
if (CurrentPhase == Phase.Ended)
|
||||
break;
|
||||
|
||||
for (var j = 0; j < NumberOfColumns; j++)
|
||||
for (var j = 0; j < NUMBER_OF_COLUMNS; j++)
|
||||
{
|
||||
if (CurrentPhase == Phase.Ended)
|
||||
break;
|
||||
|
||||
var first = _gameState[i + (j * NumberOfRows)];
|
||||
var first = _gameState[i + (j * NUMBER_OF_ROWS)];
|
||||
if (first != Field.Empty)
|
||||
for (var k = 1; k < 4; k++)
|
||||
{
|
||||
var next = _gameState[i + k + (j * NumberOfRows)];
|
||||
var next = _gameState[i + k + (j * NUMBER_OF_ROWS)];
|
||||
if (next == first)
|
||||
{
|
||||
if (k == 3)
|
||||
@@ -213,21 +213,21 @@ public sealed class Connect4Game : IDisposable
|
||||
}
|
||||
|
||||
// i'll go [0-1] in columns (and through all rows) and check to the right if 4 are connected
|
||||
for (var i = 0; i < NumberOfColumns - 3; i++)
|
||||
for (var i = 0; i < NUMBER_OF_COLUMNS - 3; i++)
|
||||
{
|
||||
if (CurrentPhase == Phase.Ended)
|
||||
break;
|
||||
|
||||
for (var j = 0; j < NumberOfRows; j++)
|
||||
for (var j = 0; j < NUMBER_OF_ROWS; j++)
|
||||
{
|
||||
if (CurrentPhase == Phase.Ended)
|
||||
break;
|
||||
|
||||
var first = _gameState[j + (i * NumberOfRows)];
|
||||
var first = _gameState[j + (i * NUMBER_OF_ROWS)];
|
||||
if (first != Field.Empty)
|
||||
for (var k = 1; k < 4; k++)
|
||||
{
|
||||
var next = _gameState[j + ((i + k) * NumberOfRows)];
|
||||
var next = _gameState[j + ((i + k) * NUMBER_OF_ROWS)];
|
||||
if (next == first)
|
||||
if (k == 3)
|
||||
EndGame(Result.CurrentPlayerWon, CurrentPlayer.UserId);
|
||||
@@ -239,17 +239,17 @@ public sealed class Connect4Game : IDisposable
|
||||
}
|
||||
|
||||
//need to check diagonal now
|
||||
for (var col = 0; col < NumberOfColumns; col++)
|
||||
for (var col = 0; col < NUMBER_OF_COLUMNS; col++)
|
||||
{
|
||||
if (CurrentPhase == Phase.Ended)
|
||||
break;
|
||||
|
||||
for (var row = 0; row < NumberOfRows; row++)
|
||||
for (var row = 0; row < NUMBER_OF_ROWS; row++)
|
||||
{
|
||||
if (CurrentPhase == Phase.Ended)
|
||||
break;
|
||||
|
||||
var first = _gameState[row + (col * NumberOfRows)];
|
||||
var first = _gameState[row + (col * NUMBER_OF_ROWS)];
|
||||
|
||||
if (first != Field.Empty)
|
||||
{
|
||||
@@ -263,12 +263,12 @@ public sealed class Connect4Game : IDisposable
|
||||
var curCol = col - i;
|
||||
|
||||
//check if current values are in range
|
||||
if (curRow is >= NumberOfRows or < 0)
|
||||
if (curRow is >= NUMBER_OF_ROWS or < 0)
|
||||
break;
|
||||
if (curCol is < 0 or >= NumberOfColumns)
|
||||
if (curCol is < 0 or >= NUMBER_OF_COLUMNS)
|
||||
break;
|
||||
|
||||
var cur = _gameState[curRow + (curCol * NumberOfRows)];
|
||||
var cur = _gameState[curRow + (curCol * NUMBER_OF_ROWS)];
|
||||
if (cur == first)
|
||||
same++;
|
||||
else break;
|
||||
@@ -290,12 +290,12 @@ public sealed class Connect4Game : IDisposable
|
||||
var curCol = col + i;
|
||||
|
||||
//check if current values are in range
|
||||
if (curRow is >= NumberOfRows or < 0)
|
||||
if (curRow is >= NUMBER_OF_ROWS or < 0)
|
||||
break;
|
||||
if (curCol is < 0 or >= NumberOfColumns)
|
||||
if (curCol is < 0 or >= NUMBER_OF_COLUMNS)
|
||||
break;
|
||||
|
||||
var cur = _gameState[curRow + (curCol * NumberOfRows)];
|
||||
var cur = _gameState[curRow + (curCol * NUMBER_OF_ROWS)];
|
||||
if (cur == first)
|
||||
same++;
|
||||
else break;
|
||||
@@ -323,7 +323,7 @@ public sealed class Connect4Game : IDisposable
|
||||
ResetTimer();
|
||||
}
|
||||
|
||||
var _ = OnGameStateUpdated?.Invoke(this);
|
||||
_= OnGameStateUpdated?.Invoke(this);
|
||||
return true;
|
||||
}
|
||||
finally { _locker.Release(); }
|
||||
@@ -337,7 +337,7 @@ public sealed class Connect4Game : IDisposable
|
||||
{
|
||||
if (CurrentPhase == Phase.Ended)
|
||||
return;
|
||||
var _ = OnGameEnded?.Invoke(this, result);
|
||||
_= OnGameEnded?.Invoke(this, result);
|
||||
CurrentPhase = Phase.Ended;
|
||||
|
||||
if (result == Result.Draw)
|
||||
@@ -357,8 +357,8 @@ public sealed class Connect4Game : IDisposable
|
||||
//column is full if there are no empty fields
|
||||
private bool IsColumnFull(int column)
|
||||
{
|
||||
var start = NumberOfRows * column;
|
||||
for (var i = start; i < start + NumberOfRows; i++)
|
||||
var start = NUMBER_OF_ROWS * column;
|
||||
for (var i = start; i < start + NUMBER_OF_ROWS; i++)
|
||||
if (_gameState[i] == Field.Empty)
|
||||
return false;
|
||||
return true;
|
||||
|
@@ -88,7 +88,7 @@ public partial class Gambling
|
||||
if (ctx.Channel.Id != arg.Channel.Id)
|
||||
return Task.CompletedTask;
|
||||
|
||||
var _ = Task.Run(async () =>
|
||||
_= Task.Run(async () =>
|
||||
{
|
||||
var success = false;
|
||||
if (int.TryParse(arg.Content, out var col)) success = await game.Input(arg.Author.Id, col);
|
||||
@@ -168,11 +168,11 @@ public partial class Gambling
|
||||
if (game.CurrentPhase is Connect4Game.Phase.P1Move or Connect4Game.Phase.P2Move)
|
||||
sb.AppendLine(GetText(strs.connect4_player_to_move(Format.Bold(game.CurrentPlayer.Username))));
|
||||
|
||||
for (var i = Connect4Game.NumberOfRows; i > 0; i--)
|
||||
for (var i = Connect4Game.NUMBER_OF_ROWS; i > 0; i--)
|
||||
{
|
||||
for (var j = 0; j < Connect4Game.NumberOfColumns; j++)
|
||||
for (var j = 0; j < Connect4Game.NUMBER_OF_COLUMNS; j++)
|
||||
{
|
||||
var cur = game.GameState[i + (j * Connect4Game.NumberOfRows) - 1];
|
||||
var cur = game.GameState[i + (j * Connect4Game.NUMBER_OF_ROWS) - 1];
|
||||
|
||||
if (cur == Connect4Game.Field.Empty)
|
||||
sb.Append("⚫"); //black circle
|
||||
@@ -185,7 +185,7 @@ public partial class Gambling
|
||||
sb.AppendLine();
|
||||
}
|
||||
|
||||
for (var i = 0; i < Connect4Game.NumberOfColumns; i++) sb.Append(numbers[i]);
|
||||
for (var i = 0; i < Connect4Game.NUMBER_OF_COLUMNS; i++) sb.Append(numbers[i]);
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
|
@@ -68,10 +68,10 @@ public partial class Gambling
|
||||
if (num > 10)
|
||||
num = 10;
|
||||
|
||||
var (ImageStream, ToSend) = await InternalDraw(num, ctx.Guild.Id);
|
||||
await using (ImageStream)
|
||||
var (imageStream, toSend) = await InternalDraw(num, ctx.Guild.Id);
|
||||
await using (imageStream)
|
||||
{
|
||||
await ctx.Channel.SendFileAsync(ImageStream, num + " cards.jpg", ToSend);
|
||||
await ctx.Channel.SendFileAsync(imageStream, num + " cards.jpg", toSend);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,10 +83,10 @@ public partial class Gambling
|
||||
if (num > 10)
|
||||
num = 10;
|
||||
|
||||
var (ImageStream, ToSend) = await InternalDraw(num);
|
||||
await using (ImageStream)
|
||||
var (imageStream, toSend) = await InternalDraw(num);
|
||||
await using (imageStream)
|
||||
{
|
||||
await ctx.Channel.SendFileAsync(ImageStream, num + " cards.jpg", ToSend);
|
||||
await ctx.Channel.SendFileAsync(imageStream, num + " cards.jpg", toSend);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -11,7 +11,7 @@ public class GameStatusEvent : ICurrencyEvent
|
||||
public bool PotEmptied { get; private set; }
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly IGuild _guild;
|
||||
private IUserMessage _msg;
|
||||
private IUserMessage msg;
|
||||
private readonly ICurrencyService _cs;
|
||||
private readonly long _amount;
|
||||
|
||||
@@ -32,9 +32,9 @@ public class GameStatusEvent : ICurrencyEvent
|
||||
.Select(x => (char)x)
|
||||
.ToArray();
|
||||
|
||||
private readonly object stopLock = new();
|
||||
private readonly object _stopLock = new();
|
||||
|
||||
private readonly object potLock = new();
|
||||
private readonly object _potLock = new();
|
||||
|
||||
public GameStatusEvent(
|
||||
DiscordSocketClient client,
|
||||
@@ -62,9 +62,7 @@ public class GameStatusEvent : ICurrencyEvent
|
||||
}
|
||||
|
||||
private void EventTimeout(object state)
|
||||
{
|
||||
var _ = StopEvent();
|
||||
}
|
||||
=> _ = StopEvent();
|
||||
|
||||
private async void OnTimerTick(object state)
|
||||
{
|
||||
@@ -83,20 +81,20 @@ public class GameStatusEvent : ICurrencyEvent
|
||||
true);
|
||||
|
||||
if (_isPotLimited)
|
||||
await _msg.ModifyAsync(m =>
|
||||
await msg.ModifyAsync(m =>
|
||||
{
|
||||
m.Embed = GetEmbed(PotSize).Build();
|
||||
},
|
||||
new() { RetryMode = RetryMode.AlwaysRetry });
|
||||
|
||||
Log.Information("Awarded {0} users {1} currency.{2}",
|
||||
Log.Information("Awarded {Count} users {Amount} currency.{Remaining}",
|
||||
toAward.Count,
|
||||
_amount,
|
||||
_isPotLimited ? $" {PotSize} left." : "");
|
||||
|
||||
if (potEmpty)
|
||||
{
|
||||
var _ = StopEvent();
|
||||
_= StopEvent();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -107,7 +105,7 @@ public class GameStatusEvent : ICurrencyEvent
|
||||
|
||||
public async Task StartEvent()
|
||||
{
|
||||
_msg = await _channel.EmbedAsync(GetEmbed(_opts.PotSize));
|
||||
msg = await _channel.EmbedAsync(GetEmbed(_opts.PotSize));
|
||||
await _client.SetGameAsync(_code);
|
||||
_client.MessageDeleted += OnMessageDeleted;
|
||||
_client.MessageReceived += HandleMessage;
|
||||
@@ -117,55 +115,55 @@ public class GameStatusEvent : ICurrencyEvent
|
||||
private IEmbedBuilder GetEmbed(long pot)
|
||||
=> _embedFunc(CurrencyEvent.Type.GameStatus, _opts, pot);
|
||||
|
||||
private async Task OnMessageDeleted(Cacheable<IMessage, ulong> msg, Cacheable<IMessageChannel, ulong> cacheable)
|
||||
private async Task OnMessageDeleted(Cacheable<IMessage, ulong> message, Cacheable<IMessageChannel, ulong> cacheable)
|
||||
{
|
||||
if (msg.Id == _msg.Id) await StopEvent();
|
||||
if (message.Id == this.msg.Id) await StopEvent();
|
||||
}
|
||||
|
||||
public async Task StopEvent()
|
||||
{
|
||||
await Task.Yield();
|
||||
lock (stopLock)
|
||||
lock (_stopLock)
|
||||
{
|
||||
if (Stopped)
|
||||
return;
|
||||
Stopped = true;
|
||||
_client.MessageDeleted -= OnMessageDeleted;
|
||||
_client.MessageReceived -= HandleMessage;
|
||||
_client.SetGameAsync(null);
|
||||
_t.Change(Timeout.Infinite, Timeout.Infinite);
|
||||
_timeout?.Change(Timeout.Infinite, Timeout.Infinite);
|
||||
_ = _client.SetGameAsync(null);
|
||||
try
|
||||
{
|
||||
var _ = _msg.DeleteAsync();
|
||||
_= msg.DeleteAsync();
|
||||
}
|
||||
catch { }
|
||||
|
||||
var os = OnEnded(_guild.Id);
|
||||
_ = OnEnded?.Invoke(_guild.Id);
|
||||
}
|
||||
}
|
||||
|
||||
private Task HandleMessage(SocketMessage msg)
|
||||
private Task HandleMessage(SocketMessage message)
|
||||
{
|
||||
var _ = Task.Run(async () =>
|
||||
_= Task.Run(async () =>
|
||||
{
|
||||
if (msg.Author is not IGuildUser gu // no unknown users, as they could be bots, or alts
|
||||
if (message.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
|
||||
|| message.Content != _code // code has to be the same
|
||||
|| (DateTime.UtcNow - gu.CreatedAt).TotalDays <= 5) // no recently created accounts
|
||||
return;
|
||||
// there has to be money left in the pot
|
||||
// and the user wasn't rewarded
|
||||
if (_awardedUsers.Add(msg.Author.Id) && TryTakeFromPot())
|
||||
if (_awardedUsers.Add(message.Author.Id) && TryTakeFromPot())
|
||||
{
|
||||
_toAward.Enqueue(msg.Author.Id);
|
||||
_toAward.Enqueue(message.Author.Id);
|
||||
if (_isPotLimited && PotSize < _amount)
|
||||
PotEmptied = true;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await msg.DeleteAsync(new() { RetryMode = RetryMode.AlwaysFail });
|
||||
await message.DeleteAsync(new() { RetryMode = RetryMode.AlwaysFail });
|
||||
}
|
||||
catch { }
|
||||
});
|
||||
@@ -175,7 +173,7 @@ public class GameStatusEvent : ICurrencyEvent
|
||||
private bool TryTakeFromPot()
|
||||
{
|
||||
if (_isPotLimited)
|
||||
lock (potLock)
|
||||
lock (_potLock)
|
||||
{
|
||||
if (PotSize < _amount)
|
||||
return false;
|
||||
|
@@ -11,8 +11,8 @@ public class ReactionEvent : ICurrencyEvent
|
||||
public bool PotEmptied { get; private set; }
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly IGuild _guild;
|
||||
private IUserMessage _msg;
|
||||
private IEmote _emote;
|
||||
private IUserMessage msg;
|
||||
private IEmote emote;
|
||||
private readonly ICurrencyService _cs;
|
||||
private readonly long _amount;
|
||||
|
||||
@@ -27,9 +27,9 @@ public class ReactionEvent : ICurrencyEvent
|
||||
private readonly EventOptions _opts;
|
||||
private readonly GamblingConfig _config;
|
||||
|
||||
private readonly object stopLock = new();
|
||||
private readonly object _stopLock = new();
|
||||
|
||||
private readonly object potLock = new();
|
||||
private readonly object _potLock = new();
|
||||
|
||||
public ReactionEvent(
|
||||
DiscordSocketClient client,
|
||||
@@ -58,9 +58,7 @@ public class ReactionEvent : ICurrencyEvent
|
||||
}
|
||||
|
||||
private void EventTimeout(object state)
|
||||
{
|
||||
var _ = StopEvent();
|
||||
}
|
||||
=> _= StopEvent();
|
||||
|
||||
private async void OnTimerTick(object state)
|
||||
{
|
||||
@@ -76,20 +74,20 @@ public class ReactionEvent : ICurrencyEvent
|
||||
await _cs.AddBulkAsync(toAward, toAward.Select(_ => "Reaction Event"), toAward.Select(_ => _amount), true);
|
||||
|
||||
if (_isPotLimited)
|
||||
await _msg.ModifyAsync(m =>
|
||||
await msg.ModifyAsync(m =>
|
||||
{
|
||||
m.Embed = GetEmbed(PotSize).Build();
|
||||
},
|
||||
new() { RetryMode = RetryMode.AlwaysRetry });
|
||||
|
||||
Log.Information("Awarded {0} users {1} currency.{2}",
|
||||
Log.Information("Awarded {Count} users {Amount} currency.{Remaining}",
|
||||
toAward.Count,
|
||||
_amount,
|
||||
_isPotLimited ? $" {PotSize} left." : "");
|
||||
|
||||
if (potEmpty)
|
||||
{
|
||||
var _ = StopEvent();
|
||||
_= StopEvent();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -100,12 +98,12 @@ public class ReactionEvent : ICurrencyEvent
|
||||
|
||||
public async Task StartEvent()
|
||||
{
|
||||
if (Emote.TryParse(_config.Currency.Sign, out var emote))
|
||||
_emote = emote;
|
||||
if (Emote.TryParse(_config.Currency.Sign, out var parsedEmote))
|
||||
this.emote = parsedEmote;
|
||||
else
|
||||
_emote = new Emoji(_config.Currency.Sign);
|
||||
_msg = await _channel.EmbedAsync(GetEmbed(_opts.PotSize));
|
||||
await _msg.AddReactionAsync(_emote);
|
||||
this.emote = new Emoji(_config.Currency.Sign);
|
||||
msg = await _channel.EmbedAsync(GetEmbed(_opts.PotSize));
|
||||
await msg.AddReactionAsync(this.emote);
|
||||
_client.MessageDeleted += OnMessageDeleted;
|
||||
_client.ReactionAdded += HandleReaction;
|
||||
_t.Change(TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2));
|
||||
@@ -114,15 +112,15 @@ public class ReactionEvent : ICurrencyEvent
|
||||
private IEmbedBuilder GetEmbed(long pot)
|
||||
=> _embedFunc(CurrencyEvent.Type.Reaction, _opts, pot);
|
||||
|
||||
private async Task OnMessageDeleted(Cacheable<IMessage, ulong> msg, Cacheable<IMessageChannel, ulong> cacheable)
|
||||
private async Task OnMessageDeleted(Cacheable<IMessage, ulong> message, Cacheable<IMessageChannel, ulong> cacheable)
|
||||
{
|
||||
if (msg.Id == _msg.Id) await StopEvent();
|
||||
if (message.Id == this.msg.Id) await StopEvent();
|
||||
}
|
||||
|
||||
public async Task StopEvent()
|
||||
{
|
||||
await Task.Yield();
|
||||
lock (stopLock)
|
||||
lock (_stopLock)
|
||||
{
|
||||
if (Stopped)
|
||||
return;
|
||||
@@ -133,27 +131,27 @@ public class ReactionEvent : ICurrencyEvent
|
||||
_timeout?.Change(Timeout.Infinite, Timeout.Infinite);
|
||||
try
|
||||
{
|
||||
var _ = _msg.DeleteAsync();
|
||||
_= msg.DeleteAsync();
|
||||
}
|
||||
catch { }
|
||||
|
||||
var os = OnEnded(_guild.Id);
|
||||
_ = OnEnded?.Invoke(_guild.Id);
|
||||
}
|
||||
}
|
||||
|
||||
private Task HandleReaction(
|
||||
Cacheable<IUserMessage, ulong> msg,
|
||||
Cacheable<IUserMessage, ulong> message,
|
||||
Cacheable<IMessageChannel, ulong> cacheable,
|
||||
SocketReaction r)
|
||||
{
|
||||
var _ = Task.Run(() =>
|
||||
_= Task.Run(() =>
|
||||
{
|
||||
if (_emote.Name != r.Emote.Name)
|
||||
if (emote.Name != r.Emote.Name)
|
||||
return;
|
||||
if ((r.User.IsSpecified
|
||||
? r.User.Value
|
||||
: null) is not IGuildUser gu // no unknown users, as they could be bots, or alts
|
||||
|| msg.Id != _msg.Id // same message
|
||||
|| message.Id != this.msg.Id // same message
|
||||
|| gu.IsBot // no bots
|
||||
|| (DateTime.UtcNow - gu.CreatedAt).TotalDays <= 5 // no recently created accounts
|
||||
|| (_noRecentlyJoinedServer
|
||||
@@ -177,7 +175,7 @@ public class ReactionEvent : ICurrencyEvent
|
||||
private bool TryTakeFromPot()
|
||||
{
|
||||
if (_isPotLimited)
|
||||
lock (potLock)
|
||||
lock (_potLock)
|
||||
{
|
||||
if (PotSize < _amount)
|
||||
return false;
|
||||
|
@@ -26,8 +26,8 @@ public class PlantPickService : INService
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly GamblingConfigService _gss;
|
||||
|
||||
public readonly ConcurrentHashSet<ulong> _generationChannels = new();
|
||||
private readonly SemaphoreSlim pickLock = new(1, 1);
|
||||
private readonly ConcurrentHashSet<ulong> _generationChannels;
|
||||
private readonly SemaphoreSlim _pickLock = new(1, 1);
|
||||
|
||||
public PlantPickService(
|
||||
DbService db,
|
||||
@@ -101,6 +101,7 @@ public class PlantPickService : INService
|
||||
/// Get a random currency image stream, with an optional password sticked onto it.
|
||||
/// </summary>
|
||||
/// <param name="pass">Optional password to add to top left corner.</param>
|
||||
/// <param name="extension">Extension of the file, defaults to png</param>
|
||||
/// <returns>Stream of the currency image</returns>
|
||||
public Stream GetRandomCurrencyImage(string pass, out string extension)
|
||||
{
|
||||
@@ -140,7 +141,7 @@ public class PlantPickService : INService
|
||||
pass = pass.TrimTo(10, true).ToLowerInvariant();
|
||||
using var img = Image.Load<Rgba32>(curImg, out var format);
|
||||
// choose font size based on the image height, so that it's visible
|
||||
var font = _fonts.NotoSans.CreateFont(img.Height / 12, FontStyle.Bold);
|
||||
var font = _fonts.NotoSans.CreateFont(img.Height / 12.0f, FontStyle.Bold);
|
||||
img.Mutate(x =>
|
||||
{
|
||||
// measure the size of the text to be drawing
|
||||
@@ -171,7 +172,7 @@ public class PlantPickService : INService
|
||||
if (!_generationChannels.Contains(channel.Id))
|
||||
return Task.CompletedTask;
|
||||
|
||||
var _ = Task.Run(async () =>
|
||||
_= Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -245,7 +246,7 @@ public class PlantPickService : INService
|
||||
ulong uid,
|
||||
string pass)
|
||||
{
|
||||
await pickLock.WaitAsync();
|
||||
await _pickLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
long amount;
|
||||
@@ -276,7 +277,7 @@ public class PlantPickService : INService
|
||||
try
|
||||
{
|
||||
// delete all of the plant messages which have just been picked
|
||||
var _ = ch.DeleteMessagesAsync(ids);
|
||||
_= ch.DeleteMessagesAsync(ids);
|
||||
}
|
||||
catch { }
|
||||
|
||||
@@ -285,7 +286,7 @@ public class PlantPickService : INService
|
||||
}
|
||||
finally
|
||||
{
|
||||
pickLock.Release();
|
||||
_pickLock.Release();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -13,14 +13,10 @@ public class CurrencyRaffleService : INService
|
||||
|
||||
public Dictionary<ulong, CurrencyRaffleGame> Games { get; } = new();
|
||||
private readonly SemaphoreSlim _locker = new(1, 1);
|
||||
private readonly DbService _db;
|
||||
private readonly ICurrencyService _cs;
|
||||
|
||||
public CurrencyRaffleService(DbService db, ICurrencyService cs)
|
||||
{
|
||||
_db = db;
|
||||
_cs = cs;
|
||||
}
|
||||
public CurrencyRaffleService(ICurrencyService cs)
|
||||
=> _cs = cs;
|
||||
|
||||
public async Task<(CurrencyRaffleGame, JoinErrorType?)> JoinOrCreateGame(
|
||||
ulong channelId,
|
||||
@@ -57,7 +53,7 @@ public class CurrencyRaffleService : INService
|
||||
|
||||
if (newGame)
|
||||
{
|
||||
var _t = Task.Run(async () =>
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(60000);
|
||||
await _locker.WaitAsync();
|
||||
@@ -68,7 +64,7 @@ public class CurrencyRaffleService : INService
|
||||
|
||||
await _cs.AddAsync(winner.DiscordUser.Id, "Currency Raffle Win", won);
|
||||
Games.Remove(channelId, out _);
|
||||
var oe = onEnded(winner.DiscordUser, won);
|
||||
_ = onEnded(winner.DiscordUser, won);
|
||||
}
|
||||
catch { }
|
||||
finally { _locker.Release(); }
|
||||
|
@@ -211,7 +211,7 @@ public partial class Gambling
|
||||
}
|
||||
finally
|
||||
{
|
||||
var _ = Task.Run(async () =>
|
||||
_= Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(1000);
|
||||
_runningUsers.Remove(ctx.User.Id);
|
||||
|
Reference in New Issue
Block a user