#!/usr/bin/env ruby # encoding: utf-8 require 'rubygems' require 'dragongoserver' require 'choice' version = Dragongoserver::VERSION Choice.options do # header '' # header 'Specific options:' #... #separator '' separator 'Common options: ' option :help do short '-h' long '--help' desc 'Show this message' end option :version do short '-v' long '--version' desc 'Show version' action do puts "dgs #{version}" exit end end option :waiting do short '-w' long '--waiting' desc 'Show waiting games' end option :waitingloop do short '-wl' long '--waiting-loop' desc 'Show waiting games in an intelligent endless loop' end option :running do short '-r' long '--running' desc 'Show running games' end option :osd do short '-osd' long '--osd' desc 'Show messages as an on-screen-display, "osd_cat" must be installed (see http://www.ignavus.net/software.html for further details)' end option :finished_games do short '-fg' long '--finished-games' desc 'display the most recent finished games' #cast Integer #default 10 end # listet auf, wie oft ein Spieler in den beendeten Spielen # durch Timeout verloren hat option :check_timeout_finished do short '-ctf' long '--check-timeout-finished' cast Integer # User-id end option :with_name do short '-wn' long '--with-name' desc 'Show games with name' end option :min_wait do short '-mw' long '--min-wait' desc 'Minimum wait time in minutes' cast Integer default 1 end option :threshold do short '-s' long '--threshold' desc 'Minimum number of waiting games' cast Integer default 1 end option :add_game do short '-a' long '--add-game' desc 'Add a new game in the waiting room' end end if Choice.choices['check_timeout_finished'] puts "timeout check for user #{Choice.choices['check_timeout_finished']}" dgs_username = ENV['DGS_USERNAME'] dgs_password = ENV['DGS_PASSWORD'] @dgs = Dgs.new(dgs_username, dgs_password) finished = @dgs.finished_games_for_user(Choice.choices['check_timeout_finished']) count = 0 finished.each {|g| if g[:reason] == 'Time' and g[:winner] != g[:player_color] #p g[:winner] # #g[:winner] != g[:player_color] count += 1 end } puts "at least #{finished.size} finished games" puts "#{count} games finished with timeout (#{count.to_f/finished.size.to_f*100.0} %)" exit end @cache_gameinfos = {} #p Choice.choices opt_waiting = Choice.choices['waiting'] # true/false opt_running = Choice.choices['running'] # true/false opt_finished_games = Choice.choices['finished_games'] # true/false $opt_with_name = Choice.choices['with_name'] # true/false @opt_osd = Choice.choices['osd'] # true/false opt_waitingloop = Choice.choices['waitingloop'] # true/false opt_add_game = Choice.choices['add_game'] # true/false if opt_waiting dgs = Dgs.new waiting_games = dgs.waiting_games puts "you have to move in the following games:" if waiting_games.size > 0 puts "goto: http://www.dragongoserver.net/status.php" if waiting_games.size > 0 waiting_games.each { |game_id| puts game_id } end if opt_running dgs = Dgs.new running_games = dgs.running_games if running_games.size > 0 puts "you have a total of #{running_games.size} running games" puts "running games:" running_games.each { |game_id| puts game_id } else puts "you have no running games" end end if opt_finished_games =begin 393488 sgf maggie b 19 4 0.5 213 B+60.5 Yes Yes 2008-04-23 22:48 =end dgs = Dgs.new finished_games = dgs.finished_games if finished_games.size > 0 puts "you have a total of #{finished_games.size} finished games" puts "finished games:" finished_games.each { |game| puts "#{game[:id]} #{game[:finished_at]}" } else puts "you have no finished games" end end def plural(n) if n == 1 "" else "s" end end def new_moves?(dgs) begin w = dgs.waiting_games rescue Timeout::Error puts "timeout error. retrying in 30 secs" sleep 30 retry end if $opt_with_name print '[' w.each {|game| print "#{game} " # ggf. aus Cache holen, anstatt jedesmal neu von DGS ermitteln, # da sich die Namen der Spieler in der Regel nicht waehrend der # Laufzeit dieses Skripts aendern gi=cache_gameinfo(game) if gi print "[c] " end unless gi begin gi = dgs.gameinfo(game) rescue Timeout::Error puts "timeout error. retrying in 30 secs" sleep 30 retry end @cache_gameinfos[game] = gi end print gi[:white][:name]+' ' unless gi[:white][:name].include? "(#{dgs.username})" print gi[:black][:name]+',' unless gi[:black][:name].include? "(#{dgs.username})" puts } puts ']' else p w end #if dgs.waiting_games.size > 0 # @page.body.include?(NO_GAMES) if w.size > 0 # @page.body.include?(NO_GAMES) if $threshold if w.size < $threshold.to_i return false else return true end else return true end else return false end end def dringende_zuege(dgs=nil) false # TODO: end $nur_dringende = false # TODO: $min_secs = Choice.choices[:min_wait].to_i*60 #60*15 $secs = $min_secs $count = 0 $threshold = Choice.choices[:threshold].to_i def osd_output? return @opt_osd end def puts_x(arg) puts arg if osd_output? $osd.puts(arg) $osd.flush end end def cache_gameinfo(game) @cache_gameinfos[game] end def schleife dgs = Dgs.new $min_secs = Choice.choices[:min_wait].to_i*60 #60*15 $dringend = false changes = false if $nur_dringende if dringende_zuege?(dgs) $dringend = true changes = true else changes = false end elsif new_moves? dgs changes = true else puts "no waiting moves" end if changes $count = 0 puts "waiting games" $secs = $secs / 2 if $secs < $min_secs $secs = $min_secs end puts "decreasing request interval to #{$secs} seconds" puts "waiting games" if $dringend $osd.puts("DRINGENDE Spiele") end puts_x("Your opponents at Dragon Go Server are waiting for your moves") else $count += 1 if $count >= 3 $secs = $secs*2 # puts "erhoehe Abfrageintervall auf #{$secs} Sekunden(n)" puts "increasing request interval to #{$secs} seconds" $count = 0 end end end if opt_waitingloop while 1 # $osd = IO.popen("osd_cat -p middle -f -adobe-helvetica-*-*-*-*-24-*-*-*- *-*-*-* -s 3 -S lightgray --delay=60","w+") if osd_output? $osd = IO.popen("osd_cat -p middle -f -sony-fixed-medium-*-normal-*-24-*-*-*-*-*-*-* -s 3 -S lightgray --delay=60","w+") end begin schleife rescue SocketError puts "SocketError - waiting 60 secs and retrying" sleep 60 retry end if osd_output? $osd.close_write $osd.close end # puts "warte #{$secs} Sekunden (#{$secs/60} Minuten)" puts "waiting #{$secs} seconds (#{$secs/60} minute#{plural($secs/60)})" sleep($secs) end end if opt_add_game dgs = Dgs.new dgs.add_game puts "1 Game(s) added to the waiting room" end