Skip to content

Commit

Permalink
refactoring statistics to points services
Browse files Browse the repository at this point in the history
  • Loading branch information
kortirso committed Sep 2, 2023
1 parent fe12862 commit 7eb9af4
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 80 deletions.
12 changes: 6 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ GEM
msgpack (~> 1.2)
brakeman (6.0.1)
builder (3.2.4)
commento (0.1.2)
commento (0.1.3)
concurrent-ruby (1.2.2)
connection_pool (2.4.1)
crass (1.0.6)
Expand All @@ -134,7 +134,7 @@ GEM
concurrent-ruby (~> 1.0)
dry-core (~> 1.0, < 2)
zeitwerk (~> 2.6)
dry-schema (1.13.2)
dry-schema (1.13.3)
concurrent-ruby (~> 1.0)
dry-configurable (~> 1.0, >= 1.0.1)
dry-core (~> 1.0, < 2)
Expand Down Expand Up @@ -170,8 +170,8 @@ GEM
faraday-net_http (3.0.2)
ffi (1.15.5)
foreman (0.87.2)
globalid (1.1.0)
activesupport (>= 5.0)
globalid (1.2.0)
activesupport (>= 6.1)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
jsbundling-rails (1.1.2)
Expand Down Expand Up @@ -223,7 +223,7 @@ GEM
parser (3.2.2.3)
ast (~> 2.4.1)
racc
pg (1.5.3)
pg (1.5.4)
pghero (3.3.3)
activerecord (>= 6)
public_suffix (5.0.3)
Expand Down Expand Up @@ -322,7 +322,7 @@ GEM
rspec-mocks (~> 3.12)
rspec-support (~> 3.12)
rspec-support (3.12.1)
rubocop (1.56.1)
rubocop (1.56.2)
base64 (~> 0.1.1)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
Expand Down
8 changes: 6 additions & 2 deletions app/operations/games/update_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ module Games
class UpdateOperation
prepend ApplicationService

POINTS_CALCULATE_SERVICES = {
'basketball' => ::Games::Players::Points::Calculate::BasketballService,
'football' => ::Games::Players::Points::Calculate::FootballService
}.freeze

def initialize(
game_update_service: ::Games::UpdateService,
points_calculate_service: ::Games::Players::Points::CalculateService.new,
form_change_service: ::Teams::Players::Form::ChangeService,
lineups_players_points_update_job: ::Lineups::Players::Points::UpdateJob,
players_statistic_update_job: ::Players::Statistic::UpdateJob
)
@game_update_service = game_update_service
@points_calculate_service = points_calculate_service
@form_change_service = form_change_service
@lineups_players_points_update_job = lineups_players_points_update_job
@players_statistic_update_job = players_statistic_update_job
Expand All @@ -35,6 +38,7 @@ def call(game:, game_data:)
@games_players_update_data = []
@team_player_ids = []
@player_ids = []
@points_calculate_service = POINTS_CALCULATE_SERVICES[@game.week.season.league.sport_kind].new

# this data is updated immediately
ActiveRecord::Base.transaction do
Expand Down
35 changes: 12 additions & 23 deletions app/services/games/players/points/calculate/basketball_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,20 @@ module Games
module Players
module Points
module Calculate
class BasketballService
prepend ApplicationService

METHODS_FOR_STATS = {
'P' => 'field_goals_points',
'REB' => 'rebounds_points',
'A' => 'assists_points',
'BLK' => 'blocks_points',
'STL' => 'steals_points',
'TO' => 'turnovers_points'
}.freeze

# rubocop: disable Lint/UnusedMethodArgument
def call(statistic:, position_kind: nil)
@result = statistic.inject(0.0) do |acc, (param, value)|
method_name = METHODS_FOR_STATS[param]
next acc unless method_name

acc + send(method_name, value.to_i)
end
end
# rubocop: enable Lint/UnusedMethodArgument

class BasketballService < Games::Players::Points::CalculateService
private

def methods_for_stats
{
'P' => :field_goals_points,
'REB' => :rebounds_points,
'A' => :assists_points,
'BLK' => :blocks_points,
'STL' => :steals_points,
'TO' => :turnovers_points
}
end

