Fix login & Leaderboard
This commit is contained in:
parent
1007ac87da
commit
43d498a822
10 changed files with 205 additions and 10 deletions
|
@ -5,8 +5,6 @@ import static be.jeffcheasey88.peeratcode.framework.RequestType.OPTIONS;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.Timer;
|
|
||||||
import java.util.TimerTask;
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
import javax.net.ssl.SSLServerSocket;
|
import javax.net.ssl.SSLServerSocket;
|
||||||
|
@ -20,7 +18,6 @@ import be.jeffcheasey88.peeratcode.framework.Response;
|
||||||
import be.jeffcheasey88.peeratcode.framework.Route;
|
import be.jeffcheasey88.peeratcode.framework.Route;
|
||||||
import be.jeffcheasey88.peeratcode.framework.Router;
|
import be.jeffcheasey88.peeratcode.framework.Router;
|
||||||
import be.jeffcheasey88.peeratcode.framework.User;
|
import be.jeffcheasey88.peeratcode.framework.User;
|
||||||
import be.jeffcheasey88.peeratcode.model.Completion;
|
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||||
import be.jeffcheasey88.peeratcode.routes.BadgeDetails;
|
import be.jeffcheasey88.peeratcode.routes.BadgeDetails;
|
||||||
import be.jeffcheasey88.peeratcode.routes.ChapterElement;
|
import be.jeffcheasey88.peeratcode.routes.ChapterElement;
|
||||||
|
|
|
@ -4,7 +4,6 @@ import java.net.Socket;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.jose4j.jwa.AlgorithmConstraints.ConstraintType;
|
import org.jose4j.jwa.AlgorithmConstraints.ConstraintType;
|
||||||
import org.jose4j.jwk.RsaJsonWebKey;
|
|
||||||
import org.jose4j.jws.AlgorithmIdentifiers;
|
import org.jose4j.jws.AlgorithmIdentifiers;
|
||||||
import org.jose4j.jwt.JwtClaims;
|
import org.jose4j.jwt.JwtClaims;
|
||||||
import org.jose4j.jwt.consumer.JwtConsumer;
|
import org.jose4j.jwt.consumer.JwtConsumer;
|
||||||
|
|
|
@ -7,6 +7,7 @@ import java.io.InputStreamReader;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
|
|
@ -56,6 +56,7 @@ Content-Type: text/javascript
|
||||||
|
|
||||||
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) key = reader.getHeader("Sec-Websocket-Key");
|
||||||
if (key == null) throw new IllegalArgumentException();
|
if (key == null) throw new IllegalArgumentException();
|
||||||
|
|
||||||
writer.write("HTTP/1.1 101 Switching Protocols\n");
|
writer.write("HTTP/1.1 101 Switching Protocols\n");
|
||||||
|
|
|
@ -59,8 +59,75 @@ public enum DatabaseQuery {
|
||||||
|
|
||||||
// BADGES
|
// BADGES
|
||||||
GET_BADGE("SELECT * FROM badges WHERE id_badge = ?"), GET_BADGES_OF_PLAYER(
|
GET_BADGE("SELECT * FROM badges WHERE id_badge = ?"), GET_BADGES_OF_PLAYER(
|
||||||
"SELECT * FROM badges b LEFT JOIN containsBadges cb ON cb.fk_badge = b.id_badge WHERE cb.fk_player = ?");
|
"SELECT * FROM badges b LEFT JOIN containsBadges cb ON cb.fk_badge = b.id_badge WHERE cb.fk_player = ?"),
|
||||||
|
|
||||||
|
//TRIGGER
|
||||||
|
FIRST_TRY("CREATE OR REPLACE TRIGGER FirstTry\r\n"
|
||||||
|
+ "AFTER INSERT\r\n"
|
||||||
|
+ " ON completions FOR EACH ROW\r\n"
|
||||||
|
+ "BEGIN\r\n"
|
||||||
|
+ " DECLARE badge INT;\r\n"
|
||||||
|
+ " DECLARE player INT;\r\n"
|
||||||
|
+ " DECLARE contain INT;\r\n"
|
||||||
|
+ "\r\n"
|
||||||
|
+ " SELECT id_badge\r\n"
|
||||||
|
+ " INTO badge\r\n"
|
||||||
|
+ " FROM badges\r\n"
|
||||||
|
+ " WHERE name = 'FirstTry';\r\n"
|
||||||
|
+ "\r\n"
|
||||||
|
+ " IF @badge is not null THEN\r\n"
|
||||||
|
+ "\r\n"
|
||||||
|
+ " SELECT fk_player\r\n"
|
||||||
|
+ " INTO player\r\n"
|
||||||
|
+ " FROM inserted;\r\n"
|
||||||
|
+ "\r\n"
|
||||||
|
+ " SELECT count(*)\r\n"
|
||||||
|
+ " INTO contain\r\n"
|
||||||
|
+ " FROM containsBadges\r\n"
|
||||||
|
+ " WHERE fk_badge = badge AND fk_player = player;\r\n"
|
||||||
|
+ "\r\n"
|
||||||
|
+ " IF (@contain = 0) THEN\r\n"
|
||||||
|
+ "\r\n"
|
||||||
|
+ " INSERT INTO containsBadges(fk_player, fk_badge) VALUES (@player, @badge);\r\n"
|
||||||
|
+ " END IF;\r\n"
|
||||||
|
+ " END IF;\r\n"
|
||||||
|
+ "END;"),
|
||||||
|
EventParticipation("CREATE OR REPLACE TRIGGER EventParticipation\r\n"
|
||||||
|
+ "AFTER INSERT\r\n"
|
||||||
|
+ " ON completions FOR EACH ROW\r\n"
|
||||||
|
+ "BEGIN\r\n"
|
||||||
|
+ " DECLARE badge INT;\r\n"
|
||||||
|
+ " DECLARE endDate datetime;\r\n"
|
||||||
|
+ " DECLARE player INT;\r\n"
|
||||||
|
+ " DECLARE contain INT;\r\n"
|
||||||
|
+ "\r\n"
|
||||||
|
+ " SELECT id_badge\r\n"
|
||||||
|
+ " INTO badge\r\n"
|
||||||
|
+ " FROM badges\r\n"
|
||||||
|
+ " WHERE name = 'EventParticipation';\r\n"
|
||||||
|
+ "\r\n"
|
||||||
|
+ " IF @badge is not null THEN\r\n"
|
||||||
|
+ "\r\n"
|
||||||
|
+ " SELECT c.end_date, i.fk_player\r\n"
|
||||||
|
+ " INTO endDate, player\r\n"
|
||||||
|
+ " FROM inserted i\r\n"
|
||||||
|
+ " JOIN puzzles p ON i.fk_puzzle = p.id_puzzle\r\n"
|
||||||
|
+ " JOIN chapters c on p.fk_chapter = c.id_chapter;\r\n"
|
||||||
|
+ "\r\n"
|
||||||
|
+ " IF @endDate is not null THEN\r\n"
|
||||||
|
+ "\r\n"
|
||||||
|
+ " SELECT count(*)\r\n"
|
||||||
|
+ " INTO contain\r\n"
|
||||||
|
+ " FROM containsBadges\r\n"
|
||||||
|
+ " WHERE fk_badge = badge AND fk_player = player;\r\n"
|
||||||
|
+ "\r\n"
|
||||||
|
+ " IF (@contain = 0) THEN\r\n"
|
||||||
|
+ " INSERT INTO containsBadges(fk_player, fk_badge) VALUES (@player, @badge);\r\n"
|
||||||
|
+ " END IF;\r\n"
|
||||||
|
+ " END IF;\r\n"
|
||||||
|
+ " END IF;\r\n"
|
||||||
|
+ "END;");
|
||||||
|
|
||||||
private String request;
|
private String request;
|
||||||
|
|
||||||
DatabaseQuery(DatabaseQuery parent, String request) {
|
DatabaseQuery(DatabaseQuery parent, String request) {
|
||||||
|
@ -80,3 +147,81 @@ public enum DatabaseQuery {
|
||||||
return this.request;
|
return this.request;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* TRIGGER PLACE
|
||||||
|
*
|
||||||
|
|
||||||
|
CREATE OR REPLACE TRIGGER FirstTry
|
||||||
|
AFTER INSERT
|
||||||
|
ON completions FOR EACH ROW
|
||||||
|
BEGIN
|
||||||
|
DECLARE badge INT;
|
||||||
|
DECLARE player INT;
|
||||||
|
DECLARE contain INT;
|
||||||
|
|
||||||
|
SELECT id_badge
|
||||||
|
INTO badge
|
||||||
|
FROM badges
|
||||||
|
WHERE name = 'FirstTry';
|
||||||
|
|
||||||
|
IF @badge is not null THEN
|
||||||
|
|
||||||
|
SELECT fk_player
|
||||||
|
INTO player
|
||||||
|
FROM inserted;
|
||||||
|
|
||||||
|
SELECT count(*)
|
||||||
|
INTO contain
|
||||||
|
FROM containsBadges
|
||||||
|
WHERE fk_badge = badge AND fk_player = player;
|
||||||
|
|
||||||
|
IF (@contain = 0) THEN
|
||||||
|
|
||||||
|
INSERT INTO containsBadges(fk_player, fk_badge) VALUES (@player, @badge);
|
||||||
|
END IF;
|
||||||
|
END IF;
|
||||||
|
END;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
CREATE OR REPLACE TRIGGER EventParticipation
|
||||||
|
AFTER INSERT
|
||||||
|
ON completions FOR EACH ROW
|
||||||
|
BEGIN
|
||||||
|
DECLARE badge INT;
|
||||||
|
DECLARE endDate datetime;
|
||||||
|
DECLARE player INT;
|
||||||
|
DECLARE contain INT;
|
||||||
|
|
||||||
|
SELECT id_badge
|
||||||
|
INTO badge
|
||||||
|
FROM badges
|
||||||
|
WHERE name = 'EventParticipation';
|
||||||
|
|
||||||
|
IF @badge is not null THEN
|
||||||
|
|
||||||
|
SELECT c.end_date, i.fk_player
|
||||||
|
INTO endDate, player
|
||||||
|
FROM inserted i
|
||||||
|
JOIN puzzles p ON i.fk_puzzle = p.id_puzzle
|
||||||
|
JOIN chapters c on p.fk_chapter = c.id_chapter;
|
||||||
|
|
||||||
|
IF @endDate is not null THEN
|
||||||
|
|
||||||
|
SELECT count(*)
|
||||||
|
INTO contain
|
||||||
|
FROM containsBadges
|
||||||
|
WHERE fk_badge = badge AND fk_player = player;
|
||||||
|
|
||||||
|
IF (@contain = 0) THEN
|
||||||
|
INSERT INTO containsBadges(fk_player, fk_badge) VALUES (@player, @badge);
|
||||||
|
END IF;
|
||||||
|
END IF;
|
||||||
|
END IF;
|
||||||
|
END;
|
||||||
|
|
||||||
|
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
|
@ -32,7 +32,7 @@ public class DatabaseRepository {
|
||||||
public DatabaseRepository(Configuration config) {
|
public DatabaseRepository(Configuration config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ensureConnection() throws SQLException {
|
private void ensureConnection() throws SQLException {
|
||||||
if (con == null || (!con.isValid(5))) {
|
if (con == null || (!con.isValid(5))) {
|
||||||
this.con = DriverManager.getConnection(
|
this.con = DriverManager.getConnection(
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes;
|
package be.jeffcheasey88.peeratcode.routes;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Base64;
|
|
||||||
import java.util.SortedSet;
|
import java.util.SortedSet;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
|
|
@ -40,10 +40,10 @@ public class Login implements Response {
|
||||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *",
|
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *",
|
||||||
"Access-Control-Expose-Headers: Authorization",
|
"Access-Control-Expose-Headers: Authorization",
|
||||||
"Authorization: Bearer " + this.router.createAuthUser(id));
|
"Authorization: Bearer " + this.router.createAuthUser(id));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
|
|
||||||
}
|
}
|
||||||
|
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package be.jeffcheasey88.peeratcode.routes;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
import org.json.simple.JSONObject;
|
|
||||||
import org.junit.jupiter.api.AfterAll;
|
import org.junit.jupiter.api.AfterAll;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
54
test/be/jeffcheasey88/peeratcode/routes/TriggerTests.java
Normal file
54
test/be/jeffcheasey88/peeratcode/routes/TriggerTests.java
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
package be.jeffcheasey88.peeratcode.routes;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.TestInstance;
|
||||||
|
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||||
|
|
||||||
|
import be.jeffcheasey88.peeratcode.Main;
|
||||||
|
import be.jeffcheasey88.peeratcode.webclient.WebClient;
|
||||||
|
|
||||||
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
|
public class TriggerTests {
|
||||||
|
|
||||||
|
private Thread server;
|
||||||
|
private WebClient client;
|
||||||
|
private boolean ready;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
void init(){
|
||||||
|
server = new Thread(new Runnable(){
|
||||||
|
@Override
|
||||||
|
public void run(){
|
||||||
|
try {
|
||||||
|
Main.main(null);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
server.start();
|
||||||
|
client = new WebClient("localhost", 80);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
void close(){
|
||||||
|
server.interrupt();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testOnDeployed(){
|
||||||
|
try {
|
||||||
|
client.auth("JeffCheasey88", "TheoPueDesPieds");
|
||||||
|
client.route("/puzzleResponse/9", "POST", WebClient.buildMultiPartData("1"));
|
||||||
|
|
||||||
|
client.route("/player/", "GET");
|
||||||
|
}catch(Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
fail(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue