-
Notifications
You must be signed in to change notification settings - Fork 2
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
Refactor income credits calculation #81
Changes from 1 commit
08e953c
dbd67db
07d9533
f56c608
5d60501
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,40 @@ class Income < ActiveRecord::Base | |
def team_name | ||
self.team.team_name | ||
end | ||
|
||
def credits | ||
income_to_credit = get_income_to_credits_array | ||
safe_get_credits_value(income_to_credit) | ||
end | ||
|
||
private | ||
def get_income_to_credits_array | ||
case self.team_name | ||
when 'China' | ||
[2, 3, 5, 7, 9, 10, 12, 14, 16] | ||
when 'Japan' | ||
[3, 5, 6, 8, 9, 10, 12, 13, 14] | ||
when 'Russian Federation' | ||
[2, 4, 5, 6, 7, 8, 9, 10, 11] | ||
when 'United Kingdom' | ||
[2, 4, 6, 7, 8, 9, 10, 11, 12] | ||
when 'USA' | ||
[1, 3, 5, 7, 9, 11, 13, 15, 17] | ||
when 'Germany' | ||
[2, 5, 6, 7, 8, 9, 10, 12, 14] | ||
else | ||
[2, 5, 6, 7, 8, 9, 10, 11, 12] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should explicitly state India here and then have an overall default (which can be the same array as India's anyway). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On second thought, this should just be a hash where keys are the country names and values is the array. Store this at the top of the class as a constant like COUNTRY_CREDIT_LEVELS. Then in this method, |
||
end | ||
end | ||
|
||
def safe_get_credits_value(income_to_credit) | ||
if self.amount > income_to_credit.length | ||
income_to_credit[income_to_credit.length - 1] | ||
elsif self.amount < 1 | ||
0 | ||
else | ||
income_to_credit[self.amount - 1] | ||
end | ||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,55 @@ | ||
require 'rails_helper' | ||
|
||
def income_for_team(team_name) | ||
team = Team.find_by_team_name(team_name) | ||
Income.find_by(team: team) | ||
end | ||
|
||
RSpec.describe Income, :type => :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
|
||
Game::COUNTRIES.each { |country| Team.find_or_create_by(team_name: country) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should just make the Team call in You could also do this in a |
||
|
||
let(:game) { FactoryGirl.create(:game) } | ||
|
||
before(:each) do | ||
Game::COUNTRIES.each do |country| | ||
#Income starts at 6. | ||
team = Team.find_by_team_name(country) | ||
income = Income.find_or_create_by(game: game, round: 0, amount: 6, team: team) | ||
end | ||
end | ||
|
||
it 'can get amount of credits from income object' do | ||
income_for_usa = income_for_team('USA') | ||
income_for_china = income_for_team('China') | ||
income_for_france = income_for_team('France') | ||
|
||
expect(income_for_usa.credits).to eq(11) | ||
expect(income_for_china.credits).to eq(10) | ||
expect(income_for_france.credits).to eq(9) | ||
|
||
income_for_usa.amount = 7 | ||
income_for_china.amount = 7 | ||
income_for_france.amount = 7 | ||
|
||
expect(income_for_usa.credits).to eq(13) | ||
expect(income_for_china.credits).to eq(12) | ||
expect(income_for_france.credits).to eq(10) | ||
end | ||
|
||
it 'can get amount of credits from income object when amount higher than expected maximum' do | ||
income_for_usa = income_for_team('USA') | ||
|
||
income_for_usa.amount = 9 | ||
expect(income_for_usa.credits).to eq(17) | ||
income_for_usa.amount = 15 | ||
expect(income_for_usa.credits).to eq(17) | ||
end | ||
|
||
it 'can get amount of credits from income object when amount lower than 1' do | ||
income_for_usa = income_for_team('USA') | ||
|
||
income_for_usa.amount = 0 | ||
expect(income_for_usa.credits).to eq(0) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An other possibility to get this array for the corresponding team could be to add they array to the team model.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now, the user/team structure is sort of in flux. It's sort of a messed up design which needs to be rethought. So it's fine to leave this here until.