summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/roulette.rb
blob: d57bc621e0b092e1988c31dd7cd92418c448ea33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
40
41
42
43
44
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
RouletteHistory = Struct.new("RouletteHistory", :games, :shots, :deaths, :misses, :wins)

class RoulettePlugin < Plugin
  def initialize
    super
    reset_chambers
    @players = Array.new
  end
  def help(plugin, topic="")
    "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)"
  end
  def clearstats(m, params)
    @registry.clear
    m.okay
  end

  def roulette(m, params)
    if m.private?
      m.reply "you gotta play roulette in channel dude"
      return
    end

    playerdata = nil
    if @registry.has_key?(m.sourcenick)
      playerdata = @registry[m.sourcenick]
    else
      playerdata = RouletteHistory.new(0,0,0,0,0)
    end
    
    unless @players.include?(m.sourcenick)
      @players << m.sourcenick
      playerdata.games += 1
    end
    playerdata.shots += 1
    
    shot = @chambers.pop
    if shot
      m.reply "#{m.sourcenick}: chamber #{6 - @chambers.length} of 6 => *BANG*"
      playerdata.deaths += 1
      @players.each {|plyr|
        next if plyr == m.sourcenick
        pdata = @registry[plyr]
        next if pdata == nil
        pdata.wins += 1
        @registry[plyr] = pdata
      }
    else
      m.reply "#{m.sourcenick}: chamber #{6 - @chambers.length} of 6 => +click+"
      playerdata.misses += 1
    end

    @registry[m.sourcenick] = playerdata
    
    if shot || @chambers.empty?
      reload(m)
    end
  end
  def reload(m, params = {})
    @bot.action m.replyto, "reloads"
    reset_chambers
    # all players win on a reload
    # (allows you to play 3-shot matches etc)
    @players.each {|plyr|
      pdata = @registry[plyr]
      next if pdata == nil
      pdata.wins += 1
      @registry[plyr] = pdata
    }
    @players = Array.new
  end
  def reset_chambers
    @chambers = [false, false, false, false, false, false]
    @chambers[rand(@chambers.length)] = true
  end
  def playerstats(m, params)
    player = params[:player]
    pstats = @registry[player]
    if pstats.nil?
      m.reply "#{player} hasn't played enough games yet"
    else
      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."
    end
  end
  def stats(m, params)
    total_players = 0
    total_games = 0
    total_shots = 0
    
    died_most = [nil,0]
    won_most = [nil,0]
    h_win_percent = [nil,0]
    l_win_percent = [nil,0]
    h_luck_percent = [nil,0]
    l_luck_percent = [nil,0]
    @registry.each {|k,v|
      total_players += 1
      total_games += v.deaths
      total_shots += v.shots
      
      win_rate = v.wins.to_f / v.games * 100
      if h_win_percent[0].nil? || win_rate > h_win_percent[1] && v.games > 2
        h_win_percent = [[k], win_rate]
      elsif win_rate == h_win_percent[1] && v.games > 2
        h_win_percent[0] << k
      end
      if l_win_percent[0].nil? || win_rate < l_win_percent[1] && v.games > 2
        l_win_percent = [[k], win_rate]
      elsif win_rate == l_win_percent[1] && v.games > 2
        l_win_percent[0] << k
      end

      luck = v.misses.to_f / v.shots * 100
      if h_luck_percent[0].nil? || luck > h_luck_percent[1] && v.games > 2
        h_luck_percent = [[k], luck]
      elsif luck == h_luck_percent[1] && v.games > 2
        h_luck_percent[0] << k
      end
      if l_luck_percent[0].nil? || luck < l_luck_percent[1] && v.games > 2
        l_luck_percent = [[k], luck]
      elsif luck == l_luck_percent[1] && v.games > 2
        l_luck_percent[0] << k
      end

      if died_most[0].nil? || v.deaths > died_most[1]
        died_most = [[k], v.deaths]
      elsif v.deaths == died_most[1]
        died_most[0] << k
      end
      if won_most[0].nil? || v.wins > won_most[1]
        won_most = [[k], v.wins]
      elsif v.wins == won_most[1]
        won_most[0] << k
      end
    }
    if total_games < 1
      m.reply "roulette stats: no games played yet"
    else
      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]})."
    end
  end
end
plugin = RoulettePlugin.new
plugin.map 'roulette reload', :action => 'reload'
plugin.map 'roulette stats :player', :action => 'playerstats'
plugin.map 'roulette stats', :action => 'stats'
plugin.map 'roulette clearstats', :action => 'clearstats', :auth => 'config'
plugin.map 'roulette'