merged
This commit is contained in:
commit
fa6f0774a1
4 changed files with 64 additions and 40 deletions
|
@ -1,5 +1,7 @@
|
|||
package be.jeffcheasey88.peeratcode.model;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Completion {
|
||||
private int puzzleId;
|
||||
private int playerId;
|
||||
|
@ -7,19 +9,29 @@ public class Completion {
|
|||
private String fileName;
|
||||
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();
|
||||
}
|
||||
|
||||
private void updateScore() {
|
||||
if (tries > 1) {
|
||||
score = score * (1 - ((tries - 1) / 10));
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -20,7 +20,6 @@ public enum DatabaseQuery {
|
|||
GET_GROUP_ID_BY_DATA("SELECT id_group FROM groups WHERE name = ? AND fk_chapter = ? AND fk_puzzle = ?"),
|
||||
INSERT_GROUP("INSERT INTO groups (name, fk_chapter, fk_puzzle) VALUES (?,?,?)"),
|
||||
INSERT_PLAYER_IN_GROUP("INSERT INTO containsGroups (fk_player, fk_group) VALUES (?,?)"),
|
||||
UPDATE_COMPLETION("UPDATE completions SET tries = ?, filename = ?, score = ? WHERE fk_puzzle = ? AND fk_player = ?"),
|
||||
LEAVE_GROUP("DELETE FROM containsGroups WHERE fk_player = ? AND fk_group = ?"),
|
||||
|
||||
// LEADERBOARD
|
||||
|
@ -45,6 +44,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
|
||||
|
|
|
@ -43,7 +43,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);
|
||||
}
|
||||
|
||||
|
@ -53,9 +54,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 {
|
||||
|
@ -83,6 +83,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"));
|
||||
}
|
||||
|
@ -457,14 +458,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;
|
||||
|
@ -475,7 +478,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());
|
||||
|
@ -537,13 +539,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();
|
||||
}
|
||||
}
|
|
@ -20,6 +20,7 @@ import be.jeffcheasey88.peeratcode.framework.Route;
|
|||
import be.jeffcheasey88.peeratcode.framework.User;
|
||||
import be.jeffcheasey88.peeratcode.model.Completion;
|
||||
import be.jeffcheasey88.peeratcode.model.Player;
|
||||
import be.jeffcheasey88.peeratcode.model.Puzzle;
|
||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||
|
||||
public class PuzzleResponse implements Response {
|
||||
|
@ -33,13 +34,19 @@ public class PuzzleResponse implements Response {
|
|||
|
||||
@Route(path = "^\\/puzzleResponse\\/([0-9]+)$", type = POST, needLogin = true)
|
||||
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());
|
||||
|
@ -54,10 +61,14 @@ public class PuzzleResponse implements Response {
|
|||
writer.write(responseJSON.toJSONString());
|
||||
}
|
||||
|
||||
private void saveSourceCode(ReceivedResponse received, Player player) throws IOException {
|
||||
Path path = Paths.get(String.format("%s/%s/puz%04d-%s", usersFilesPath, player.getPseudo(),
|
||||
received.getPuzzleId(), received.getFileName()));
|
||||
Files.write(path, received.getSourceCode());
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -70,10 +81,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);
|
||||
|
|
Loading…
Add table
Reference in a new issue