4 # :title: A-Z Game Plugin for rbot
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
7 # Author:: Yaohan Chen <yaohan.chen@gmail.com>: Japanese support
9 # Copyright:: (C) 2006 Giuseppe Bilotta
10 # Copyright:: (C) 2007 GIuseppe Bilotta, Yaohan Chen
14 # A-Z Game: guess the word by reducing the interval of allowed ones
16 # TODO allow manual addition of words
20 attr_reader :range, :word
21 attr_reader :lang, :rules, :listener
22 attr_accessor :tries, :total_tries, :total_failed, :failed, :winner
23 def initialize(plugin, lang, rules, word)
28 @range = [@rules[:first].dup, @rules[:last].dup]
29 @listener = @rules[:listener]
31 @total_failed = 0 # not used, reported, updated
33 @failed = Hash.new(0) # not used, not reported, updated
36 return "%s -- %s" % self
42 debug "checking #{w} for #{@word} in #{@range}"
43 # Since we're called threaded, bail out early if a winner
44 # was assigned already
45 return [:ignore, nil] if @winner
46 return [:bingo, nil] if w == @word
47 return [:out, @range] if w < @range.first or w > @range.last
48 return [:ignore, @range] if w == @range.first or w == @range.last
49 # This is potentially slow (for languages that check online)
50 return [:noexist, @range] unless @plugin.send("is_#{@lang}?", w)
52 # Check again if there was a winner in the mean time,
53 # and bail out if there was
54 return [:ignore, nil] if @winner
55 if w < @word and w > @range.first
56 @range.first.replace(w)
58 elsif w > @word and w < @range.last
59 @range.last.replace(w)
65 # TODO scoring: base score is t = ceil(100*exp(-((n-1)^2)/(50^2)))+p for n attempts
66 # done by p players; players that didn't win but contributed
67 # with a attempts will get t*a/n points
73 p = @tries.keys.length
74 t = (100*exp(-((n-1)**2)/(50.0**2))).ceil + p
75 debug "Total score: #{t}"
78 ret[k] = [t*a/n, n_("%{count} try", "%{count} tries", a) % {:count => a}]
81 debug "replacing winner score of %d with %d" % [ret[@winner].first, t]
82 tries = ret[@winner].last
83 ret[@winner] = [t, _("winner, %{tries}") % {:tries => tries}]
85 return ret.sort_by { |h| h.last.first }.reverse
90 class AzGamePlugin < Plugin
94 # if @registry.has_key?(:games)
95 # @games = @registry[:games]
99 if @registry.has_key?(:wordcache) and @registry[:wordcache]
100 @wordcache = @registry[:wordcache]
102 @wordcache = Hash.new
104 debug "A-Z wordcache: #{@wordcache.pretty_inspect}"
108 :good => /s\.f\.|s\.m\.|agg\.|v\.tr\.|v\.(pronom\.)?intr\./, # avv\.|pron\.|cong\.
111 :last => 'zuzzurellone',
112 :url => "http://www.demauroparavia.it/%s",
113 :wapurl => "http://wap.demauroparavia.it/index.php?lemma=%s",
114 :listener => /^[a-z]+$/
117 :good => /(?:singular )?noun|verb|adj/,
120 :url => "http://www.chambersharrap.co.uk/chambers/features/chref/chref.py/main?query=%s&title=21st",
121 :listener => /^[a-z]+$/
125 japanese_wordlist = "#{@bot.botclass}/azgame/wordlist-japanese"
126 if File.exist?(japanese_wordlist)
127 words = File.readlines(japanese_wordlist) \
128 .map {|line| line.strip} .uniq
129 if(words.length >= 4) # something to guess
130 @rules[:japanese] = {
137 debug "Japanese wordlist loaded, #{@rules[:japanese][:list].length} lines; first word: #{@rules[:japanese][:first]}, last word: #{@rules[:japanese][:last]}"
143 # @registry[:games] = @games
144 @registry[:wordcache] = @wordcache
148 return if m.channel.nil? or m.address?
149 k = m.channel.downcase.to_s # to_sym?
150 return unless @games.key?(k)
152 word = m.plugin.downcase
153 return unless word =~ @games[k].listener
154 word_check(m, k, word)
157 def word_check(m, k, word)
158 # Not really safe ... what happens
160 isit = @games[k].check(word)
163 m.reply _("%{bold}BINGO!%{bold} the word was %{underline}%{word}%{underline}. Congrats, %{bold}%{player}%{bold}!") % {:bold => Bold, :underline => Underline, :word => word, :player => m.sourcenick}
164 @games[k].total_tries += 1
165 @games[k].tries[m.source] += 1
166 @games[k].winner = m.source
167 ar = @games[k].score.inject([]) { |res, kv|
168 res.push("%s: %d (%s)" % kv.flatten)
170 m.reply _("The game was won after %{tries} tries. Scores for this game: %{scores}") % {:tries => @games[k].total_tries, :scores => ar.join('; ')}
173 m.reply _("%{word} is not in the range %{bold}%{range}%{bold}") % {:word => word, :bold => Bold, :range => isit.last} if m.address?
175 # bail out early if the game was won in the mean time
176 return if !@games[k] or @games[k].winner
177 m.reply _("%{word} doesn't exist or is not acceptable for the game") % {:word => word}
178 @games[k].total_failed += 1
179 @games[k].failed[m.source] += 1
181 # bail out early if the game was won in the mean time
182 return if !@games[k] or @games[k].winner
183 m.reply _("close, but no cigar. New range: %{bold}%{range}%{bold}") % {:bold => Bold, :range => isit.last}
184 @games[k].total_tries += 1
185 @games[k].tries[m.source] += 1
187 m.reply _("%{word} is already one of the range extrema: %{range}") % {:word => word, :range => isit.last} if m.address?
189 m.reply _("hm, something went wrong while verifying %{word}")
194 def manual_word_check(m, params)
195 k = m.channel.downcase.to_s
196 word = params[:word].downcase
197 if not @games.key?(k)
198 m.reply _("no A-Z game running here, can't check if %{word} is valid, can I?")
202 m.reply _("I only accept single words composed by letters only, sorry")
205 word_check(m, k, word)
208 def stop_game(m, params)
209 return if m.channel.nil? # Shouldn't happen, but you never know
210 k = m.channel.downcase.to_s # to_sym?
212 m.reply _("the word in %{bold}%{range}%{bold} was: %{bold}%{word}%{bold}") % {:bold => Bold, :range => @games[k].range, :word => @games[k].word}
213 ar = @games[k].score.inject([]) { |res, kv|
214 res.push("%s: %d (%s)" % kv.flatten)
216 m.reply _("The game was cancelled after %{tries} tries. Scores for this game would have been: %{scores}") % {:tries => @games[k].total_tries, :scores => ar.join('; ')}
219 m.reply _("no A-Z game running in this channel ...")
223 def start_game(m, params)
224 return if m.channel.nil? # Shouldn't happen, but you never know
225 k = m.channel.downcase.to_s # to_sym?
226 unless @games.key?(k)
227 lang = (params[:lang] || @bot.config['core.language']).to_sym
228 method = 'random_pick_'+lang.to_s
229 m.reply _("let me think ...")
230 if @rules.has_key?(lang) and self.respond_to?(method)
231 word = self.send(method)
233 m.reply _("couldn't think of anything ...")
237 m.reply _("I can't play A-Z in %{lang}, sorry") % {:lang => lang}
241 @games[k] = AzGame.new(self, lang, @rules[lang], word)
243 tr = @games[k].total_tries
244 # this message building code is rewritten to make translation easier
248 f_tr = @games[k].total_failed
250 tr_msg = _(" (after %{total_tries} and %{invalid_tries})") %
251 { :total_tries => n_("%{count} try", "%{count} tries", tr) %
253 :invalid_tries => n_("%{count} invalid try", "%{count} invalid tries", tr) %
256 tr_msg = _(" (after %{total_tries})") %
257 { :total_tries => n_("%{count} try", "%{count} tries", tr) %
262 m.reply _("A-Z: %{bold}%{range}%{bold}") % {:bold => Bold, :range => @games[k].range} + tr_msg
266 def wordlist(m, params)
267 pars = params[:params]
268 lang = (params[:lang] || @bot.config['core.language']).to_sym
269 wc = @wordcache[lang] || Hash.new rescue Hash.new
270 cmd = params[:cmd].to_sym rescue :count
273 m.reply n_("I have %{count} %{lang} word in my cache", "I have %{count} %{lang} words in my cache", wc.size) % {:count => wc.size, :lang => lang}
276 m.reply _("provide a regexp to match")
281 matches = wc.keys.map { |k|
288 m.reply _("no %{lang} word I know match %{pattern}") % {:lang => lang, :pattern => pars[0]}
289 elsif matches.size > 25
290 m.reply _("more than 25 %{lang} words I know match %{pattern}, try a stricter matching") % {:lang => lang, :pattern => pars[0]}
292 m.reply "#{matches.join(', ')}"
296 m.reply _("provide a word")
299 word = pars[0].downcase.to_sym
301 m.reply _("I don't know any %{lang} word %{word}") % {:lang => lang, :word => word}
304 if wc[word].key?(:when)
305 tr = _("%{word} learned from %{user} on %{date}") % {:word => word, :user => wc[word][:who], :date => wc[word][:when]}
307 tr = _("%{word} learned from %{user}") % {:word => word, :user => wc[word][:who]}
312 m.reply _("provide a word")
315 word = pars[0].downcase.to_sym
317 m.reply _("I don't know any %{lang} word %{word}") % {:lang => lang, :word => word}
324 m.reply _("provide a word")
327 word = pars[0].downcase.to_sym
329 m.reply _("I already know the %{lang} word %{word}")
332 wc[word] = { :who => m.sourcenick, :when => Time.now }
338 def is_japanese?(word)
339 @rules[:japanese][:list].include?(word)
342 # return integer between min and max, inclusive
343 def rand_between(min, max)
344 rand(max - min + 1) + min
347 def random_pick_japanese(min=nil, max=nil)
348 rules = @rules[:japanese]
349 min = rules[:first] if min.nil_or_empty?
350 max = rules[:last] if max.nil_or_empty?
351 debug "Randomly picking word between #{min} and #{max}"
352 min_index = rules[:list].index(min)
353 max_index = rules[:list].index(max)
354 debug "Index between #{min_index} and #{max_index}"
355 index = rand_between(min_index + 1, max_index - 1)
356 debug "Index generated: #{index}"
357 word = rules[:list][index]
358 debug "Randomly picked #{word}"
362 def is_italian?(word)
363 unless @wordcache.key?(:italian)
364 @wordcache[:italian] = Hash.new
366 wc = @wordcache[:italian]
367 return true if wc.key?(word.to_sym)
368 rules = @rules[:italian]
369 p = @bot.httputil.get(rules[:wapurl] % word, :open_timeout => 60, :read_timeout => 60)
371 error "could not connect!"
375 p.scan(/<anchor>#{word} - (.*?)<go href="lemma.php\?ID=([^"]*?)"/) { |qual, url|
376 debug "new word #{word} of type #{qual}"
377 if qual =~ rules[:good] and qual !~ rules[:bad]
378 wc[word.to_sym] = {:who => :dict}
386 def random_pick_italian(min=nil,max=nil)
387 # Try to pick a random word between min and max
392 m.reply "#{min} > #{max}"
395 rules = @rules[:italian]
396 min = rules[:first] if min.empty?
397 max = rules[:last] if max.empty?
398 debug "looking for word between #{min.inspect} and #{max.inspect}"
399 return word if min.empty? or max.empty?
401 while (word <= min or word >= max or word !~ /^[a-z]+$/)
402 debug "looking for word between #{min} and #{max} (prev: #{word.inspect})"
403 # TODO for the time being, skip words with extended characters
404 unless @wordcache.key?(:italian)
405 @wordcache[:italian] = Hash.new
407 wc = @wordcache[:italian]
410 cache_or_url = rand(2)
412 debug "getting word from wordcache"
413 word = wc.keys[rand(wc.size)].to_s
418 # TODO when doing ranges, adapt this choice
419 l = ('a'..'z').to_a[rand(26)]
420 debug "getting random word from dictionary, starting with letter #{l}"
421 first = rules[:url] % "lettera_#{l}_0_50"
422 p = @bot.httputil.get(first)
423 max_page = p.match(/ \/ (\d+)<\/label>/)[1].to_i
424 pp = rand(max_page)+1
425 debug "getting random word from dictionary, starting with letter #{l}, page #{pp}"
426 p = @bot.httputil.get(first+"&pagina=#{pp}") if pp > 1
430 # We look for a lemma composed by a single word and of length at least two
431 p.scan(/<li><a href="([^"]+?)" title="consulta il lemma ([^ "][^ "]+?)">.*? (.+?)<\/li>/) { |url, prelemma, tipo|
432 lemma = prelemma.downcase.to_sym
433 debug "checking lemma #{lemma} (#{prelemma}) of type #{tipo} from url #{url}"
434 next if wc.key?(lemma)
438 debug "refusing, #{bad}"
443 wc[lemma] = {:who => :dict}
445 debug "refusing, not #{good}"
448 word = lemmi[rand(lemmi.length)].to_s
451 error "error #{e.inspect} while looking up a word"
452 error e.backtrace.join("\n")
457 def is_english?(word)
458 unless @wordcache.key?(:english)
459 @wordcache[:english] = Hash.new
461 wc = @wordcache[:english]
462 return true if wc.key?(word.to_sym)
463 rules = @rules[:english]
464 p = @bot.httputil.get(rules[:url] % CGI.escape(word))
466 error "could not connect!"
470 if p =~ /<span class="(?:hwd|srch)">#{word}<\/span>([^\n]+?)<span class="psa">#{rules[:good]}<\/span>/i
471 debug "new word #{word}"
472 wc[word.to_sym] = {:who => :dict}
478 def random_pick_english(min=nil,max=nil)
479 # Try to pick a random word between min and max
484 m.reply "#{min} > #{max}"
487 rules = @rules[:english]
488 min = rules[:first] if min.empty?
489 max = rules[:last] if max.empty?
490 debug "looking for word between #{min.inspect} and #{max.inspect}"
491 return word if min.empty? or max.empty?
493 while (word <= min or word >= max or word !~ /^[a-z]+$/)
494 debug "looking for word between #{min} and #{max} (prev: #{word.inspect})"
495 # TODO for the time being, skip words with extended characters
496 unless @wordcache.key?(:english)
497 @wordcache[:english] = Hash.new
499 wc = @wordcache[:english]
502 cache_or_url = rand(2)
504 debug "getting word from wordcache"
505 word = wc.keys[rand(wc.size)].to_s
510 # TODO when doing ranges, adapt this choice
511 l = ('a'..'z').to_a[rand(26)]
512 ll = ('a'..'z').to_a[rand(26)]
513 random = [l,ll].join('*') + '*'
514 debug "getting random word from dictionary, matching #{random}"
515 p = @bot.httputil.get(rules[:url] % CGI.escape(random))
519 # We look for a lemma composed by a single word and of length at least two
520 p.scan(/<span class="(?:hwd|srch)">(.*?)<\/span>([^\n]+?)<span class="psa">#{rules[:good]}<\/span>/i) { |prelemma, discard|
521 lemma = prelemma.downcase
522 debug "checking lemma #{lemma} (#{prelemma}) and discarding #{discard}"
523 next if wc.key?(lemma.to_sym)
524 if lemma =~ /^[a-z]+$/
527 wc[lemma.to_sym] = {:who => :dict}
529 debug "funky characters, not good"
533 word = lemmi[rand(lemmi.length)]
536 error "error #{e.inspect} while looking up a word"
537 error e.backtrace.join("\n")
542 def help(plugin, topic="")
545 return _("az [lang] word [count|list|add|delete] => manage the az wordlist for language lang (defaults to current bot language)")
547 return _("az cancel => abort current game")
549 return _('az check <word> => checks <word> against current game')
551 return _("try to guess the word the bot is thinking of; if you guess wrong, the bot will use the new word to restrict the range of allowed words: eventually, the range will be so small around the correct word that you can't miss it")
553 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")
555 return _("az topics: play, rules, cancel, manage, check")
560 plugin = AzGamePlugin.new
561 plugin.map 'az [:lang] word :cmd *params', :action=>'wordlist', :defaults => { :lang => nil, :cmd => 'count', :params => [] }, :auth_path => '!az::edit!'
562 plugin.map 'az cancel', :action=>'stop_game', :private => false
563 plugin.map 'az check :word', :action => 'manual_word_check', :private => false
564 plugin.map 'az [play] [:lang]', :action=>'start_game', :private => false, :defaults => { :lang => nil }