Class: Cinch::Plugins::Decide

Inherits:
Object
  • Object
show all
Includes:
Cinch::Plugin
Defined in:
lib/cinch/plugins/decide.rb

Instance Method Summary collapse

Instance Method Details

#decide!(list) ⇒ Object


17
18
19
# File 'lib/cinch/plugins/decide.rb', line 17

def decide!(list)
  decide_list_to_array(list).sample
end

#decide_list_to_array(list) ⇒ Object


21
22
23
24
# File 'lib/cinch/plugins/decide.rb', line 21

def decide_list_to_array(list)
  list = list.gsub(/\x03([0-9]{2}(,[0-9]{2})?)?/,"") #strips IRC colors
  return list.gsub(/ or /i, ",").split(",").map(&:strip).reject(&:empty?)
end

#execute_best_out_of_x(m, maximum, list) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cinch/plugins/decide.rb', line 33

def execute_best_out_of_x(m, maximum, list)
  maximum = maximum.to_i
  return m.reply("#{maximum} must not be greater than 100") if maximum > 100
  list = decide_list_to_array list
  rounds = []
  maximum.times {|round|
    rounds << list.sample
  }
  best = rounds.group_by { |n| n }.values.max_by(&:size)
  best_count, best_value = best.count, best.first

  m.reply "#{best_value} wins #{best_count} out of #{maximum}!"
end

#execute_coinflip(m) ⇒ Object


48
49
50
51
# File 'lib/cinch/plugins/decide.rb', line 48

def execute_coinflip(m)
  face = Random.new.rand(1..2) == 1 ? "heads" : "tails";
  m.safe_reply("I choose \"#{face}\"!",true);
end

#execute_decision(m, list) ⇒ Object


28
29
30
# File 'lib/cinch/plugins/decide.rb', line 28

def execute_decision(m, list)
  m.safe_reply("I choose \"#{decide! list}\"!",true);
end

#execute_random(m, x, y) ⇒ Object


55
56
57
58
59
60
61
62
63
# File 'lib/cinch/plugins/decide.rb', line 55

def execute_random(m, x, y)
  x = x.to_f.prettify
  y = y.to_f.prettify
  xy = "(x=#{x}, y=#{y})"
  return m.reply("X must not be equal to Y. #{xy}", true) if x == y
  return m.reply("X must be lesser than Y. #{xy}") if x > y

  m.reply "Your number is: #{Random.new.rand(x..y)}.", true
end

#execute_token(m, length) ⇒ Object


66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cinch/plugins/decide.rb', line 66

def execute_token(m, length)
  max_length = 256
  def power_of_2?(number)
    (1..32).each { |bit| return true if number == (1 << bit) }
    false
  end

  return m.reply "Your token length can only be 2, 4, 8, 16, 32, 64, 128, and 256." unless power_of_2?(length.to_i)
  return m.reply "Your token length must be 256 or below." if length.to_i > 256
  characters = ('a'..'z').to_a + ('A'..'Z').to_a + (0..9).to_a
  key = (0..length.to_i-1).map{characters.sample}.join
  m.reply "Your token is: #{key}", true
  m.user.notice "Alternatively, you may want it in these formats: #{key.scan(/.{0,#{key.length / (key.length / 8)}}/).reject(&:empty?).join("-")}, #{key.upcase.scan(/.{0,#{key.length / (key.length / 8)}}/).reject(&:empty?).join("-")}"
end

#power_of_2?(number) ⇒ Boolean

Returns:

  • (Boolean)

68
69
70
71
# File 'lib/cinch/plugins/decide.rb', line 68

def power_of_2?(number)
  (1..32).each { |bit| return true if number == (1 << bit) }
  false
end