Skip to content

Commit

Permalink
Add Curura rankings controller and services
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel-k committed Apr 11, 2024
1 parent ca29489 commit 861980b
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 0 deletions.
42 changes: 42 additions & 0 deletions app/controllers/api/v1/curura/rankings_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

module Api
module V1
module Curura
class RankingsController < ApplicationController
def index
binding.pry
set_rankings
set_leaderboards
end

private

def set_rankings
@international_rank, @national_rank =
RankingCalculator
.call(games: games, country: country, score: score)
.values_at(:international_rank, :national_rank)
end

def set_leaderboards
@best_players, @players_by_country =
Curura::Leaderboard.call(games: games).values_at(:best_players, :players_by_country)
end

def games
binding.pry
@games ||= CururaGame.today
end

def country
@country ||= params.require(:country)
end

def score
params.require(:score).to_i
end
end
end
end
end
Empty file removed app/controllers/concerns/.keep
Empty file.
31 changes: 31 additions & 0 deletions app/services/curura/leaderboard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Curura
class Leaderboard
class << self
def call(games:)
@games = games

{ best_players: best_players, players_by_country: players_by_country }
end

private

def best_players
@games
.order(score: :desc)
.limit(10)
.map
.with_index(1) { |game, rank| { rank: rank, score: game.score, country: game.country } }
end

def players_by_country
@games
.group_by(&:country)
.transform_values(&:count)
.sort_by { |country, count| [-count, country] }
.first(10)
.map
.with_index(1) { |(country, count), rank| { rank: rank, country: country, count: count } }
end
end
end
end
30 changes: 30 additions & 0 deletions app/services/curura/ranking_calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Curura
class RankingCalculator
class << self
def call(games:, score:, country:)
@games = games
@score = score
@country = country

{ international_rank: international_rank, national_rank: national_rank }
end

private

def international_rank
return "-" if @games.empty?

position = @games.won_above_score(@score).count.nonzero? || 1
"#{position}/#{@games.size}"
end

def national_rank
games = @games.country(@country)
return "-" if games.empty?
position = @games.country(@country).won_above_score(@score).count.nonzero? || 1

"#{position}/#{games.size}"
end
end
end
end
6 changes: 6 additions & 0 deletions app/views/api/v1/curura/rankings/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

json.national_rank @national_rank
json.international_rank @international_rank
json.best_players @best_players
json.players_by_country @players_by_country
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
resources :missing_words, only: %i[index create]
resources :rankings, only: %i[index]
resources :sms_forwarders, only: %i[create]
namespace :curura do
resources :rankings, only: %i[index]
end
end
end

Expand Down

0 comments on commit 861980b

Please sign in to comment.