Update puzzle response to ensure 0 point on bad response which was not the case before
This commit is contained in:
parent
87c4fd8bc1
commit
909350972a
4 changed files with 65 additions and 42 deletions
|
@ -1,5 +1,7 @@
|
||||||
package be.jeffcheasey88.peeratcode.model;
|
package be.jeffcheasey88.peeratcode.model;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class Completion {
|
public class Completion {
|
||||||
private int puzzleId;
|
private int puzzleId;
|
||||||
private int playerId;
|
private int playerId;
|
||||||
|
@ -7,19 +9,29 @@ public class Completion {
|
||||||
private String fileName;
|
private String fileName;
|
||||||
private byte[] code;
|
private byte[] code;
|
||||||
private int score;
|
private int score;
|
||||||
|
|
||||||
public Completion(int playerId, int puzzleId, String fileName, int score) {
|
public Completion(int playerId, int puzzleId, int tries, String fileName, int score) {
|
||||||
this(playerId, puzzleId, -1, 1, fileName, score, null);
|
this(playerId, puzzleId, tries, score, fileName, null, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Completion(int playerId, int puzzleId, int idCompletion, int tries, String fileName, int score,
|
public Completion(int playerId, int puzzleId, String fileName, byte[] file, byte[] response, Puzzle currentPuzzle) {
|
||||||
byte[] file) {
|
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.playerId = playerId;
|
||||||
this.puzzleId = puzzleId;
|
this.puzzleId = puzzleId;
|
||||||
this.tries = tries;
|
|
||||||
this.fileName = fileName;
|
this.fileName = fileName;
|
||||||
|
if (code != null)
|
||||||
|
this.code = Arrays.copyOf(file, file.length);
|
||||||
|
|
||||||
|
// Updated with addTry
|
||||||
|
this.tries = tries;
|
||||||
this.score = score;
|
this.score = score;
|
||||||
this.code = file;
|
|
||||||
|
if (currentPuzzle != null)
|
||||||
|
addTry(currentPuzzle, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPuzzleId() {
|
public int getPuzzleId() {
|
||||||
|
@ -34,14 +46,17 @@ public class Completion {
|
||||||
return tries;
|
return tries;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addTry() {
|
public void addTry(Puzzle currentPuzzle, byte[] response) {
|
||||||
this.tries++;
|
if (score <= 0) {
|
||||||
updateScore();
|
tries++;
|
||||||
}
|
if (response != null && Arrays.equals(currentPuzzle.getSoluce(), response)) {
|
||||||
|
if (tries > 1) { // Loose 5% each try with a minimum of 1 for score
|
||||||
private void updateScore() {
|
score = (int) Math.ceil(currentPuzzle.getScoreMax() * (1 - ((tries - 1) / 20.)));
|
||||||
if (tries > 1) {
|
if (score < 1)
|
||||||
score = score * (1 - ((tries - 1) / 10));
|
score = 1;
|
||||||
|
} else
|
||||||
|
score = currentPuzzle.getScoreMax();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,8 +17,6 @@ public enum DatabaseQuery {
|
||||||
|
|
||||||
// GROUPS
|
// GROUPS
|
||||||
ALL_GROUPS("SELCT * FROM groups"), INSERT_GROUP("INSERT INTO groups (name, fk_chapter, fk_puzzle) VALUES (?,?,?)"),
|
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
|
// LEADERBOARD
|
||||||
ALL_PLAYERS_FOR_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 = ?"),
|
"SELECT id_completion, tries, fileName, score FROM completions WHERE fk_puzzle = ? AND fk_player = ?"),
|
||||||
INSERT_COMPLETION(
|
INSERT_COMPLETION(
|
||||||
"INSERT INTO completions (fk_puzzle, fk_player, tries, code, fileName, score) values (?, ?, ?, ?, ?, ?)"),
|
"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 = ?"),
|
SCORE("SELECT score FROM completions WHERE fk_player = ? AND fk_puzzle = ?"),
|
||||||
|
|
||||||
// PLAYERS
|
// PLAYERS
|
||||||
|
|
|
@ -42,7 +42,8 @@ public class DatabaseRepository {
|
||||||
|
|
||||||
private Puzzle makePuzzle(ResultSet puzzleResult) throws SQLException {
|
private Puzzle makePuzzle(ResultSet puzzleResult) throws SQLException {
|
||||||
return new Puzzle(puzzleResult.getInt("id_puzzle"), puzzleResult.getString("name"),
|
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);
|
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 {
|
private Completion makeCompletion(int playerId, int puzzleId, ResultSet completionResult) throws SQLException {
|
||||||
return new Completion(playerId, puzzleId, completionResult.getInt("id_completion"),
|
return new Completion(playerId, puzzleId, completionResult.getInt("tries"),
|
||||||
completionResult.getInt("tries"), completionResult.getString("fileName"),
|
completionResult.getString("fileName"), completionResult.getInt("score"));
|
||||||
completionResult.getInt("score"), null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Player makePlayer(ResultSet playerResult) throws SQLException {
|
private Player makePlayer(ResultSet playerResult) throws SQLException {
|
||||||
|
@ -82,6 +82,7 @@ public class DatabaseRepository {
|
||||||
private Group makeGroup(ResultSet result) throws SQLException {
|
private Group makeGroup(ResultSet result) throws SQLException {
|
||||||
return new Group(result.getString("name"), result.getInt("fk_chapter"), result.getInt("fk_puzzle"));
|
return new Group(result.getString("name"), result.getInt("fk_chapter"), result.getInt("fk_puzzle"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Player makeGroupPlayer(ResultSet result) throws SQLException {
|
private Player makeGroupPlayer(ResultSet result) throws SQLException {
|
||||||
return new Player(result.getString("pseudo"), result.getInt("score"), result.getInt("tries"));
|
return new Player(result.getString("pseudo"), result.getInt("score"), result.getInt("tries"));
|
||||||
}
|
}
|
||||||
|
@ -456,14 +457,16 @@ public class DatabaseRepository {
|
||||||
return -1;
|
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 {
|
try {
|
||||||
Puzzle currentPuzzle = getPuzzle(puzzleId);
|
ensureConnection();
|
||||||
Completion completion = getCompletion(userId, puzzleId);
|
Completion completion = getCompletion(userId, puzzleId);
|
||||||
if (completion == null) {
|
if (completion == null) {
|
||||||
insertCompletion(new Completion(userId, puzzleId, fileName, currentPuzzle.getScoreMax()));
|
completion = new Completion(userId, puzzleId, fileName, code, response, currentPuzzle);
|
||||||
|
insertCompletion(completion);
|
||||||
} else {
|
} else {
|
||||||
completion.addTry();
|
completion.addTry(currentPuzzle, response);
|
||||||
updateCompletion(completion);
|
updateCompletion(completion);
|
||||||
}
|
}
|
||||||
return completion;
|
return completion;
|
||||||
|
@ -474,7 +477,6 @@ public class DatabaseRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void insertCompletion(Completion newCompletion) throws SQLException {
|
private void insertCompletion(Completion newCompletion) throws SQLException {
|
||||||
// Insert completions
|
|
||||||
PreparedStatement statement = DatabaseQuery.INSERT_COMPLETION.prepare(this.con);
|
PreparedStatement statement = DatabaseQuery.INSERT_COMPLETION.prepare(this.con);
|
||||||
statement.setInt(1, newCompletion.getPuzzleId());
|
statement.setInt(1, newCompletion.getPuzzleId());
|
||||||
statement.setInt(2, newCompletion.getPlayerId());
|
statement.setInt(2, newCompletion.getPlayerId());
|
||||||
|
@ -499,13 +501,11 @@ public class DatabaseRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateCompletion(Completion completionToUpdate) throws SQLException {
|
private void updateCompletion(Completion completionToUpdate) throws SQLException {
|
||||||
// Update completions
|
|
||||||
PreparedStatement statement = DatabaseQuery.UPDATE_COMPLETION.prepare(this.con);
|
PreparedStatement statement = DatabaseQuery.UPDATE_COMPLETION.prepare(this.con);
|
||||||
statement.setInt(1, completionToUpdate.getTries());
|
statement.setInt(1, completionToUpdate.getTries());
|
||||||
statement.setString(2, completionToUpdate.getFileName());
|
statement.setInt(2, completionToUpdate.getScore());
|
||||||
statement.setInt(3, completionToUpdate.getScore());
|
statement.setInt(3, completionToUpdate.getPuzzleId());
|
||||||
statement.setInt(4, completionToUpdate.getPuzzleId());
|
statement.setInt(4, completionToUpdate.getPlayerId());
|
||||||
statement.setInt(5, completionToUpdate.getPlayerId());
|
|
||||||
statement.executeUpdate();
|
statement.executeUpdate();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -12,6 +12,7 @@ import org.json.simple.JSONObject;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Completion;
|
import be.jeffcheasey88.peeratcode.model.Completion;
|
||||||
import be.jeffcheasey88.peeratcode.model.Player;
|
import be.jeffcheasey88.peeratcode.model.Player;
|
||||||
|
import be.jeffcheasey88.peeratcode.model.Puzzle;
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||||
import be.jeffcheasey88.peeratcode.webserver.HttpReader;
|
import be.jeffcheasey88.peeratcode.webserver.HttpReader;
|
||||||
import be.jeffcheasey88.peeratcode.webserver.HttpUtil;
|
import be.jeffcheasey88.peeratcode.webserver.HttpUtil;
|
||||||
|
@ -29,16 +30,22 @@ public class PuzzleResponse implements Response {
|
||||||
usersFilesPath = initUsersFilesPath;
|
usersFilesPath = initUsersFilesPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Route(path = "^\\/puzzleResponse\\/([0-9]+)$", type = "POST", needLogin = true)
|
@Route(path = "^\\/puzzleResponse\\/([0-9]+)$", type = "POST")
|
||||||
@Override
|
@Override
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
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);
|
ReceivedResponse received = new ReceivedResponse(matcher, reader);
|
||||||
saveSourceCode(received, databaseRepo.getPlayer(user.getId()));
|
saveSourceCode(received, databaseRepo.getPlayer(user.getId()));
|
||||||
|
|
||||||
JSONObject responseJSON = new JSONObject();
|
JSONObject responseJSON = new JSONObject();
|
||||||
Completion completion = databaseRepo.insertOrUpdatePuzzleResponse(received.getPuzzleId(), 3,
|
Puzzle currentPuzzle = databaseRepo.getPuzzle(received.getPuzzleId());
|
||||||
received.getFileName(), received.getSourceCode());
|
Completion completion = databaseRepo.insertOrUpdatePuzzleResponse(received.getPuzzleId(), user.getId(),
|
||||||
if (Arrays.equals(received.getResponse(), databaseRepo.getPuzzle(received.getPuzzleId()).getSoluce())) {
|
received.getFileName(), received.getSourceCode(), received.getResponse(), currentPuzzle);
|
||||||
|
if (completion.getScore() > 0) {
|
||||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *", "Content-Type: application/json");
|
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *", "Content-Type: application/json");
|
||||||
responseJSON.put("score", completion.getScore());
|
responseJSON.put("score", completion.getScore());
|
||||||
responseJSON.put("tries", completion.getTries());
|
responseJSON.put("tries", completion.getTries());
|
||||||
|
@ -53,10 +60,14 @@ public class PuzzleResponse implements Response {
|
||||||
writer.write(responseJSON.toJSONString());
|
writer.write(responseJSON.toJSONString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveSourceCode(ReceivedResponse received, Player player) throws IOException {
|
private void saveSourceCode(ReceivedResponse received, Player player) {
|
||||||
Path path = Paths.get(String.format("%s/%s/puz%04d-%s", usersFilesPath, player.getPseudo(),
|
try {
|
||||||
received.getPuzzleId(), received.getFileName()));
|
Path path = Paths.get(String.format("%s/%s/puz%04d-%s", usersFilesPath, player.getPseudo(),
|
||||||
Files.write(path, received.getSourceCode());
|
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 String fileName;
|
||||||
private byte[] sourceCode;
|
private byte[] sourceCode;
|
||||||
|
|
||||||
private HttpReader reader;
|
|
||||||
|
|
||||||
public ReceivedResponse(Matcher matcher, HttpReader reader) throws Exception {
|
public ReceivedResponse(Matcher matcher, HttpReader reader) throws Exception {
|
||||||
this.reader = reader;
|
|
||||||
puzzleId = Integer.parseInt(matcher.group(1));
|
puzzleId = Integer.parseInt(matcher.group(1));
|
||||||
|
|
||||||
List<String> multiPartData = HttpUtil.readMultiPartData(reader);
|
List<String> multiPartData = HttpUtil.readMultiPartData(reader);
|
||||||
|
|
Loading…
Add table
Reference in a new issue