Xmotoctl: Unterschied zwischen den Versionen

Aus C3D2
Zur Navigation springen Zur Suche springen
K (→‎Usage: Dateinamen lügen)
(This page must use the Rübÿ Spëëd Mëtäl Cödïng template!!!111)
Zeile 1: Zeile 1:
[[Category:Ruby]]
[[Category:Ruby]]
=xmotoctl.rb=
=xmotoctl.rb=
Dient dem Anzeigen und Validieren aller pending Highscores in einem Privaten Raum. Benötigt [[Ruby-MediaWiki]].
Dient dem Anzeigen und Validieren aller pending Highscores in einem Privaten Raum. Benötigt [[Ruby-MediaWiki]].
==Usage==
==Usage==
Für Replay 11154 wurde vorher ein schnelleres Replay im selben Level validiert, weshalb man dieses nicht mehr validieren kann. xmotoctl.rb kann dies ohne Hilfe eines neuronalen Netzes erkennen. ;-)
Für Replay 11154 wurde vorher ein schnelleres Replay im selben Level validiert, weshalb man dieses nicht mehr validieren kann. xmotoctl.rb kann dies ohne Hilfe eines neuronalen Netzes erkennen. ;-)
<pre>% ruby xmotoctl.rb 31 Astro *********************** validateall
<pre>% ruby xmotoctl.rb 31 Astro *********************** validateall
Performing POST request
Performing POST request
Zeile 20: Zeile 15:
Error: No validate action available
Error: No validate action available
</pre>
</pre>
==Source==
==Source==
<pre>#!/usr/bin/env ruby
<pre>#!/usr/bin/env ruby
$: << 'ruby-mediawiki/lib/'
$: << 'ruby-mediawiki/lib/'
require 'mediawiki/minibrowser'
require 'mediawiki/minibrowser'
require 'htree'
require 'htree'
require 'rexml/document'
require 'rexml/document'
class REXML::Element
class REXML::Element
   def first_element(s)
   def first_element(s)
Zeile 36: Zeile 27:
   end
   end
end
end
class HTTPDocument
class HTTPDocument
   def initialize(url, browser=MediaWiki::MiniBrowser.new(URI::parse(url)))
   def initialize(url, browser=MediaWiki::MiniBrowser.new(URI::parse(url)))
Zeile 42: Zeile 32:
     @browser = browser
     @browser = browser
   end
   end
   def perform_request
   def perform_request
     puts "Performing GET request"
     puts "Performing GET request"
     @body = @browser.get_content(@url)
     @body = @browser.get_content(@url)
   end
   end
   def body
   def body
     perform_request unless @body
     perform_request unless @body
     @body
     @body
   end
   end
   def html
   def html
     @html = HTree(body).to_rexml unless @html
     @html = HTree(body).to_rexml unless @html
Zeile 58: Zeile 45:
   end
   end
end
end
class HTTPPostDocument < HTTPDocument
class HTTPPostDocument < HTTPDocument
   def initialize(url, params)
   def initialize(url, params)
Zeile 64: Zeile 50:
     super(url)
     super(url)
   end
   end
   def perform_request
   def perform_request
     puts "Performing POST request"
     puts "Performing POST request"
Zeile 70: Zeile 55:
   end
   end
