Skip to content

Commit

Permalink
Added Karma persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
gisikw committed Dec 15, 2011
1 parent 1ac4f4a commit cae7750
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
10 changes: 10 additions & 0 deletions Readme.rdoc
Expand Up @@ -11,6 +11,16 @@ It runs with straight Ruby. You'll need the Cinch gem and Ruby 1.9, and you'll w
nick: my_bot
channel: ecruby

If you plan to use the Karma plugin, you'll need a bit more:

bot.yml
settings:
nick: my_bot
channel: ecruby
persistence_url: ecruby.org
username: some_user
password: sekrit

Then just run it with

ruby chatbot.rb
Expand Down
6 changes: 3 additions & 3 deletions chatbot.rb
Expand Up @@ -2,7 +2,7 @@
require 'cinch'
require 'yaml'
begin
settings = YAML.load(File.read("bot.yml"))
$settings = YAML.load(File.read("bot.yml"))
rescue
puts "create bot.yml and populate it with values. See the readme file!"
end
Expand All @@ -16,8 +16,8 @@

configure do |c|
c.server = "irc.freenode.org"
c.nick = settings["settings"]["nick"]
c.channels = [settings["settings"]["channel"]]
c.nick = $settings["settings"]["nick"]
c.channels = [$settings["settings"]["channel"]]
c.plugins.plugins = [Karma, LinkCatcher]
end

Expand Down
33 changes: 21 additions & 12 deletions plugins/karma.rb
@@ -1,9 +1,14 @@
require 'open-uri'
require 'net/http'
require 'json'
require 'uri'

class Karma
include Cinch::Plugin

def initialize(*args)
super
@karma_points = {}
@karma_points = JSON.parse open("http://#{$settings['settings']['persistence_url']}/scoreboard").read
end

$help_messages << "!props <nick> Give props"
Expand Down Expand Up @@ -77,29 +82,33 @@ def points(m)
end

def scoreboard(m)
@karma_points.each do |key, val|
m.reply points_for(key)
@karma_points.each do |j|
m.reply points_for(j['nick'])
end
end

# ****************************
def record_for(nick)
@karma_points.select{|j|j['nick']==nick}[0] || (@karma_points << {'nick' => nick, 'points' => 0}).last
end

def reduce_points(nick, number)
default(nick)
@karma_points[nick] -= number
record_for(nick)['points'] -= number
post_points(nick)
end

def add_points(nick, number)
default(nick)
@karma_points[nick] += number
record_for(nick)['points'] += number
post_points(nick)
end

def default(nick)
@karma_points[nick] ||= 0
def post_points(nick)
uri = URI.parse("http://#{$settings['settings']['username']}:#{$settings['settings']['password']}@#{$settings['settings']['persistence_url']}/#{nick}")
Net::HTTP.post_form(uri,{'points' => record_for(nick)['points']})
end

def points_for(nick)
default(nick)
"#{nick} has #{@karma_points[nick]} points"
"#{nick} has #{record_for(nick)['points']} points"
end

end
Expand Down

0 comments on commit cae7750

Please sign in to comment.