public Boolean EnsureJoinedToChannel(String channelName) { Boolean isConnected = SpinWaitService.SpinUntil(() => { if (!Client.IsConnected) { Client.Connect(); ThreadService.Sleep(TimeSpan.FromSeconds(1)); return(Client.IsConnected); } return(true); }, TimeSpan.FromSeconds(10)); if (!isConnected) { return(false); } Boolean isJoined = SpinWaitService.SpinUntil(() => { Client.JoinChannel(channelName); return(Client.JoinedChannels.Any(x => x.Channel.Equals(channelName, StringComparison.OrdinalIgnoreCase))); }, TimeSpan.FromSeconds(10)); return(isJoined); }
private void InitiateNewHeist(ChatMessage message) { using var context = ContextFactory.GetContext(); var player = context.GetPlayer(Client, message); // Trying to initiate new heist var now = DateTime.Now; var oldLastHeistInitiated = player.LastHeistInitiated; var timeSinceLastHeistInitiated = now - oldLastHeistInitiated; if (timeSinceLastHeistInitiated >= HeistCooldown) { // Update last heist initiated to prevent initiating // multiple heists in multiple channels. player.LastHeistInitiated = now; context.SaveChanges(); var heist = new Heist(message, Random, Client); OngoingHeists.TryAdd(message.Channel, heist); Client.SpoolMessageAsMe(message.Channel, player, Messages.NewHeistInitiated.Format(HeistWaitTime.Format())); // Join the heist in a separate thread so that the heist // countdown can start immediately as the join is being // processed. This should only save around a second or so of // time. Task.Run(() => JoinHeist(message, player, context)); // Since we are sleeping, this needs to be async. ThreadService.Sleep(HeistWaitTime); if (!heist.Start(context)) { // No one joined the heist. Let the user initiate another heist. player.LastHeistInitiated = oldLastHeistInitiated; context.SaveChanges(); } OngoingHeists.TryRemove(message.Channel, out _); return; } var timeUntilNextHeistAvailable = HeistCooldown - timeSinceLastHeistInitiated; var timeToWait = timeUntilNextHeistAvailable.Format(); Client.SpoolMessageAsMe(message.Channel, player, Messages.Wait.Format(timeToWait, Random.NextElement(EmoteManager.Get(message.Channel, EmoteCategory.Waiting)))); }