Skip to content

Commit

Permalink
override the session break; closes #989
Browse files Browse the repository at this point in the history
  • Loading branch information
jflamy committed Jan 14, 2025
1 parent cd61855 commit 2a882e1
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 32 deletions.
58 changes: 57 additions & 1 deletion owlcms/src/main/java/app/owlcms/data/group/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@
import app.owlcms.data.athlete.Athlete;
import app.owlcms.data.athlete.AthleteRepository;
import app.owlcms.data.athleteSort.AbstractLifterComparator;
import app.owlcms.data.competition.Competition;
import app.owlcms.data.config.Config;
import app.owlcms.data.platform.Platform;
import app.owlcms.fieldofplay.FieldOfPlay;
import app.owlcms.init.OwlcmsSession;
import app.owlcms.utils.DateTimeUtils;
import app.owlcms.utils.IdUtils;
Expand Down Expand Up @@ -283,7 +285,6 @@ public static DisplayGroup getEmptyDisplayGroup() {
@Column(columnDefinition = "boolean default false")
private boolean done;
@Id
// @GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String jury1;
private String jury2;
Expand All @@ -303,6 +304,8 @@ public static DisplayGroup getEmptyDisplayGroup() {
private String weighIn1;
private String weighIn2;
private LocalDateTime weighInTime;
@Column(columnDefinition = "integer default null")
private Integer cleanJerkBreakDuration;
@Transient
@JsonIgnore
Pattern pattern = Pattern.compile("(\\d+)\\s+(\\w+)");
Expand Down Expand Up @@ -1237,4 +1240,57 @@ private void setHourFormatter(Locale locale) {
.ofLocalizedTime(FormatStyle.SHORT)
.withLocale(locale));
}

public Integer getCleanJerkBreakDuration() {
return cleanJerkBreakDuration;
}

public void setCleanJerkBreakDuration(Integer cleanJerkBreakDuration) {
this.cleanJerkBreakDuration = cleanJerkBreakDuration;
}

public int cjBreakDuration(FieldOfPlay fieldOfPlay) {
Group cGroup = fieldOfPlay.getGroup();
int millisRemaining;
Competition cCur = Competition.getCurrent();
Integer cleanJerkBreakDuration = cGroup.getCleanJerkBreakDuration();
if (cleanJerkBreakDuration != null) {
millisRemaining = cleanJerkBreakDuration * 60 * 1000;
} else {
millisRemaining = 10 * 60 * 1000;
int size = fieldOfPlay.getLiftingOrder().size();
if (cCur.getShorterBreakMin() != null && size > cCur.getShorterBreakMin()) {
millisRemaining = (cCur.getShorterBreakDuration() != null ? cCur.getShorterBreakDuration() : 10) * 60
* 1000;
} else if (cCur.getLongerBreakMax() != null && size < cCur.getLongerBreakMax()) {
millisRemaining = (cCur.getLongerBreakDuration() != null ? cCur.getLongerBreakDuration() : 10) * 60
* 1000;
}
}
return millisRemaining;
}

@Transient
@JsonIgnore
public int getCleanJerkBreakMinutes() {
int minutesRemaining = 0;
Competition cCur = Competition.getCurrent();
Integer cleanJerkBreakDuration = this.getCleanJerkBreakDuration();
if (cleanJerkBreakDuration != null) {
minutesRemaining = cleanJerkBreakDuration;
} else {
minutesRemaining = 10;
List<Athlete> athletes = this.getAthletes();
int size = athletes != null ? athletes.size() : 0;
if (cCur.getShorterBreakMin() != null && size > cCur.getShorterBreakMin()) {
minutesRemaining = (cCur.getShorterBreakDuration() != null ? cCur.getShorterBreakDuration() : 10);
} else if (cCur.getLongerBreakMax() != null && size < cCur.getLongerBreakMax()) {
minutesRemaining = (cCur.getLongerBreakDuration() != null ? cCur.getLongerBreakDuration() : 10);
}
}
return minutesRemaining;
}

public void setCleanJerkBreakMinutes(int ignored) {
}
}
11 changes: 10 additions & 1 deletion owlcms/src/main/java/app/owlcms/fieldofplay/FOPEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import app.owlcms.data.athlete.Athlete;
import app.owlcms.data.category.Category;
import app.owlcms.data.group.Group;
import app.owlcms.data.jpa.JPAService;
import app.owlcms.init.OwlcmsSession;
import app.owlcms.uievents.BreakType;
import app.owlcms.uievents.CeremonyType;
Expand Down Expand Up @@ -627,7 +628,15 @@ public boolean equals(Object obj) {
}

public Group getGroup() {
return this.group;
// force reloading the group.
if (group == null) {
return null;
}
Group updatedGroup = JPAService.runInTransaction(em -> {
Group updated = em.find(Group.class, this.group.getId());
return updated;
});
return updatedGroup;
}

