-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Curura rankings controller and services (#134)
- Loading branch information
Showing
14 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
module V1 | ||
module Curura | ||
class GamesController < ApplicationController | ||
def create | ||
::Curura::Game.create!(game_params) | ||
end | ||
|
||
private | ||
|
||
def game_params | ||
params.require(:game).permit(:country, :score, :timezone) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
module V1 | ||
module Curura | ||
class RankingsController < ApplicationController | ||
def index | ||
set_rankings | ||
set_leaderboards | ||
end | ||
|
||
private | ||
|
||
def set_rankings | ||
@international_rank, @national_rank = | ||
::Curura::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 | ||
@games ||= ::Curura::Game.today | ||
end | ||
|
||
def country | ||
@country ||= params.require(:country) | ||
end | ||
|
||
def score | ||
params.require(:score).to_i | ||
end | ||
end | ||
end | ||
end | ||
end |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Curura | ||
def self.table_name_prefix | ||
"curura_" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Curura::Game < ApplicationRecord | ||
validates :score, :country, :start_time, presence: true | ||
|
||
scope :today, -> { where("start_time >= ?", Time.zone.now.beginning_of_day) } | ||
scope :won_above_score, ->(score) { where("score > ?", score) } | ||
scope :country, ->(country) { where(country: country) } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CreateCururaGames < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :curura_games do |t| | ||
t.float :score, default: 0.0 | ||
t.string :country | ||
t.datetime :start_time | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddTimezoneToCururaGames < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :curura_games, :timezone, :string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FactoryBot.define do | ||
factory :curura_game, class: "Curura::Game" do | ||
score { 15 } | ||
country { "MyString" } | ||
start_time { "2024-04-11 19:38:30" } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Curura::Game, type: :model do | ||
describe "validations" do | ||
it { is_expected.to validate_presence_of(:score) } | ||
it { is_expected.to validate_presence_of(:country) } | ||
it { is_expected.to validate_presence_of(:start_time) } | ||
end | ||
end |