nachof-zheng 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/zheng CHANGED
@@ -7,5 +7,9 @@ include Zheng
7
7
  if ARGV.size > 0
8
8
  Actions::call(*ARGV)
9
9
  else
10
- Actions::script $stdin
10
+ if $stdin.tty?
11
+ Interactive.run
12
+ else
13
+ Actions.script $stdin
14
+ end
11
15
  end
data/config/init.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'sequel'
3
3
  require 'erb'
4
+ require 'readline'
4
5
 
5
6
  require 'shellwords' # Needed for parsing the script action
6
7
 
data/lib/zheng.rb CHANGED
@@ -4,3 +4,4 @@ require File.join(File.dirname(__FILE__), 'zheng', 'rating')
4
4
  require File.join(File.dirname(__FILE__), 'zheng', 'player')
5
5
  require File.join(File.dirname(__FILE__), 'zheng', 'game')
6
6
  require File.join(File.dirname(__FILE__), 'zheng', 'actions')
7
+ require File.join(File.dirname(__FILE__), 'zheng', 'interactive')
data/lib/zheng/game.rb CHANGED
@@ -1,19 +1,21 @@
1
1
  module Zheng
2
2
  class Game < Sequel::Model
3
+ before_create :set_ratings
4
+ before_create :set_date
5
+
3
6
  set_schema do
4
7
  primary_key :id
5
8
  foreign_key :left_id, :players
6
9
  foreign_key :right_id, :players
7
10
  text :winner, :null => false
11
+ float :left_rating_before, :null => false
12
+ float :right_rating_before, :null => false
13
+ date :date, :null => false
8
14
  end
9
15
 
10
- include Util
11
-
12
16
  belongs_to :left, :class => :Player
13
17
  belongs_to :right, :class => :Player
14
18
 
15
- sequel_accessor :right_id, :left_id
16
-
17
19
  def winner
18
20
  @values[:winner].to_sym
19
21
  end
@@ -27,9 +29,28 @@ module Zheng
27
29
  return Parameters::Con(player.rating) * (result(which_player) - Parameters::expected(player.rating, opponent.rating))
28
30
  end
29
31
 
32
+ def rating_before which_player
33
+ send("#{which_player.to_s}_rating_before".to_sym)
34
+ end
35
+ def rating_after which_player
36
+ rating_before(which_player) + rating_change_for(which_player)
37
+ end
38
+
30
39
  def result which_player
31
40
  (which_player == winner) ? 1 : 0
32
41
  end
42
+
43
+ private
44
+
45
+ def set_ratings
46
+ self.left_rating_before = left.rating
47
+ self.right_rating_before = right.rating
48
+ end
49
+
50
+ def set_date
51
+ self.date = Date.today if date.nil?
52
+ end
53
+
33
54
  end
34
55
  end
35
56
 
data/lib/zheng/player.rb CHANGED
@@ -3,14 +3,10 @@ module Zheng
3
3
  set_schema do
4
4
  primary_key :id
5
5
  text :name, :null => false, :unique => true
6
- integer :rating, :null => false
6
+ float :rating, :null => false
7
7
  boolean :external, :default => false
8
8
  end
9
9
 
10
- include Util
11
-
12
- sequel_accessor :name, :rating
13
-
14
10
  def rank
15
11
  Rating.new(rating).rank
16
12
  end
data/lib/zheng/util.rb CHANGED
@@ -1,27 +1,4 @@
1
1
  module Zheng
2
- module Util
3
-
4
- def self.included(base)
5
- base.extend ClassMethods
6
- end
7
-
8
- module ClassMethods
9
- def sequel_accessor *params
10
- params.each do |method|
11
- class_eval <<-end_eval
12
- def #{method.to_s}
13
- @values[:#{method.to_s}]
14
- end
15
- def #{method.to_s}= v
16
- @values[:#{method.to_s}] = v
17
- end
18
- end_eval
19
- end
20
- end
21
- end
22
-
23
- end
24
-
25
2
  module_function
26
3
  # I put it here so I can easily silence it for tests
27
4
  def output text
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nachof-zheng
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nacho Facello
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-09 00:00:00 -08:00
12
+ date: 2009-01-26 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency