diff --git a/src/be/jeffcheasey88/peeratcode/Main.java b/src/be/jeffcheasey88/peeratcode/Main.java index 9d3d298..af75795 100644 --- a/src/be/jeffcheasey88/peeratcode/Main.java +++ b/src/be/jeffcheasey88/peeratcode/Main.java @@ -70,14 +70,6 @@ public class Main{ initRoutes(router, config); 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) { diff --git a/src/be/jeffcheasey88/peeratcode/framework/HttpUtil.java b/src/be/jeffcheasey88/peeratcode/framework/HttpUtil.java index 2960886..b006821 100644 --- a/src/be/jeffcheasey88/peeratcode/framework/HttpUtil.java +++ b/src/be/jeffcheasey88/peeratcode/framework/HttpUtil.java @@ -17,11 +17,6 @@ public class HttpUtil{ writer.flush(); } - public static void skipHeaders(HttpReader reader) throws Exception{ - String line; - while(((line = reader.readLine()) != null) && (line.length() > 0)); - } - public static List readMultiPartData(HttpReader reader) throws Exception{ List list = new ArrayList<>(); @@ -29,9 +24,7 @@ public class HttpUtil{ while(reader.ready()){ String line; - while (((line = reader.readLine()) != null) && (line.length() > 0)){ - - } + while (((line = reader.readLine()) != null) && (line.length() > 0)); String buffer = ""; while (((line = reader.readLine()) != null) && (!line.startsWith("------WebKitFormBoundary"))){ buffer += line; @@ -41,6 +34,25 @@ public class HttpUtil{ 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{ String key = reader.getHeader("Sec-WebSocket-Key"); diff --git a/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java b/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java index 0d0eb57..d5d7da1 100644 --- a/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java +++ b/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java @@ -47,7 +47,8 @@ public enum DatabaseQuery { 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_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 GET_PLAYER_SIMPLE("SELECT pseudo, email, firstname, lastname, description FROM players WHERE id_player = ?"), GET_PLAYER_DETAILS( diff --git a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java index d9db14d..b998da9 100644 --- a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java +++ b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java @@ -136,16 +136,21 @@ public class DatabaseRepository { return null; } - public int getScore(int user, int puzzle) { + public int getScore(int user, int puzzle){ try { 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(2, puzzle); - ResultSet result = stmt.executeQuery(); - if (result.next()) - result.getInt("score"); + result = stmt.executeQuery(); + if(result.next()) return result.getInt("score"); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java b/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java index aa6f9c2..f6d349f 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java +++ b/src/be/jeffcheasey88/peeratcode/routes/ChapterElement.java @@ -24,9 +24,9 @@ public class ChapterElement implements Response { } @Route(path = "^\\/chapter\\/([0-9]+)$", needLogin = true) - public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception { - Chapter chapter = databaseRepo.getChapter(extractId(matcher)); - if (chapter != null) { + public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{ + Chapter chapter = databaseRepo.getChapter(Integer.parseInt(matcher.group(1))); + if (chapter != null){ JSONObject chapterJSON = new JSONObject(); chapterJSON.put("id", chapter.getId()); chapterJSON.put("name", chapter.getName()); @@ -35,12 +35,14 @@ public class ChapterElement implements Response { if (chapter.getEndDate() != null) chapterJSON.put("endDate", chapter.getEndDate().toString()); JSONArray puzzles = new JSONArray(); - for (Puzzle puzzle : chapter.getPuzzles()) { + for (Puzzle puzzle : chapter.getPuzzles()){ JSONObject puzzleJSON = new JSONObject(); puzzleJSON.put("id", puzzle.getId()); puzzleJSON.put("name", puzzle.getName()); - if (puzzle.getTags() != null) - puzzleJSON.put("tags", puzzle.getJsonTags()); + puzzleJSON.put("scoreMax", puzzle.getScoreMax()); + 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); } chapterJSON.put("puzzles", puzzles); @@ -51,7 +53,4 @@ public class ChapterElement implements Response { } } - private int extractId(Matcher matcher) { - return Integer.parseInt(matcher.group(1)); - } } diff --git a/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java b/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java index bcb359a..f7810dc 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java +++ b/src/be/jeffcheasey88/peeratcode/routes/PuzzleElement.java @@ -29,10 +29,11 @@ public class PuzzleElement implements Response { puzzleJSON.put("id", puzzle.getId()); puzzleJSON.put("name", puzzle.getName()); puzzleJSON.put("content", puzzle.getContent()); - if (puzzle.getTags() != null) - puzzleJSON.put("tags", puzzle.getJsonTags()); - if (puzzle.getDepend() > 0) - puzzleJSON.put("depend", puzzle.getDepend()); + puzzleJSON.put("scoreMax", puzzle.getScoreMax()); + 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); + if(puzzle.getDepend() > 0) puzzleJSON.put("depend", puzzle.getDepend()); HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *", "Content-Type: application/json"); writer.write(puzzleJSON.toJSONString()); } diff --git a/src/be/jeffcheasey88/peeratcode/routes/groups/GroupCreate.java b/src/be/jeffcheasey88/peeratcode/routes/groups/GroupCreate.java index 365c9fc..0ee4898 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/groups/GroupCreate.java +++ b/src/be/jeffcheasey88/peeratcode/routes/groups/GroupCreate.java @@ -25,8 +25,6 @@ public class GroupCreate implements Response { @Route(path = "^\\/groupCreate$", type = POST, needLogin = true) 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)) { HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); } else { diff --git a/src/be/jeffcheasey88/peeratcode/routes/groups/GroupJoin.java b/src/be/jeffcheasey88/peeratcode/routes/groups/GroupJoin.java index 4a1382b..c17fc47 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/groups/GroupJoin.java +++ b/src/be/jeffcheasey88/peeratcode/routes/groups/GroupJoin.java @@ -25,7 +25,6 @@ public class GroupJoin implements Response{ @Route(path = "^\\/groupJoin$", type = POST, needLogin = true) 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)) { HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); } else { diff --git a/src/be/jeffcheasey88/peeratcode/routes/groups/GroupQuit.java b/src/be/jeffcheasey88/peeratcode/routes/groups/GroupQuit.java index 8f579ac..dd21bdb 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/groups/GroupQuit.java +++ b/src/be/jeffcheasey88/peeratcode/routes/groups/GroupQuit.java @@ -25,8 +25,6 @@ public class GroupQuit implements Response{ @Route(path = "^\\/groupQuit$", type = POST, needLogin = true) 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)) { HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); } else { diff --git a/test/be/jeffcheasey88/peeratcode/routes/TmpRoutesTests.java b/test/be/jeffcheasey88/peeratcode/routes/TmpRoutesTests.java index f94d3f3..e28511a 100644 --- a/test/be/jeffcheasey88/peeratcode/routes/TmpRoutesTests.java +++ b/test/be/jeffcheasey88/peeratcode/routes/TmpRoutesTests.java @@ -42,17 +42,11 @@ public class TmpRoutesTests { @Test void testOnDeployed(){ try { - JSONObject content = new JSONObject(); - content.put("avatar", ""); - content.put("description", ""); - content.put("email", "ouistiti@gmail.com"); - content.put("firstname", "Stiti"); - content.put("lastname", "Oui"); - content.put("passwd", "TheoPueDesPieds"); - content.put("pseudo", "Ouistiti"); - content.put("sgroup", ""); + client.auth("JeffCheasey88", "TheoPueDesPieds"); + client.route("/puzzleResponse/8", "POST", WebClient.buildMultiPartData("1")); + client.route("/puzzleResponse/8", "POST", WebClient.buildMultiPartData("1")); + client.route("/puzzleResponse/8", "POST", WebClient.buildMultiPartData("one")); - client.route("/register", "POST", content.toJSONString()); client.assertResponseCode(200); }catch(Exception e){ e.printStackTrace(); diff --git a/test/be/jeffcheasey88/peeratcode/webclient/WebClient.java b/test/be/jeffcheasey88/peeratcode/webclient/WebClient.java index 32d00ee..3fcbc7c 100644 --- a/test/be/jeffcheasey88/peeratcode/webclient/WebClient.java +++ b/test/be/jeffcheasey88/peeratcode/webclient/WebClient.java @@ -4,6 +4,7 @@ import static org.junit.Assert.fail; import java.net.Socket; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.regex.Matcher; 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) this.content.add(line); + + System.out.println(Arrays.toString(this.content.toArray(new String[0]))); } public void assertResponseCode(int expected){ @@ -95,4 +98,18 @@ public class WebClient { } 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; + } }