diff --git a/src/be/jeffcheasey88/peeratcode/Main.java b/src/be/jeffcheasey88/peeratcode/Main.java index 1e08e06..cb26751 100644 --- a/src/be/jeffcheasey88/peeratcode/Main.java +++ b/src/be/jeffcheasey88/peeratcode/Main.java @@ -8,7 +8,6 @@ import java.util.regex.Pattern; import javax.net.ssl.SSLServerSocket; import javax.net.ssl.SSLServerSocketFactory; -import javax.net.ssl.SSLSocket; import be.jeffcheasey88.peeratcode.repository.DatabaseRepository; import be.jeffcheasey88.peeratcode.routes.ChapterElement; @@ -16,6 +15,7 @@ import be.jeffcheasey88.peeratcode.routes.ChapterList; import be.jeffcheasey88.peeratcode.routes.Login; import be.jeffcheasey88.peeratcode.routes.PuzzleElement; import be.jeffcheasey88.peeratcode.routes.Register; +import be.jeffcheasey88.peeratcode.routes.Result; import be.jeffcheasey88.peeratcode.webserver.Client; import be.jeffcheasey88.peeratcode.webserver.HttpReader; import be.jeffcheasey88.peeratcode.webserver.HttpUtil; @@ -59,6 +59,7 @@ public class Main { router.register(new PuzzleElement(repo)); router.register(new Register(repo)); router.register(new Login(repo)); + router.register(new Result(repo)); } private static void startWebServer(Configuration config, Router router) throws IOException { diff --git a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java index c0a641b..a282814 100644 --- a/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java +++ b/src/be/jeffcheasey88/peeratcode/repository/DatabaseRepository.java @@ -23,7 +23,8 @@ public class DatabaseRepository { private static final String CHECK_EMAIL_AVAILABLE_QUERY = "SELECT * FROM players WHERE email = ?"; private static final String REGISTER_QUERY = "INSERT INTO players (pseudo, email, passwd, firstname, lastname, description, sgroup, avatar) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; private static final String CHECK_PASSWORD = "SELECT passwd FROM players WHERE pseudo=?"; - + private static final String SCORE = "SELECT score FROM completions WHERE fk_player = ? AND fk_puzzle = ?"; + private Connection con; private Configuration config; @@ -77,6 +78,20 @@ public class DatabaseRepository { } return null; } + + public int getScore(int user, int puzzle){ + try { + PreparedStatement stmt = this.con.prepareStatement(SCORE); + stmt.setInt(1, user); + stmt.setInt(2, puzzle); + + ResultSet result = stmt.executeQuery(); + if(result.next()) result.getInt("score"); + }catch(Exception e){ + e.printStackTrace(); + } + return -1; + } /** * Get a specific chapter diff --git a/src/be/jeffcheasey88/peeratcode/routes/Result.java b/src/be/jeffcheasey88/peeratcode/routes/Result.java new file mode 100644 index 0000000..e28e57f --- /dev/null +++ b/src/be/jeffcheasey88/peeratcode/routes/Result.java @@ -0,0 +1,41 @@ +package be.jeffcheasey88.peeratcode.routes; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import be.jeffcheasey88.peeratcode.repository.DatabaseRepository; +import be.jeffcheasey88.peeratcode.webserver.HttpReader; +import be.jeffcheasey88.peeratcode.webserver.HttpUtil; +import be.jeffcheasey88.peeratcode.webserver.HttpWriter; +import be.jeffcheasey88.peeratcode.webserver.Response; + +public class Result implements Response{ + + private DatabaseRepository repo; + + public Result(DatabaseRepository repo){ + this.repo = repo; + } + + @Override + public void exec(Matcher matcher, HttpReader reader, HttpWriter writer) throws Exception { + int puzzle = Integer.parseInt(matcher.group(1)); + + int score = this.repo.getScore(0, puzzle); + if(score < 0) { + HttpUtil.responseHeaders(writer, 425, "Access-Control-Allow-Origin: *"); + }else{ + HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); + writer.write(score+""); + writer.flush(); + writer.close(); + } + } + + @Override + public Pattern getPattern(){ + return Pattern.compile("^\\/result\\/(\\d+)$"); + } + + +}