+ added Answer, Calculator and SessionStatus

This commit is contained in:
danny 2026-09-19 14:09:16 +02:00
parent a5ff3da1ad
commit eddb88ad29
3 changed files with 96 additions and 0 deletions

View file

@ -0,0 +1,18 @@
package com.danieldiekert.buzzword.question;
import java.time.Instant;
public record Answer(
String playerId,
String value,
Instant submittedAt
) {
public Answer {
if (playerId == null || playerId.isBlank())
throw new IllegalArgumentException("playerId must not be blank");
if (value == null)
throw new IllegalArgumentException("value must not be null");
if (submittedAt == null)
throw new IllegalArgumentException("submittedAt must not be null");
}
}

View file

@ -0,0 +1,60 @@
package com.danieldiekert.buzzword.question;
import java.util.Locale;
public class ScoreCalculator {
public int score(Question question, String input) {
if (input == null || input.isBlank()) {
return 0;
}
return switch (question) {
case EstimateQuestion eq -> scoreEstimate(eq, input);
case ChoiceQuestion ch -> scoreChoice(ch, input);
case TextQuestion tq -> scoreText(tq, input);
};
}
private int scoreEstimate(EstimateQuestion question, String input) {
double guess;
try {
guess = Double.parseDouble(input.trim().replace(',', '.'));
} catch (NumberFormatException e) {
return 0;
}
double correct = question.correctValue();
if (correct == 0) {
return guess == 0 ? question.points() : 0;
}
double deviation = Math.abs(guess - correct) / Math.abs(correct);
if (deviation <= 0.05) return question.points();
if (deviation <= 0.15) return question.points() / 2;
return 0;
}
private int scoreChoice(ChoiceQuestion question, String input) {
int index;
try {
index = Integer.parseInt(input.trim());
} catch (NumberFormatException e) {
return 0;
}
return index == question.correctIndex() ? question.points() : 0;
}
private int scoreText(TextQuestion question, String input) {
String normalized = normalize(input);
if (normalize(question.correctAnswer()).equals(normalized)) {
return question.points();
}
return question.acceptedVariants().stream()
.map(this::normalize)
.anyMatch(normalized::equals)
? question.points()
: 0;
}
private String normalize(String value) {
return value.trim().toLowerCase(Locale.ROOT);
}
}

View file

@ -0,0 +1,18 @@
package com.danieldiekert.buzzword.session;
public enum SessionStatus {
PREPARING,
RUNNING,
QUESTION_OPEN,
LOCKED,
REVIEWING,
FINISHED;
public boolean allowsBuzzing() {
return this == QUESTION_OPEN;
}
public boolean isActive() {
return this != PREPARING && this != FINISHED;
}
}