Update puzzle response to ensure 0 point on bad response which was not the case before

This commit is contained in:
Francois G 2023-04-09 21:02:03 +02:00
parent 87c4fd8bc1
commit 909350972a
4 changed files with 65 additions and 42 deletions

View file

@ -1,5 +1,7 @@
package be.jeffcheasey88.peeratcode.model;
import java.util.Arrays;
public class Completion {
private int puzzleId;
private int playerId;
@ -8,18 +10,28 @@ public class Completion {
private byte[] code;
private int score;
public Completion(int playerId, int puzzleId, String fileName, int score) {
this(playerId, puzzleId, -1, 1, fileName, score, null);
public Completion(int playerId, int puzzleId, int tries, String fileName, int score) {
this(playerId, puzzleId, tries, score, fileName, null, null, null);
}
public Completion(int playerId, int puzzleId, int idCompletion, int tries, String fileName, int score,
byte[] file) {
public Completion(int playerId, int puzzleId, String fileName, byte[] file, byte[] response, Puzzle currentPuzzle) {
this(playerId, puzzleId, 0, 0, fileName, file, response, currentPuzzle);
}
public Completion(int playerId, int puzzleId, int tries, int score, String fileName, byte[] file, byte[] response,
Puzzle currentPuzzle) {
this.playerId = playerId;
this.puzzleId = puzzleId;
this.tries = tries;
this.fileName = fileName;
if (code != null)
this.code = Arrays.copyOf(file, file.length);
// Updated with addTry
this.tries = tries;
this.score = score;
this.code = file;
if (currentPuzzle != null)
addTry(currentPuzzle, response);
}
public int getPuzzleId() {
@ -34,14 +46,17 @@ public class Completion {
return tries;
}
public void addTry() {
this.tries++;
updateScore();
public void addTry(Puzzle currentPuzzle, byte[] response) {
if (score <= 0) {
tries++;
if (response != null && Arrays.equals(currentPuzzle.getSoluce(), response)) {
if (tries > 1) { // Loose 5% each try with a minimum of 1 for score
score = (int) Math.ceil(currentPuzzle.getScoreMax() * (1 - ((tries - 1) / 20.)));
if (score < 1)
score = 1;
} else
score = currentPuzzle.getScoreMax();
}
private void updateScore() {
if (tries > 1) {
score = score * (1 - ((tries - 1) / 10));
}
}

View file

@ -17,8 +17,6 @@ public enum DatabaseQuery {
// GROUPS
ALL_GROUPS("SELCT * FROM groups"), INSERT_GROUP("INSERT INTO groups (name, fk_chapter, fk_puzzle) VALUES (?,?,?)"),
UPDATE_COMPLETION(
"UPDATE completions SET tries = ?, filename = ?, score = ? WHERE fk_puzzle = ? AND fk_player = ?"),
// LEADERBOARD
ALL_PLAYERS_FOR_LEADERBOARD(
@ -42,6 +40,8 @@ public enum DatabaseQuery {
"SELECT id_completion, tries, fileName, score FROM completions WHERE fk_puzzle = ? AND fk_player = ?"),
INSERT_COMPLETION(
"INSERT INTO completions (fk_puzzle, fk_player, tries, code, fileName, score) values (?, ?, ?, ?, ?, ?)"),
UPDATE_COMPLETION(
"UPDATE completions SET tries = ?, score = ? WHERE fk_puzzle = ? AND fk_player = ?"),
SCORE("SELECT score FROM completions WHERE fk_player = ? AND fk_puzzle = ?"),
// PLAYERS

View file

@ -42,7 +42,8 @@ public class DatabaseRepository {
private Puzzle makePuzzle(ResultSet puzzleResult) throws SQLException {
return new Puzzle(puzzleResult.getInt("id_puzzle"), puzzleResult.getString("name"),
puzzleResult.getString("content"), null, "", 0, puzzleResult.getString("tags"),
puzzleResult.getString("content"), puzzleResult.getBytes("soluce"), puzzleResult.getString("verify"),
puzzleResult.getInt("score_max"), puzzleResult.getString("tags"),
hasColumn(puzzleResult, "origin") ? puzzleResult.getInt("origin") : -1);
}
@ -52,9 +53,8 @@ public class DatabaseRepository {
}
private Completion makeCompletion(int playerId, int puzzleId, ResultSet completionResult) throws SQLException {
return new Completion(playerId, puzzleId, completionResult.getInt("id_completion"),
completionResult.getInt("tries"), completionResult.getString("fileName"),
completionResult.getInt("score"), null);
return new Completion(playerId, puzzleId, completionResult.getInt("tries"),
completionResult.getString("fileName"), completionResult.getInt("score"));
}
private Player makePlayer(ResultSet playerResult) throws SQLException {
@ -82,6 +82,7 @@ public class DatabaseRepository {
private Group makeGroup(ResultSet result) throws SQLException {
return new Group(result.getString("name"), result.getInt("fk_chapter"), result.getInt("fk_puzzle"));
}
private Player makeGroupPlayer(ResultSet result) throws SQLException {
return new Player(result.getString("pseudo"), result.getInt("score"), result.getInt("tries"));
}
@ -456,14 +457,16 @@ public class DatabaseRepository {
return -1;
}
public Completion insertOrUpdatePuzzleResponse(int puzzleId, int userId, String fileName, byte[] code) {
public Completion insertOrUpdatePuzzleResponse(int puzzleId, int userId, String fileName, byte[] code,
byte[] response, Puzzle currentPuzzle) {
try {
Puzzle currentPuzzle = getPuzzle(puzzleId);
ensureConnection();
Completion completion = getCompletion(userId, puzzleId);
if (completion == null) {
insertCompletion(new Completion(userId, puzzleId, fileName, currentPuzzle.getScoreMax()));
completion = new Completion(userId, puzzleId, fileName, code, response, currentPuzzle);
insertCompletion(completion);
} else {
completion.addTry();
completion.addTry(currentPuzzle, response);
updateCompletion(completion);
}
return completion;
@ -474,7 +477,6 @@ public class DatabaseRepository {
}
private void insertCompletion(Completion newCompletion) throws SQLException {
// Insert completions
PreparedStatement statement = DatabaseQuery.INSERT_COMPLETION.prepare(this.con);
statement.setInt(1, newCompletion.getPuzzleId());
statement.setInt(2, newCompletion.getPlayerId());
@ -499,13 +501,11 @@ public class DatabaseRepository {
}
private void updateCompletion(Completion completionToUpdate) throws SQLException {
// Update completions
PreparedStatement statement = DatabaseQuery.UPDATE_COMPLETION.prepare(this.con);
statement.setInt(1, completionToUpdate.getTries());
statement.setString(2, completionToUpdate.getFileName());
statement.setInt(3, completionToUpdate.getScore());
statement.setInt(4, completionToUpdate.getPuzzleId());
statement.setInt(5, completionToUpdate.getPlayerId());
statement.setInt(2, completionToUpdate.getScore());
statement.setInt(3, completionToUpdate.getPuzzleId());
statement.setInt(4, completionToUpdate.getPlayerId());
statement.executeUpdate();
}
}

View file

@ -12,6 +12,7 @@ import org.json.simple.JSONObject;
import be.jeffcheasey88.peeratcode.model.Completion;
import be.jeffcheasey88.peeratcode.model.Player;
import be.jeffcheasey88.peeratcode.model.Puzzle;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
import be.jeffcheasey88.peeratcode.webserver.HttpReader;
import be.jeffcheasey88.peeratcode.webserver.HttpUtil;
@ -29,16 +30,22 @@ public class PuzzleResponse implements Response {
usersFilesPath = initUsersFilesPath;
}
@Route(path = "^\\/puzzleResponse\\/([0-9]+)$", type = "POST", needLogin = true)
@Route(path = "^\\/puzzleResponse\\/([0-9]+)$", type = "POST")
@Override
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
if (user == null) {
HttpUtil.responseHeaders(writer, 401, "Access-Control-Allow-Origin: *");
return;
}
ReceivedResponse received = new ReceivedResponse(matcher, reader);
saveSourceCode(received, databaseRepo.getPlayer(user.getId()));
JSONObject responseJSON = new JSONObject();
Completion completion = databaseRepo.insertOrUpdatePuzzleResponse(received.getPuzzleId(), 3,
received.getFileName(), received.getSourceCode());
if (Arrays.equals(received.getResponse(), databaseRepo.getPuzzle(received.getPuzzleId()).getSoluce())) {
Puzzle currentPuzzle = databaseRepo.getPuzzle(received.getPuzzleId());
Completion completion = databaseRepo.insertOrUpdatePuzzleResponse(received.getPuzzleId(), user.getId(),
received.getFileName(), received.getSourceCode(), received.getResponse(), currentPuzzle);
if (completion.getScore() > 0) {
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *", "Content-Type: application/json");
responseJSON.put("score", completion.getScore());
responseJSON.put("tries", completion.getTries());
@ -53,10 +60,14 @@ public class PuzzleResponse implements Response {
writer.write(responseJSON.toJSONString());
}
private void saveSourceCode(ReceivedResponse received, Player player) throws IOException {
private void saveSourceCode(ReceivedResponse received, Player player) {
try {
Path path = Paths.get(String.format("%s/%s/puz%04d-%s", usersFilesPath, player.getPseudo(),
received.getPuzzleId(), received.getFileName()));
Files.write(path, received.getSourceCode());
} catch (IOException e) {
System.err.println("ERREUR D'ENREGISTREMENT DU CODE POUR PUZZLE_RESPONSE : " + e.toString());
}
}
@ -69,10 +80,7 @@ class ReceivedResponse {
private String fileName;
private byte[] sourceCode;
private HttpReader reader;
public ReceivedResponse(Matcher matcher, HttpReader reader) throws Exception {
this.reader = reader;
puzzleId = Integer.parseInt(matcher.group(1));
List<String> multiPartData = HttpUtil.readMultiPartData(reader);