Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Changing 'game' model to 'table' model to better encapsulate concepts

now we have a game_type for a table
  • Loading branch information...
commit 1013774ff588b4727c590771eb27428500361e34 1 parent 365a9ff
Ben Feigin authored
5 Gemfile
@@ -23,6 +23,7 @@ gem 'database_cleaner', :group => "test"
23 23 # Padrino Stable Gem
24 24 gem 'padrino', '0.10.5'
25 25
  26 +
26 27 # Or Padrino Edge
27 28 # gem 'padrino', :git => 'git://github.com/padrino/padrino-framework.git'
28 29
@@ -30,3 +31,7 @@ gem 'padrino', '0.10.5'
30 31 # %w(core gen helpers cache mailer admin).each do |g|
31 32 # gem 'padrino-' + g, '0.10.5'
32 33 # end
  34 +group :development do
  35 + gem 'guard'
  36 + gem 'rb-readline'
  37 +end
7 Gemfile.lock
@@ -19,9 +19,13 @@ GEM
19 19 diff-lcs (1.1.3)
20 20 factory_girl (2.5.2)
21 21 activesupport (>= 2.3.9)
  22 + ffi (1.0.11)
22 23 grit (2.4.1)
23 24 diff-lcs (~> 1.1)
24 25 mime-types (~> 1.15)
  26 + guard (1.0.0)
  27 + ffi (>= 0.5.0)
  28 + thor (~> 0.14.6)
25 29 haml (3.1.4)
26 30 http_router (0.10.2)
27 31 rack (>= 1.0.0)
@@ -68,6 +72,7 @@ GEM
68 72 rack-test (0.6.1)
69 73 rack (>= 1.0)
70 74 rake (0.9.2.2)
  75 + rb-readline (0.4.2)
71 76 rspec (2.8.0)
72 77 rspec-core (~> 2.8.0)
73 78 rspec-expectations (~> 2.8.0)
@@ -100,10 +105,12 @@ DEPENDENCIES
100 105 bcrypt-ruby
101 106 database_cleaner
102 107 factory_girl
  108 + guard
103 109 haml
104 110 padrino (= 0.10.5)
105 111 rack-test
106 112 rake
  113 + rb-readline
107 114 rspec
108 115 sinatra-flash
109 116 sqlite3
23 Guardfile
... ... @@ -0,0 +1,23 @@
  1 +# A sample Guardfile
  2 +# More info at https://github.com/guard/guard#readme
  3 +
  4 +guard 'rspec', :version => 2 do
  5 + watch(%r{^spec/.+_spec\.rb$})
  6 + watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
  7 + watch('spec/spec_helper.rb') { "spec" }
  8 +
  9 + # Rails example
  10 + watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
  11 + watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
  12 + watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
  13 + watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
  14 + watch('config/routes.rb') { "spec/routing" }
  15 + watch('app/controllers/application_controller.rb') { "spec/controllers" }
  16 + # Capybara request specs
  17 + watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
  18 +end
  19 +
  20 +
  21 +guard :bundler do
  22 + watch('Gemfile')
  23 +end
2  admin/app.rb
@@ -33,7 +33,7 @@ class Admin < Padrino::Application
33 33 access_control.roles_for :admin do |role|
34 34 role.project_module :actions, "/actions"
35 35 role.project_module :hands, "/hands"
36   - role.project_module :games, "/games"
  36 + role.project_module :tables, "/tables"
