public TwitchConnection_Poll() { Channel = Config.Instance().TwitchChannel; Username = Config.Instance().TwitchUsername; Oauth = Config.Instance().TwitchOAuthToken; ConnectionCredentials credentials; if (Channel == null || Username == null || Oauth == null || Channel == "" || Username == "" || Oauth == "") { return; } else { credentials = new ConnectionCredentials(Username, Oauth); } var protocol = TwitchLib.Client.Enums.ClientProtocol.WebSocket; // If we're not on Windows 10, force a TCP connection if (Environment.OSVersion.Version.Major < 10) { protocol = TwitchLib.Client.Enums.ClientProtocol.TCP; } TryConnect(credentials, protocol); socket = new WebSocketServer("ws://0.0.0.0:31337"); socket.Start(socket => { socket.OnOpen = () => { Auth auth = new Auth { data = Config.Instance().TwitchPollsPassphrase }; socket.Send(JsonConvert.SerializeObject(auth)); connections.Add(socket); }; socket.OnMessage = message => { PollType pollType = JsonConvert.DeserializeObject <PollType>(message); if (pollType.type == "created") { PollCreateData data = JsonConvert.DeserializeObject <PollCreateData>(message); if (createdPoll) { activePoll = new Poll() { id = data.id, remaining = Config.Instance().TwitchVotingTime }; createdPoll = false; } } else if (pollType.type == "update") { PollUpdateData data = JsonConvert.DeserializeObject <PollUpdateData>(message); if (activePoll == null || activePoll.id != data.poll.id) { return; } activePoll = data.poll; for (int i = 0; i < 3; i++) { effectVoting.SetVotes(i, data.poll.choices[i].votes); } } }; socket.OnClose = () => { if (connections.Contains(socket)) { connections.Remove(socket); } }; }); }
public void SetVoting(int votingMode, int untilRapidFire = -1, List <IVotingElement> votingElements = null) { VotingMode = votingMode; if (VotingMode == 1) { effectVoting.Clear(); effectVoting.GenerateRandomEffects(); lastChoice = -1; if (Config.Instance().TwitchPollsPostMessages) { if (Config.Instance().TwitchCombineChatMessages) { string messageToSend = "Voting has started! On mobile? Type \"/vote ID\" (1, 2 or 3) to vote instead! "; foreach (PollVotingElement element in effectVoting.GetVotingElements()) { string description = element.Effect.GetDescription(); if (element.Effect.Word.Equals("IWontTakeAFreePass") && Config.Instance().TwitchAppendFakePassCurrentMission) { description = $"{description} (Fake)"; } messageToSend += $"#{element.Id + 1}: {description}. "; } SendMessage(messageToSend); } else { SendMessage("Voting has started! On mobile? Type \"/vote ID\" (1, 2 or 3) to vote instead!"); foreach (PollVotingElement element in effectVoting.GetVotingElements()) { string description = element.Effect.GetDescription(); if (element.Effect.Word.Equals("IWontTakeAFreePass") && Config.Instance().TwitchAppendFakePassCurrentMission) { description = $"{description} (Fake)"; } SendMessage($"#{element.Id + 1}: {description}"); } } } CreatePoll createPoll = new CreatePoll() { duration = Config.Instance().TwitchVotingTime / 1000, choices = effectVoting.GetVotingElements().Select(elements => { string description = elements.Effect.GetDescription(); if (elements.Effect.Word.Equals("IWontTakeAFreePass") && Config.Instance().TwitchAppendFakePassCurrentMission) { description = $"{description} (Fake)"; } else if (elements.Effect.Word.Equals("HONKHONK")) { // Twitch doesn't like honks, so Honk Honk, everyone! description = description.Replace("O", "ഠ"); } return(description); }).ToList(), subscriberMultiplier = Config.Instance().TwitchPollsSubcriberMultiplier, subscriberOnly = Config.Instance().TwitchPollsSubscriberOnly, bits = Config.Instance().TwitchPollsBitsCost }; createdPoll = true; SocketBroadcast(JsonConvert.SerializeObject(createPoll)); } else if (VotingMode == 2) { rapidFireVoters.Clear(); SendMessage("ATTENTION, ALL GAMERS! RAPID-FIRE HAS BEGUN! VALID EFFECTS WILL BE ENABLED FOR 15 SECONDS!"); } else if (VotingMode == 3) // Poll Failed { SendEffectVotingToGame(false); SendMessage($"Cooldown has started! ({untilRapidFire} until Rapid-Fire) - Poll Failed :("); if (untilRapidFire == 1) { SendMessage("Rapid-Fire is coming up! Get your cheats ready! - List of all effects: https://bit.ly/gta-sa-chaos-mod"); } // Make sure we end poll, thank //if (activePoll != null) //{ // EndPoll endPoll = new EndPoll() // { // id = activePoll.id // }; // SocketBroadcast(JsonConvert.SerializeObject(endPoll)); //} activePoll = null; } else { if (votingElements != null && votingElements.Count > 0) { SendEffectVotingToGame(false); string allEffects = string.Join(", ", votingElements.Select(e => e.GetEffect().GetDescription())); SendMessage($"Cooldown has started! ({untilRapidFire} until Rapid-Fire) - Enabled effects: {allEffects}"); if (untilRapidFire == 1) { SendMessage("Rapid-Fire is coming up! Get your cheats ready! - List of all effects: https://bit.ly/gta-sa-chaos-mod"); } // Make sure we end poll, thank //if (activePoll != null) //{ // EndPoll endPoll = new EndPoll() // { // id = activePoll.id // }; // SocketBroadcast(JsonConvert.SerializeObject(endPoll)); //} activePoll = null; } else { SendMessage($"Cooldown has started! ({untilRapidFire} until Rapid-Fire)"); if (untilRapidFire == 1) { SendMessage("Rapid-Fire is coming up! Get your cheats ready! - List of all effects: https://bit.ly/gta-sa-chaos-mod"); } } } }