@Override
Expand Down
13 changes: 3 additions & 10 deletions owlcms/src/main/java/app/owlcms/fieldofplay/FieldOfPlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -2181,14 +2181,7 @@ private void pushOutSnatchDone() {
if (CompetitionSimulator.isRunning()) {
millisRemaining = 10 * 1000;
} else {
millisRemaining = 10 * 60 * 1000;
if (cCur.getShorterBreakMin() != null && this.liftingOrder.size() > cCur.getShorterBreakMin()) {
millisRemaining = (cCur.getShorterBreakDuration() != null ? cCur.getShorterBreakDuration() : 10) * 60
* 1000;
} else if (cCur.getLongerBreakMax() != null && this.liftingOrder.size() < cCur.getLongerBreakMax()) {
millisRemaining = (cCur.getLongerBreakDuration() != null ? cCur.getLongerBreakDuration() : 10) * 60
* 1000;
}
millisRemaining = group.cjBreakDuration(this);
if (millisRemaining <= 0) {
return;
}
Expand Down Expand Up @@ -2286,10 +2279,10 @@ private void recomputeCurrentLeaders(List<Athlete> rankedAthletes) {
}
List<Athlete> nLeaders = new ArrayList<Athlete>(getLeaders().size());
for (Athlete a : getLeaders()) {
// the leaders come from the medals computation. This does not reflect the last requested weight changes
// the leaders come from the medals computation. This does not reflect the last requested weight changes
// fetch the most up-to-date info for athletes in the current group.
Athlete found = null;
for (Athlete curGroupAthlete: getLiftingOrder()) {
for (Athlete curGroupAthlete : getLiftingOrder()) {
if (curGroupAthlete.getId().equals(a.getId())) {
found = curGroupAthlete;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ private GridCrud<Group> createGrid(OwlcmsCrudFormFactory<Group> crudFormFactory)
grid.addColumn(Group::getName).setHeader(Translator.translate("Name")).setComparator(Group::compareTo).setAutoWidth(true);
grid.addColumn(Group::getDescription).setHeader(Translator.translate("Group.Description")).setAutoWidth(true);
grid.addColumn(Group::size).setHeader(Translator.translate("GroupSize")).setTextAlign(ColumnTextAlign.CENTER);
grid.addColumn(Group::getCleanJerkBreakMinutes).setHeader(Translator.translate("CJ_BreakDuration")).setTextAlign(ColumnTextAlign.CENTER);
grid.addColumn(LocalDateTimeField.getRenderer(Group::getWeighInTime, this.getLocale()))
.setHeader(Translator.translate("WeighInTime")).setComparator(Group::compareToWeighIn);
grid.addColumn(LocalDateTimeField.getRenderer(Group::getCompetitionTime, this.getLocale()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@
import com.vaadin.flow.component.orderedlayout.FlexLayout.FlexDirection;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.tabs.TabSheet;
import com.vaadin.flow.component.textfield.NumberField;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.component.textfield.TextFieldVariant;
import com.vaadin.flow.data.binder.Binder;
import com.vaadin.flow.data.binder.BinderValidationStatus;
import com.vaadin.flow.data.binder.Result;
import com.vaadin.flow.data.binder.ValueContext;
import com.vaadin.flow.data.converter.Converter;
import com.vaadin.flow.data.provider.ListDataProvider;
import com.vaadin.flow.data.validator.StringLengthValidator;

Expand All @@ -48,7 +53,7 @@ public class SessionEditingFormFactory
extends OwlcmsCrudFormFactory<Group>
implements CustomFormFactory<Group> {

private static final String HEIGHT = "37rem";
private static final String HEIGHT = "32rem";
@SuppressWarnings("unused")
private Logger logger = (Logger) LoggerFactory.getLogger(SessionEditingFormFactory.class);
private SessionContent origin;
Expand Down Expand Up @@ -172,7 +177,7 @@ private void addRuler(FormLayout formLayout) {
private FlexLayout createTabSheets(Component footer, List<Platform> allPlatforms) {
TabSheet ts = new TabSheet();

FormLayout groupLayout = groupLayout(allPlatforms);
FormLayout groupLayout = sessionLayout(allPlatforms);
FormLayout officialsLayout = officialsLayout();
FormLayout juryLayout = juryLayout();

Expand Down Expand Up @@ -203,7 +208,7 @@ private FlexLayout createTabSheets(Component footer, List<Platform> allPlatforms
return mainLayout;
}

private FormLayout groupLayout(List<Platform> allPlatforms) {
private FormLayout sessionLayout(List<Platform> allPlatforms) {
FormLayout formLayout = new FormLayout();
TextField nameField = new TextField(Translator.translate("Name"));
formLayout.add(nameField);
Expand Down Expand Up @@ -263,6 +268,29 @@ private FormLayout groupLayout(List<Platform> allPlatforms) {
}
});

addRuler(formLayout);
NumberField breakDurationField = new NumberField(Translator.translate("CJ_BreakDuration"));
breakDurationField.setPlaceholder(Translator.translate("CJ_BreakDurationPlaceHolder"));
breakDurationField.addThemeVariants(TextFieldVariant.LUMO_ALIGN_RIGHT);
formLayout.add(breakDurationField);

// Add a custom converter to handle Integer values and null representation
binder.forField(breakDurationField).withConverter(
new Converter<Double, Integer>() {
@Override
public Result<Integer> convertToModel(Double value, ValueContext context) {
if (value == null) {
return Result.ok(null);
}
return Result.ok(value.intValue());
}

@Override
public Double convertToPresentation(Integer value, ValueContext context) {
return value == null ? null : value.doubleValue();
}
}).bind(Group::getCleanJerkBreakDuration, Group::setCleanJerkBreakDuration);

return formLayout;
}

Expand Down
38 changes: 31 additions & 7 deletions owlcms/src/main/java/app/owlcms/nui/shared/BreakManagement.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,13 @@ private void addListeners() {
computeDefaultTimeValues();
}
CountdownType mapBreakTypeToCountdownType = mapBreakTypeToDurationValue(bType);
// logger.debug("setting countdown {} ignored={}", mapBreakTypeToCountdownType);
setCountdownTypeValue(mapBreakTypeToCountdownType);
setBreakValue(bType);
if (bType == BreakType.FIRST_CJ) {
fixCJBreakDuration();
} else {
setDurationField(DEFAULT_DURATION);
}
setBreakType(bType);
this.startCountdown.setEnabled(true);
});
Expand All @@ -259,6 +263,17 @@ private void addListeners() {
});
}

public void fixCJBreakDuration() {
if (countdownRadios.getValue() == BreakType.FIRST_CJ) {
Group group2 = OwlcmsSession.getFop().getGroup();
if (group2 != null) {
int computedCJBreakDuration = group2.getCleanJerkBreakMinutes();
setDurationField(Duration.ofMinutes(computedCJBreakDuration));
} else {
}
}
}

private void assembleDialog(VerticalLayout dialogLayout) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("assembleDialog {} {}", this.requestedBreak, LoggerUtils.whereFrom());
Expand Down Expand Up @@ -310,7 +325,7 @@ private void assembleDialog(VerticalLayout dialogLayout) {
}

private void computeDefaultTimeValues() {
// logger.debug("setting default duration as default {}", LoggerUtils.whereFrom());
logger.debug("setting default duration as default {} {}", LoggerUtils.whereFrom(), this.fop.getBreakType());
setDurationField(DEFAULT_DURATION);

if (this.fop.getGroup() != null
Expand Down Expand Up @@ -785,11 +800,14 @@ private LocalDateTime getTarget() {

private void inferCountdownFromStage() {
switch (this.fop.getCurrentStage()) {
case CLEANJERK -> this.countdownRadios.setValue(BreakType.FIRST_CJ);
case CLEANJERK -> {
this.countdownRadios.setValue(BreakType.FIRST_CJ);
}
case SNATCH -> this.countdownRadios.setValue(BreakType.FIRST_SNATCH);
default -> throw new IllegalArgumentException("Unexpected value: " + this.fop.getCurrentStage());
}
this.durationField.setValue(DEFAULT_DURATION);
fixCJBreakDuration();
this.setCountdownType(CountdownType.DURATION);
}

Expand Down Expand Up @@ -989,10 +1007,16 @@ private void setBreakValue(BreakType breakType) {
// default value
switch (this.fop.getBreakType()) {
case BEFORE_INTRODUCTION -> this.countdownRadios.setValue(BreakType.BEFORE_INTRODUCTION);
case FIRST_CJ -> this.countdownRadios.setValue(BreakType.FIRST_CJ);
case FIRST_CJ -> {
this.countdownRadios.setValue(BreakType.FIRST_CJ);
fixCJBreakDuration();
}
case FIRST_SNATCH -> this.countdownRadios.setValue(BreakType.FIRST_SNATCH);
case GROUP_DONE -> this.countdownRadios.setValue(BreakType.FIRST_CJ);
case SNATCH_DONE -> this.countdownRadios.setValue(BreakType.FIRST_CJ);
case GROUP_DONE -> this.countdownRadios.setValue(BreakType.BEFORE_INTRODUCTION);
case SNATCH_DONE -> {
this.countdownRadios.setValue(BreakType.FIRST_CJ);
fixCJBreakDuration();
}
default -> inferCountdownFromStage();
}
}
Expand Down Expand Up @@ -1082,7 +1106,7 @@ private void setCountdownTypeValue(CountdownType ct2) {
}

private void setDurationField(Duration duration) {
// logger.debug("{} {}", duration, LoggerUtils.whereFrom());
logger.debug("=========== {} {}", duration, LoggerUtils.whereFrom());
this.durationField.setValue(duration);
}

Expand Down
Loading

0 comments on commit 2a882e1

Please sign in to comment.