37 37 role.project_module :players, "/players"
38 38 role.project_module :tournaments, "/tournaments"
39 39 role.project_module :accounts, "/accounts"
47 admin/controllers/games.rb
... ... @@ -1,47 +0,0 @@
1   -Admin.controllers :games do
2   -
3   - get :index do
4   - @games = Game.all
5   - render 'games/index'
6   - end
7   -
8   - get :new do
9   - @game = Game.new
10   - render 'games/new'
11   - end
12   -
13   - post :create do
14   - @game = Game.new(params[:game])
15   - if @game.save
16   - flash[:notice] = 'Game was successfully created.'
17   - redirect url(:games, :edit, :id => @game.id)
18   - else
19   - render 'games/new'
20   - end
21   - end
22   -
23   - get :edit, :with => :id do
24   - @game = Game.find(params[:id])
25   - render 'games/edit'
26   - end
27   -
28   - put :update, :with => :id do
29   - @game = Game.find(params[:id])
30   - if @game.update_attributes(params[:game])
31   - flash[:notice] = 'Game was successfully updated.'
32   - redirect url(:games, :edit, :id => @game.id)
33   - else
34   - render 'games/edit'
35   - end
36   - end
37   -
38   - delete :destroy, :with => :id do
39   - game = Game.find(params[:id])
40   - if game.destroy
41   - flash[:notice] = 'Game was successfully destroyed.'
42   - else
43   - flash[:error] = 'Unable to destroy Game!'
44   - end
45   - redirect url(:games, :index)
46   - end
47   -end
47 admin/controllers/tables.rb
... ... @@ -0,0 +1,47 @@
  1 +Admin.controllers :tables do
  2 +
  3 + get :index do
  4 + @tables = Game.all
  5 + render 'tables/index'
  6 + end
  7 +
  8 + get :new do
  9 + @table = Game.new
  10 + render 'tables/new'
  11 + end
  12 +
  13 + post :create do
  14 + @table = Game.new(params[:table])
  15 + if @table.save
  16 + flash[:notice] = 'Game was successfully created.'
  17 + redirect url(:tables, :edit, :id => @table.id)
  18 + else
  19 + render 'tables/new'
  20 + end
  21 + end
  22 +
  23 + get :edit, :with => :id do
  24 + @table = Game.find(params[:id])
  25 + render 'tables/edit'
  26 + end
  27 +
  28 + put :update, :with => :id do
  29 + @table = Game.find(params[:id])
  30 + if @table.update_attributes(params[:table])
  31 + flash[:notice] = 'Game was successfully updated.'
  32 + redirect url(:tables, :edit, :id => @table.id)
  33 + else
  34 + render 'tables/edit'
  35 + end
  36 + end
  37 +
  38 + delete :destroy, :with => :id do
  39 + table = Game.find(params[:id])
  40 + if table.destroy
  41 + flash[:notice] = 'Game was successfully destroyed.'
  42 + else
  43 + flash[:error] = 'Unable to destroy Game!'
  44 + end
  45 + redirect url(:tables, :index)
  46 + end
  47 +end
6 admin/views/actions/_form.haml
... ... @@ -1,7 +1,7 @@
1 1 .group
2   - =f.label :game_id
3   - =f.error_message_on :game_id
4   - =f.text_field :game_id, :class => :text_field
  2 + =f.label :table_id
  3 + =f.error_message_on :table_id
  4 + =f.text_field :table_id, :class => :text_field
5 5 %span.description Ex: a simple text
6 6
7 7 .group
4 admin/views/actions/index.haml
@@ -11,7 +11,7 @@
11 11 %table.table
12 12 %tr
13 13 %th.first=mat(:action, :id)
14   - %th=mat(:action, :game_id)
  14 + %th=mat(:action, :table_id)
15 15 %th=mat(:action, :betting_round)
16 16 %th=mat(:action, :turn_number)
17 17 %th=mat(:action, :action)
@@ -21,7 +21,7 @@
21 21 -@actions.each do |action|
22 22 %tr
23 23 %td.first=action.id
24   - %td=action.game_id
  24 + %td=action.table_id
