Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WCA Live Result Admin #1: Submit and Updating Results #10776

Merged
merged 25 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions app/controllers/live_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,74 @@
# frozen_string_literal: true

class LiveController < ApplicationController
def admin
@competition_id = params[:competition_id]
@competition = Competition.find(@competition_id)
@round = Round.find(params[:round_id])
@event_id = @round.event.id
@competitors = @round.registrations_with_wcif_id
end

def add_result
results = params.require(:attempts)
round_id = params.require(:round_id)
registration_id = params.require(:registration_id)

if LiveResult.exists?(round_id: round_id, registration_id: registration_id)
return render json: { status: "result already exist" }, status: :unprocessable_entity
end

AddLiveResultJob.perform_later(results, round_id, registration_id, current_user)

render json: { status: "ok" }
end

def update_result
results = params.require(:attempts)
round = Round.find(params.require(:round_id))
registration_id = params.require(:registration_id)

result = LiveResult.includes(:live_attempts).find_by(round: round, registration_id: registration_id)

return render json: { status: "result does not exist" }, status: :unprocessable_entity unless result.present?

previous_attempts = result.live_attempts

new_attempts = results.map.with_index(1) do |r, i|
same_result = previous_attempts.find_by(result: r, attempt_number: i)
if same_result.present?
same_result
else
different_result = previous_attempts.find_by(attempt_number: i)
new_result = LiveAttempt.build(result: r, attempt_number: i, entered_at: Time.now.utc, entered_by: current_user)
different_result&.update(replaced_by_id: new_result.id)
new_result
end
end

# TODO: What is the best way to do this?
r = Result.build({ value1: results[0], value2: results[1], value3: results[2], value4: results[3] || 0, value5: results[4] || 0, event_id: round.event.id, round_type_id: round.round_type_id, format_id: round.format_id })

result.update(average: r.compute_correct_average, best: r.compute_correct_best, live_attempts: new_attempts, last_attempt_entered_at: Time.now.utc)

render json: { status: "ok" }
end

def round_results
round_id = params.require(:round_id)

# TODO: Figure out why this fires a query for every live_attempt
# LiveAttempt Load (0.6ms) SELECT `live_attempts`.* FROM `live_attempts` WHERE `live_attempts`.`live_result_id` = 39 AND `live_attempts`.`replaced_by_id` IS NULL ORDER BY `live_attempts`.`attempt_number` ASC
render json: Round.includes(live_results: [:live_attempts, :round, :event]).find(round_id).live_results
end

def double_check
@round = Round.find(params.require(:round_id))
@competition = Competition.find(params.require(:competition_id))

@competitors = @round.registrations_with_wcif_id
end

def schedule_admin
@competition_id = params.require(:competition_id)
@competition = Competition.find(@competition_id)
Expand Down
9 changes: 8 additions & 1 deletion app/models/live_attempt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
class LiveAttempt < ApplicationRecord
include Comparable

belongs_to :live_result
# If the Attempt has been replaced, it no longer points to a live_result, but instead is being pointed to
# by another Attempt
belongs_to :live_result, optional: true
belongs_to :replaced_by, class_name: "LiveAttempt", optional: true
validate :needs_live_result_or_replaced_by

belongs_to :entered_by, class_name: 'User'

Expand All @@ -25,4 +26,10 @@ def serializable_hash(options = nil)
def <=>(other)
result <=> other.result
end

def needs_live_result_or_replaced_by
if replaced_by.nil? && live_result.nil?
errors.add(:replaced_by, "When unlinking an attempt from a live result you need to set replaced_by")
end
end
end
35 changes: 35 additions & 0 deletions app/models/round.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,41 @@ def name
Round.name_from_attributes(event, round_type)
end

def registrations
if number == 1
Registration.joins(:registration_competition_events)
.where(
competition_id: competition_event.competition_id,
competing_status: 'accepted',
registration_competition_events: { competition_event_id: competition_event_id },
).includes([:user])
else
advancing = previous_round.live_results.where(advancing: true).pluck(:registration_id)
Registration.find(advancing)
end
end

def registrations_with_wcif_id
FinnIckler marked this conversation as resolved.
Show resolved Hide resolved
if number == 1
Registration.where(competition_id: competition_event.competition_id)
.includes([:user])
.wcif_ordered
.to_enum
.with_index(1)
.select { |r, registrant_id| r.competing_status == 'accepted' && r.event_ids.include?(event.id) }
.map { |r, registrant_id| r.as_json({ include: [user: { only: [:name], methods: [], include: [] }] }).merge("registration_id" => registrant_id) }
else
advancing = previous_round.live_results.where(advancing: true).pluck(:registration_id)
Registration.where(competition_id: competition_event.competition_id)
.includes([:user])
.wcif_ordered
.to_enum
.with_index(1)
.select { |r, registrant_id| advancing.include?(r.id) }
.map { |r, registrant_id| r.as_json({ include: [user: { only: [:name], methods: [], include: [] }] }).merge("registration_id" => registrant_id) }
end
end

