Score in puzzle

This commit is contained in:
jeffcheasey88 2023-04-12 09:50:00 +02:00
parent a975b355b2
commit 1bbe38b540
11 changed files with 66 additions and 50 deletions

View file

@ -70,14 +70,6 @@ public class Main{
initRoutes(router, config); initRoutes(router, config);
startWebServer(config, router); startWebServer(config, router);
new TimerTask(){
int i = 4;
@Override
public void run() {
System.out.println("oui "+i);
}
};
} }
private static void initRoutes(Router router, Configuration config) { private static void initRoutes(Router router, Configuration config) {

View file

@ -17,11 +17,6 @@ public class HttpUtil{
writer.flush(); writer.flush();
} }
public static void skipHeaders(HttpReader reader) throws Exception{
String line;
while(((line = reader.readLine()) != null) && (line.length() > 0));
}
public static List<String> readMultiPartData(HttpReader reader) throws Exception{ public static List<String> readMultiPartData(HttpReader reader) throws Exception{
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
@ -29,9 +24,7 @@ public class HttpUtil{
while(reader.ready()){ while(reader.ready()){
String line; String line;
while (((line = reader.readLine()) != null) && (line.length() > 0)){ while (((line = reader.readLine()) != null) && (line.length() > 0));
}
String buffer = ""; String buffer = "";
while (((line = reader.readLine()) != null) && (!line.startsWith("------WebKitFormBoundary"))){ while (((line = reader.readLine()) != null) && (!line.startsWith("------WebKitFormBoundary"))){
buffer += line; buffer += line;
@ -42,6 +35,25 @@ public class HttpUtil{
return list; return list;
} }
/*
*
------WebKitFormBoundaryNUjiLBAMuX2dhxU7
Content-Disposition: form-data; name="answer"
12
------WebKitFormBoundaryNUjiLBAMuX2dhxU7
Content-Disposition: form-data; name="filename"
webpack.js
------WebKitFormBoundaryNUjiLBAMuX2dhxU7
Content-Disposition: form-data; name="code_file"; filename="webpack.js"
Content-Type: text/javascript
------WebKitFormBoundaryNUjiLBAMuX2dhxU7--
*
*/
public static void switchToWebSocket(HttpReader reader, HttpWriter writer) throws Exception{ public static void switchToWebSocket(HttpReader reader, HttpWriter writer) throws Exception{
String key = reader.getHeader("Sec-WebSocket-Key"); String key = reader.getHeader("Sec-WebSocket-Key");
if (key == null) throw new IllegalArgumentException(); if (key == null) throw new IllegalArgumentException();

View file

@ -47,6 +47,7 @@ public enum DatabaseQuery {
UPDATE_COMPLETION( UPDATE_COMPLETION(
"UPDATE completions SET tries = ?, score = ? WHERE fk_puzzle = ? AND fk_player = ?"), "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 = ?"),
SCORE_GROUP("SELECT c.score FROM containsGroups cg LEFT JOIN groups g ON cg.fk_group = g.id_group LEFT JOIN completions c ON cg.fk_player = c.fk_player WHERE cg.fk_player = ? AND g.fk_puzzle = ? AND c.fk_puzzle = g.fk_puzzle"),
// PLAYERS // PLAYERS
GET_PLAYER_SIMPLE("SELECT pseudo, email, firstname, lastname, description FROM players WHERE id_player = ?"), GET_PLAYER_SIMPLE("SELECT pseudo, email, firstname, lastname, description FROM players WHERE id_player = ?"),

View file

@ -136,16 +136,21 @@ public class DatabaseRepository {
return null; return null;
} }
public int getScore(int user, int puzzle) { public int getScore(int user, int puzzle){
try { try {
ensureConnection(); ensureConnection();
PreparedStatement stmt = DatabaseQuery.SCORE.prepare(this.con); PreparedStatement stmt = DatabaseQuery.SCORE_GROUP.prepare(this.con);
stmt.setInt(1, user);
stmt.setInt(2, puzzle);
ResultSet result = stmt.executeQuery();
if(result.next()) return result.getInt("score");
stmt = DatabaseQuery.SCORE.prepare(this.con);
stmt.setInt(1, user); stmt.setInt(1, user);
stmt.setInt(2, puzzle); stmt.setInt(2, puzzle);
ResultSet result = stmt.executeQuery(); result = stmt.executeQuery();
if (result.next()) if(result.next()) return result.getInt("score");
result.getInt("score");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View file

@ -24,9 +24,9 @@ public class ChapterElement implements Response {
} }
@Route(path = "^\\/chapter\\/([0-9]+)$", needLogin = true) @Route(path = "^\\/chapter\\/([0-9]+)$", needLogin = true)
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{
Chapter chapter = databaseRepo.getChapter(extractId(matcher)); Chapter chapter = databaseRepo.getChapter(Integer.parseInt(matcher.group(1)));
if (chapter != null) { if (chapter != null){
JSONObject chapterJSON = new JSONObject(); JSONObject chapterJSON = new JSONObject();
chapterJSON.put("id", chapter.getId()); chapterJSON.put("id", chapter.getId());
chapterJSON.put("name", chapter.getName()); chapterJSON.put("name", chapter.getName());
@ -35,12 +35,14 @@ public class ChapterElement implements Response {
if (chapter.getEndDate() != null) if (chapter.getEndDate() != null)
chapterJSON.put("endDate", chapter.getEndDate().toString()); chapterJSON.put("endDate", chapter.getEndDate().toString());
JSONArray puzzles = new JSONArray(); JSONArray puzzles = new JSONArray();
for (Puzzle puzzle : chapter.getPuzzles()) { for (Puzzle puzzle : chapter.getPuzzles()){
JSONObject puzzleJSON = new JSONObject(); JSONObject puzzleJSON = new JSONObject();
puzzleJSON.put("id", puzzle.getId()); puzzleJSON.put("id", puzzle.getId());
puzzleJSON.put("name", puzzle.getName()); puzzleJSON.put("name", puzzle.getName());
if (puzzle.getTags() != null) puzzleJSON.put("scoreMax", puzzle.getScoreMax());
puzzleJSON.put("tags", puzzle.getJsonTags()); if (puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getJsonTags());
int score = this.databaseRepo.getScore(user.getId(), puzzle.getId());
if(score >= 0) puzzleJSON.put("score", score);
puzzles.add(puzzleJSON); puzzles.add(puzzleJSON);
} }
chapterJSON.put("puzzles", puzzles); chapterJSON.put("puzzles", puzzles);
@ -51,7 +53,4 @@ public class ChapterElement implements Response {
} }
} }
private int extractId(Matcher matcher) {
return Integer.parseInt(matcher.group(1));
}
} }

View file

@ -29,10 +29,11 @@ public class PuzzleElement implements Response {
puzzleJSON.put("id", puzzle.getId()); puzzleJSON.put("id", puzzle.getId());
puzzleJSON.put("name", puzzle.getName()); puzzleJSON.put("name", puzzle.getName());
puzzleJSON.put("content", puzzle.getContent()); puzzleJSON.put("content", puzzle.getContent());
if (puzzle.getTags() != null) puzzleJSON.put("scoreMax", puzzle.getScoreMax());
puzzleJSON.put("tags", puzzle.getJsonTags()); if(puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getJsonTags());
if (puzzle.getDepend() > 0) int score = this.databaseRepo.getScore(user.getId(), puzzle.getId());
puzzleJSON.put("depend", puzzle.getDepend()); if(score >= 0) puzzleJSON.put("score", score);
if(puzzle.getDepend() > 0) puzzleJSON.put("depend", puzzle.getDepend());
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *", "Content-Type: application/json"); HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *", "Content-Type: application/json");
writer.write(puzzleJSON.toJSONString()); writer.write(puzzleJSON.toJSONString());
} }

View file

@ -25,8 +25,6 @@ public class GroupCreate implements Response {
@Route(path = "^\\/groupCreate$", type = POST, needLogin = true) @Route(path = "^\\/groupCreate$", type = POST, needLogin = true)
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 {
HttpUtil.skipHeaders(reader);
if (this.repo.insertGroup(new Group((JSONObject) HttpUtil.readJson(reader)), user)) { if (this.repo.insertGroup(new Group((JSONObject) HttpUtil.readJson(reader)), user)) {
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
} else { } else {

View file

@ -25,7 +25,6 @@ public class GroupJoin implements Response{
@Route(path = "^\\/groupJoin$", type = POST, needLogin = true) @Route(path = "^\\/groupJoin$", type = POST, needLogin = true)
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 {
HttpUtil.skipHeaders(reader);
if (this.repo.insertUserInGroup(new Group((JSONObject) HttpUtil.readJson(reader)), user)) { if (this.repo.insertUserInGroup(new Group((JSONObject) HttpUtil.readJson(reader)), user)) {
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
} else { } else {

View file

@ -25,8 +25,6 @@ public class GroupQuit implements Response{
@Route(path = "^\\/groupQuit$", type = POST, needLogin = true) @Route(path = "^\\/groupQuit$", type = POST, needLogin = true)
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 {
HttpUtil.skipHeaders(reader);
if (this.repo.leaveGroup(new Group((JSONObject) HttpUtil.readJson(reader)), user)) { if (this.repo.leaveGroup(new Group((JSONObject) HttpUtil.readJson(reader)), user)) {
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
} else { } else {

View file

@ -42,17 +42,11 @@ public class TmpRoutesTests {
@Test @Test
void testOnDeployed(){ void testOnDeployed(){
try { try {
JSONObject content = new JSONObject(); client.auth("JeffCheasey88", "TheoPueDesPieds");
content.put("avatar", ""); client.route("/puzzleResponse/8", "POST", WebClient.buildMultiPartData("1"));
content.put("description", ""); client.route("/puzzleResponse/8", "POST", WebClient.buildMultiPartData("1"));
content.put("email", "ouistiti@gmail.com"); client.route("/puzzleResponse/8", "POST", WebClient.buildMultiPartData("one"));
content.put("firstname", "Stiti");
content.put("lastname", "Oui");
content.put("passwd", "TheoPueDesPieds");
content.put("pseudo", "Ouistiti");
content.put("sgroup", "");
client.route("/register", "POST", content.toJSONString());
client.assertResponseCode(200); client.assertResponseCode(200);
}catch(Exception e){ }catch(Exception e){
e.printStackTrace(); e.printStackTrace();

View file

@ -4,6 +4,7 @@ import static org.junit.Assert.fail;
import java.net.Socket; import java.net.Socket;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -79,6 +80,8 @@ public class WebClient {
while(((line = reader.readLine()) != null) && line.length() > 0) this.headers.add(line); while(((line = reader.readLine()) != null) && line.length() > 0) this.headers.add(line);
while((line = reader.readLine()) != null) this.content.add(line); while((line = reader.readLine()) != null) this.content.add(line);
System.out.println(Arrays.toString(this.content.toArray(new String[0])));
} }
public void assertResponseCode(int expected){ public void assertResponseCode(int expected){
@ -95,4 +98,18 @@ public class WebClient {
} }
fail("Line <"+expected+"> not found in "+this.headers.size()+" headers"); fail("Line <"+expected+"> not found in "+this.headers.size()+" headers");
} }
public static String[] buildMultiPartData(String... datas){
if(datas == null) return new String[0];
String[] result = new String[1+(4*datas.length)];
int index = 0;
for(String data : datas){
result[index++] = "------WebKitFormBoundaryNUjiLBAMuX2dhxU7";
result[index++] = "Content-Disposition: form-data; name=\"?\"";
result[index++] = "";
result[index++] = data;
}
result[index++] = "------WebKitFormBoundaryNUjiLBAMuX2dhxU7";
return result;
}
} }