private async Task SetStickAsyncImpl(SwitchStick s, short x, short y, ushort ms, BotSource <PokeBotConfig> bot) { if (!Enum.IsDefined(typeof(SwitchStick), s)) { await ReplyAsync($"Unknown stick: {s}").ConfigureAwait(false); return; } await bot.Bot.Connection.SendAsync(SwitchCommand.SetStick(s, x, y), bot.Bot.Config.ConnectionType, CancellationToken.None).ConfigureAwait(false); await ReplyAsync($"{bot.Bot.Connection.Name} has performed: {s}").ConfigureAwait(false); await Task.Delay(ms).ConfigureAwait(false); await bot.Bot.Connection.SendAsync(SwitchCommand.ResetStick(s), bot.Bot.Config.ConnectionType, CancellationToken.None).ConfigureAwait(false); await ReplyAsync($"{bot.Bot.Connection.Name} has reset the stick position.").ConfigureAwait(false); }
private async Task SetStickAsyncImpl(SwitchStick s, short x, short y, ushort ms) { if (!Enum.IsDefined(typeof(SwitchStick), s)) { await ReplyAsync($"Unknown stick: {s}").ConfigureAwait(false); return; } var b = Bot; await b.Connection.SendAsync(SwitchCommand.SetStick(s, x, y, b.UseCRLF), CancellationToken.None).ConfigureAwait(false); await ReplyAsync($"{b.Connection.Name} has performed: {s}").ConfigureAwait(false); await Task.Delay(ms).ConfigureAwait(false); await b.Connection.SendAsync(SwitchCommand.ResetStick(s, b.UseCRLF), CancellationToken.None).ConfigureAwait(false); await ReplyAsync($"{b.Connection.Name} has reset the stick position.").ConfigureAwait(false); }
private async Task SetStickAsyncImpl(SwitchStick s, short x, short y, ushort ms, BotSource <PokeBotState> bot) { if (!Enum.IsDefined(typeof(SwitchStick), s)) { await ReplyAsync($"Unknown stick: {s}").ConfigureAwait(false); return; } var b = bot.Bot; var crlf = b is SwitchRoutineExecutor <PokeBotState> { UseCRLF : true }; await b.Connection.SendAsync(SwitchCommand.SetStick(s, x, y, crlf), CancellationToken.None).ConfigureAwait(false); await ReplyAsync($"{b.Connection.Name} has performed: {s}").ConfigureAwait(false); await Task.Delay(ms).ConfigureAwait(false); await b.Connection.SendAsync(SwitchCommand.ResetStick(s, crlf), CancellationToken.None).ConfigureAwait(false); await ReplyAsync($"{b.Connection.Name} has reset the stick position.").ConfigureAwait(false); } }
public async Task <bool> SpinUntilChangedSurprise(int waitms, CancellationToken token) { const int fastSleep = 10; // between commands const int defaultSleep = 50; // original const int delay = 100; // per stick command const int m = 25_000; // magnitude of stick movement var sw = new Stopwatch(); bool changed = false; var searching = await Connection.ReadBytesAsync(SurpriseTradeSearchOffset, 4, token).ConfigureAwait(false); await Connection.SendAsync(SwitchCommand.Configure(SwitchConfigureParameter.mainLoopSleepTime, fastSleep), token).ConfigureAwait(false); sw.Start(); do { // Spin the Left Stick in a circle counter-clockwise, starting from 0deg (polar) in increments of 90deg. if (!await SpinCircle().ConfigureAwait(false)) { continue; } changed = true; break; } while (sw.ElapsedMilliseconds < waitms); async Task <bool> SpinCircle() { return(await Step(m, 0).ConfigureAwait(false) || // → await Step(0, m).ConfigureAwait(false) || // ↑ await Step(-m, 0).ConfigureAwait(false) || // ← await Step(0, -m).ConfigureAwait(false)); // ↓ async Task <bool> Step(short x, short y) { var now = sw.ElapsedMilliseconds; await SetStick(SwitchStick.LEFT, x, y, 2 *fastSleep, token).ConfigureAwait(false); if (await ReadIsChanged(SurpriseTradeSearchOffset, searching, token).ConfigureAwait(false)) { return(true); } // wait the rest of this step's delay var wait = delay - (sw.ElapsedMilliseconds - now); if (wait > 0) { await Task.Delay((int)wait, token).ConfigureAwait(false); } return(false); } } // Gracefully clean up await Connection.SendAsync(SwitchCommand.ResetStick(SwitchStick.LEFT), token).ConfigureAwait(false); await Task.Delay(fastSleep, token).ConfigureAwait(false); await Connection.SendAsync(SwitchCommand.Configure(SwitchConfigureParameter.mainLoopSleepTime, defaultSleep), token).ConfigureAwait(false); await Task.Delay(defaultSleep, token).ConfigureAwait(false); return(changed); }