Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

move code to play plugin

  • Loading branch information...
commit fb4494f3514a3352ee7a87ed1b3fba8f639f004b 1 parent d2201d7
@geraldb geraldb authored
View
1  Gemfile
@@ -10,6 +10,7 @@ gem 'sportdb', '0.8.1' # see https://github.com/geraldb/sport.db
### todo/fix: make it work w/ autoload - will sportdb/market work?
gem 'sportdb-market', '0.1.3', require: 'sportdb/market/market'
+gem 'sportdb-play', '0.1.0'
group :production do
gem 'pg'
View
2  Gemfile.lock
@@ -101,6 +101,7 @@ GEM
activerecord (~> 3.2)
worlddb (~> 0.3.2)
sportdb-market (0.1.3)
+ sportdb-play (0.1.0)
sprockets (2.1.2)
hike (~> 1.2)
rack (~> 1.0)
@@ -141,6 +142,7 @@ DEPENDENCIES
sass-rails (~> 3.2.3)
sportdb (= 0.8.1)
sportdb-market (= 0.1.3)
+ sportdb-play (= 0.1.0)
sqlite3
thin
uglifier (>= 1.0.3)
View
8 app/models/bonus_point.rb
@@ -15,10 +15,4 @@
# updated_at :datetime not null
#
-class BonusPoint < ActiveRecord::Base
-
- belongs_to :user
- belongs_to :pool
- belongs_to :round, :class_name => 'BonusRound', :foreign_key => 'round_id'
-
-end # class BonusPoint
+BonusPoint = SportDB::Models::BonusPoint
View
8 app/models/bonus_question.rb
@@ -10,11 +10,5 @@
# updated_at :datetime not null
#
+BonusQuestion = SportDB::Models::BonusQuestion
-class BonusQuestion < ActiveRecord::Base
-
- self.table_name = 'bonus_questions'
-
- has_many :tips, :class_name => 'BonusTip', :foreign_key => 'question_id'
-
-end # class BonusQuestion
View
9 app/models/bonus_round.rb
@@ -11,11 +11,4 @@
#
-class BonusRound < ActiveRecord::Base
-
- self.table_name = 'bonus_rounds'
-
- has_many :questions, :order => 'pos', :class_name => 'BonusQuestion', :foreign_key => 'round_id'
- belongs_to :pool
-
-end # class BonusRound
+BonusRound = SportDB::Models::BonusRound
View
7 app/models/bonus_tip.rb
@@ -11,10 +11,5 @@
#
-class BonusTip < ActiveRecord::Base
-
- self.table_name = 'bonus_tips'
+BonusTip = SportDB::Models::BonusTip
- belongs_to :user
-
-end # class BonusTip
View
4 app/models/calc_tip.rb
@@ -26,8 +26,8 @@ class CalcTip < Tip
after_initialize :on_after_initialize
- belongs_to :calc_team1, :class_name => 'Team', :foreign_key => 'calc_team1_id'
- belongs_to :calc_team2, :class_name => 'Team', :foreign_key => 'calc_team2_id'
+ belongs_to :calc_team1, :class_name => 'SportDB::Models::Team', :foreign_key => 'calc_team1_id'
+ belongs_to :calc_team2, :class_name => 'SportDB::Models::Team', :foreign_key => 'calc_team2_id'
## note: use different calc forumula
# check for matching teams first
View
6 app/models/event.rb
@@ -19,10 +19,10 @@ module SportDB::Models
class Event
- has_many :fix_rounds, :conditions => { :fix => true }, :order => 'pos', :class_name => 'Round', :foreign_key => 'event_id'
- has_many :fix_playoff_rounds, :conditions => { :fix => true , :playoff => true }, :order => 'pos', :class_name => 'Round', :foreign_key => 'event_id'
+ has_many :fix_rounds, :conditions => { :fix => true }, :order => 'pos', :class_name => 'SportDB::Models::Round', :foreign_key => 'event_id'
+ has_many :fix_playoff_rounds, :conditions => { :fix => true , :playoff => true }, :order => 'pos', :class_name => 'SportDB::Models::Round', :foreign_key => 'event_id'
- has_many :flex_rounds, :conditions => { :flex => true }, :order => 'pos', :class_name => 'Round', :foreign_key => 'event_id'
+ has_many :flex_rounds, :conditions => { :flex => true }, :order => 'pos', :class_name => 'SportDB::Models::Round', :foreign_key => 'event_id'
end # class Event
View
74 app/models/game.rb
@@ -35,23 +35,10 @@ module SportDB::Models
class Game
- has_many :tips
-
-
after_save :log_action
- def job_running!
- @job_running = true
- end
-
- def job_done!
- @job_running = false
- end
-
- def job_running?
- (@job_running ||= false) == true
- end
-
+ ################
+ #### todo: move to sportdb.gem for reuse
def score_str
if score5.present? && score6.present? # im Elfmeterschiessen i.E.?
@@ -71,6 +58,9 @@ def score2_str
if score2.blank? then '-' else score2.to_s end
end
+ ## end move
+ ###################
+
def team1_style_class
buf = ''
buf << 'game-team-calc ' if team1.calc?
@@ -87,20 +77,7 @@ def team2_style_class
buf
end
-
- def tip_1_style_class
- toto12x == '1' ? ' bingo ' : ' '
- end
-
- def tip_2_style_class
- toto12x == '2' ? ' bingo ' : ' '
- end
-
- def tip_x_style_class
- toto12x == 'X' ? ' bingo ' : ' '
- end
-
-
+
def play_at_str
play_at.strftime( "%a. %d. %b. / %H:%M" )
end
@@ -111,45 +88,6 @@ def play_at_str_db
play_at.strftime( '%Y-%m-%d %H:%M %z' ) # NB: removed seconds (:%S)
end
-
- ############ some methods for stats
-
- def complete_tips
- tips.where( 'toto12x is not null' )
- end
-
- def complete_tips_1
- tips.where( 'toto12x is not null' ).where( :toto12x => '1' ).order( 'score1 desc,score2 desc')
- end
-
- def complete_tips_2
- tips.where( 'toto12x is not null' ).where( :toto12x => '2' ).order( 'score2 desc,score1 desc')
- end
-
- def complete_tips_x
- tips.where( 'toto12x is not null' ).where( :toto12x => 'X' ).order( 'score1 desc,score2 desc')
- end
-
-
- def incomplete_tips
- tips.where( 'toto12x is null' )
- end
-
- def tip_1_count
- complete_tips.where( :toto12x => '1' ).count()
- end
-
- def tip_2_count
- complete_tips.where( :toto12x => '2' ).count()
- end
-
- def tip_x_count
- complete_tips.where( :toto12x => 'X' ).count()
- end
-
- def tip_12x_count
- complete_tips.count()
- end
def log_action
# add news feed item after save
View
57 app/models/play.rb
@@ -14,40 +14,17 @@
# updated_at :datetime not null
#
-
-class Play < ActiveRecord::Base
+module SportDB::Models
- belongs_to :user
- belongs_to :pool
+## NB: extend from sport.db-play gem
+
+
+class Play
- belongs_to :team1, :class_name => 'Team', :foreign_key => 'team1_id'
- belongs_to :team2, :class_name => 'Team', :foreign_key => 'team2_id'
- belongs_to :team3, :class_name => 'Team', :foreign_key => 'team3_id'
after_create :log_action_create
after_update :log_action_update
-
- def job_running!
- @job_running = true
- end
-
- def job_done!
- @job_running = false
- end
-
- def job_running?
- (@job_running ||= false) == true
- end
-
- def public?
- return true if pool.public?
-
- # team1, team2 public after kickoff of event
- ## use past?
- Time.now.utc > pool.event.start_at.utc
- end
-
def log_action_create
a = Action.new
@@ -83,25 +60,9 @@ def log_action_update
a.text = buf
a.save!
end
-
-
- ## todo/fix: can it be done w/ a has_many macro and a condition?
- def tips
- recs = Tip.where( :pool_id => pool_id, :user_id => user_id ).all
- recs
- end
-
- ## todo/fix: can it be done w/ a has_many macro and a condition?
- def complete_rankings # fix rename to points and remove points column from play table??
- recs = Point.where( :pool_id => pool_id, :user_id => user_id ).joins( :round ).order('rounds.pos').all
- recs
- end
-
- def export?
- # check if user entered some data
- # - do NOT export nil records (all teams blank)
- (team1_id.blank? && team2_id.blank? && team3_id.blank?)==false
- end
-
end # class Play
+
+end # module SportDB::Models
+
+Play = SportDB::Models::Play
View
46 app/models/point.rb
@@ -17,48 +17,4 @@
# updated_at :datetime not null
#
-class Point < ActiveRecord::Base
-
- belongs_to :user
- belongs_to :pool
- belongs_to :round, :class_name => 'SportDB::Models::Round'
-
- def round_pts_str
- buf = ''
- round_pts.times { buf << '' }
- buf
- end
-
- def total_pts_str
- buf = ''
- total_pts.times { buf << '' }
- buf
- end
-
-
- def diff_total_pos_style_class
- if diff_total_pos > 0
- ' ranking-up '
- elsif diff_total_pos < 0
- ' ranking-down '
- else # == 0
- ' '
- end
- end
-
- def diff_total_pos_str
- ## todo: diff 2 use ⇑⇑
- # diff 3 use ⇑⇑⇑
- # diff 4 use ⇑⇑⇑⇑
- # etc.
-
- if diff_total_pos > 0
- "#{diff_total_pos}"
- elsif diff_total_pos < 0
- "#{diff_total_pos.abs}"
- else # == 0
- ""
- end
- end
-
-end # class Point
+Point = SportDB::Models::Point
View
32 app/models/pool.rb
@@ -16,18 +16,13 @@
# updated_at :datetime not null
#
-class Pool < ActiveRecord::Base
-
- belongs_to :user # is owner/admin/manager
-
- has_many :bonus_rounds
-
- has_many :plays # pools_users join table
-
- ## rename to users from players??
- has_many :players, :through => :plays, :source => :user
+module SportDB::Models
+
+## NB: extend from sport.db-play gem
+
- belongs_to :event, :class_name => 'SportDB::Models::Event'
+class Pool
+
after_create :log_action_create
@@ -65,9 +60,9 @@ def log_action_create
a.save!
end
- def full_title
- "#{title} #{event.title}#{fix? ? ' Fix' : ''}"
- end
+## def full_title
+## "#{title} #{event.title}#{fix? ? ' Fix' : ''}"
+## end
def fix?
fix == true
@@ -77,8 +72,9 @@ def flex?
fix != true
end
- def team3? # tip for 3rd place?
- event.team3 == true
- end
-end
+end # class Pool
+
+end # module SportDB::Models
+
+Pool = SportDB::Models::Pool
View
240 app/models/tip.rb
@@ -23,23 +23,14 @@
# updated_at :datetime not null
#
-class Tip < ActiveRecord::Base
-
- belongs_to :user
- belongs_to :pool
- belongs_to :game, :class_name => 'SportDB::Models::Game'
+module SportDB::Models
- before_save :calc_toto12x
- after_save :log_action
+## NB: extend from sport.db-play gem
+class Tip
- ## todo: rename to find_by_play_and_game ????
- def self.find_by_user_and_pool_and_game( user_arg, pool_arg, game_arg )
- recs = self.where( :user_id => user_arg.id, :pool_id => pool_arg.id, :game_id => game_arg.id )
- recs.first
- end
-
-
+ after_save :log_action
+
def log_action
# add news feed item after save
@@ -56,193 +47,6 @@ def log_action
a.save!
end
-
-
-
- def export?
- # check if user entered some data
- # - do NOT export nil records (all scores blank)
-
- (score1.blank? && score2.blank? && score3.blank? && score4.blank? && score5.blank? && score6.blank?)==false
- end
-
-
- def calc_points_worker
- pts = 0
-
- if(((game.score1 == game.score2) && (score1 == score2)) ||
- ((game.score1 > game.score2) && (score1 > score2)) ||
- ((game.score1 < game.score2) && (score1 < score2)))
- pts += 1
- end
-
- # tordifferenz richtig? todo: auch fuer unentschieden???
- if((game.score1-game.score2) == (score1-score2))
- ## nb: for now now points for tordifferenz
- ### pts +=1
- end
-
- # ergebnis richtig?
- if game.score1 == score1 && game.score2 == score2
- pts += 2
- end
-
- ## check n.V.
-
- if (game.score3.present? && game.score4.present? && score3.present? && score4.present?)
-
- if(((game.score3 == game.score4) && (score3 == score4)) ||
- ((game.score3 > game.score4) && (score3 > score4)) ||
- ((game.score3 < game.score4) && (score3 < score4)))
- pts += 1
- end
- end
-
- ## check i.E.
-
- if (game.score5.present? && game.score6.present? && score5.present? && score6.present?)
-
- if(((game.score5 > game.score6) && (score5 > score6)) ||
- ((game.score5 < game.score6) && (score5 < score6)))
- pts += 1
- end
- end
-
- pts
- end
-
- def calc_points
- pts = 0
- pts = calc_points_worker() if complete?
- pts
- end
-
- def calc_points_str
- buf = ''
- calc_points.times { buf << '' }
- buf
- end
-
-
- ## todo: use tip-fail, tip-bingo, etc.
-
- def bingo_style_class
- if incomplete?
- # show missing label for upcoming games only
- if game.over?
- ''
- elsif score1.blank? || score2.blank?
- 'missing' # missing tip scores
- else
- '' # tip scores filled in; game scores not yet available
- end
- else
- pts = calc_points()
- if pts == 0
- 'fail'
- elsif pts == 1
- 'bingo'
- elsif pts == 2
- 'bingoo'
- elsif pts == 3
- 'bingooo'
- else
- '' # unknown state; return empty (css) class
- end
- end
- end
-
- # like bingo_style_class but only for pts>0 (that is not for fail)
- def bingo_win_style_class
- if incomplete?
- # show missing label for upcoming games only
- if game.over?
- ''
- elsif score1.blank? || score2.blank?
- 'missing' # missing tip scores
- else
- '' # tip scores filled in; game scores not yet available
- end
- else
- pts = calc_points()
- if pts == 0
- ''
- elsif pts == 1
- 'bingo'
- elsif pts == 2
- 'bingoo'
- elsif pts == 3
- 'bingooo'
- else
- '' # unknown state; return empty (css) class
- end
- end
- end
-
- def bingo_text
- if incomplete?
- # show missing label for upcoming games only
- if game.over?
- ''
- elsif score1.blank? || score2.blank?
- '?' # missing tip scores
- else
- '' # tip scores filled in; game scores not yet available
- end
- else
- pts = calc_points()
- if pts == 0
- if game.calc? && (game.team1_id != calc_team1_id || game.team2_id != calc_team2_id )
- ## sorry, wrong teams - show team1 n team2 tags
- "× Leider, nein. Richtige Spielpaarung (#{game.team1.tag}) - (#{game.team2.tag})."
- else
- "× Leider, nein. Richtiger Tipp #{game.toto12x}." # return 1,2,X from game
- end
- elsif pts == 1
- '♣ 1 Pkt - Ja!'
- elsif pts == 2
- '♣♣ 2 Pkte - Jaaa!'
- elsif pts == 3
- '♣♣♣ 3 Pkte - Jaaaaa!'
- else
- '' # unknown state; return empty (css) class
- end
- end
- end
-
- def calc_toto12x
- if score1.nil? || score2.nil?
- self.toto12x = nil
- elsif score1 == score2
- self.toto12x = 'X'
- elsif score1 > score2
- self.toto12x = '1'
- elsif score1 < score2
- self.toto12x = '2'
- end
- end
-
-
- def complete?
- game.score1.present? && game.score2.present? && score1.present? && score2.present?
- end
-
- def incomplete?
- complete? == false
- end
-
- def locked?
- return true if pool.fix? && pool.locked? # if fix pool is locked all games are (automatically) locked too
- game.locked?
- end
-
- def public?
- return true if pool.public?
- return true if locked? # if fix pool is locked or game (make tip public)
-
- ## todo: use builtin utc.past? method ???
- Time.now.utc > game.play_at.utc
- end
def public_user_name_str
@@ -257,36 +61,8 @@ def public_toto12x_str
public? ? toto12x : '#'
end
- def score_str
- ## fix: use new game.toto12x instead of game.over ??? (doesn't depend on time)
- if score1.blank? && score2.blank? && game.over?
- # return no data marker (e.g. middot) if not touched by user
- '·'
- else
- str = ''
- if score5.present? && score6.present? # im Elfmeterschiessen i.E.?
- str = "#{score1_str} : #{score2_str} / #{score3} : #{score4} n.V. / #{score5} : #{score6} i.E."
- elsif score3.present? && score4.present? # nach Verlaengerung n.V.?
- str = "#{score1_str} : #{score2_str} / #{score3} : #{score4} n.V."
- else
- str = "#{score1_str} : #{score2_str}"
- end
-
- if calc
- str_calc_team1 = calc_team1_id.blank? ? '' : calc_team1.tag
- str_calc_team2 = calc_team2_id.blank? ? '' : calc_team2.tag
- str = "(#{str_calc_team1}) #{str} (#{str_calc_team2})"
- end
- str
- end
- end
-
- def score1_str
- if score1.blank? then '?' else score1.to_s end
- end
+end # class Tip
- def score2_str
- if score2.blank? then '?' else score2.to_s end
- end
+end # module SportDB::Models
-end # class Tip
+Tip = SportDB::Models::Tip
View
14 app/models/user.rb
@@ -14,11 +14,12 @@
# updated_at :datetime not null
#
-class User < ActiveRecord::Base
+module SportDB::Models
+
+## NB: extend from sport.db-play gem
+
+class User
- has_many :pools
- has_many :tips
- has_many :plays
has_many :actions
has_secure_password # use built-in rails macro for password to password_digest machinery
@@ -55,3 +56,8 @@ def self.create_from_ary!( users )
end
end # class User
+
+
+end # module SportDB::Models
+
+User = SportDB::Models::User
View
137 db/migrate/001_create_db.rb
@@ -10,8 +10,9 @@ def up
WorldDB.create
SportDB.create
- SportDB::Market::CreateDB.up ### fix: rename to SportDB::Market.create
-
+ SportDB::Market::create # market quotes
+ SportDB::Play::create # pools, plays, tips, etc.
+
#####################################
## add columns / change tables
####################################
@@ -43,158 +44,40 @@ def up
end
change_table :games do |t|
- t.boolean :locked, :null => false, :default => false
-
t.string :type # NOTE: Rails System Attribute Required for Single-Table Inheritance (STI)
t.boolean :calc, :null => false, :default => false
end
-#####################################
-## new tables / create tables
-####################################
-
-create_table :users do |t|
+change_table :users do |t|
t.string :name, :null => false
t.string :email, :null => false
t.string :password_digest
- t.string :key, :null => false # import/export key
t.boolean :admin, :null => false, :default => false
t.boolean :guest, :null => false, :default => false # read-only access (cannot add tips,join pools,etc.)
t.boolean :active, :null => false, :default => true
- t.timestamps
end
-add_index :users, :key, :unique => true
add_index :users, :email, :unique => true # make email unique
-create_table :pools do |t|
- t.references :event, :null => false
- t.string :title, :null => false
- t.references :user, :null => false # owner/manager/admin of pool
+change_table :pools do |t|
t.boolean :fix, :null => false, :default => false
- t.boolean :public, :null => false, :default => true # make all tips public (not private/secret)
- t.boolean :locked, :null => false, :default => false
t.text :welcome
t.text :welcome_html
- t.string :key # import/export key
- t.timestamps
-end
-
-add_index :pools, :key, :unique => true
-add_index :pools, :event_id
-add_index :pools, :user_id
-
-
-create_table :plays do |t|
- t.references :user, :null => false
- t.references :pool, :null => false
- t.references :team1 # winner (1st)
- t.references :team2 # runnerup (2nd)
- t.references :team3 # 2n runnerup (3nd)
-
- t.integer :total_pts, :null => false, :default => 0 # cached total player points
- t.integer :total_pos, :null => false, :default => 0 # cached total ranking/position
-
- t.timestamps
end
-add_index :plays, [:user_id,:pool_id], :unique => true # enforce only one play per user and pool
-add_index :plays, :user_id
-add_index :plays, :pool_id
-
-
-create_table :tips do |t|
- t.references :user, :null => false
- t.references :pool, :null => false
- t.references :game, :null => false
- t.integer :score1
- t.integer :score2
- t.integer :score3 # verlaengerung (opt)
- t.integer :score4
- t.integer :score5 # elfmeter (opt)
- t.integer :score6
- t.string :toto12x # 1,2,X,nil calculate on save
- t.string :type # NOTE: Rails System Attribute Required for Single-Table Inheritance (STI)
+change_table :tips do |t|
+ t.string :type # NOTE: Rails System Attribute Required for Single-Table Inheritance (STI)
t.boolean :calc, :null => false, :default => false
t.references :calc_team1 # optional ref for team1 guess from user
t.references :calc_team2 # optional ref for team2 guess from user
-
- t.timestamps
-end
-
-add_index :tips, [:user_id,:pool_id,:game_id], :unique => true
-add_index :tips, :user_id
-add_index :tips, :pool_id
-add_index :tips, :game_id
-
-
-
-create_table :points do |t|
- t.references :user, :null => false
- t.references :pool, :null => false
- t.references :round, :null => false
-
- t.integer :round_pts, :null => false, :default => 0 # points for this round
- t.integer :round_pos, :null => false, :default => 0 # ranking/position for this round
-
- t.integer :total_pts, :null => false, :default => 0 # total points up to(*) this round (including) (* rounds sorted by pos)
- t.integer :total_pos, :null => false, :default => 0 # ranking/position for points up to this round
-
- t.integer :diff_total_pos, :null => false, :default => 0
-
- t.timestamps
-end
-
-add_index :points, [:user_id,:pool_id,:round_id], :unique => true
-
-
-
-create_table :bonus_rounds do |t|
- t.references :pool, :null => false
- t.string :title, :null => false
- t.integer :pos, :null => false
- t.timestamps
-end
-
-create_table :bonus_questions do |t|
- t.references :round, :null => false
- t.string :title, :null => false
- t.integer :pos, :null => false
- t.timestamps
-end
-
-create_table :bonus_answers do |t|
- # to be done
- t.timestamps
-end
-
-create_table :bonus_tips do |t|
- t.references :user, :null => false
- t.references :question, :null => false
- t.integer :pts , :null => false, :default => 0
- t.timestamps
-end
-
-create_table :bonus_points do |t|
- t.references :user, :null => false
- t.references :pool, :null => false ## todo: check if we keep reference to pool (because round_id depends itself on pool)
- t.references :round, :null => false # nb: is bonus_round_id
-
- t.integer :round_pts, :null => false, :default => 0 # points for this round
- t.integer :round_pos, :null => false, :default => 0 # ranking/position for this round
-
- t.integer :total_pts, :null => false, :default => 0 # total points up to(*) this round (including) (* rounds sorted by pos)
- t.integer :total_pos, :null => false, :default => 0 # ranking/position for points up to this round
-
- t.integer :diff_total_pos, :null => false, :default => 0
-
- t.timestamps
end
-add_index :bonus_points, [:user_id,:pool_id,:round_id], :unique => true
+#####################################
+## new tables / create tables
+####################################
## todo: use polymorphic assoc?? for actions??
Please sign in to comment.
Something went wrong with that request. Please try again.