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.



11
12
13
14
15
16
17
18
# File 'lib/cinch/plugins/yamlscore.rb', line 11

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cinch/plugins/yamlscore.rb', line 36

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
  else
    m.reply "User #{nick} is not in the channel, who do you want to score?"
  end
end

#score(m, nick) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/cinch/plugins/yamlscore.rb', line 26

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



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

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

#update_storeObject



53
54
55
56
57
58
59
# File 'lib/cinch/plugins/yamlscore.rb', line 53

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