This commit is contained in:
Evan Burkey 2025-01-25 17:37:01 -08:00
parent d2a9bed4da
commit 38308f6483
6 changed files with 58 additions and 18 deletions

View File

@ -1,5 +1,5 @@
class LeaderboardController < ApplicationController
def index
@players = Player.order(purse: :desc)
@players = Player.where(main_player_id: nil).order(purse: :desc)
end
end

View File

@ -1,2 +1,43 @@
class Player < ApplicationRecord
# A Player can be an alt of another player (the main)
belongs_to :main_player, class_name: "Player", optional: true, foreign_key: "main_player_id"
# A Player can have multiple alts
has_many :alternate_players, class_name: "Player", foreign_key: "main_player_id"
validate :no_circular_references
def total_wins
w = self.wins
alternate_players.each { |alt|
w += alt.wins
}
w
end
def total_losses
l = self.losses
alternate_players.each { |alt|
l += alt.losses
}
l
end
def total_purse
p = self.purse
alternate_players.each { |alt|
p += alt.purse
}
p
end
def alt?
self.main_player
end
def no_circular_references
if main_player_id.present? && (main_player.main_player== self)
errors.add(:main_account, "circular reference")
end
end
end

View File

@ -1,19 +1,19 @@
<h1>Force Kin Gamba Leaderboard</h1>
<% if @players.any? %>
<div class="throne">
<div class="throne-box">
<h3 class="throne-title gambalord">Gambalord</h3>
<h4 class="throne-name"><%= @players.first.name %></h4>
<h5 class="throne-purse"><%= @players.first.purse %></h5>
<h5 class="throne-purse"><%= @players.first.total_purse %></h5>
</div>
<div class="throne-box">
<h3 class="throne-title gumbalord">Gumbalord</h3>
<h4 class="throne-name"><%= @players.last.name %></h4>
<h5 class="throne-purse"><%= @players.last.purse %></h5>
<h5 class="throne-purse"><%= @players.last.total_purse %></h5>
</div>
</div>
<% if @players.any? %>
<div class="wrapper">
<div class="table">
<div class="row header">
@ -25,14 +25,14 @@
<% @players.each do |player| %>
<div class="row">
<div class="cell" data-title="Player"><%= player.name %></div>
<div class="cell" data-title="Purse"><%= player.purse %></div>
<div class="cell" data-title="Wins"><%= player.wins %></div>
<div class="cell" data-title="Losses"><%= player.losses %></div>
<div class="cell" data-title="Purse"><%= player.total_purse %></div>
<div class="cell" data-title="Wins"><%= player.total_wins %></div>
<div class="cell" data-title="Losses"><%= player.total_losses %></div>
</div>
<% end %>
</div>
<a href="/games">Game History</a>
</div>
<% else %>
<p>No Players!</p>
<h1>No Players!</h1>
<% end %>

View File

@ -0,0 +1,6 @@
class AddMainPlayerIdToPlayers < ActiveRecord::Migration[8.0]
def change
add_column :players, :main_player_id, :integer
add_index :players, :main_player_id
end
end

4
db/schema.rb generated
View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2025_01_24_201602) do
ActiveRecord::Schema[8.0].define(version: 2025_01_26_011348) do
create_table "api_keys", force: :cascade do |t|
t.string "key"
t.string "client"
@ -39,5 +39,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_24_201602) do
t.integer "purse"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "main_player_id"
t.index ["main_player_id"], name: "index_players_on_main_player_id"
end
end

View File

@ -1,9 +0,0 @@
# This file should ensure the existence of records required to run the application in every environment (production,
# development, test). The code here should be idempotent so that it can be executed at any point in every environment.
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
#
# Example:
#
# ["Action", "Comedy", "Drama", "Horror"].each do |genre_name|
# MovieGenre.find_or_create_by!(name: genre_name)
# end