Class: TwitchPlays::Plugin
- Inherits:
-
Object
- Object
- TwitchPlays::Plugin
- Includes:
- Cinch::Plugin
- Defined in:
- lib/twitch_plays/twitch_plays.rb
Instance Method Summary collapse
- #calculate_ratio ⇒ Object
- #format_log_line(name, command) ⇒ Object
-
#initialize(*args) ⇒ Plugin
constructor
A new instance of Plugin.
- #on_connect(msg) ⇒ Object
- #on_message(msg) ⇒ Object
- #start_anarchy ⇒ Object
- #start_democracy ⇒ Object
Constructor Details
#initialize(*args) ⇒ Plugin
Returns a new instance of Plugin.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/twitch_plays/twitch_plays.rb', line 13 def initialize(*args) super @keys = config[:keys] @use_touch = config[:use_touch] touch_move_amount = config[:touch_move_amount] @touch_directions = { touch_up: [0, -touch_move_amount], touch_down: [0, touch_move_amount], touch_left: [-touch_move_amount, 0], touch_right: [touch_move_amount, 0] } @democracy_votes = 0 @anarchy_votes = 0 @use_democracy = config[:use_democracy] @democracy_mode = @use_democracy && config[:start_in_democracy] @democracy_ratio_needed = config[:democracy_ratio_needed] @anarchy_ratio_needed = config[:anarchy_ratio_needed] @button_votes = Hash.new(0) @voting_time = config[:voting_time] @voting_thread = nil @total_votes = 0 @use_savestates = config[:use_savestates] @savestate_interval = config[:savestate_interval] @savestate_thread = nil @log_line_length = config[:log_line_length] start_democracy if @democracy_mode end |
Instance Method Details
#calculate_ratio ⇒ Object
41 42 43 |
# File 'lib/twitch_plays/twitch_plays.rb', line 41 def calculate_ratio @democracy_votes.to_f / [@democracy_votes + @anarchy_votes, 1].max end |
#format_log_line(name, command) ⇒ Object
89 90 91 |
# File 'lib/twitch_plays/twitch_plays.rb', line 89 def format_log_line(name, command) "#{name}#{' ' * (@log_line_length - name.length - command.length)}#{command}" end |
#on_connect(msg) ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/twitch_plays/twitch_plays.rb', line 93 def on_connect(msg) return unless @use_savestates @savestate_thread = Thread.new do key = @keys[:save_state] loop do sleep @savestate_interval Output.press(key) end end end |
#on_message(msg) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/twitch_plays/twitch_plays.rb', line 104 def (msg) = msg. case when /^anarchy$/i return unless @use_democracy puts format_log_line(msg.user.nick, .downcase) synchronize(:votes_mutex) do @anarchy_votes += 1 if @democracy_mode && calculate_ratio <= @anarchy_ratio_needed start_anarchy end end when /^democracy$/i return unless @use_democracy puts format_log_line(msg.user.nick, .downcase) synchronize(:votes_mutex) do @democracy_votes += 1 if !@democracy_mode && calculate_ratio >= @democracy_ratio_needed start_democracy end end when /^(?:up|down|left|right|a|b|x|y|l|r|start|select)$/i btn = $&.to_sym return unless @keys.has_key?(btn) if @democracy_mode synchronize(:votes_mutex) do @total_votes += 1 @button_votes[btn] += 1 end return end puts format_log_line(msg.user.nick, .downcase) Output.press(@keys[btn]) when /^(?:touch_up|touch_down|touch_left|touch_right)$/i return unless @use_touch btn = $&.to_sym if @democracy_mode synchronize(:votes_mutex) do @total_votes += 1 @button_votes[btn] += 1 end return end puts format_log_line(msg.user.nick, .downcase) Output.touch_move(*@touch_directions[btn]) when /^touch_press$/i return unless @use_touch if @democracy_mode synchronize(:votes_mutex) do @total_votes += 1 @button_votes[$&.to_sym] += 1 end return end puts format_log_line(msg.user.nick, .downcase) Output.touch_press when /^touch_release$/i return unless @use_touch if @democracy_mode synchronize(:votes_mutex) do @total_votes += 1 @button_votes[$&.to_sym] += 1 end return end puts format_log_line(msg.user.nick, .downcase) Output.touch_release end end |
#start_anarchy ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/twitch_plays/twitch_plays.rb', line 81 def start_anarchy return unless @democracy_mode @democracy_mode = false @voting_thread.join @voting_thread = nil puts '---STARTING ANARCHY MODE---' end |
#start_democracy ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/twitch_plays/twitch_plays.rb', line 45 def start_democracy return if @democracy_mode @democracy_mode = true puts '---STARTING DEMOCRACY MODE---' @voting_thread = Thread.new do while @democracy_mode synchronize(:votes_mutex) do @button_votes.clear @total_votes = 0 end sleep @voting_time next if @total_votes.zero? winner = synchronize(:votes_mutex) do puts "VOTES:\n" + @button_votes.sort {|(_, count1), (_, count2)| count1 <=> count2 }.first(5).map {|(btn, count)| [btn, count.to_f / @total_votes * 100] }.map {|(btn, percent)| format_log_line(btn, "#{percent}%") }.join("\n") @button_votes.max_by {|(_, count)| count}[0] end case winner when :touch_up, :touch_down, :touch_left, :touch_right Output.touch_move(*@touch_directions[winner]) when :touch_press Output.touch_press when :touch_release Output.touch_release else Output.press(@keys[winner]) end end end end |