25 25 %td=action.betting_round
26 26 %td=action.turn_number
27 27 %td=action.action
15 admin/views/games/edit.haml
... ... @@ -1,15 +0,0 @@
1   -.block
2   - .secondary-navigation
3   - %ul.wat-cf
4   - %li.first=link_to pat(:list), url(:games, :index)
5   - %li=link_to pat(:new), url(:games, :new)
6   - %li.active=link_to pat(:edit), url(:games, :edit, :id => @game.id)
7   - .content
8   - %h2.title
9   - =pat(:edit)
10   - =mt(:game)
11   - .inner
12   - -form_for :game, url(:games, :update, :id => @game.id), :method => :put, :class => :form do |f|
13   - =partial "games/form", :locals => { :f => f }
14   -
15   --content_for :sidebar, partial("base/sidebar")
33 admin/views/games/index.haml
... ... @@ -1,33 +0,0 @@
1   -.block
2   - .secondary-navigation
3   - %ul.wat-cf
4   - %li.first.active=link_to pat(:list), url(:games, :index)
5   - %li=link_to pat(:new), url(:games, :new)
6   - .content
7   - %h2.title
8   - =pat(:all)
9   - =mt(:games)
10   - .inner
11   - %table.table
12   - %tr
13   - %th.first=mat(:game, :id)
14   - %th=mat(:game, :tournament_id)
15   - %th=mat(:game, :betting_round)
16   - %th=mat(:game, :turn)
17   - %th=mat(:game, :action_to)
18   - %th.last="&nbsp;"
19   - -@games.each do |game|
20   - %tr
21   - %td.first=game.id
22   - %td=game.tournament_id
23   - %td=game.betting_round
24   - %td=game.turn
25   - %td=game.action_to
26   - %td.last
27   - =button_to pat(:edit), url(:games, :edit, :id => game.id), :method => :get, :class => :button_to
28   - ="|"
29   - =button_to pat(:delete), url(:games, :destroy, :id => game.id), :method => :delete, :class => :button_to, :onsubmit => "return confirm('#{pat(:confirm)}')"
30   - .actions-bar.wat-cf
31   - .actions="&nbsp;"
32   -
33   --content_for :sidebar, partial("base/sidebar")
14 admin/views/games/new.haml
... ... @@ -1,14 +0,0 @@
1   -.block
2   - .secondary-navigation
3   - %ul.wat-cf
4   - %li.first=link_to pat(:list), url(:games, :index)
5   - %li.active=link_to pat(:new), url(:games, :new)
6   - .content
7   - %h2.title
8   - =pat(:new)
9   - =mt(:game)
10   - .inner
11   - -form_for :game, url(:games, :create), :class => :form do |f|
12   - =partial "games/form", :locals => { :f => f }
13   -
14   --content_for :sidebar, partial("base/sidebar")
2  admin/views/games/_form.haml → admin/views/tables/_form.haml
@@ -25,4 +25,4 @@
25 25
26 26 .group.navform.wat-cf
27 27 =f.submit pat(:save), :class => :button
28   - =f.submit pat(:cancel), :onclick => "window.location='#{url(:games, :index)}';return false", :class => :button
  28 + =f.submit pat(:cancel), :onclick => "window.location='#{url(:tables, :index)}';return false", :class => :button
15 admin/views/tables/edit.haml
... ... @@ -0,0 +1,15 @@
  1 +.block
  2 + .secondary-navigation
  3 + %ul.wat-cf
  4 + %li.first=link_to pat(:list), url(:tables, :index)
  5 + %li=link_to pat(:new), url(:tables, :new)
  6 + %li.active=link_to pat(:edit), url(:tables, :edit, :id => @table.id)
  7 + .content
  8 + %h2.title
  9 + =pat(:edit)
  10 + =mt(:table)
  11 + .inner
  12 + -form_for :table, url(:tables, :update, :id => @table.id), :method => :put, :class => :form do |f|
  13 + =partial "tables/form", :locals => { :f => f }
  14 +
  15 +-content_for :sidebar, partial("base/sidebar")
33 admin/views/tables/index.haml
... ... @@ -0,0 +1,33 @@
  1 +.block
  2 + .secondary-navigation
  3 + %ul.wat-cf
  4 + %li.first.active=link_to pat(:list), url(:tables, :index)
  5 + %li=link_to pat(:new), url(:tables, :new)
  6 + .content
  7 + %h2.title
  8 + =pat(:all)
  9 + =mt(:tables)
  10 + .inner
  11 + %table.table
  12 + %tr
  13 + %th.first=mat(:table, :id)
  14 + %th=mat(:table, :tournament_id)
  15 + %th=mat(:table, :betting_round)
  16 + %th=mat(:table, :turn)
  17 + %th=mat(:table, :action_to)
  18 + %th.last="&nbsp;"
  19 + -@tables.each do |table|
  20 + %tr
  21 + %td.first=table.id
  22 + %td=table.tournament_id
  23 + %td=table.betting_round
  24 + %td=table.turn
  25 + %td=table.action_to
  26 + %td.last
  27 + =button_to pat(:edit), url(:tables, :edit, :id => table.id), :method => :get, :class => :button_to
  28 + ="|"
  29 + =button_to pat(:delete), url(:tables, :destroy, :id => table.id), :method => :delete, :class => :button_to, :onsubmit => "return confirm('#{pat(:confirm)}')"
  30 + .actions-bar.wat-cf
  31 + .actions="&nbsp;"
  32 +
  33 +-content_for :sidebar, partial("base/sidebar")
