void _twitchChatEngine_MessageReceived(string username, string message) { var messageUpper = message.ToUpper(); if (messageUpper.Contains(" ")) { var split = messageUpper.Split(' '); switch (split[0]) { case "REPEAT": if (_slowmotionCountdown < 0 && _slowmotionRepeatRequest == null) { int amount; if (int.TryParse(split[1], out amount)) { //amount can be between 2 and 15. amount = Math.Min(Math.Max(amount, 2), 25); _slowmotionRepeatRequest = new RepeatRequest() { Amount = amount, RequestAuthor = username, CommandIndex = _lastCommandIndex }; } } break; //allows power-users to reposition the window. case "REPOSITION": if (_powerUsers.Any(p => string.Equals(p, username, StringComparison.OrdinalIgnoreCase)) && split.Length > 2) { int x; int y; if (int.TryParse(split[1], out x) && int.TryParse(split[2], out y)) { Dispatcher.Invoke(delegate() { Left = x; Top = y; }); } } break; } } }
private async void StartKeyPressLoop() { //HACK: to keep this async. await Task.Delay(1); var random = new Random((int)DateTime.UtcNow.Ticks); var frame = 0; var lastTick = DateTime.UtcNow; const int SmallDelay = 3; var commandList = new LinkedList<string>(); while (true) { var delay = SmallDelay; if (_slowmotionCountdown < 0) { delay = 3000; SlowMotionCountdown.Text = "Slowdown mode! Now you can use the \"repeat <number>\" command in the chat."; } else { SlowMotionCountdown.Text = "Next slowdown in " + (_slowmotionCountdown) + " seconds ..."; } var difference = (int)(DateTime.UtcNow - lastTick).TotalMilliseconds; Thread.Sleep(Math.Max(delay - difference, 1)); lastTick = DateTime.UtcNow; frame++; //every 10 seconds, save progress. if (frame == 10000 / SmallDelay) { _gameboy.SaveState(); frame = 0; } Action command; string commandName; _lastCommandIndex = _slowmotionRepeatRequest == null ? Math.Min(random.Next(0, 7), 6) : _slowmotionRepeatRequest.CommandIndex; switch (_lastCommandIndex) { case 0: commandName = "→"; command = _gameboy.TapRight; break; case 1: commandName = "←"; command = _gameboy.TapLeft; break; case 2: commandName = "↓"; command = _gameboy.TapDown; break; case 3: commandName = "↑"; command = _gameboy.TapUp; break; case 4: commandName = "A"; command = _gameboy.TapA; break; case 5: commandName = "B"; command = _gameboy.TapB; break; case 6: commandName = "S"; command = delegate() { Thread.Sleep(10); _gameboy.TapStart(); Thread.Sleep(100); _gameboy.TapStart(); Thread.Sleep(10); }; break; default: throw new InvalidOperationException(); } if (_slowmotionRepeatRequest != null) { commandName += " [X" + _slowmotionRepeatRequest.Amount + " by " + _slowmotionRepeatRequest.RequestAuthor + "]"; } commandList.AddFirst(commandName); if (commandList.Count > 100) { commandList.RemoveLast(); } Log.ItemsSource = null; Log.ItemsSource = commandList; FormsApplication.DoEvents(); var repeat = _slowmotionRepeatRequest == null ? 1 : _slowmotionRepeatRequest.Amount; for (var i = 0; i < repeat; i++) { command(); if (i != 0) { Thread.Sleep(100); } } _slowmotionRepeatRequest = null; } }