public void vJoySendNominee(Nominee n) { for (int i = 0; i < n.Keys.Count; i++) { if (n.Keys[i] == Program.KeyType.HOLD) { Thread.Sleep(500); } else { vJoySendButton((uint)n.Keys[i], n.Counts[i]); } Thread.Sleep(100); } }
public static void OnChannelMessage(object sender, IrcEventArgs e) { // Active Flags User u = null; lock (Users.All) { u = Users.All.Find(x => x.Name.Equals(e.Data.Nick.ToLower())); } if (u == null) { u = CreateUser(e.Data.Nick, false); } u.LastActive = DateTime.Now; u.IncrementVotes(); LastInteraction = DateTime.Now; Log(e.Data.Nick + ": " + e.Data.Message); if (!_logform.InputEnabled) return; // Disable Voting UI Flag // Website Filters string[] explicit_blacklist = { "dotcom", "(dot)", "░", "█" }; foreach (string m in explicit_blacklist) { if (e.Data.Message.ToLower().Contains(m)) { BanUser(u, false, "filter evasion / Unicode art"); return; } } Regex filter = new Regex(@"[A-z0-9-]+(?:\.[A-z]{2,3})+", RegexOptions.RightToLeft); string stripped_message = Regex.Replace(e.Data.Message, @"[()]", ""); bool is_clean = true; List<string> dirty_matches = new List<string>(); foreach (Match m in filter.Matches(stripped_message)) { WebsiteFilter website_filter = WebsiteFilters.Find(x => x.Domain.Equals(m.Value)); if (website_filter != null) { if (website_filter.Type == WebsiteFilter.FilterType.BLACKLIST) BanUser(u, false, "blacklisted website: '" + m.Value + "'"); return; } else { is_clean = false; dirty_matches.Add(m.Value); } } if (!is_clean) { BanUser(u, true, "unlisted website(s): " + String.Join(", ", dirty_matches)); return; } Nominee keyNominee = null; string command = e.Data.Message.ToUpper(); bool downvote = false; if (command.StartsWith("NO") || command.StartsWith("-")) { downvote = true; if (command.StartsWith("NOT")) command = command.Substring(3); else if (command.StartsWith("NO")) command = command.Substring(2); else if (command.StartsWith("-")) command = command.Substring(1); command.TrimStart(' '); } if (command.StartsWith("@")) // Function Command { string[] function_msg = command.Substring(1).Split(' '); if (function_msg.Count() == 0) return; string opcode = function_msg.First(); string[] payload = function_msg.Skip(1).ToArray(); Console.WriteLine("Function! Opcode: " + opcode + ", Payload: " + String.Join(" ", payload)); switch (opcode) { case "BLAME": if (payload.Count() == 0) return; switch (payload.First()) { case "GOAL": ModFunctions.BlameGoal(u); break; } break; case "GET": if (payload.Count() == 0) return; switch (payload.First()) { case "GOAL": ModFunctions.GetGoal(u); break; case "UPTIME": ModFunctions.GetUptime(u); break; } break; case "SET": if (payload.Count() == 0) return; switch (payload.First()) { case "GOAL": ModFunctions.SetGoal(u, String.Join(" ", payload.Skip(1))); break; case "QUIETMODE": if (ModFunctions.SetQuietMode(u)) SendIRCNotice("Quiet mode on."); break; case "R9K": if (ModFunctions.SetAprilFools(u)) SendIRCNotice("r9k mode on."); break; } break; case "CLEAR": if (payload.Count() == 0) return; switch (payload.First()) { case "GOAL": if (!ModFunctions.ClearGoal(u)) Log("Unable to get goal, last get goal was less than 1 minute ago."); break; case "QUIETMODE": if (ModFunctions.ClearQuietMode(u)) SendIRCNotice("Quiet mode off."); break; case "R9K": if (ModFunctions.ClearAprilFools(u)) SendIRCNotice("r9k mode off."); break; } break; case "ENABLE": if (payload.Count() == 0) return; switch (payload.First()) { case "INPUT": break; // TODO } break; case "DISABLE": if (payload.Count() == 0) return; switch (payload.First()) { case "INPUT": break; // TODO } break; case "SAVE": if (payload.Count() == 0) return; switch (payload.First()) { case "STATE": if (ModFunctions.SaveState(u)) SendIRCNotice("Saved the twitchplays state."); break; case "GAME": if (ModFunctions.SaveGame(u)) SendIRCNotice("The game has been saved."); break; } break; case "RELOAD": if (payload.Count() == 0) return; switch (payload.First()) { case "FILTERS": if (ModFunctions.ReloadFilters(u)) SendIRCNotice("Reloaded filter lists..."); break; case "STATE": if (ModFunctions.LoadState(u)) SendIRCNotice("Reloaded the twitchplays state."); break; case "GAME": if (ModFunctions.ReloadGame(u)) SendIRCNotice("The game has been reloaded."); break; } break; case "MUTE": // TODO break; default: break; } } else if (command.Equals("ANARCHY")) { if (IsAprilFools) { ShowMessage(e.Data.Nick, (u.GameMode == User.ModeType.DEMOCRACY) ? "*DEMOCRACY" : "DEMOCRACY"); u.GameMode = User.ModeType.DEMOCRACY; } else { ShowMessage(e.Data.Nick, (u.GameMode == User.ModeType.ANARCHY) ? "*ANARCHY" : "ANARCHY"); u.GameMode = User.ModeType.ANARCHY; } } else if (command.Equals("DEMOCRACY")) { if (IsAprilFools) { ShowMessage(e.Data.Nick, (u.GameMode == User.ModeType.ANARCHY) ? "*ANARCHY" : "ANARCHY"); u.GameMode = User.ModeType.ANARCHY; } else { ShowMessage(e.Data.Nick, (u.GameMode == User.ModeType.DEMOCRACY) ? "*DEMOCRACY" : "DEMOCRACY"); u.GameMode = User.ModeType.DEMOCRACY; } } else { if (e.Data.Message.Length > 35) return; // don't parse long messages // Check to make sure it starts with one of our keys. foreach (string key in KeyMap.Keys) { if (command.StartsWith(key)) goto _goto_valid_command; } return; // not valid command _goto_valid_command: Regex regex = new Regex(@"(?:(?<key>[a-z]+)\s*(?<count>\d?\d?))\s*", RegexOptions.IgnoreCase); List<KeyType> keys = new List<KeyType>(); List<uint> counts = new List<uint>(); foreach (Match match in regex.Matches(command)) { GroupCollection groups = match.Groups; string _key = groups["key"].Value.ToUpper(); if (keyMap.ContainsKey(_key)) { uint count = 0; UInt32.TryParse(groups["count"].Value, out count); if (count == 0 || (_key.Equals("START") && count > 1)) count = 1; keys.Add(keyMap[_key]); counts.Add(count); } else break; } if (keys.Count > 0) { keyNominee = new Nominee(keys, counts); // Load if already exists if (keyNominee.CountsSum > 20) // No more than 20 queued inputs { ShowMessage(e.Data.Nick, "#" + keyNominee.Name); return; } Nominee existing = null; if (null != (existing = nominees.Find(x => x.Name.Equals(keyNominee.Name)))) keyNominee = existing; } } if (keyNominee != null) { if (GameMode == User.ModeType.ANARCHY) // Live Mode { Nominee n = new Nominee(keyNominee.Keys[0], 1); //Thread vJoyThread = new Thread(() => _logform.vJoySendNominee(n)); Thread vjoy_thread = new Thread(() => _logform.vJoySendButton((uint)n.Keys[0], 1)); vjoy_thread.Start(); u.LastNominee = n; ShowMessage(e.Data.Nick, n.Name); return; } else if (GameMode == User.ModeType.DEMOCRACY) { if (!nominees.Contains(keyNominee)) nominees.Add(keyNominee); string right_msg = (downvote) ? "-" + keyNominee.Name : keyNominee.Name; if (IsVotingClosed) { right_msg = "#" + right_msg; } else { bool contains = false; if (downvote) { lock (Users.Downvoted) { contains = Users.Downvoted.Contains(u); } } else { lock (Users.Voted) { contains = Users.Voted.Contains(u); } } if (contains) { Nominee lastNominee = (downvote) ? u.LastDownNominee : u.LastNominee; if (lastNominee == keyNominee) { right_msg = "*" + right_msg; } else { if (lastNominee != null) if (downvote) lastNominee.Vote(); else lastNominee.Unvote(); if (downvote) keyNominee.Unvote(); else keyNominee.Vote(); right_msg = ((downvote) ? "-" + lastNominee.Name : lastNominee.Name) + "=>" + right_msg; } } else { if (downvote) { lock (Users.Downvoted) { Users.Downvoted.Add(u); } keyNominee.Unvote(); } else { lock (Users.Voted) { Users.Voted.Add(u); } keyNominee.Vote(); } } if (downvote) u.LastDownNominee = keyNominee; else u.LastNominee = keyNominee; } ShowMessage(e.Data.Nick, right_msg); } else { // Unable to find key, wtf? } } }
public static void OnChannelMessage(object sender, IrcEventArgs e) { // Active Flags User u = null; lock (Users.All) { u = Users.All.Find(x => x.Name.Equals(e.Data.Nick.ToLower())); } if (u == null) { u = CreateUser(e.Data.Nick, false); } u.LastActive = DateTime.Now; u.IncrementVotes(); LastInteraction = DateTime.Now; Log(e.Data.Nick + ": " + e.Data.Message); if (!_logform.InputEnabled) { return; // Disable Voting UI Flag } // Website Filters string[] explicit_blacklist = { "dotcom", "(dot)", "░", "█" }; foreach (string m in explicit_blacklist) { if (e.Data.Message.ToLower().Contains(m)) { BanUser(u, false, "filter evasion / Unicode art"); return; } } Regex filter = new Regex(@"[A-z0-9-]+(?:\.[A-z]{2,3})+", RegexOptions.RightToLeft); string stripped_message = Regex.Replace(e.Data.Message, @"[()]", ""); bool is_clean = true; List <string> dirty_matches = new List <string>(); foreach (Match m in filter.Matches(stripped_message)) { WebsiteFilter website_filter = WebsiteFilters.Find(x => x.Domain.Equals(m.Value)); if (website_filter != null) { if (website_filter.Type == WebsiteFilter.FilterType.BLACKLIST) { BanUser(u, false, "blacklisted website: '" + m.Value + "'"); } return; } else { is_clean = false; dirty_matches.Add(m.Value); } } if (!is_clean) { BanUser(u, true, "unlisted website(s): " + String.Join(", ", dirty_matches)); return; } Nominee keyNominee = null; string command = e.Data.Message.ToUpper(); bool downvote = false; if (command.StartsWith("NO") || command.StartsWith("-")) { downvote = true; if (command.StartsWith("NOT")) { command = command.Substring(3); } else if (command.StartsWith("NO")) { command = command.Substring(2); } else if (command.StartsWith("-")) { command = command.Substring(1); } command.TrimStart(' '); } if (command.StartsWith("@")) // Function Command { string[] function_msg = command.Substring(1).Split(' '); if (function_msg.Count() == 0) { return; } string opcode = function_msg.First(); string[] payload = function_msg.Skip(1).ToArray(); Console.WriteLine("Function! Opcode: " + opcode + ", Payload: " + String.Join(" ", payload)); switch (opcode) { case "BLAME": if (payload.Count() == 0) { return; } switch (payload.First()) { case "GOAL": ModFunctions.BlameGoal(u); break; } break; case "GET": if (payload.Count() == 0) { return; } switch (payload.First()) { case "GOAL": ModFunctions.GetGoal(u); break; case "UPTIME": ModFunctions.GetUptime(u); break; } break; case "SET": if (payload.Count() == 0) { return; } switch (payload.First()) { case "GOAL": ModFunctions.SetGoal(u, String.Join(" ", payload.Skip(1))); break; case "QUIETMODE": if (ModFunctions.SetQuietMode(u)) { SendIRCNotice("Quiet mode on."); } break; case "R9K": if (ModFunctions.SetAprilFools(u)) { SendIRCNotice("r9k mode on."); } break; } break; case "CLEAR": if (payload.Count() == 0) { return; } switch (payload.First()) { case "GOAL": if (!ModFunctions.ClearGoal(u)) { Log("Unable to get goal, last get goal was less than 1 minute ago."); } break; case "QUIETMODE": if (ModFunctions.ClearQuietMode(u)) { SendIRCNotice("Quiet mode off."); } break; case "R9K": if (ModFunctions.ClearAprilFools(u)) { SendIRCNotice("r9k mode off."); } break; } break; case "ENABLE": if (payload.Count() == 0) { return; } switch (payload.First()) { case "INPUT": break; // TODO } break; case "DISABLE": if (payload.Count() == 0) { return; } switch (payload.First()) { case "INPUT": break; // TODO } break; case "SAVE": if (payload.Count() == 0) { return; } switch (payload.First()) { case "STATE": if (ModFunctions.SaveState(u)) { SendIRCNotice("Saved the twitchplays state."); } break; case "GAME": if (ModFunctions.SaveGame(u)) { SendIRCNotice("The game has been saved."); } break; } break; case "RELOAD": if (payload.Count() == 0) { return; } switch (payload.First()) { case "FILTERS": if (ModFunctions.ReloadFilters(u)) { SendIRCNotice("Reloaded filter lists..."); } break; case "STATE": if (ModFunctions.LoadState(u)) { SendIRCNotice("Reloaded the twitchplays state."); } break; case "GAME": if (ModFunctions.ReloadGame(u)) { SendIRCNotice("The game has been reloaded."); } break; } break; case "MUTE": // TODO break; default: break; } } else if (command.Equals("ANARCHY")) { if (IsAprilFools) { ShowMessage(e.Data.Nick, (u.GameMode == User.ModeType.DEMOCRACY) ? "*DEMOCRACY" : "DEMOCRACY"); u.GameMode = User.ModeType.DEMOCRACY; } else { ShowMessage(e.Data.Nick, (u.GameMode == User.ModeType.ANARCHY) ? "*ANARCHY" : "ANARCHY"); u.GameMode = User.ModeType.ANARCHY; } } else if (command.Equals("DEMOCRACY")) { if (IsAprilFools) { ShowMessage(e.Data.Nick, (u.GameMode == User.ModeType.ANARCHY) ? "*ANARCHY" : "ANARCHY"); u.GameMode = User.ModeType.ANARCHY; } else { ShowMessage(e.Data.Nick, (u.GameMode == User.ModeType.DEMOCRACY) ? "*DEMOCRACY" : "DEMOCRACY"); u.GameMode = User.ModeType.DEMOCRACY; } } else { if (e.Data.Message.Length > 35) { return; // don't parse long messages } // Check to make sure it starts with one of our keys. foreach (string key in KeyMap.Keys) { if (command.StartsWith(key)) { goto _goto_valid_command; } } return; // not valid command _goto_valid_command: Regex regex = new Regex(@"(?:(?<key>[a-z]+)\s*(?<count>\d?\d?))\s*", RegexOptions.IgnoreCase); List <KeyType> keys = new List <KeyType>(); List <uint> counts = new List <uint>(); foreach (Match match in regex.Matches(command)) { GroupCollection groups = match.Groups; string _key = groups["key"].Value.ToUpper(); if (keyMap.ContainsKey(_key)) { uint count = 0; UInt32.TryParse(groups["count"].Value, out count); if (count == 0 || (_key.Equals("START") && count > 1)) { count = 1; } keys.Add(keyMap[_key]); counts.Add(count); } else { break; } } if (keys.Count > 0) { keyNominee = new Nominee(keys, counts); // Load if already exists if (keyNominee.CountsSum > 20) // No more than 20 queued inputs { ShowMessage(e.Data.Nick, "#" + keyNominee.Name); return; } Nominee existing = null; if (null != (existing = nominees.Find(x => x.Name.Equals(keyNominee.Name)))) { keyNominee = existing; } } } if (keyNominee != null) { if (GameMode == User.ModeType.ANARCHY) // Live Mode { Nominee n = new Nominee(keyNominee.Keys[0], 1); //Thread vJoyThread = new Thread(() => _logform.vJoySendNominee(n)); Thread vjoy_thread = new Thread(() => _logform.vJoySendButton((uint)n.Keys[0], 1)); vjoy_thread.Start(); u.LastNominee = n; ShowMessage(e.Data.Nick, n.Name); return; } else if (GameMode == User.ModeType.DEMOCRACY) { if (!nominees.Contains(keyNominee)) { nominees.Add(keyNominee); } string right_msg = (downvote) ? "-" + keyNominee.Name : keyNominee.Name; if (IsVotingClosed) { right_msg = "#" + right_msg; } else { bool contains = false; if (downvote) { lock (Users.Downvoted) { contains = Users.Downvoted.Contains(u); } } else { lock (Users.Voted) { contains = Users.Voted.Contains(u); } } if (contains) { Nominee lastNominee = (downvote) ? u.LastDownNominee : u.LastNominee; if (lastNominee == keyNominee) { right_msg = "*" + right_msg; } else { if (lastNominee != null) { if (downvote) { lastNominee.Vote(); } else { lastNominee.Unvote(); } } if (downvote) { keyNominee.Unvote(); } else { keyNominee.Vote(); } right_msg = ((downvote) ? "-" + lastNominee.Name : lastNominee.Name) + "=>" + right_msg; } } else { if (downvote) { lock (Users.Downvoted) { Users.Downvoted.Add(u); } keyNominee.Unvote(); } else { lock (Users.Voted) { Users.Voted.Add(u); } keyNominee.Vote(); } } if (downvote) { u.LastDownNominee = keyNominee; } else { u.LastNominee = keyNominee; } } ShowMessage(e.Data.Nick, right_msg); } else { // Unable to find key, wtf? } } }
private void Tick() { while (true) { Thread.Sleep(75); // UserCount //SetUserCount(Users.All.Count); // Tallying bool tallied = false; bool winnerBool = false; bool isPopularVote = false; Nominee winner = null; if ((Program.TallyTime - DateTime.Now).TotalMilliseconds < 0 && _wasTallySet) // Perform Nominee { _wasTallySet = false; tallied = true; List <Nominee> nominees = Program.SortNominees(); winner = (nominees.Count >= 1) ? nominees[0] : null; Nominee runnerup = (nominees.Count >= 2) ? nominees[1] : null; if (winner == null) { goto _goto_not_voting; } if (winner.Votes > 0) { winnerBool = true; if (runnerup != null && winner.Votes == runnerup.Votes) { isPopularVote = false; } else { isPopularVote = true; Thread vJoyThread = new Thread(() => vJoySendNominee(winner)); vJoyThread.IsBackground = true; vJoyThread.Start(); } Program.ClearNominees(); } else { if ((DateTime.Now - Program.LastInteraction).TotalSeconds > 3.0) // Check for Twitch Lag { LagNoticeEnabled = true; } } } _goto_not_voting: if (this.InvokeRequired) // TODO macro this { try { this.Invoke(new Action( () => { lblTimespan.Text = (Program.RunStart - DateTime.Now).ToString(@"dh':'mm':'ss"); //lblTimespan.Text = (DateTime.Now - Program.RunStart).ToString(@"d'd 'h'h 'm'm 's's'"); if (Program.TallyTime > DateTime.Now) // Counting down { lblVoteClock.Text = Program.VoteSpan.ToString(@"ss\.fff's left'"); double voting_time = Program.VoteDuration + Program.DurationOffset - Program.LagPropagationDelay; if (Program.IsVotingClosed) { if (lblVoteClock.ForeColor != Color.Silver) { lblVoteClock.ForeColor = Color.Silver; voteBox.ForeColor = Color.Silver; } } else if (Program.VoteSpan.TotalSeconds <= voting_time * 1.0 / 3.0) { if (lblVoteClock.ForeColor != Color.Crimson) { lblVoteClock.ForeColor = Color.Crimson; } } else if (Program.VoteSpan.TotalSeconds <= voting_time * 2.0 / 3.0) { if (lblVoteClock.ForeColor != Color.Tomato) { lblVoteClock.ForeColor = Color.Tomato; } } else { if (lblVoteClock.ForeColor != Color.White) { lblVoteClock.ForeColor = Color.White; voteBox.ForeColor = Color.White; } } if (!_isVotingOpen && !Program.IsVotingClosed) { _isVotingOpen = true; //Program.SendIRCMessage("++ " + lblPollNum.Text + " is now open!"); } } else // Zero { lblVoteClock.Text = "--.---s left"; lblVoteClock.ForeColor = Color.White; if (Program.GameMode == User.ModeType.DEMOCRACY) { StartVote(); } } if (tallied && Program.GameMode == User.ModeType.DEMOCRACY) { if (winnerBool) { if (isPopularVote) { lblVoteResult.Text = "WINNER: " + winner.Name; } else { lblVoteResult.Text = "TIE, NO ACTION"; } } else { lblVoteResult.Text = "NO VOTES"; } Program.SendIRCNotice(lblPollNum.Text + " " + lblVoteResult.Text); if (voteCount == 999) { voteCount = -1; } lblPollNum.Text = "Poll #" + (++voteCount).ToString().PadLeft(3, '0'); } }) ); } catch (Exception) { } } else if (Program.IsMainThread) { lblTimespan.Text = (Program.RunStart - DateTime.Now).ToString(@"dh':'mm':'ss"); //lblTimespan.Text = (DateTime.Now - Program.RunStart).ToString(@"d'd 'h'h 'm'm 's's'"); if (Program.TallyTime > DateTime.Now) // Counting down { lblVoteClock.Text = Program.VoteSpan.ToString(@"ss\.fff's left'"); double voting_time = Program.VoteDuration + Program.DurationOffset - Program.LagPropagationDelay; if (Program.IsVotingClosed) { if (lblVoteClock.ForeColor != Color.Silver) { lblVoteClock.ForeColor = Color.Silver; voteBox.ForeColor = Color.Silver; } } else if (Program.VoteSpan.TotalSeconds <= voting_time * 1.0 / 3.0) { if (lblVoteClock.ForeColor != Color.Crimson) { lblVoteClock.ForeColor = Color.Crimson; } } else if (Program.VoteSpan.TotalSeconds <= voting_time * 2.0 / 3.0) { if (lblVoteClock.ForeColor != Color.Tomato) { lblVoteClock.ForeColor = Color.Tomato; } } else { if (lblVoteClock.ForeColor != Color.White) { lblVoteClock.ForeColor = Color.White; voteBox.ForeColor = Color.White; } } if (!_isVotingOpen && !Program.IsVotingClosed) { _isVotingOpen = true; //Program.SendIRCMessage("++ " + lblPollNum.Text + " is now open!"); } } else // Zero { lblVoteClock.Text = "--.---s left"; lblVoteClock.ForeColor = Color.White; if (Program.GameMode == User.ModeType.DEMOCRACY) { StartVote(); } } if (tallied && Program.GameMode == User.ModeType.DEMOCRACY) { if (winnerBool) { if (isPopularVote) { lblVoteResult.Text = "WINNER: " + winner.Name; } else { lblVoteResult.Text = "TIE, NO ACTION"; } } else { lblVoteResult.Text = "NO VOTES"; } Program.SendIRCNotice(lblPollNum.Text + " " + lblVoteResult.Text); if (voteCount == 999) { voteCount = -1; } lblPollNum.Text = "Poll #" + (++voteCount).ToString().PadLeft(3, '0'); } } } }
public void vJoySendNominee(Nominee n) { for (int i = 0; i < n.Keys.Count; i++) { if (n.Keys[i] == Program.KeyType.HOLD) Thread.Sleep(500); else vJoySendButton((uint)n.Keys[i], n.Counts[i]); Thread.Sleep(100); } }