dragongoserver 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,4 +1,9 @@
1
- == 0.0.1 2007-08-28
2
-
1
+ == 0.0.3 2008-01-01
2
+ * 1 enhancement:
3
+ * new option -ml/--missing-loop in dgs script
4
+ == 0.0.2 2007-12-29
5
+ * 1 bug-fix:
6
+ * missing file dragongoserver/dragongoserver.rb fixed
7
+ == 0.0.1 2007-12-29
3
8
  * 1 major enhancement:
4
9
  * Initial release
data/Manifest.txt CHANGED
@@ -7,6 +7,7 @@ bin/dgs
7
7
  config/hoe.rb
8
8
  config/requirements.rb
9
9
  lib/dragongoserver.rb
10
+ lib/dragongoserver/dragongoserver.rb
10
11
  lib/dragongoserver/version.rb
11
12
  log/debug.log
12
13
  script/destroy
data/bin/dgs CHANGED
@@ -1,12 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'rubygems'
4
- require 'lib/dragongoserver/version'
5
- require 'lib/dragongoserver/dragongoserver'
4
+ require 'dragongoserver'
6
5
  require 'choice'
7
6
 
8
7
  version = Dragongoserver::VERSION::STRING
9
- #oputs "dgs 0.1.0"
10
8
  Choice.options do
11
9
  header ''
12
10
  header 'Specific options:'
@@ -36,10 +34,24 @@ Choice.options do
36
34
  desc 'Show waiting games'
37
35
  end
38
36
 
37
+ option :waitingloop do
38
+ short '-wl'
39
+ long '--waiting-loop'
40
+ desc 'Show waiting games in an intelligent endless loop'
41
+ end
42
+
43
+ option :running do
44
+ short '-r'
45
+ long '--running'
46
+ desc 'Show running games'
47
+ end
48
+
39
49
  end
40
50
 
41
51
  #p Choice.choices
42
52
  opt_waiting = Choice.choices['waiting'] # true/false
53
+ opt_running = Choice.choices['running'] # true/false
54
+ opt_waitingloop = Choice.choices['waitingloop'] # true/false
43
55
  if opt_waiting
44
56
  dgs = Dgs.new
45
57
  waiting_games = dgs.waiting_games
@@ -48,3 +60,106 @@ if opt_waiting
48
60
  puts game_id
49
61
  }
50
62
  end
63
+ if opt_running
64
+ dgs = Dgs.new
65
+ running_games = dgs.running_games
66
+ if running_games.size > 0
67
+ puts "you have a total of #{running_games.size} running games"
68
+ puts "running games:"
69
+ running_games.each { |game_id|
70
+ puts game_id
71
+ }
72
+ else
73
+ puts "you have no running games"
74
+ end
75
+ end
76
+ def plural(n)
77
+ if n == 1
78
+ ""
79
+ else
80
+ "s"
81
+ end
82
+ end
83
+ def neue_zuege?(dgs)
84
+
85
+ w = dgs.waiting_games
86
+ p w
87
+ #if dgs.waiting_games.size > 0 # @page.body.include?(NO_GAMES)
88
+ if w.size > 0 # @page.body.include?(NO_GAMES)
89
+ true
90
+ else
91
+ false
92
+ end
93
+ end
94
+
95
+ def dringende_zuege(dgs=nil)
96
+ false # TODO:
97
+ end
98
+
99
+ $nur_dringende = false # TODO:
100
+ $min_secs = 60*1
101
+ $secs = $min_secs
102
+ $count = 0
103
+
104
+ def schleife
105
+ dgs = Dgs.new
106
+ # dgs.login
107
+ #page = dgs.page
108
+ $dringend = false
109
+ changes = false
110
+ if $nur_dringende
111
+ if dringende_zuege?(dgs)
112
+ $dringend = true
113
+ changes = true
114
+ else
115
+ changes = false
116
+ end
117
+ elsif neue_zuege? dgs
118
+ changes = true
119
+ else
120
+ # puts "keine neuen Zuege"
121
+ puts "no waiting moves"
122
+ #
123
+ end
124
+ if changes
125
+ $count = 0
126
+ # puts "neue Zuege, neue Methode"
127
+ puts "waiting games"
128
+ $secs = $secs / 2
129
+ if $secs < $min_secs
130
+ $secs = $min_secs
131
+ end
132
+ # puts "vermindere Abfrageintervall auf #{$secs} Sekunden(n)"
133
+ puts "decreasing request interval to #{$secs} seconds"
134
+ # puts "neue Zuege"
135
+ puts "waiting games"
136
+ if $dringend
137
+ $osd.puts("DRINGENDE Spiele")
138
+ end
139
+ # puts("Deine Mitspieler bei Dragon Go Server warten auf deine Spielzuege")
140
+ puts("Your opponents at Dragon Go Server are waiting for your moves")
141
+ #-- $osd.puts("Deine Mitspieler bei Dragon Go Server warten auf deine Spielzuege")
142
+ #-- $osd.flush
143
+ else
144
+ $count+=1
145
+ if $count >= 3
146
+ $secs = $secs*2
147
+ # puts "erhoehe Abfrageintervall auf #{$secs} Sekunden(n)"
148
+ puts "increasing request interval to #{$secs} seconds"
149
+ $count = 0
150
+ end
151
+ end
152
+ end
153
+ if opt_waitingloop
154
+ while 1
155
+ # $osd = IO.popen("osd_cat -p middle -f -adobe-helvetica-*-*-*-*-24-*-*-*- *-*-*-* -s 3 -S lightgray --delay=60","w+")
156
+ #-- $osd = IO.popen("osd_cat -p middle -f -sony-fixed-medium-*-normal-*-24-*-* -*-*-*-*-* -s 3 -S lightgray --delay=60","w+")
157
+ schleife
158
+ #-- $osd.close_write
159
+ #-- $osd.close
160
+ # puts "warte #{$secs} Sekunden (#{$secs/60} Minuten)"
161
+ puts "waiting #{$secs} seconds (#{$secs/60} minute#{plural($secs/60)})"
162
+ sleep($secs)
163
+ end
164
+
165
+ end
@@ -1,7 +1,7 @@
1
1
  # $:.unshift File.dirname(__FILE__)
