Skip to content
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

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Then run the following to start the application:

This will start the server.

To start the server locally and setup the test database, you will need postgresql installed with a root/root user.

If you need to reset to seed after creating the database, run rake db:reset

Other rake commands available via rake -T
Expand Down
37 changes: 9 additions & 28 deletions app/controllers/api/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,33 +150,14 @@ def income
user_team = Team.find_by_id(current_user.team_id)

public_relations_list = PublicRelation.where(game: @game, team: user_team, round: round - 1)
income_level = Income.where(game: @game, round: round, team: user_team).limit(1).pluck(:amount)

# TO DO: This is in progress of being renamed.
reserves = BonusCredit.where(game: @game, round: round, team: user_team).limit(1).pluck(:amount)

# TO DO: This still needs to be moved.
@income_values = {}
@income_values['Brazil'] =[2,5,6,7,8,9,10,11,12]
@income_values['China']= [2,3,5,7,9,10,12,14,16]
@income_values['France']= [2,5,6,7,8,9,10,11,12]
@income_values['India']= [2,5,6,7,8,9,10,11,12]
@income_values['Japan']= [3,5,6,8,9,10,12,13,14]
@income_values['Russian Federation'] = [2,4,5,6,7,8,9,10,11]
@income_values['United Kingdom'] =[2,4,6,7,8,9,10,11,12]
@income_values['USA']= [1,3,5,7,9,11,13,15,17]
@income_values['Germany'] =[2,5,6,7,8,9,10,12,14]

# Don't explode if there is no data yet.
if income_level[0]
if income_level[0] >= @income_values[user_team.team_name].length
income_value = @income_values[user_team.team_name][@income_values[user_team.team_name].length - 1]
else
income_value = @income_values[user_team.team_name][income_level[0] - 1]
end
else
income_level = 0
income_value = 0
end
team_bonus_credits = BonusCredit.find_by(game: @game, round: round, team: user_team)
reserves = team_bonus_credits ? team_bonus_credits.amount : 0

team_income = Income.find_by(game: @game, round: round, team: user_team)
income_level = team_income ? team_income.amount : 0
income_value = team_income ? team_income.credits : 0

begin
#generate overall embedded result
Expand All @@ -185,8 +166,8 @@ def income
"round"=> round,
},
"pr" => public_relations_list,
"income_level" => income_level[0],
"reserves" => reserves[0],
"income_level" => income_level,
"reserves" => reserves,
"income_value" => income_value,
}
rescue
Expand Down
17 changes: 0 additions & 17 deletions app/controllers/games_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,7 @@ def human_control
)
@countries = Team.countries
@teams = Team.all_without_incomes

#To Do: Move income values into stored structure somewhere
@income_values = {}
@income_values['Brazil'] =[2,5,6,7,8,9,10,11,12]
@income_values['China']= [2,3,5,7,9,10,12,14,16]
@income_values['France']= [2,5,6,7,8,9,10,11,12]
@income_values['India']= [2,5,6,7,8,9,10,11,12]
@income_values['Japan']= [3,5,6,8,9,10,12,13,14]
@income_values['Russian Federation'] = [2,4,5,6,7,8,9,10,11]
@income_values['United Kingdom'] =[2,4,6,7,8,9,10,11,12]
@income_values['USA']= [1,3,5,7,9,11,13,15,17]
@income_values['Germany'] =[2,5,6,7,8,9,10,12,14]

# Todo: Refactor this so that we get the team name with the income
@incomes = @game.incomes.where(round: @current_round)
if @last_round > 0
@previous_income = @game.incomes.where(round: @last_round)
end
end

def update_den
Expand Down
39 changes: 39 additions & 0 deletions app/models/income.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,46 @@ class Income < ActiveRecord::Base
belongs_to :game
belongs_to :team

COUNTRY_CREDIT_LEVELS = {
'Brazil' =>
[2, 5, 6, 7, 8, 9, 10, 11, 12],
'China' =>
[2, 3, 5, 7, 9, 10, 12, 14, 16],
'France' =>
[2, 5, 6, 7, 8, 9, 10, 11, 12],
'India' =>
[2, 5, 6, 7, 8, 9, 10, 11, 12],
'Japan' =>
[3, 5, 6, 8, 9, 10, 12, 13, 14],
'Russian Federation' =>
[2, 4, 5, 6, 7, 8, 9, 10, 11],
'United Kingdom' =>
[2, 4, 6, 7, 8, 9, 10, 11, 12],
'USA' =>
[1, 3, 5, 7, 9, 11, 13, 15, 17],
'Germany' =>
[2, 5, 6, 7, 8, 9, 10, 12, 14]
}
COUNTRY_CREDIT_LEVELS.default = [2, 5, 6, 7, 8, 9, 10, 11, 12]

def team_name
self.team.team_name
end

def credits
income_to_credit = COUNTRY_CREDIT_LEVELS[self.team_name]
safe_get_credits_value(income_to_credit)
end

private
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
8 changes: 1 addition & 7 deletions app/views/games/human_control.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@
<tr>
<td><%= income.team_name %></td>
<td><%= income.amount%></td>
<td>
<% if @income_values[income.team_name].length <= income.amount %>
<%= @income_values[income.team_name][@income_values[income.team_name].length - 1] %>
<% else %>
<%= @income_values[income.team_name][income.amount-1]%>
<% end %>
</td>
<td><%= income.credits%></td>
<td><%= @game.bonus_credits.where(team: income.team, round: @current_round, recurring: false).sum(:amount) %></td>
<td><%= @game.bonus_credits.where('team_id = ? AND round <= ? AND recurring = TRUE', income.team_id, @current_round).sum(:amount) %></td>
</tr>
Expand Down
4 changes: 2 additions & 2 deletions app/views/users/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
<%= f.hidden_field :role, :value => @user.role %>

<%= f.label :team %> </br>
<%= f.select :team, options_for_select(@teams.collect{ |team| [team.team_name, team.id]}) %></br><br />
<%= f.select :team, options_for_select(@teams.collect{ |team| [team.team_name, team.id]}) %></br></br>

<%= f.label :team_role %> </br>
<%= f.select :team_role, options_for_select(@team_roles.collect{ |role| [role.role_display_name, role.id]}) %></br><br />
<%= f.select :team_role, options_for_select(@team_roles.collect{ |role| [role.role_display_name, role.id]}) %></br></br>

<% if @super_admin %>
<%= f.label :game %> </br>
Expand Down
3 changes: 2 additions & 1 deletion spec/models/game_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
require 'rails_helper'
RSpec.describe Game, :type => :model do
let(:game){ FactoryGirl.create(:game)}

Game::COUNTRIES.each{|country| Team.find_or_create_by(team_name: country)}

let(:game){ FactoryGirl.create(:game)}

before(:each) do
Game::COUNTRIES.each do |country|
#Income starts at 6.
Expand Down
50 changes: 49 additions & 1 deletion spec/models/income_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,53 @@
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__}"

let(:game) { FactoryGirl.create(:game) }

before(:each) do
Game::COUNTRIES.each do |country|
#Income starts at 6.
team = Team.find_or_create_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