Skip to content

Wins losses on player #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/models/player.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,13 @@ def balance
def _credit_account
transactions.create(kind: "credit", amount: 1000)
end

def win_percentage
if (wins + losses) == 0
0
else
100*wins/(wins+losses)
end
end

end
6 changes: 5 additions & 1 deletion app/resources/v1/player_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module V1
class PlayerResource < BaseResource

attribute :name
attribute :wins
attribute :losses

has_one :user

Expand All @@ -11,7 +13,9 @@ class << self
def creatable_fields(context)
[
:user,
:name
:name,
:wins,
:losses
]
end

Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20150917173215_add_wins_and_losses_to_players.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddWinsAndLossesToPlayers < ActiveRecord::Migration
def change
add_column :players, :wins, :integer, default: 0
add_column :players, :losses, :integer, default: 0
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150917151639) do
ActiveRecord::Schema.define(version: 20150917173215) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -82,6 +82,8 @@
t.datetime "updated_at", null: false
t.integer "user_id", null: false
t.string "name", default: "Unnamed", null: false
t.integer "wins", default: 0
t.integer "losses", default: 0
end

add_index "players", ["user_id"], name: "index_players_on_user_id", using: :btree
Expand Down
30 changes: 30 additions & 0 deletions spec/models/player_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,34 @@
expect(subject.name).not_to be_blank
end

it "has a wins attribute" do
subject.wins = 3
expect(subject.wins).to eq 3
end

it "initializes with wins" do
expect(subject.wins).not_to be_blank
end

it "has a losses attribute" do
subject.losses = 3
expect(subject.losses).to eq 3
end

it "initializes with losses" do
expect(subject.losses).not_to be_blank
end

it "calculates win percent" do
subject.wins = 8
subject.losses = 8
expect(subject.win_percentage).to eq 50
subject.wins = 4
subject.losses = 0
expect(subject.win_percentage).to eq 100
subject.wins = 0
subject.losses = 8
expect(subject.win_percentage).to eq 0
end

end
9 changes: 9 additions & 0 deletions spec/policies/player_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@
it "can destroy the player" do
expect(subject.destroy?).to eq true
end
# TODO: How would we implement this?
#it "cannot update wins" do
#subject.wins = 1
#expect(subject.update?).to eq false
#end
#it "cannot update losses" do
#subject.losses = 1
#expect(subject.update?).to eq false
#end
it "has the player in scope" do
expect(subject.scope).to include record
end
Expand Down
4 changes: 3 additions & 1 deletion spec/resources/v1/player_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module V1
let :creatable_and_updatable_fields do
[
:user,
:name
:name,
:wins,
:losses
].sort
end

Expand Down