2
2
 
3
- #module Dragongoserver
4
- #end
3
+ module Dragongoserver
4
+ end
5
5
 
6
- require 'dragongoserver/version.rb'
7
- require 'dragongoserver/dragongoserver.rb'
6
+ require 'dragongoserver/version'
7
+ require 'dragongoserver/dragongoserver'
@@ -0,0 +1,145 @@
1
+ require 'rubygems'
2
+ require 'mechanize'
3
+ require 'gorank'
4
+
5
+ class Dgs
6
+ LOGGED_IN="Sorry, you have to be logged in to do that"
7
+ YOUR_TURN="Your turn to move in the following games"
8
+ DGS_STATUS_PAGE = 'http://www.dragongoserver.net/status.php/'
9
+ DGS_INDEX_PAGE = 'http://www.dragongoserver.net/index.php'
10
+ USER_AGENT_ALIAS = 'Mac Safari' # should work with every browser type
11
+
12
+ attr_reader :username
13
+
14
+ def initialize(username=nil,password=nil)
15
+ @username = username
16
+ @password = password
17
+ if @username == nil or @password == nil
18
+ @username = ENV['DGS_USERNAME']
19
+ @password = ENV['DGS_PASSWORD']
20
+ if @username == nil or @password == nil
21
+ raise "username and/or password not set"
22
+ end
23
+ end
24
+ end
25
+
26
+ def waiting_games
27
+ games = []
28
+ @home = DGS_STATUS_PAGE
29
+ agent = WWW::Mechanize.new
30
+ agent.user_agent_alias = USER_AGENT_ALIAS
31
+
32
+ page = agent.get(@home)
33
+ if page.body.include?(LOGGED_IN)
34
+ # dann erst zum Einloggen gehen
35
+ page = login(agent)
36
+ end
37
+
38
+ # muss kommen, sonst sind wir noch nicht auf der richtigen Seite
39
+ if !page.body.include?(YOUR_TURN)
40
+ raise "PageError: #{DGS_STATUS_PAGE} not found"
41
+ end
42
+ page.body.each {|line|
43
+ if line =~ /gid=([0-9]+)"/
44
+ games << $1.to_i
45
+ end
46
+ }
47
+ # aus Bequemlichkeitsgruenden (gid= kommt mehrfach vor): uniq
48
+ games.uniq
49
+ end
50
+
51
+ # http://www.dragongoserver.net/show_games.php?uid=10737
52
+ def running_games
53
+ games = []
54
+ @home = 'http://www.dragongoserver.net/show_games.php?uid=10737' #TODO:
55
+ agent = WWW::Mechanize.new
56
+ agent.user_agent_alias = USER_AGENT_ALIAS
57
+
58
+ page = agent.get(@home)
59
+ if page.body.include?(LOGGED_IN)
60
+ page = login(agent)
61
+ end
62
+
63
+ if !page.body.include?(YOUR_TURN)
64
+ raise "PageError: #{DGS_STATUS_PAGE} not found"
65
+ end
66
+
67
+ page = agent.click page.links.text('Show running games')
68
+ page.body.each {|line|
69
+ if line =~ /gid=([0-9]+)"/
70
+ games << $1.to_i
71
+ end
72
+ }
73
+ # aus Bequemlichkeitsgruenden (gid= kommt mehrfach vor): uniq
74
+ games.uniq
75
+ end
76
+
77
+ def get_rank
78
+ @home = DGS_STATUS_PAGE
79
+ agent = WWW::Mechanize.new
80
+ agent.user_agent_alias = USER_AGENT_ALIAS
81
+
82
+ page = agent.get(@home)
83
+ if page.body.include?(LOGGED_IN)
84
+ page = login(agent)
85
+ end
86
+
87
+ if !page.body.include?(YOUR_TURN)
88
+ raise "PageError: #{DGS_STATUS_PAGE} not found"
89
+ end
90
+ a = show_regexp(page.body, /(\d+)&nbsp;(kyu|dan)/)
91
+ num = a[0]
92
+ what = a[1].to_sym
93
+ myRank = GoRank.new num, what
94
+ myRank
95
+ end
96
+
97
+ def userid
98
+ @home = DGS_STATUS_PAGE
99
+ agent = WWW::Mechanize.new
100
+ agent.user_agent_alias = USER_AGENT_ALIAS
101
+
102
+ page = agent.get(@home)
103
+ if page.body.include?(LOGGED_IN)
104
+ page = login(agent)
105
+ end
106
+
107
+ if !page.body.include?(YOUR_TURN)
108
+ raise "PageError: #{DGS_STATUS_PAGE} not found"
109
+ end
110
+ # Link 'http://www.dragongoserver.net/ratinggraph.php?uid=10737'
111
+ # enthaelt die Userid
112
+ #p page.links
113
+ page.links.each { |link|
114
+ if link.href =~ /ratinggraph/
115
+ p link
116
+ a = show_regexp(link.href, /^ratinggraph.php\?uid=(\d+)$/)
117
+ @userid = a[0].to_i
118
+ break
119
+ end
120
+ }
121
+ @userid
122
+ end
123
+
124
+ private
125
+
126
+ def show_regexp(a, re)
127
+ if a =~ re
128
+ return ["#$1", "#$2"]
129
+ else
130
+ "no match"
131
+ end
132
+ end
133
+
134
+ def login(agent)
135
+ @home = DGS_INDEX_PAGE
136
+ page = agent.get(@home)
137
+ form = page.forms.with.name("loginform").first
138
+ form.fields.find {|f| f.name == 'userid'}.value = @username
139
+ form.fields.find {|f| f.name == 'passwd'}.value = @password
140
+ page = agent.submit(form, form.buttons.first)
141
+ return page
142
+ end
143
+
144
+
145
+ end
@@ -2,7 +2,7 @@ module Dragongoserver #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- TINY = 1
5
+ TINY = 3
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -48,6 +48,11 @@ class TestDragongoserver < Test::Unit::TestCase
48
48
  assert_equal zlist.uniq, zlist
