Class: Cinch::Plugins::YamlScore

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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ YamlScore

Returns a new instance of YamlScore.



8
9
10
11
12
13
14
15
# File 'lib/cinch/plugins/yamlscore.rb', line 8

def initialize(*args)
  super
  if File.exist?('scores.yaml')
    @scores = YAML.load_file('scores.yaml')
  else
    @scores = {}
  end
end

Instance Method Details

#change(m, nick, score) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cinch/plugins/yamlscore.rb', line 33

def change(m, nick, score)
  if nick == m.user.nick
    m.reply "You can't score for yourself..."
  elsif nick == bot.nick
    m.reply "You can't score for me..."
  elsif m.channel.has_user?(nick)
    score.sub!(/([+-]){2}/,'\11')
    @scores[nick] ||= 0
    @scores[nick] += score.to_i
    @scores.delete(nick) if @scores[nick] == 0
    m.reply "#{m.user.nick}(#{@scores[m.user.nick]}) gave #{score} for #{nick}(#{@scores[nick]})."
    update_store
  elsif %w( , : ).include?(nick[-1]) && m.channel.has_user?(nick.slice(0..-2))
    nick.slice!(-1)
    score.sub!(/([+-]){2}/,'\11')
    @scores[nick] ||= 0
    @scores[nick] += score.to_i
    @scores.delete(nick) if @scores[nick] == 0
    m.reply "#{m.user.nick}(#{@scores[m.user.nick]}) gave #{score} for #{nick}(#{@scores[nick]})."
    update_store
  elsif config[:warn_no_user_message]
    m.reply config[:warn_no_user_message] % nick
  end
end

#score(m, nick) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/cinch/plugins/yamlscore.rb', line 23

def score(m, nick)
  if @scores[nick]
    m.reply "Score for #{nick}: #{@scores[nick]}."
  else
    m.reply "No score for #{nick}."
  end
end

#scores(m) ⇒ Object



18
19
20
# File 'lib/cinch/plugins/yamlscore.rb', line 18

def scores(m)
  m.reply "Scores: #{@scores.sort_by{|k,v| -v }.map{|k,v| "#{k}: #{v}" }*", "}."
end

#update_storeObject



58
59
60
61
62
63
64
# File 'lib/cinch/plugins/yamlscore.rb', line 58

def update_store
  synchronize(:update) do
    File.open('scores.yaml', 'w') do |fh|
      YAML.dump(@scores, fh)
    end
  end
end