def field_goals_points(value)
value
end
Expand Down
46 changes: 18 additions & 28 deletions app/services/games/players/points/calculate/football_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,26 @@ module Games
module Players
module Points
module Calculate
class FootballService
prepend ApplicationService

METHODS_FOR_STATS = {
'MP' => 'minutes_played_points',
'GS' => 'goals_scored_points',
'A' => 'assists_points',
'CS' => 'clean_sheets_points',
'GC' => 'goals_conceded_points',
'OG' => 'own_goals_points',
'PS' => 'penalties_saved_points',
'PM' => 'penalties_missed_points',
'YC' => 'yellow_cards_points',
'RC' => 'red_cards_points',
'S' => 'saves_points',
'B' => 'bonus_points'
}.freeze

def call(statistic:, position_kind:)
@position_kind = position_kind
@result = statistic.inject(0.0) do |acc, (param, value)|
method_name = METHODS_FOR_STATS[param]
next acc unless method_name

acc + send(method_name, value.to_i)
end
end

class FootballService < Games::Players::Points::CalculateService
private

def methods_for_stats
{
'MP' => :minutes_played_points,
'GS' => :goals_scored_points,
'A' => :assists_points,
'CS' => :clean_sheets_points,
'GC' => :goals_conceded_points,
'OG' => :own_goals_points,
'PS' => :penalties_saved_points,
'PM' => :penalties_missed_points,
'YC' => :yellow_cards_points,
'RC' => :red_cards_points,
'S' => :saves_points,
'B' => :bonus_points
}
end

def minutes_played_points(value)
return 2 if value >= 60

Expand Down
26 changes: 9 additions & 17 deletions app/services/games/players/points/calculate_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,20 @@ module Points
class CalculateService
prepend ApplicationService

def initialize(
football_service: Games::Players::Points::Calculate::FootballService,
basketball_service: Games::Players::Points::Calculate::BasketballService
)
@football_service = football_service
@basketball_service = basketball_service
end
def call(statistic:, position_kind: nil)
@position_kind = position_kind
@result = statistic.inject(0.0) do |acc, (param, value)|
method_name = methods_for_stats[param]
next acc unless method_name

def call(position_kind:, statistic:)
@result =
service_for_call(Sports.position(position_kind)['sport_kind'])
.call(position_kind: position_kind, statistic: statistic)
.result
acc + send(method_name, value.to_i)
end
end

private

def service_for_call(sport_kind)
case sport_kind
when Sportable::FOOTBALL then @football_service
when Sportable::BASKETBALL then @basketball_service
end
def methods_for_stats
raise NotImplementedError
end
end
end
Expand Down
6 changes: 2 additions & 4 deletions spec/operations/games/update_operation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
subject(:service_call) {
described_class
.new(
points_calculate_service: points_calculate_service,
form_change_service: form_change_service
)
.call(game: game, game_data: game_data)
}

let(:points_calculate_service) { double }
let(:form_change_service) { double }
let(:points_calculate_service_call) { double }
let(:points_result) { 10 }

let!(:game) { create :game, source: Sourceable::INSTAT }
Expand All @@ -31,8 +29,8 @@
create :games_player, game: game, teams_player: teams_player1
create :games_player, game: game, teams_player: teams_player2

allow(points_calculate_service).to receive(:call).and_return(points_calculate_service_call)
allow(points_calculate_service_call).to receive(:result).and_return(points_result)
allow(Games::Players::Points::Calculate::FootballService).to receive(:new).and_return(points_calculate_service)
allow(points_calculate_service).to receive_messages(call: points_calculate_service, result: points_result)
allow(form_change_service).to receive(:call)
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

describe Games::Players::Points::Calculate::BasketballService, type: :service do
subject(:service_call) { described_class.call(statistic: statistic) }

let(:statistic) { { 'P' => 10, 'REB' => 2, 'BLK' => 4 } }

it 'returns points' do
expect(service_call.result).to eq 24.4
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

describe Games::Players::Points::Calculate::FootballService, type: :service do
subject(:service_call) { described_class.call(statistic: statistic, position_kind: Positionable::GOALKEEPER) }

let(:statistic) { { 'MP' => 61, 'CS' => 1 } }

it 'returns points' do
expect(service_call.result).to eq 6
end
end

0 comments on commit 7eb9af4

Please sign in to comment.