14 admin/views/tables/new.haml
... ... @@ -0,0 +1,14 @@
  1 +.block
  2 + .secondary-navigation
  3 + %ul.wat-cf
  4 + %li.first=link_to pat(:list), url(:tables, :index)
  5 + %li.active=link_to pat(:new), url(:tables, :new)
  6 + .content
  7 + %h2.title
  8 + =pat(:new)
  9 + =mt(:table)
  10 + .inner
  11 + -form_for :table, url(:tables, :create), :class => :form do |f|
  12 + =partial "tables/form", :locals => { :f => f }
  13 +
  14 +-content_for :sidebar, partial("base/sidebar")
BIN  db/enova_poker_development.db
Binary file not shown
0  db/enova_poker_production.db
No changes.
69 db/schema.rb
@@ -12,59 +12,60 @@
12 12
13 13 ActiveRecord::Schema.define(:version => 9) do
14 14
15   - create_table "accounts", :force => true do |t|
  15 + create_table "tournaments", :force => true do |t|
16 16 t.string "name"
17   - t.string "surname"
18   - t.string "email"
19   - t.string "crypted_password"
20   - t.string "role"
21 17 end
22 18
23   - create_table "actions", :force => true do |t|
24   - t.integer "game_id"
25   - t.string "betting_round"
26   - t.integer "turn_number"
27   - t.string "action"
28   - t.integer "amount"
29   - t.integer "player_id"
  19 + create_table "players", :force => true do |t|
  20 + t.integer "tournament_id"
  21 + t.string "hostname"
  22 + t.integer "chips"
  23 + t.string "name"
30 24 end
31 25
32   - create_table "cards", :force => true do |t|
33   - t.string "value"
34   - t.string "suit"
  26 + create_table "seatings", :force => true do |t|
35 27 t.integer "player_id"
36   - t.integer "hand_id"
  28 + t.integer "table_id"
  29 + t.integer "seat_number"
  30 + t.boolean "active"
37 31 end
38 32
39   - create_table "games", :force => true do |t|
  33 + create_table "tables", :force => true do |t|
40 34 t.integer "tournament_id"
41   - t.string "betting_round"
42   - t.integer "turn"
43   - t.integer "action_to"
  35 + t.integer "hands_played"
  36 + t.string "game_type"
44 37 end
45 38
46 39 create_table "hands", :force => true do |t|
47   - t.integer "player_id"
  40 + t.integer "table_id"
48 41 t.string "cards"
49   - t.integer "turn_number"
50 42 end
  43 +
  44 + create_table "rounds", :force => true do |t|
  45 + t.integer 'hand_id'
  46 + t.string 'betting_phase'
  47 + t.boolean 'open'
  48 + end
51 49
52   - create_table "players", :force => true do |t|
53   - t.integer "tournament_id"
54   - t.string "hostname"
55   - t.integer "chips"
56   - t.string "name"
  50 + create_table "actions", :force => true do |t|
  51 + t.integer "round_id"
  52 + t.integer "player_id"
  53 + t.string "action"
  54 + t.integer "amount"
57 55 end
58   -
59   - create_table "seatings", :force => true do |t|
  56 +
  57 + create_table "cards", :force => true do |t|
  58 + t.string "value"
  59 + t.string "suit"
60 60 t.integer "player_id"
61   - t.integer "game_id"
62   - t.integer "seat_number"
63   - t.boolean "active"
  61 + t.integer "hand_id"
