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);
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) {

View file

@ -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<String> readMultiPartData(HttpReader reader) throws Exception{
List<String> 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;
@ -42,6 +35,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");
if (key == null) throw new IllegalArgumentException();

View file

@ -47,6 +47,7 @@ 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 = ?"),

View file

@ -139,13 +139,18 @@ public class DatabaseRepository {
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();
}

View file

@ -25,7 +25,7 @@ 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));
Chapter chapter = databaseRepo.getChapter(Integer.parseInt(matcher.group(1)));
if (chapter != null){
JSONObject chapterJSON = new JSONObject();
chapterJSON.put("id", chapter.getId());
@ -39,8 +39,10 @@ public class ChapterElement implements Response {
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));
}
}

View file

@ -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());
}

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {

View file

@ -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();

View file

@ -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;
}
}