end
end
class Login < HTTPPostDocument
class Login < HTTPPostDocument
   def initialize(id, username, password)
   def initialize(id, username, password)
     super("http://xmoto.free.fr/index.php", {:page=>:rooms, :action=>:change, :id_room=>id, :login=>username, :password=>password})
     super("http://xmoto.free.fr/index.php", {:page=>:rooms, :action=>:change, :id_room=>id, :login=>username, :password=>password})
     error = nil
     error = nil
     html.each_element('//div[@class=\'message_erreur\']') { |div|
     html.each_element('//div[@class=\'message_erreur\']') { |div|
Zeile 81: Zeile 64:
     raise error if error
     raise error if error
   end
   end
   def highscoresvalidation
   def highscoresvalidation
     HighscoresValidation.new(@browser)
     HighscoresValidation.new(@browser)
   end
   end
end
end
class HighscoresValidation < HTTPDocument
class HighscoresValidation < HTTPDocument
   def initialize(browser)
   def initialize(browser)
     super("http://xmoto.free.fr/index.php?page=highscoresvalidation", browser)
     super("http://xmoto.free.fr/index.php?page=highscoresvalidation", browser)
   end
   end
   def table
   def table
     html.first_element('//table[@class=\'admin_data\']')
     html.first_element('//table[@class=\'admin_data\']')
   end
   end
   def get_columns
   def get_columns
     res = []
     res = []
Zeile 103: Zeile 82:
     res
     res
   end
   end
   def columns
   def columns
     @columns = get_columns unless @columns
     @columns = get_columns unless @columns
     @columns
     @columns
   end
   end
   def get_highscores
   def get_highscores
     res = []
     res = []
     table.each_element('tr[starts-with(@class, \'admin_data_line\')]') { |tr|
     table.each_element('tr[starts-with(@class, \'admin_data_line\')]') { |tr|
       i = 0
       i = 0
Zeile 119: Zeile 95:
         i += 1
         i += 1
       }
       }
       res << row
       res << row
     }
     }
     res
     res
   end
   end
   def highscores
   def highscores
     @highscores = get_highscores unless @highscores
     @highscores = get_highscores unless @highscores
     @highscores
     @highscores
   end
   end
   def dump_highscores
   def dump_highscores
     column_widths = {}
     column_widths = {}
Zeile 140: Zeile 112:
       column_widths[column] = width
       column_widths[column] = width
     end
     end
     puts columns.collect { |c|
     puts columns.collect { |c|
       c.center(column_widths[c])
       c.center(column_widths[c])
Zeile 154: Zeile 125:
     end
     end
   end
   end
   def highscore_actions(id)
   def highscore_actions(id)
     res = nil
     res = nil
Zeile 164: Zeile 134:
       }
       }
     }
     }
     res
     res
   end
   end
   def validate(id)
   def validate(id)
     actions = highscore_actions(id)
     actions = highscore_actions(id)
Zeile 177: Zeile 145:
   end
   end
end
end
if ARGV.size < 3
if ARGV.size < 3
   puts "Usage: #{$0} <room id> <username> <password> [validateall]"
   puts "Usage: #{$0} <room id> <username> <password> [validateall]"
   exit!
   exit!
