Skip to content

Commit

Permalink
Generate proper error message so that #23 is understood by the user
Browse files Browse the repository at this point in the history
  • Loading branch information
dbroeglin committed Jul 28, 2024
1 parent 7ae8f3d commit 94fd036
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 32 deletions.
31 changes: 21 additions & 10 deletions app/models/taikai.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,16 @@ def delete_scores
teams.each do |team|
team.scores.destroy_all
end
if form_matches?
matches.each do |match|
match.update!(winner: nil)
end
return unless form_matches?

matches.each do |match|
match.update!(winner: nil)
end
end

def self.create_from_2in1(taikai_id, current_user, shortname_suffix, name_suffix, bracket_size)
logger.info "Creating new '#{name_suffix}' Taikai from 2in1 Taikai #{taikai_id} with suffix #{shortname_suffix}"

taikai = Taikai.includes(
{
participating_dojos: [
Expand Down Expand Up @@ -183,6 +185,19 @@ def self.create_from_2in1(taikai_id, current_user, shortname_suffix, name_suffix
return new_taikai
end

old_teams = taikai.teams.ranked(true).values.flatten
if old_teams.size < bracket_size
new_taikai.errors.add(:base, :not_enough_teams, bracket_size: bracket_size)
return new_taikai
end

old_teams = old_teams.reject(&:mixed).take(bracket_size)
if old_teams.size < bracket_size
new_taikai.errors.add(:base, :not_enough_non_mixed_teams_html,
bracket_size: bracket_size)
return new_taikai
end

return new_taikai unless new_taikai.save

taikai.staffs.each do |staff|
Expand All @@ -207,11 +222,7 @@ def self.create_from_2in1(taikai_id, current_user, shortname_suffix, name_suffix

# TODO: select only the 4/8 best teams to copy
new_teams = []
taikai
.teams.ranked(true).values.flatten
.reject(&:mixed) # TODO: generate error if not enough teams
.first(bracket_size)
.each_with_index do |team, index|
old_teams.each_with_index do |team, index|
logger.info "Creating new team #{team.shortname}"
new_team = new_participating_dojos[team.participating_dojo_id].teams.create!(
shortname: team.shortname,
Expand Down Expand Up @@ -306,4 +317,4 @@ def to_ascii
def taikai
self
end
end
end
4 changes: 2 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
<% if notice %>
<div class="notification is-info mb-5">
<button class="delete"></button>
<%= notice %>
<%= sanitize(notice) %>
</div>
<% end %>
<% if alert %>
<div class="notification is-danger mb-5">
<button class="delete"></button>
<%= alert %>
<%= sanitize(alert) %>
</div>
<% end %>
</div>
Expand Down
4 changes: 3 additions & 1 deletion config/locales/models/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ fr:
taikai:
attributes:
base:
not_finalized: Tous les résultats doivent être finalisés avant de créer le tournoi par équipe.
not_finalized: Tous les résultats doivent être finalisés avant de créer le tournoi en matchs.
not_enough_teams: Il n'y a pas assez d'équipes pour créer un tournoi en matchs de %{bracket_size} équipes.
not_enough_non_mixed_teams_html: Il n'y a pas assez d'équipes <b>non mixtes</b> pour créer un tournoi en matchs de %{bracket_size} équipes.
total_num_arrows:
inclusion: "doit être 8, 12 ou 20 pour kinteki, 4 pour des les matches et libre pour enteki."
tachi_size:
Expand Down
19 changes: 7 additions & 12 deletions docs/NOTES.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
Useful notes
============
# Useful notes

Commands
--------
## Commands

Automatically generate random results for all participants in a Taikai:

Expand All @@ -11,15 +9,15 @@ Automatically generate random results for all participants in a Taikai:
.each { |r|
r.status = ['hit', 'miss'].sample
r.final = true
r.save
r.save!
}

Taikai.find_by(shortname: '2in1-test')
.participants.map(&:results).flatten
.each { |r|
r.status = ['hit', 'miss'].sample
r.final = true
r.save
r.save!
}

Only generate results for first round:
Expand All @@ -30,7 +28,7 @@ Only generate results for first round:
.each { |r|
r.status = ['hit', 'miss'].sample
r.final = true
r.save
r.save!
}

Automatically generate random results for a specific match in a Taikai:
Expand All @@ -40,7 +38,7 @@ Automatically generate random results for a specific match in a Taikai:
.each { |r|
r.status = ['hit', 'miss'].sample
r.final = true
r.save
r.save!
}

Reset all results in a Taikai:
Expand All @@ -61,17 +59,14 @@ Generate results for an Enteki Taikai:
r.value = [0, 3, 5, 7, 9, 10].sample
r.status = r.value == 0 ? 'miss' : 'hit'
r.final = true
r.save
r.save!
}


Reset all passwords:

User.all.each { |u| u.update(encrypted_password: User.new.send(:password_digest, 'password')) }


Rebuild fixtures:

rake spec:fixture_builder:rebuild
RAILS_ENV=test rake db:fixtures:load

3 changes: 3 additions & 0 deletions lib/tasks/ops.rake
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ namespace :ops do
my_ip = my_ip()
rg_name = 'rg-pikaichu-production-002'
server_name = `az postgres flexible-server list --resource-group #{rg_name} --query '[0].name' --output tsv`.strip

raise "Unable to get server name from Azure" if server_name.empty?

sh "az postgres flexible-server firewall-rule update " \
"--name #{server_name} --resource-group #{rg_name} " \
"--rule-name Backup --start-ip-address #{my_ip} --end-ip-address #{my_ip}"
Expand Down
45 changes: 44 additions & 1 deletion test/models/taikai_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ class TaikaiTest < ActiveSupport::TestCase

TAIKAI_DATA.each do |form, distributed, total_num_arrows, scoring|
dist = distributed ? :distributed : :local
next unless distributed

test "#{form} #{dist} #{total_num_arrows} #{scoring} validates" do
@taikai.scoring = scoring
@taikai.distributed = distributed
@taikai.form = form
@taikai.total_num_arrows = total_num_arrows
@taikai.save!
end if distributed
end
end

[
Expand Down Expand Up @@ -64,4 +66,45 @@ class TaikaiTest < ActiveSupport::TestCase

# TODO: test that we cannot change the form, scoring, etc.
end

test "cannot create part two without enough teams" do
@taikai = taikais(:'2in1_dist_12_kinteki')
@taikai.current_user = users(:jean_bon)
@taikai.transition_to!(:registration)
@taikai.teams.last.destroy!
@taikai.transition_to!(:marking)
generate_taikai_results(@taikai)
@taikai.transition_to!(:tie_break)
@taikai.transition_to!(:done)
new_taikai = Taikai.create_from_2in1(@taikai.id, users(:jean_bon), "part2", "partie 2", 8)

assert_equal :not_enough_teams, new_taikai.errors.details[:base].first[:error]
end

test "cannot create part two without enough non-mixed teams" do
@taikai = taikais(:'2in1_dist_12_kinteki')
@taikai.current_user = users(:jean_bon)
@taikai.transition_to!(:registration)
@taikai.teams.each { |team| team.update(mixed: true) }
@taikai.transition_to!(:marking)
generate_taikai_results(@taikai)
@taikai.transition_to!(:tie_break)
@taikai.transition_to!(:done)
new_taikai = Taikai.create_from_2in1(@taikai.id, users(:jean_bon), "part2", "partie 2", 8)

assert_equal :not_enough_non_mixed_teams_html, new_taikai.errors.details[:base].first[:error]
end

test "can create part two" do
@taikai = taikais(:'2in1_dist_12_kinteki')
@taikai.current_user = users(:jean_bon)
@taikai.transition_to!(:registration)
@taikai.transition_to!(:marking)
generate_taikai_results(@taikai)
@taikai.transition_to!(:tie_break)
@taikai.transition_to!(:done)
new_taikai = Taikai.create_from_2in1(@taikai.id, users(:jean_bon), "part2", "partie 2", 8)

assert new_taikai.errors.empty?
end
end
19 changes: 13 additions & 6 deletions test/taikais_test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,21 @@ def taikai_shortname(form, distributed, total_num_arrows, scoring)
end

def find_test_taikai(*data)
begin
Taikai.find_by!(shortname: taikai_shortname(*data))
rescue ActiveRecord::RecordNotFound
raise "Taikai #{taikai_shortname(*data)} not found in test database"
end
Taikai.find_by!(shortname: taikai_shortname(*data))
rescue ActiveRecord::RecordNotFound
raise "Taikai #{taikai_shortname(*data)} not found in test database"
end

def assert_taikai_title(name)
assert_selector 'p.title.is-4', text: name
end
end

def generate_taikai_results(taikai)
scope = Result.joins(score: { participant: :taikai }).where('taikais.id = ?', taikai.id)
if taikai.scoring == 'kinteki'
scope.update_all(status: :hit, final: true)
elsif taikai.scoring == 'enteki'
scope.update_all(status: :hit, value: 3, final: true)
end
end
end

0 comments on commit 94fd036

Please sign in to comment.