return "%s -- %s" % self
end
if @rules[:list]
- @check = Proc.new { |w| @rules[:list].include?(w) }
+ @check_method = "is_#{@rules[:addlang]}?"
+ # trick: if addlang was not in rules, this will be is_? which is
+ # not a method of the plugin
+ if @check_method and not @plugin.respond_to? @check_method
+ @check_method = nil
+ end
+ @check = Proc.new do |w|
+ wl = @rules[:list].include?(w)
+ if !wl and @check_method
+ if wl = @plugin.send(@check_method, w)
+ debug "adding #{w} to #{@rules[:addfile]}"
+ begin
+ File.open(@rules[:addfile], "a") do |f|
+ f.puts w
+ end
+ rescue Exception => e
+ error "failed to add #{w} to #{@rules[:addfile]}"
+ error e
+ end
+ end
+ end
+ wl
+ end
else
@check_method = "is_#{@lang}?"
@check = Proc.new { |w| @plugin.send(@check_method, w) }
:good => /(?:singular )?noun|verb|adj/,
:first => 'abacus',
:last => 'zuni',
- :url => "http://www.chambersharrap.co.uk/chambers/features/chref/chref.py/main?query=%s&title=21st",
+ :url => "http://www.chambers.co.uk/search.php?query=%s&title=21st",
:listener => /^[a-z]+$/
},
}
- @wordlist_base = "#{@bot.botclass}/azgame/wordlist-"
+ @autoadd_base = datafile "autoadd-"
end
- def initialize_wordlist(lang)
- wordlist = @wordlist_base + lang.to_s
- if File.exist?(wordlist)
+ def initialize_wordlist(params)
+ lang = params[:lang]
+ addlang = params[:addlang]
+ autoadd = @autoadd_base + addlang.to_s
+ if Wordlist.exist?(lang)
# wordlists are assumed to be UTF-8, but we need to strip the BOM, if present
- words = File.readlines(wordlist).map {|line| line.sub("\xef\xbb\xbf",'').strip}.uniq.sort
+ words = Wordlist.get(lang)
+ if addlang and File.exist?(autoadd)
+ word += File.readlines(autoadd).map {|line| line.sub("\xef\xbb\xbf",'').strip}
+ end
+ words.uniq!
+ words.sort!
if(words.length >= 4) # something to guess
rules = {
:good => /^\S+$/,
:list => words,
:first => words[0],
:last => words[-1],
+ :addlang => addlang,
+ :addfile => autoadd,
:listener => /^\S+$/
}
debug "#{lang} wordlist loaded, #{rules[:list].length} lines; first word: #{rules[:first]}, last word: #{rules[:last]}"
end
m.reply _("got it!")
@games[k] = AzGame.new(self, lang, @rules[lang], word)
- elsif !@rules.has_key?(lang) and rules = initialize_wordlist(lang)
+ elsif !@rules.has_key?(lang) and rules = initialize_wordlist(params)
word = random_pick_wordlist(rules)
if word.empty?
m.reply _("couldn't think of anything ...")
if wc[word].key?(:when)
tr = _("%{word} learned from %{user} on %{date}") % {:word => word, :user => wc[word][:who], :date => wc[word][:when]}
else
- tr = _("%{word} learned from %{user}") % {:word => word, :user => wc[word][:who]}
+ tr = _("%{word} learned from %{user}") % {:word => word, :user => wc[word][:who]}
end
m.reply tr
- when :delete
+ when :delete
if pars.empty?
m.reply _("provide a word")
return
debug "getting random word from dictionary, matching #{random}"
p = @bot.httputil.get(rules[:url] % CGI.escape(random))
debug p
+ raise 'unable to get search results' if not p.match /id="fullsearchresults"/i
lemmi = Array.new
good = rules[:good]
# We look for a lemma composed by a single word and of length at least two
when 'play'
return _("az => start a game if none is running, show the current word range otherwise; you can say 'az <language>' if you want to play in a language different from the current bot default")
end
- offset = @wordlist_base.length
langs = @rules.keys
- wls = Dir.glob(@wordlist_base + "*").map { |f| f[offset,f.length].intern rescue nil }.compact - langs
+ wls = Wordlist.list
return [
_("az topics: play, rules, cancel, manage, check"),
_("available languages: %{langs}") % { :langs => langs.join(", ") },
plugin.map 'az [:lang] word :cmd *params', :action=>'wordlist', :defaults => { :lang => nil, :cmd => 'count', :params => [] }, :auth_path => '!az::edit!'
plugin.map 'az cancel', :action=>'stop_game', :private => false
plugin.map 'az check :word', :action => 'manual_word_check', :private => false
-plugin.map 'az [play] [:lang]', :action=>'start_game', :private => false, :defaults => { :lang => nil }
+plugin.map 'az [play] [:lang] [autoadd :addlang]', :action=>'start_game', :private => false, :defaults => { :lang => nil, :addlang => nil }