update blank question rule

This commit is contained in:
LostInLinearPast 2025-10-31 13:58:45 +08:00
parent dcd3e0a386
commit 024a9672d5
4 changed files with 55 additions and 13 deletions

1
.gitignore vendored
View File

@ -33,3 +33,4 @@ build/
.vscode/
out/
src/main/resources/bak.yml

View File

@ -202,7 +202,7 @@ public class PlayerController {
question.setOptions(result.toString());
} else if(type == 2){
String options = question.getOptions();
String processed = options.replaceAll("\\$\\{.*?}", "\\${}");
String processed = options.replaceAll("\\$(\\d+)?\\{.*?}", "\\${}");
String[] parts = processed.split("(?<=\\$\\{})|(?=\\$\\{})");
JsonArray result = new Gson().toJsonTree(Arrays.asList(parts)).getAsJsonArray();
question.setOptions(result.toString());
@ -280,20 +280,45 @@ public class PlayerController {
List<String> resultList = new ArrayList<>();
JsonArray asJsonArray = JsonParser.parseString(answer).getAsJsonArray();
for (JsonElement element : asJsonArray) {
resultList.add(element.getAsString());
resultList.add(element.getAsString().trim());
}
String regex = "\\$\\{(.+?)}";
String regex = "\\$(\\d+)?\\{(.+?)}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(questions.getOptions());
List<String> result = new ArrayList<>();
int score = 0;
int i = 0;
int j = 0; //没有标记分数的答对题目数
int k = 0; //没有标记分数的答案数量
//后续计算之后表示所有未标记分数的答案的总分
int maxScore = questions.getScore();
while (matcher.find()) {
result.add(matcher.group(1));
String scoreString = matcher.group(1);
String answerString = matcher.group(2);
int parseInt = 0;
try {parseInt = Integer.parseInt(scoreString);}
catch (NumberFormatException ignored) {}
//若没有标记分数k++
//若标记了分数最大分数减去后续计算未标记分数的答案的均分
if(parseInt == 0) k++;
else maxScore -= parseInt;
if (Arrays.stream(answerString.split("\\|")).toList().contains(resultList.get(i))) {
score += parseInt;
if(parseInt == 0) j++;
}
i++;
}
if(resultList.equals(result)){
playerAnswers.setScore(questions.getScore());
}else {
playerAnswers.setScore(0);
if(k > 0) {
//未标记分数的答案的每题平均分
int averageScore = Math.round((float)maxScore / k);
//答对的未标记 * 未标记的平均分 = 未标记的答对总分
int rightScore;
//如果答对的未标记 == 总未标记直接给未标记的满分为了弥补四舍五入带来的误差
//否则直接给 未标记平均分 * 未标记答对
if(j == k) rightScore = maxScore;
else rightScore = averageScore * j;
score += rightScore;
}
playerAnswers.setScore(score);
}
answers.add(playerAnswers);
}

View File

@ -14,6 +14,8 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@RestController
@RequestMapping("/api/questions")
@ -25,6 +27,10 @@ public class QuestionController {
public Result<?> saveQuestion(@RequestBody QuestionSaveDTO questionSaveDTO) {
if (questionSaveDTO != null) {
Questions questions = new Questions();
if(questionSaveDTO.getId() != null) questions.setId(questionSaveDTO.getId());
questions.setTitle(questionSaveDTO.getTitle());
questions.setScore(questionSaveDTO.getScore());
questions.setType(questionSaveDTO.getType());
if(questionSaveDTO.getType() == (byte) 1){
JsonArray inputArray = new JsonArray();
JsonArray correct = new JsonArray();
@ -45,11 +51,20 @@ public class QuestionController {
questions.setOptions(resultArray.toString());
} else if (questionSaveDTO.getType() == (byte) 2) {
questions.setOptions(questionSaveDTO.getBlankContent());
Integer originScore = questions.getScore();
String regex = "\\$(\\d+)\\{(.+?)}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(questions.getOptions());
int score = 0;
while (matcher.find()) {
String scoreString = matcher.group(1);
int parseInt = 0;
try {parseInt = Integer.parseInt(scoreString);}
catch (NumberFormatException ignored) {}
score += parseInt;
}
if(score > originScore) questions.setScore(score);
}
if(questionSaveDTO.getId() != null) questions.setId(questionSaveDTO.getId());
questions.setTitle(questionSaveDTO.getTitle());
questions.setScore(questionSaveDTO.getScore());
questions.setType(questionSaveDTO.getType());
Questions result = questionsService.saveQuestions(questions);
return result == null ? Result.error("服务器错误") : Result.success();
}

View File

@ -27,6 +27,7 @@ public class Players {
@Column(name = "create_time")
private LocalDateTime createTime;
@Lob
private String description;
@OneToOne
@JoinColumn(name = "email")