private void OnTurnRequest(TwitchUser user) { if (!_queuedUsers.Contains(user)) { Console.WriteLine($"{user.UserName} requested turn"); _queuedUsers.Add(user); _userQueue.Enqueue(user); if (_activeUser == null) { // No other users, so this user can just play immediately Console.WriteLine("No other users, user can play immediately"); OnNextTurn(); } else if (!_turnExpirationTimer.Enabled) { // If there were no other waiting users, we need to restart timer Console.WriteLine("Restarting timer since it was stopped"); _turnLength = 30000; _turnExpirationTimer.Interval = _turnLength; _turnExpirationTimer.Start(); _expirationTime = DateTimeOffset.UtcNow.AddMilliseconds(_turnLength).ToUnixTimeMilliseconds(); Broadcast($"CLIENT_ACTIVE {_activeUser.UserId} {_activeUser.UserName} {_activeUser.Picture} {_expirationTime} {_turnLength}"); } } else { Console.WriteLine($"{user.UserName} tried to request multiple turns"); } }
private void OnTurnCancel(TwitchUser user) { if (user == null) { return; } if (_queuedUsers.Contains(user)) { Console.WriteLine($"{user.UserName} cancelled waiting for turn"); _queuedUsers.Remove(user); if (_queuedUsers.Count == 0) { // No other users, user gets unlimited time _expirationTime = -1; _turnLength = -1; Broadcast($"CLIENT_ACTIVE {_activeUser.UserId} {_activeUser.UserName} {_activeUser.Picture} {_expirationTime} {_turnLength}"); } } else if (user.Equals(_activeUser)) { Console.WriteLine($"{user.UserName} cancelled their active turn, picking next player"); _turnExpirationTimer.Enabled = false; _activeUser = null; OnNextTurn(); } }
private void OnInputFrame(TwitchUser user, string frame) { if (_activeUser.Equals(user)) { _sink.Update(InputFrame.ParseInputString(frame)); } }
private void OnNextTurn() { while (true) { if (_userQueue.TryDequeue(out var nextUser)) { if (_queuedUsers.Contains(nextUser)) { _queuedUsers.Remove(nextUser); _activeUser = nextUser; if (_userQueue.Count > 0) { // More clients in the queue _turnLength = 30000; _turnExpirationTimer.Interval = _turnLength; _turnExpirationTimer.Start(); _expirationTime = DateTimeOffset.UtcNow.AddSeconds(_turnLength).ToUnixTimeMilliseconds(); Console.WriteLine($"{_activeUser.UserName} is now playing, turn expires in 30 seconds"); Broadcast( $"CLIENT_ACTIVE {_activeUser.UserId} {_activeUser.UserName} {_activeUser.Picture} {_expirationTime} {_turnLength}"); break; } else { // No more clients, user's turn lasts forever Console.WriteLine($"{_activeUser.UserName} is now playing forever"); _expirationTime = -1; _turnLength = -1; Broadcast( $"CLIENT_ACTIVE {_activeUser.UserId} {_activeUser.UserName} {_activeUser.Picture} {_expirationTime} {_turnLength}"); break; } } else { Console.WriteLine($"Skipping {nextUser.UserName} as they cancelled their turn"); } } else if (_activeUser == null) { Console.WriteLine("Not able to pick a player"); Broadcast("NO_CLIENTS"); break; } else { break; // One client, turn lasts forever } } }
protected override void OnMessage(MessageEventArgs e) { var wsArgs = e.Data.Split(' '); var cmd = wsArgs[0]; var restOfArgs = string.Join(" ", wsArgs.Skip(1)); if (cmd == "PING") { Send($"PONG {DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()} {restOfArgs}"); } else if (cmd == "TWITCH_LOGIN") { try { MyUser = new TwitchUser(wsArgs[1], wsArgs[2]); //Console.WriteLine($"{MyUser.UserName} logged in"); Send("TWITCH_VERIFIED"); } catch (SecurityTokenValidationException) { MyUser = null; //Console.WriteLine("Invalid login attempt"); Send("TWITCH_INVALID"); } } else if (cmd == "TWITCH_LOGOUT") { Console.WriteLine("Logged out"); } else if (cmd == "UPDATE") { InputCallback(MyUser, restOfArgs); } else if (cmd == "REQUEST_TURN") { TurnRequestCallback(MyUser); } else if (cmd == "CANCEL_TURN") { TurnCancelCallback(MyUser); } else if (cmd == "LISTENER") { IsListener = true; OnListenerAddedCallback(this); } }