root/lang/ruby/chokan/branches/citrus/plugins/plusplus.rb @ 6454

Revision 6454, 2.7 kB (checked in by cho45, 5 years ago)

lang/ruby/chokan/branches/citrus/plugins/plusplus.rb:

\w がマルチバイトにマッチするように修正

Line 
1
2require "yaml"
3
4class Plusplus < Citrus::Plugin
5
6        def initialize(config, chokan)
7                super
8                @datafile = datafile(@config["data"] || "plusplus.yaml")
9                @least    = @config['least'] || 1
10                @excludes = @config['excludes'] || []
11
12                unless @datafile.exist?
13                        @datafile.open("w") {|f| YAML.dump({}, f) }
14                end
15        end
16
17        def on_privmsg(prefix, channel, message)
18                return unless @channels.nil? || @channels.include?(channel)
19                case message
20                when /karma for (\S+)/
21                        nick = Regexp.last_match[1]
22                        notice_karma(channel, nick)
23
24                when /\(([^)]{#{@least},})\)(\+\+|--)/, /(\w{#{@least},})(\+\+|--)/u
25                        nick, dir = Regexp.last_match.captures
26                        return if @excludes.include?(nick)
27
28                        @datafile.open("r+") do |f|
29                                data = YAML.load(f)
30                                (data[nick] ||= { "++" => 0, "--" => 0 })[dir] += 1
31                                f.rewind
32                                YAML.dump(data, f)
33                                f.truncate(f.tell)
34                        end
35
36                        notice_karma(channel, nick)
37                end
38        end
39
40        def notice_karma(channel, nick)
41                plus, minus = karma_for(nick)
42                if plus
43                        karma = plus - minus
44                        notice(channel, "#{nick}: #{karma} (#{plus}++ #{minus}--)")
45                else
46                        notice(channel, "don't know #{nick}")
47                end
48        end
49
50        def karma_for(nick)
51                data = @datafile.open {|f| YAML.load(f) }
52                if data[nick]
53                        plus  = data[nick]["++"]
54                        minus = data[nick]["--"]
55                        [plus, minus]
56                else
57                        nil
58                end
59        end
60end
61
62tests do
63
64        describe Plusplus do
65                before :all do
66                        @core   = DummyCore.new({})
67                        @socket = @core.socket
68                        @prefix = Net::IRC::Prefix.new("foo!foo@localhsot")
69
70                        @plugin = Plusplus.new(@core, { "Plusplus" => {
71                        } })
72                end
73
74                it "should reply correctly" do
75                        @socket.string = ""
76                        @plugin.on_privmsg(@prefix, "#test", "foo++")
77                        @socket.string.should == "NOTICE #test :foo: 1 (1++ 0--)\r\n"
78
79                        @socket.string = ""
80                        @plugin.on_privmsg(@prefix, "#test", "foo++")
81                        @socket.string.should == "NOTICE #test :foo: 2 (2++ 0--)\r\n"
82
83                        @socket.string = ""
84                        @plugin.on_privmsg(@prefix, "#test", "foo--")
85                        @socket.string.should == "NOTICE #test :foo: 1 (2++ 1--)\r\n"
86
87                        @socket.string = ""
88                        @plugin.on_privmsg(@prefix, "#test", "karma for foo")
89                        @socket.string.should == "NOTICE #test :foo: 1 (2++ 1--)\r\n"
90
91                        @socket.string = ""
92                        @plugin.on_privmsg(@prefix, "#test", "(C++)++")
93                        @socket.string.should == "NOTICE #test :C++: 1 (1++ 0--)\r\n"
94
95                        @socket.string = ""
96                        @plugin.on_privmsg(@prefix, "#test", "karma for C++")
97                        @socket.string.should == "NOTICE #test :C++: 1 (1++ 0--)\r\n"
98
99                        @socket.string = ""
100                        @plugin.on_privmsg(@prefix, "#test", "karma for unk")
101                        @socket.string.should == "NOTICE #test :don't know unk\r\n"
102
103                        @socket.string = ""
104                        @plugin.on_privmsg(@prefix, "#test", "文乃さん++")
105                        @socket.string.should == "NOTICE #test :文乃さん: 1 (1++ 0--)\r\n"
106
107                end
108        end
109
110end
Note: See TracBrowser for help on using the browser.