def time_limit_to_s
time_limit.to_s(self)
end
Expand Down
5 changes: 5 additions & 0 deletions app/views/live/admin.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% provide(:title, @round.name) %>

<%= render layout: 'competitions/nav' do %>
<%= react_component('Live/Admin/Results', { competitionId: @competition_id, competitors: @competitors, roundId: @round.id, eventId: @event_id }) %>
<% end %>
5 changes: 5 additions & 0 deletions app/views/live/double_check.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% provide(:title, "#{@round.name} Double Check") %>

<%= render layout: 'competitions/nav' do %>
<%= react_component('Live/Admin/DoubleCheck', { competitors: @competitors.as_json({ methods: [:name, :user]}), round: @round.as_json( { include: [:event], only: [:id], methods: [:name]}), competitionId: @competition.id, }) %>
<% end %>
144 changes: 144 additions & 0 deletions app/webpacker/components/Live/Admin/DoubleCheck/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import React, {
useMemo, useState,
} from 'react';
import {
Button, Card,
Grid, Message,
Segment,
} from 'semantic-ui-react';
import { useMutation, useQuery } from '@tanstack/react-query';
import { events } from '../../../../lib/wca-data.js.erb';
import WCAQueryClientProvider from '../../../../lib/providers/WCAQueryClientProvider';
import updateRoundResults from '../../api/updateRoundResults';
import getRoundResults from '../../api/getRoundResults';
import Loading from '../../../Requests/Loading';
import AttemptsForm from '../../components/AttemptsForm';

export default function Wrapper({
competitionId, competitors, round,
}) {
if (competitors.length === 0) {
return (
<Message negative>
No Results entered yet
</Message>
);
}

return (
<WCAQueryClientProvider>
<DoubleCheck
competitionId={competitionId}
competitors={competitors}
round={round}
/>
</WCAQueryClientProvider>
);
}

function DoubleCheck({
competitionId, competitors, round,
}) {
const event = events.byId[round.event_id];
const solveCount = event.recommendedFormat().expectedSolveCount;
const [currentIndex, setCurrentIndex] = useState(0);
const [registrationId, setRegistrationId] = useState(competitors[0].id);
FinnIckler marked this conversation as resolved.
Show resolved Hide resolved
FinnIckler marked this conversation as resolved.
Show resolved Hide resolved
const [attempts, setAttempts] = useState(_.times(solveCount, _.constant(0)));

const { data: results, isLoading } = useQuery({
queryKey: [round.id, 'results'],
queryFn: () => getRoundResults(round.id, competitionId),
});

const {
mutate: mutateUpdate, isPending: isPendingUpdate,
} = useMutation({
mutationFn: updateRoundResults,
});

const handleSubmit = async () => {
mutateUpdate({
roundId: round.id, registrationId, competitionId, attempts,
});
};

const handleAttemptChange = (index, value) => {
const newAttempts = [...attempts];
newAttempts[index] = value;
setAttempts(newAttempts);
};

const handleRegistrationIdChange = (_, { value }) => {
setRegistrationId(value);
};

const currentCompetitor = useMemo(() => {
if (results) {
return competitors.find((r) => r.id === results[currentIndex].registration_id);
}
return {};
}, [competitors, currentIndex, results]);

const onPrevious = () => {
setAttempts(results[currentIndex - 1].attempts.map((a) => a.result));
setCurrentIndex((oldIndex) => oldIndex - 1);
};

const onNext = () => {
setAttempts(results[currentIndex + 1].attempts.map((a) => a.result));
setCurrentIndex((oldIndex) => oldIndex + 1);
};

if (isLoading) {
return <Loading />;
}

return (
<Grid>
<Grid.Column width={1} verticalAlign="middle">
{ currentIndex !== 0 && <Button onClick={onPrevious}>{'<'}</Button>}
</Grid.Column>
<Grid.Column width={7}>
<Segment loading={isPendingUpdate}>
<AttemptsForm
registrationId={currentCompetitor.id}
handleAttemptChange={handleAttemptChange}
handleSubmit={handleSubmit}
handleRegistrationIdChange={handleRegistrationIdChange}
header="Double Check Result"
attempts={attempts}
competitors={competitors}
solveCount={solveCount}
eventId={round.event_id}
/>
</Segment>
</Grid.Column>
<Grid.Column width={1} verticalAlign="middle">
{currentIndex !== results.length - 1 && <Button onClick={onNext}>{'>'}</Button>}
</Grid.Column>
<Grid.Column textAlign="center" width={7} verticalAlign="middle">
<Card fluid raised>
<Card.Header>
{currentIndex + 1}
{' '}
of
{' '}
{results.length}
<br />
{round.name}
</Card.Header>
<Card.Content>
Double-check
</Card.Content>
<Card.Description>
Here you can iterate over results ordered by entry time (newest first).
When doing double-check you can place a
scorecard next to the form to quickly compare attempt results.
For optimal experience make sure to always put
entered/updated scorecard at the top of the pile.
</Card.Description>
</Card>
</Grid.Column>
</Grid>
);
}
Loading