private void Run() { Log.InfoFormat("[{0}] Queue Running",this); var sendStatusUpdatesBlock = new TimeBlock(TimeSpan.FromSeconds(30)); var readyTimeout = new TimeBlock(TimeSpan.FromSeconds(60)); var disposeThis = new TimeBlock(TimeSpan.FromHours(1)); disposeThis.SetRun(); while (_runCancelToken.IsCancellationRequested == false && Disposed == false) { lock (_users) { try { if (_users.Count > 0) { disposeThis.SetRun(); } if (disposeThis.IsTime) { Log.InfoFormat("[{0}] Dispose timer timed out",this); Disposed = true; this.Dispose(); break; } switch (State) { case MatchmakingQueueState.WaitingForUsers: if (_users.Count >= MaxPlayers) { Log.InfoFormat("[{0}] Got enough players for ready queue.",this); State = MatchmakingQueueState.WaitingForReadyUsers; var readyMessage = new MatchmakingReadyRequest(null, this.QueueId); foreach (var p in _users) { p.IsReady = false; p.IsInReadyQueue = false; } for (var i = 0; i < MaxPlayers; i++) { readyMessage.To = _users[i]; _users[i].IsInReadyQueue = true; Bot.Messanger.Send(readyMessage); } readyTimeout.SetRun(); } break; case MatchmakingQueueState.WaitingForReadyUsers: if (readyTimeout.IsTime) { Log.InfoFormat("[{0}] Timed out waiting for users to ready up.",this); // This happens if 60 seconds has passed since ready messages were sent out. // This shouldn't happen unless someone didn't respond back with a ready response. foreach (var p in _users.Where(x => x.IsInReadyQueue).ToArray()) { // If player didn't signal ready throw them in the back of the queue. if (p.IsReady == false) { p.FailedReadyCount++; _users.Remove(p); // If they've been knocked to the back of the queue 4 or more times, kick them. if (p.FailedReadyCount < 4) { Log.InfoFormat("[{0}] {1} User failed ready 4 times, getting kicked.",this,p); _users.Add(p); } } p.IsInReadyQueue = false; p.IsReady = false; } } break; case MatchmakingQueueState.WaitingForHostedGame: if (_hostGameTimeout.IsTime) { Log.WarnFormat("[{0}] Timed out waiting for hosted game to be created...Oh Noes!",this); State = MatchmakingQueueState.WaitingForUsers; foreach (var u in _users) { u.IsInReadyQueue = false; u.IsReady = false; } } break; } // Send status messages if (sendStatusUpdatesBlock.IsTime) { var mess = new MatchmakingInLineUpdateMessage(AverageTime.Time, null, QueueId); foreach (var u in _users.Where(x => x.IsInReadyQueue == false)) { mess.To = u.JidUser; mess.GenerateId(); Bot.Messanger.Send(mess); } } } catch (Exception e) { Log.Error ("[" + this + "] Run",e); } } if (_runCancelToken.IsCancellationRequested == false && !Disposed) Thread.Sleep(TimeSpan.FromSeconds(1)); } Log.InfoFormat("[{0}] Queue done running",this); }
private void Run() { Log.InfoFormat("[{0}] Queue Running", this); var sendStatusUpdatesBlock = new TimeBlock(TimeSpan.FromSeconds(30)); var readyTimeout = new TimeBlock(TimeSpan.FromSeconds(60)); var disposeThis = new TimeBlock(TimeSpan.FromHours(1)); disposeThis.SetRun(); while (_runCancelToken.IsCancellationRequested == false && Disposed == false) { lock (_users) { try { if (_users.Count > 0) { disposeThis.SetRun(); } if (disposeThis.IsTime) { Log.InfoFormat("[{0}] Dispose timer timed out", this); Disposed = true; this.Dispose(); break; } switch (State) { case MatchmakingQueueState.WaitingForUsers: if (_users.Count >= MaxPlayers) { Log.InfoFormat("[{0}] Got enough players for ready queue.", this); State = MatchmakingQueueState.WaitingForReadyUsers; var readyMessage = new MatchmakingReadyRequest(null, this.QueueId); foreach (var p in _users) { p.IsReady = false; p.IsInReadyQueue = false; } for (var i = 0; i < MaxPlayers; i++) { readyMessage.To = _users[i]; _users[i].IsInReadyQueue = true; Bot.Messanger.Send(readyMessage); } readyTimeout.SetRun(); } break; case MatchmakingQueueState.WaitingForReadyUsers: if (readyTimeout.IsTime) { Log.InfoFormat("[{0}] Timed out waiting for users to ready up.", this); // This happens if 60 seconds has passed since ready messages were sent out. // This shouldn't happen unless someone didn't respond back with a ready response. foreach (var p in _users.Where(x => x.IsInReadyQueue).ToArray()) { // If player didn't signal ready throw them in the back of the queue. if (p.IsReady == false) { p.FailedReadyCount++; _users.Remove(p); // If they've been knocked to the back of the queue 4 or more times, kick them. if (p.FailedReadyCount < 4) { Log.InfoFormat("[{0}] {1} User failed ready 4 times, getting kicked.", this, p); _users.Add(p); } } p.IsInReadyQueue = false; p.IsReady = false; } } break; case MatchmakingQueueState.WaitingForHostedGame: if (_hostGameTimeout.IsTime) { Log.WarnFormat("[{0}] Timed out waiting for hosted game to be created...Oh Noes!", this); State = MatchmakingQueueState.WaitingForUsers; foreach (var u in _users) { u.IsInReadyQueue = false; u.IsReady = false; } } break; } // Send status messages if (sendStatusUpdatesBlock.IsTime) { var mess = new MatchmakingInLineUpdateMessage(AverageTime.Time, null, QueueId); foreach (var u in _users.Where(x => x.IsInReadyQueue == false)) { mess.To = u.JidUser; mess.GenerateId(); Bot.Messanger.Send(mess); } } } catch (Exception e) { Log.Error("[" + this + "] Run", e); } } if (_runCancelToken.IsCancellationRequested == false && !Disposed) { Thread.Sleep(TimeSpan.FromSeconds(1)); } } Log.InfoFormat("[{0}] Queue done running", this); }