64 62 end
65 63
66   - create_table "tournaments", :force => true do |t|
  64 + create_table "accounts", :force => true do |t|
67 65 t.string "name"
  66 + t.string "surname"
  67 + t.string "email"
  68 + t.string "crypted_password"
  69 + t.string "role"
68 70 end
69   -
70 71 end
6 models/game.rb
... ... @@ -1,6 +0,0 @@
1   -class Game < ActiveRecord::Base
2   - has_many :hands
3   - belongs_to :tournament
4   - has_many :players, :through => :seatings, :conditions => {:active => true}
5   -
6   -end
3  models/hand.rb
... ... @@ -1,4 +1,3 @@
1 1 class Hand < ActiveRecord::Base
2   - belongs_to :game
3   -
  2 + belongs_to :table
4 3 end
4 models/player.rb
... ... @@ -1,8 +1,8 @@
1 1 class Player < ActiveRecord::Base
2 2 belongs_to :tournament
3 3 has_one :seating
4   - has_many :games, :through => :seatings do
5   - def current_game
  4 + has_many :tables, :through => :seatings do
  5 + def current_table
6 6 where(:active => true)
7 7 end
8 8 end
5 models/round.rb
@@ -2,4 +2,9 @@ class Round < ActiveRecord::Base
2 2 belongs_to :hand
3 3 has_many :actions
4 4
  5 + def rounds_for(table_type = :texas_holdem)
  6 + @rounds ||= {:texas_holdem => [:pre_flop, :flop, :turn, :river]}
  7 + return @rounds[table_type]
  8 + end
  9 +
5 10 end
2  models/seating.rb
... ... @@ -1,5 +1,5 @@
1 1 class Seating < ActiveRecord::Base
2 2 belongs_to :player
3   - belongs_to :game
  3 + belongs_to :table
4 4
5 5 end
14 models/table.rb
... ... @@ -0,0 +1,14 @@
  1 +class Table < ActiveRecord::Base
  2 + has_many :hands
  3 + belongs_to :tournament
  4 + has_many :players, :through => :seatings, :conditions => {:active => true}
  5 +
  6 + def initialize
  7 + super
  8 + self.game_type ||= :texas_holdem
  9 + end
  10 +
  11 + def begin_new_hand!
  12 + end
  13 +
  14 +end
2  models/tournament.rb
... ... @@ -1,6 +1,6 @@
1 1 class Tournament < ActiveRecord::Base
2 2 has_many :players
3   - has_many :games
  3 + has_many :tables
4 4
5 5 # Attempt to seat all available players.
6 6 def seat!
12 spec/models/game_spec.rb
... ... @@ -1,12 +0,0 @@
1   -require 'spec_helper'
2   -
3   -describe "Game Model" do
4   - let(:game) { Game.new }
5   - it 'can be created' do
6   - game.should_not be_nil
7   - end
8   -
9   - it 'should start in the pre_flop betting round' do
10   - game.betting_round.should == 'pre-flop'
11   - end
12   -end
1  spec/models/hand_spec.rb
@@ -5,4 +5,5 @@
5 5 it 'can be created' do
6 6 hand.should_not be_nil
7 7 end
  8 +
8 9 end
5 spec/models/round_spec.rb
@@ -5,4 +5,9 @@
5 5 it 'can be created' do
6 6 round.should_not be_nil
7 7 end
  8 +
  9 + it 'knows the betting rounds for texas holdem' do
  10 + round.rounds_for(:texas_holdem).should == [:pre_flop, :flop, :turn, :river]
  11 + end
  12 +
8 13 end
16 spec/models/table_spec.rb
... ... @@ -0,0 +1,16 @@
  1 +require 'spec_helper'
  2 +
  3 +describe "Game Model" do
  4 + let(:table) { Table.new }
  5 + it 'can be created' do
  6 + table.should_not be_nil
  7 + end
  8 +
  9 + it 'should have a game type' do
  10 + table.game_type.should_not be_nil
  11 + end
  12 +
  13 + it 'can begin a new hand' do
  14 + table.begin_new_hand!
  15 + end
  16 +end

0 comments on commit 1013774

Please sign in to comment.
Something went wrong with that request. Please try again.