1 define_structure :RouletteHistory, :games, :shots, :deaths, :misses, :wins, :points
3 class RoulettePlugin < Plugin
4 Config.register Config::BooleanValue.new('roulette.autospin',
6 :desc => "Automatically spins the roulette at the butlast shot")
7 Config.register Config::BooleanValue.new('roulette.kick',
9 :desc => "Kicks shot players from the channel")
10 Config.register Config::BooleanValue.new('roulette.twice_in_a_row',
12 :desc => "Allow players to go twice in a row")
21 def help(plugin, topic="")
22 "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"
25 def clearstats(m, params)
30 def roulette(m, params)
32 m.reply "you gotta play roulette in channel dude"
37 if @registry.has_key?("player " + m.sourcenick)
38 playerdata = @registry["player " + m.sourcenick]
40 playerdata = RouletteHistory.new(0,0,0,0,0,0)
44 if @registry.has_key?("totals")
45 totals = @registry["totals"]
47 totals = RouletteHistory.new(0,0,0,0,0,0)
50 if @last == m.sourcenick and not @bot.config['roulette.twice_in_a_row']
51 m.reply "you can't go twice in a row!"
56 unless @players.include?(m.sourcenick)
57 @players << m.sourcenick
64 chamberNo = 6 - @chambers.length
66 m.reply "#{m.sourcenick}: chamber #{chamberNo} of 6 => *BANG*"
67 playerdata.deaths += 1
70 next if plyr == m.sourcenick
71 pdata = @registry["player " + plyr]
75 @registry["player " + plyr] = pdata
79 @bot.kick(m.replyto, m.sourcenick, "*BANG*") if @bot.config['roulette.kick']
81 m.reply "#{m.sourcenick}: chamber #{chamberNo} of 6 => +click+"
82 playerdata.misses += 1
83 playerdata.points += 2**chamberNo
87 @registry["player " + m.sourcenick] = playerdata
88 @registry["totals"] = totals
90 if shot || @chambers.empty?
92 elsif @chambers.length == 1 and @bot.config['roulette.autospin']
97 def reload(m, params = {})
99 m.reply "you gotta play roulette in channel dude"
105 # all players win on a reload
106 # (allows you to play 3-shot matches etc)
108 if @registry.has_key?("totals")
109 totals = @registry["totals"]
111 totals = RouletteHistory.new(0,0,0,0,0,0)
114 @players.each {|plyr|
115 pdata = @registry["player " + plyr]
119 @registry["player " + plyr] = pdata
123 @registry["totals"] = totals
129 def spin(m, params={})
130 # Spinning is just like resetting, except that nobody wins
132 m.reply "you gotta play roulette in channel dude"
136 m.act "spins the cylinder"
141 @chambers = [false, false, false, false, false, false]
142 @chambers[rand(@chambers.length)] = true
145 def playerstats(m, params)
146 player = params[:player]
147 pstats = @registry["player " + player]
149 m.reply "#{player} hasn't played enough games yet"
151 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."
156 if @registry.has_key?("totals")
157 totals = @registry["totals"]
158 total_games = totals.games
159 total_shots = totals.shots
169 h_win_percent = [nil,0]
170 l_win_percent = [nil,0]
171 h_luck_percent = [nil,0]
172 l_luck_percent = [nil,0]
173 @registry.each {|k,v|
174 match = /player (.+)/.match(k)
180 win_rate = v.wins.to_f / v.games * 100
181 if h_win_percent[0].nil? || win_rate > h_win_percent[1] && v.games > 2
182 h_win_percent = [[k], win_rate]
183 elsif win_rate == h_win_percent[1] && v.games > 2
184 h_win_percent[0] << k
186 if l_win_percent[0].nil? || win_rate < l_win_percent[1] && v.games > 2
187 l_win_percent = [[k], win_rate]
188 elsif win_rate == l_win_percent[1] && v.games > 2
189 l_win_percent[0] << k
192 luck = v.misses.to_f / v.shots * 100
193 if h_luck_percent[0].nil? || luck > h_luck_percent[1] && v.games > 2
194 h_luck_percent = [[k], luck]
195 elsif luck == h_luck_percent[1] && v.games > 2
196 h_luck_percent[0] << k
198 if l_luck_percent[0].nil? || luck < l_luck_percent[1] && v.games > 2
199 l_luck_percent = [[k], luck]
200 elsif luck == l_luck_percent[1] && v.games > 2
201 l_luck_percent[0] << k
204 if died_most[0].nil? || v.deaths > died_most[1]
205 died_most = [[k], v.deaths]
206 elsif v.deaths == died_most[1]
209 if won_most[0].nil? || v.wins > won_most[1]
210 won_most = [[k], v.wins]
211 elsif v.wins == won_most[1]
216 m.reply "roulette stats: no games completed yet"
218 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]})."
222 # Figure out who the winnar is!
225 tmpKey = params[:key].to_s
226 targetKey = tmpKey.to_sym
227 m.reply("Checking out the #{tmpKey} HoF...")
228 tmp = @registry.to_hash
230 sorted = tmp.sort { |a,b| b[1][targetKey] <=> a[1][targetKey] }
235 sorted.each do |player|
236 playerName = player[0].split(" ")[1]
237 if player[0] == "totals" or playerName == ""
240 winners << "#{playerName} has #{player[1][targetKey]}"
246 m.reply(winners.join(" | "))
250 plugin = RoulettePlugin.new
252 plugin.default_auth('clearstats', false)
254 plugin.map 'roulette reload', :action => 'reload'
255 plugin.map 'roulette spin', :action => 'spin'
256 plugin.map 'roulette stats :player', :action => 'playerstats'
257 plugin.map 'roulette stats', :action => 'stats'
258 plugin.map 'roulette clearstats', :action => 'clearstats'
259 plugin.map 'roulette hof :key', :action => 'hof', :defaults => {:key => "points"}, :requirements => {:key => /^(?:games|shots|deaths|misses|wins|points)$/}
260 plugin.map 'roulette'