end
end
room_id, username, password = ARGV.shift, ARGV.shift, ARGV.shift
room_id, username, password = ARGV.shift, ARGV.shift, ARGV.shift
l = Login.new(room_id, username, password)
l = Login.new(room_id, username, password)
h = l.highscoresvalidation
h = l.highscoresvalidation
h.dump_highscores
h.dump_highscores
if ARGV.first == 'validateall'
if ARGV.first == 'validateall'
   h.highscores.each { |h1|
   h.highscores.each { |h1|
Zeile 202: Zeile 165:
end
end
</pre>
</pre>
{{Rübÿ Spëëd Mëtäl Cödïng}}

Version vom 22. Oktober 2006, 19:24 Uhr

xmotoctl.rb

Dient dem Anzeigen und Validieren aller pending Highscores in einem Privaten Raum. Benötigt Ruby-MediaWiki.

Usage

Für Replay 11154 wurde vorher ein schnelleres Replay im selben Level validiert, weshalb man dieses nicht mehr validieren kann. xmotoctl.rb kann dies ohne Hilfe eines neuronalen Netzes erkennen. ;-)

% ruby xmotoctl.rb 31 Astro *********************** validateall
Performing POST request
Performing GET request
 Id  |      Level      |  Driver   | Time  |Current Best Time|Current Best Driver|     Date      |Somersaults|Replay| 
-----+-----------------+-----------+-------+-----------------+-------------------+---------------+-----------+------+------
11251|Corea1           |Waldmeister|0:23:03|0:24:04          |Sven               |22 october 2006|0          |      |      
11154|Joshua.wad: Round|Astro      |0:18:18|0:06:35          |Waldmeister        |21 october 2006|4          |      |      
Validating 11251...
Validating 11154...
Error: No validate action available

Source

#!/usr/bin/env ruby
$: << 'ruby-mediawiki/lib/'
require 'mediawiki/minibrowser'
require 'htree'
require 'rexml/document'
class REXML::Element
  def first_element(s)
    each_element(s) { |e| return e }
    nil
  end
end
class HTTPDocument
  def initialize(url, browser=MediaWiki::MiniBrowser.new(URI::parse(url)))
    @url = url
    @browser = browser
  end
  def perform_request
    puts "Performing GET request"
    @body = @browser.get_content(@url)
  end
  def body
    perform_request unless @body
    @body
  end
  def html
    @html = HTree(body).to_rexml unless @html
    @html
  end
end
class HTTPPostDocument < HTTPDocument
  def initialize(url, params)
    @params = params
    super(url)
  end
  def perform_request
    puts "Performing POST request"
    @body = @browser.post_content(@url, @params)
  end
end
class Login < HTTPPostDocument
  def initialize(id, username, password)
    super("http://xmoto.free.fr/index.php", {:page=>:rooms, :action=>:change, :id_room=>id, :login=>username, :password=>password})
    error = nil
    html.each_element('//div[@class=\'message_erreur\']') { |div|
      error = div.text
    }
    raise error if error
  end
  def highscoresvalidation
    HighscoresValidation.new(@browser)
  end
end
class HighscoresValidation < HTTPDocument
  def initialize(browser)
    super("http://xmoto.free.fr/index.php?page=highscoresvalidation", browser)
  end
  def table
    html.first_element('//table[@class=\'admin_data\']')
  end
  def get_columns
    res = []
    table.each_element('tr[1]/th') { |th|
      res << th.text
    }
    res
  end
  def columns
    @columns = get_columns unless @columns
    @columns
  end
  def get_highscores
    res = []
    table.each_element('tr[starts-with(@class, \'admin_data_line\')]') { |tr|
      i = 0
      row = {}
      tr.each_element('td[@class=\'admin_data_multiple\']') { |td|
        row[columns[i]] = td.text.strip if td.text
        i += 1
      }
      res << row
    }
    res
  end
  def highscores
    @highscores = get_highscores unless @highscores
    @highscores
  end
  def dump_highscores
    column_widths = {}
    columns.each do |column|
      width = column.size
      highscores.each { |h|
        width = h[column].size if (h[column]||'').size > width
      }
      column_widths[column] = width
    end
    puts columns.collect { |c|
      c.center(column_widths[c])
    }.join('|')
    puts columns.collect { |c|
      '-' * column_widths[c]
    }.join('+')
    highscores.each do |h|
      puts columns.collect { |c|
        value = h[c] || ''
        value.ljust(column_widths[c])
      }.join('|')
    end
  end
  def highscore_actions(id)
    res = nil
    # TODO: Replace contains()
    table.each_element('tr[starts-with(@class, \'admin_data_line\') and contains(td[1], '+id+')]') { |tr|
      res = {}
      tr.each_element('td[@class=\'admin_data_multiple\']/ul[@class=\'admin_submenu_actions\']/li/a') { |a|
        res[a.attributes['title']] = a.attributes['href']
      }
    }
    res
  end
  def validate(id)
    actions = highscore_actions(id)
    if actions['validate']
      @browser.get_content('/' + actions['validate'])
    else
      raise 'No validate action available'
    end
  end
end
if ARGV.size < 3
  puts "Usage: #{$0} <room id> <username> <password> [validateall]"
  exit!
end
room_id, username, password = ARGV.shift, ARGV.shift, ARGV.shift
l = Login.new(room_id, username, password)
h = l.highscoresvalidation
h.dump_highscores
if ARGV.first == 'validateall'
  h.highscores.each { |h1|
    id = h1['Id']
    puts "Validating #{id}..."
    begin
      h.validate(id)
    rescue Exception => e
      puts "Error: #{e}"
    end
  }
end


Rübÿ Spëëd Mëtäl Cödïng
Coders: Astro | Conny | Sven