49
49
  end
50
50
 
51
+ def test_bug_duplicate_numbers_running_games
52
+ zlist = @dgs.running_games
53
+ assert_equal zlist.uniq, zlist
54
+ end
55
+
51
56
  def test_running_games
52
57
  zlist = @dgs.running_games
53
58
  assert zlist.kind_of?(Array)
@@ -55,8 +60,8 @@ class TestDragongoserver < Test::Unit::TestCase
55
60
 
56
61
  # programm 'dgs' should be found
57
62
  def test_bin_dgs_version
58
- assert_equal "dgs 0.0.1\n", `#{BIN_PREFIX}dgs -v`
59
- assert_equal "dgs 0.0.1\n", `#{BIN_PREFIX}dgs --version`
63
+ assert_equal "dgs " + Dragongoserver::VERSION::STRING + "\n", `#{BIN_PREFIX}dgs -v`
64
+ assert_equal "dgs " + Dragongoserver::VERSION::STRING + "\n", `#{BIN_PREFIX}dgs --version`
60
65
  end
61
66
 
62
67
  # test waiting moves for me
@@ -68,4 +73,29 @@ class TestDragongoserver < Test::Unit::TestCase
68
73
  def test_dgs_userid
69
74
  assert_equal MY_USERID, @dgs.userid, "userid should be #{MY_USERID}"
70
75
  end
76
+
77
+ # test my running games
78
+ def test_bin_dgs_running
79
+ expected = "you have a total of 29 running games
80
+ 366511
81
+ 372651
82
+ 366534
83
+ 340021
84
+ 244839
85
+ 366494
86
+ 369415
87
+ 369412
88
+ 369306
89
+ 369303
90
+ 366535"
91
+
92
+ assert_equal expected, `#{BIN_PREFIX}dgs -r`
93
+ assert_equal expected, `#{BIN_PREFIX}dgs -running`
94
+ end
95
+
96
+ def test_bin_dgs_add_game
97
+ assert_equal "Games was added to the waitingroom", `#{BIN_PREFIX}dgs --add-game`
98
+
99
+ end
100
+
71
101
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dragongoserver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Preymesser
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2007-12-29 00:00:00 +01:00
12
+ date: 2008-01-01 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -51,6 +51,7 @@ files:
51
51
  - config/hoe.rb
52
52
  - config/requirements.rb
53
53
  - lib/dragongoserver.rb
54
+ - lib/dragongoserver/dragongoserver.rb
54
55
  - lib/dragongoserver/version.rb
55
56
  - log/debug.log
56
57
  - script/destroy