1 define_structure :RouletteHistory, :games, :shots, :deaths, :misses, :wins
3 class RoulettePlugin < Plugin
4 BotConfig.register BotConfigBooleanValue.new('roulette.autospin',
6 :desc => "Automatically spins the roulette at the butlast shot")
7 BotConfig.register BotConfigBooleanValue.new('roulette.kick',
9 :desc => "Kicks shot players from the channel")
10 BotConfig.register BotConfigBooleanValue.new('roulette.twice_in_a_row',
12 :desc => "Allow players to go twice in a row")
20 def help(plugin, topic="")
21 "roulette => play russian roulette - starts a new game if one isn't already running. One round in a six chambered gun. Take turns to say roulette to the bot, until somebody dies. roulette reload => force the gun to reload, roulette stats => show stats from all games, roulette stats <player> => show stats for <player>, roulette clearstats => clear stats (config level auth required), roulette spin => spins the cylinder"
24 def clearstats(m, params)
29 def roulette(m, params)
31 m.reply "you gotta play roulette in channel dude"
36 if @registry.has_key?("player " + m.sourcenick)
37 playerdata = @registry["player " + m.sourcenick]
39 playerdata = RouletteHistory.new(0,0,0,0,0)
43 if @registry.has_key?("totals")
44 totals = @registry["totals"]
46 totals = RouletteHistory.new(0,0,0,0,0)
49 if @players.last == m.sourcenick and not @bot.config['roulette.twice_in_a_row']
50 m.reply "you can't go twice in a row!"
54 unless @players.include?(m.sourcenick)
55 @players << m.sourcenick
63 m.reply "#{m.sourcenick}: chamber #{6 - @chambers.length} of 6 => *BANG*"
64 playerdata.deaths += 1
67 next if plyr == m.sourcenick
68 pdata = @registry["player " + plyr]
72 @registry["player " + plyr] = pdata
75 @bot.kick(m.replyto, m.sourcenick, "*BANG*") if @bot.config['roulette.kick']
77 m.reply "#{m.sourcenick}: chamber #{6 - @chambers.length} of 6 => +click+"
78 playerdata.misses += 1
82 @registry["player " + m.sourcenick] = playerdata
83 @registry["totals"] = totals
85 if shot || @chambers.empty?
87 elsif @chambers.length == 1 and @bot.config['roulette.autospin']
92 def reload(m, params = {})
94 m.reply "you gotta play roulette in channel dude"
100 # all players win on a reload
101 # (allows you to play 3-shot matches etc)
103 if @registry.has_key?("totals")
104 totals = @registry["totals"]
106 totals = RouletteHistory.new(0,0,0,0,0)
109 @players.each {|plyr|
110 pdata = @registry["player " + plyr]
114 @registry["player " + plyr] = pdata
118 @registry["totals"] = totals
123 def spin(m, params={})
124 # Spinning is just like resetting, except that nobody wins
126 m.reply "you gotta play roulette in channel dude"
130 m.act "spins the cylinder"
135 @chambers = [false, false, false, false, false, false]
136 @chambers[rand(@chambers.length)] = true
139 def playerstats(m, params)
140 player = params[:player]
141 pstats = @registry["player " + player]
143 m.reply "#{player} hasn't played enough games yet"
145 m.reply "#{player} has played #{pstats.games} games, won #{pstats.wins} and lost #{pstats.deaths}. #{player} pulled the trigger #{pstats.shots} times and found the chamber empty on #{pstats.misses} occasions."
150 if @registry.has_key?("totals")
151 totals = @registry["totals"]
152 total_games = totals.games
153 total_shots = totals.shots
163 h_win_percent = [nil,0]
164 l_win_percent = [nil,0]
165 h_luck_percent = [nil,0]
166 l_luck_percent = [nil,0]
167 @registry.each {|k,v|
168 match = /player (.+)/.match(k)
174 win_rate = v.wins.to_f / v.games * 100
175 if h_win_percent[0].nil? || win_rate > h_win_percent[1] && v.games > 2
176 h_win_percent = [[k], win_rate]
177 elsif win_rate == h_win_percent[1] && v.games > 2
178 h_win_percent[0] << k
180 if l_win_percent[0].nil? || win_rate < l_win_percent[1] && v.games > 2
181 l_win_percent = [[k], win_rate]
182 elsif win_rate == l_win_percent[1] && v.games > 2
183 l_win_percent[0] << k
186 luck = v.misses.to_f / v.shots * 100
187 if h_luck_percent[0].nil? || luck > h_luck_percent[1] && v.games > 2
188 h_luck_percent = [[k], luck]
189 elsif luck == h_luck_percent[1] && v.games > 2
190 h_luck_percent[0] << k
192 if l_luck_percent[0].nil? || luck < l_luck_percent[1] && v.games > 2
193 l_luck_percent = [[k], luck]
194 elsif luck == l_luck_percent[1] && v.games > 2
195 l_luck_percent[0] << k
198 if died_most[0].nil? || v.deaths > died_most[1]
199 died_most = [[k], v.deaths]
200 elsif v.deaths == died_most[1]
203 if won_most[0].nil? || v.wins > won_most[1]
204 won_most = [[k], v.wins]
205 elsif v.wins == won_most[1]
210 m.reply "roulette stats: no games completed yet"
212 m.reply "roulette stats: #{total_games} games completed, #{total_shots} shots fired at #{total_players} players. Luckiest: #{h_luck_percent[0].join(',')} (#{sprintf '%.1f', h_luck_percent[1]}% clicks). Unluckiest: #{l_luck_percent[0].join(',')} (#{sprintf '%.1f', l_luck_percent[1]}% clicks). Highest survival rate: #{h_win_percent[0].join(',')} (#{sprintf '%.1f', h_win_percent[1]}%). Lowest survival rate: #{l_win_percent[0].join(',')} (#{sprintf '%.1f', l_win_percent[1]}%). Most wins: #{won_most[0].join(',')} (#{won_most[1]}). Most deaths: #{died_most[0].join(',')} (#{died_most[1]})."
217 plugin = RoulettePlugin.new
219 plugin.default_auth('clearstats', false)
221 plugin.map 'roulette reload', :action => 'reload'
222 plugin.map 'roulette spin', :action => 'spin'
223 plugin.map 'roulette stats :player', :action => 'playerstats'
224 plugin.map 'roulette stats', :action => 'stats'
225 plugin.map 'roulette clearstats', :action => 'clearstats'
226 plugin.map 'roulette'