mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-12 02:08:27 -04:00
More common refactorings like renaming variables, removing empty statements and unused variables, etc
This commit is contained in:
@@ -10,19 +10,19 @@ public class TicTacToe
|
||||
private readonly ITextChannel _channel;
|
||||
private readonly IGuildUser[] _users;
|
||||
private readonly int?[,] _state;
|
||||
private Phase _phase;
|
||||
private int _curUserIndex;
|
||||
private Phase phase;
|
||||
private int curUserIndex;
|
||||
private readonly SemaphoreSlim _moveLock;
|
||||
|
||||
private IGuildUser _winner;
|
||||
private IGuildUser winner;
|
||||
|
||||
private readonly string[] _numbers =
|
||||
{
|
||||
":one:", ":two:", ":three:", ":four:", ":five:", ":six:", ":seven:", ":eight:", ":nine:"
|
||||
};
|
||||
|
||||
private IUserMessage _previousMessage;
|
||||
private Timer _timeoutTimer;
|
||||
private IUserMessage previousMessage;
|
||||
private Timer timeoutTimer;
|
||||
private readonly IBotStrings _strings;
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly Options _options;
|
||||
@@ -45,7 +45,7 @@ public class TicTacToe
|
||||
_users = new[] { firstUser, null };
|
||||
_state = new int?[,] { { null, null, null }, { null, null, null }, { null, null, null } };
|
||||
|
||||
_phase = Phase.Starting;
|
||||
phase = Phase.Starting;
|
||||
_moveLock = new(1, 1);
|
||||
}
|
||||
|
||||
@@ -81,16 +81,16 @@ public class TicTacToe
|
||||
if (!string.IsNullOrWhiteSpace(title))
|
||||
embed.WithTitle(title);
|
||||
|
||||
if (_winner is null)
|
||||
if (winner is null)
|
||||
{
|
||||
if (_phase == Phase.Ended)
|
||||
if (phase == Phase.Ended)
|
||||
embed.WithFooter(GetText(strs.ttt_no_moves));
|
||||
else
|
||||
embed.WithFooter(GetText(strs.ttt_users_move(_users[_curUserIndex])));
|
||||
embed.WithFooter(GetText(strs.ttt_users_move(_users[curUserIndex])));
|
||||
}
|
||||
else
|
||||
{
|
||||
embed.WithFooter(GetText(strs.ttt_has_won(_winner)));
|
||||
embed.WithFooter(GetText(strs.ttt_has_won(winner)));
|
||||
}
|
||||
|
||||
return embed;
|
||||
@@ -115,7 +115,7 @@ public class TicTacToe
|
||||
|
||||
public async Task Start(IGuildUser user)
|
||||
{
|
||||
if (_phase is Phase.Started or Phase.Ended)
|
||||
if (phase is Phase.Started or Phase.Ended)
|
||||
{
|
||||
await _channel.SendErrorAsync(_eb, user.Mention + GetText(strs.ttt_already_running));
|
||||
return;
|
||||
@@ -129,21 +129,21 @@ public class TicTacToe
|
||||
|
||||
_users[1] = user;
|
||||
|
||||
_phase = Phase.Started;
|
||||
phase = Phase.Started;
|
||||
|
||||
_timeoutTimer = new(async _ =>
|
||||
timeoutTimer = new(async _ =>
|
||||
{
|
||||
await _moveLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
if (_phase == Phase.Ended)
|
||||
if (phase == Phase.Ended)
|
||||
return;
|
||||
|
||||
_phase = Phase.Ended;
|
||||
phase = Phase.Ended;
|
||||
if (_users[1] is not null)
|
||||
{
|
||||
_winner = _users[_curUserIndex ^= 1];
|
||||
var del = _previousMessage?.DeleteAsync();
|
||||
winner = _users[curUserIndex ^= 1];
|
||||
var del = previousMessage?.DeleteAsync();
|
||||
try
|
||||
{
|
||||
await _channel.EmbedAsync(GetEmbed(GetText(strs.ttt_time_expired)));
|
||||
@@ -168,7 +168,7 @@ public class TicTacToe
|
||||
_client.MessageReceived += Client_MessageReceived;
|
||||
|
||||
|
||||
_previousMessage = await _channel.EmbedAsync(GetEmbed(GetText(strs.game_started)));
|
||||
previousMessage = await _channel.EmbedAsync(GetEmbed(GetText(strs.game_started)));
|
||||
}
|
||||
|
||||
private bool IsDraw()
|
||||
@@ -187,8 +187,8 @@ public class TicTacToe
|
||||
await _moveLock.WaitAsync();
|
||||
try
|
||||
{
|
||||
var curUser = _users[_curUserIndex];
|
||||
if (_phase == Phase.Ended || msg.Author?.Id != curUser.Id)
|
||||
var curUser = _users[curUserIndex];
|
||||
if (phase == Phase.Ended || msg.Author?.Id != curUser.Id)
|
||||
return;
|
||||
|
||||
if (int.TryParse(msg.Content, out var index)
|
||||
@@ -196,69 +196,69 @@ public class TicTacToe
|
||||
&& index <= 9
|
||||
&& _state[index / 3, index % 3] is null)
|
||||
{
|
||||
_state[index / 3, index % 3] = _curUserIndex;
|
||||
_state[index / 3, index % 3] = curUserIndex;
|
||||
|
||||
// i'm lazy
|
||||
if (_state[index / 3, 0] == _state[index / 3, 1] && _state[index / 3, 1] == _state[index / 3, 2])
|
||||
{
|
||||
_state[index / 3, 0] = _curUserIndex + 2;
|
||||
_state[index / 3, 1] = _curUserIndex + 2;
|
||||
_state[index / 3, 2] = _curUserIndex + 2;
|
||||
_state[index / 3, 0] = curUserIndex + 2;
|
||||
_state[index / 3, 1] = curUserIndex + 2;
|
||||
_state[index / 3, 2] = curUserIndex + 2;
|
||||
|
||||
_phase = Phase.Ended;
|
||||
phase = Phase.Ended;
|
||||
}
|
||||
else if (_state[0, index % 3] == _state[1, index % 3]
|
||||
&& _state[1, index % 3] == _state[2, index % 3])
|
||||
{
|
||||
_state[0, index % 3] = _curUserIndex + 2;
|
||||
_state[1, index % 3] = _curUserIndex + 2;
|
||||
_state[2, index % 3] = _curUserIndex + 2;
|
||||
_state[0, index % 3] = curUserIndex + 2;
|
||||
_state[1, index % 3] = curUserIndex + 2;
|
||||
_state[2, index % 3] = curUserIndex + 2;
|
||||
|
||||
_phase = Phase.Ended;
|
||||
phase = Phase.Ended;
|
||||
}
|
||||
else if (_curUserIndex == _state[0, 0]
|
||||
else if (curUserIndex == _state[0, 0]
|
||||
&& _state[0, 0] == _state[1, 1]
|
||||
&& _state[1, 1] == _state[2, 2])
|
||||
{
|
||||
_state[0, 0] = _curUserIndex + 2;
|
||||
_state[1, 1] = _curUserIndex + 2;
|
||||
_state[2, 2] = _curUserIndex + 2;
|
||||
_state[0, 0] = curUserIndex + 2;
|
||||
_state[1, 1] = curUserIndex + 2;
|
||||
_state[2, 2] = curUserIndex + 2;
|
||||
|
||||
_phase = Phase.Ended;
|
||||
phase = Phase.Ended;
|
||||
}
|
||||
else if (_curUserIndex == _state[0, 2]
|
||||
else if (curUserIndex == _state[0, 2]
|
||||
&& _state[0, 2] == _state[1, 1]
|
||||
&& _state[1, 1] == _state[2, 0])
|
||||
{
|
||||
_state[0, 2] = _curUserIndex + 2;
|
||||
_state[1, 1] = _curUserIndex + 2;
|
||||
_state[2, 0] = _curUserIndex + 2;
|
||||
_state[0, 2] = curUserIndex + 2;
|
||||
_state[1, 1] = curUserIndex + 2;
|
||||
_state[2, 0] = curUserIndex + 2;
|
||||
|
||||
_phase = Phase.Ended;
|
||||
phase = Phase.Ended;
|
||||
}
|
||||
|
||||
var reason = string.Empty;
|
||||
|
||||
if (_phase == Phase.Ended) // if user won, stop receiving moves
|
||||
if (phase == Phase.Ended) // if user won, stop receiving moves
|
||||
{
|
||||
reason = GetText(strs.ttt_matched_three);
|
||||
_winner = _users[_curUserIndex];
|
||||
winner = _users[curUserIndex];
|
||||
_client.MessageReceived -= Client_MessageReceived;
|
||||
OnEnded?.Invoke(this);
|
||||
}
|
||||
else if (IsDraw())
|
||||
{
|
||||
reason = GetText(strs.ttt_a_draw);
|
||||
_phase = Phase.Ended;
|
||||
phase = Phase.Ended;
|
||||
_client.MessageReceived -= Client_MessageReceived;
|
||||
OnEnded?.Invoke(this);
|
||||
}
|
||||
|
||||
var sendstate = Task.Run(async () =>
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
var del1 = msg.DeleteAsync();
|
||||
var del2 = _previousMessage?.DeleteAsync();
|
||||
try { _previousMessage = await _channel.EmbedAsync(GetEmbed(reason)); }
|
||||
var del2 = previousMessage?.DeleteAsync();
|
||||
try { previousMessage = await _channel.EmbedAsync(GetEmbed(reason)); }
|
||||
catch { }
|
||||
|
||||
try { await del1; }
|
||||
@@ -270,9 +270,9 @@ public class TicTacToe
|
||||
}
|
||||
catch { }
|
||||
});
|
||||
_curUserIndex ^= 1;
|
||||
curUserIndex ^= 1;
|
||||
|
||||
_timeoutTimer.Change(_options.TurnTimer * 1000, Timeout.Infinite);
|
||||
timeoutTimer.Change(_options.TurnTimer * 1000, Timeout.Infinite);
|
||||
}
|
||||
}
|
||||
finally
|
||||
|
Reference in New Issue
Block a user