Compare commits
No commits in common. "main" and "mailConfirmation" have entirely different histories.
main
...
mailConfir
85 changed files with 1343 additions and 4081 deletions
|
@ -17,8 +17,8 @@
|
|||
<classpathentry kind="lib" path="Treasure.jar"/>
|
||||
<classpathentry exported="true" kind="lib" path="JDA-5.0.0-beta.8-withDependencies.jar"/>
|
||||
<classpathentry exported="true" kind="lib" path="PeerAtCodeFramework.jar"/>
|
||||
<classpathentry exported="true" kind="lib" path="angus-activation-2.0.2.jar"/>
|
||||
<classpathentry exported="true" kind="lib" path="jakarta.activation-api-2.1.3.jar"/>
|
||||
<classpathentry exported="true" kind="lib" path="jakarta.mail-2.0.3.jar"/>
|
||||
<classpathentry exported="true" kind="lib" path="angus-activation-2.0.1.jar"/>
|
||||
<classpathentry exported="true" kind="lib" path="jakarta.activation-api-2.1.2.jar"/>
|
||||
<classpathentry exported="true" kind="lib" path="jakarta.mail-2.0.2.jar"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
|
|
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -2,7 +2,6 @@
|
|||
bin/
|
||||
.project
|
||||
config.txt
|
||||
config-test.txt
|
||||
dist/
|
||||
testApi/
|
||||
.apt_generated/*
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -1,127 +0,0 @@
|
|||
DROP TABLE IF EXISTS `containsTags`;
|
||||
DROP TABLE IF EXISTS `tags`;
|
||||
DROP TABLE IF EXISTS `containsBadges`;
|
||||
DROP TABLE IF EXISTS `badges`;
|
||||
DROP TABLE IF EXISTS `containsGroups`;
|
||||
DROP TABLE IF EXISTS `nextPart`;
|
||||
DROP TABLE IF EXISTS `groups`;
|
||||
DROP TABLE IF EXISTS `completions`;
|
||||
DROP TABLE IF EXISTS `players`;
|
||||
DROP TABLE IF EXISTS `puzzles`;
|
||||
DROP TABLE IF EXISTS `chapters`;
|
||||
|
||||
CREATE TABLE `players` (
|
||||
`id_player` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`pseudo` varchar(100) NOT NULL,
|
||||
`email` varchar(100) NOT NULL,
|
||||
`passwd` varchar(150) NOT NULL,
|
||||
`firstname` varchar(100) NOT NULL,
|
||||
`lastname` varchar(100) NOT NULL,
|
||||
`description` varchar(200) DEFAULT NULL,
|
||||
`avatar` blob DEFAULT NULL,
|
||||
PRIMARY KEY (`id_player`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
|
||||
CREATE TABLE badges (
|
||||
id_badge int(11) NOT NULL AUTO_INCREMENT,
|
||||
name varchar(50) NOT NULL,
|
||||
logo mediumblob DEFAULT NULL,
|
||||
level int(11) DEFAULT 1,
|
||||
PRIMARY KEY (id_badge)
|
||||
);
|
||||
|
||||
CREATE TABLE `chapters` (
|
||||
`id_chapter` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(150) NOT NULL,
|
||||
`start_date` datetime DEFAULT NULL,
|
||||
`end_date` datetime DEFAULT NULL,
|
||||
PRIMARY KEY (`id_chapter`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
|
||||
CREATE TABLE `puzzles` (
|
||||
`id_puzzle` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(150) NOT NULL,
|
||||
`content` text NOT NULL,
|
||||
`soluce` blob NOT NULL,
|
||||
`verify` text DEFAULT NULL,
|
||||
`score_max` int(11) NOT NULL,
|
||||
`fk_chapter` int(11) NOT NULL,
|
||||
PRIMARY KEY (`id_puzzle`),
|
||||
KEY `fk_chapter` (`fk_chapter`),
|
||||
CONSTRAINT `puzzles_ibfk_1` FOREIGN KEY (`fk_chapter`) REFERENCES `chapters` (`id_chapter`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
|
||||
|
||||
CREATE TABLE `groups` (
|
||||
`id_group` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(150) DEFAULT NULL,
|
||||
`fk_chapter` int(11) DEFAULT NULL,
|
||||
`fk_puzzle` int(11) DEFAULT NULL,
|
||||
PRIMARY KEY (`id_group`),
|
||||
KEY `fk_chapter` (`fk_chapter`),
|
||||
KEY `fk_puzzle` (`fk_puzzle`),
|
||||
CONSTRAINT `groups_ibfk_1` FOREIGN KEY (`fk_chapter`) REFERENCES `chapters` (`id_chapter`),
|
||||
CONSTRAINT `groups_ibfk_2` FOREIGN KEY (`fk_puzzle`) REFERENCES `puzzles` (`id_puzzle`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
|
||||
|
||||
CREATE TABLE `nextPart` (
|
||||
`origin` int(11) NOT NULL,
|
||||
`next` int(11) NOT NULL,
|
||||
PRIMARY KEY (`origin`,`next`),
|
||||
KEY `next` (`next`),
|
||||
CONSTRAINT `nextPart_ibfk_1` FOREIGN KEY (`origin`) REFERENCES `puzzles` (`id_puzzle`),
|
||||
CONSTRAINT `nextPart_ibfk_2` FOREIGN KEY (`next`) REFERENCES `puzzles` (`id_puzzle`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
|
||||
|
||||
CREATE TABLE `completions` (
|
||||
`id_completion` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`fk_puzzle` int(11) NOT NULL,
|
||||
`fk_player` int(11) NOT NULL,
|
||||
`tries` int(11) DEFAULT 0,
|
||||
`code` blob DEFAULT NULL,
|
||||
`score` int(11) DEFAULT 0,
|
||||
`fileName` varchar(100) DEFAULT NULL,
|
||||
PRIMARY KEY (`id_completion`),
|
||||
KEY `fk_puzzle` (`fk_puzzle`),
|
||||
KEY `fk_player` (`fk_player`),
|
||||
CONSTRAINT `completions_ibfk_1` FOREIGN KEY (`fk_puzzle`) REFERENCES `puzzles` (`id_puzzle`),
|
||||
CONSTRAINT `completions_ibfk_2` FOREIGN KEY (`fk_player`) REFERENCES `players` (`id_player`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
|
||||
CREATE TABLE `tags` (
|
||||
`id_tag` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(50) NOT NULL,
|
||||
PRIMARY KEY (`id_tag`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
|
||||
CREATE TABLE `containsBadges` (
|
||||
`fk_player` int(11) NOT NULL,
|
||||
`fk_badge` int(11) NOT NULL,
|
||||
PRIMARY KEY (`fk_player`,`fk_badge`),
|
||||
KEY `fk_badge` (`fk_badge`),
|
||||
CONSTRAINT `containsBadges_ibfk_1` FOREIGN KEY (`fk_player`) REFERENCES `players` (`id_player`),
|
||||
CONSTRAINT `containsBadges_ibfk_2` FOREIGN KEY (`fk_badge`) REFERENCES `badges` (`id_badge`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
|
||||
CREATE TABLE `containsGroups` (
|
||||
`fk_player` int(11) NOT NULL,
|
||||
`fk_group` int(11) NOT NULL,
|
||||
PRIMARY KEY (`fk_player`,`fk_group`),
|
||||
KEY `fk_group` (`fk_group`),
|
||||
CONSTRAINT `containsGroups_ibfk_1` FOREIGN KEY (`fk_player`) REFERENCES `players` (`id_player`),
|
||||
CONSTRAINT `containsGroups_ibfk_2` FOREIGN KEY (`fk_group`) REFERENCES `groups` (`id_group`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
|
||||
CREATE TABLE `containsTags` (
|
||||
`fk_tag` int(11) NOT NULL,
|
||||
`fk_puzzle` int(11) NOT NULL,
|
||||
PRIMARY KEY (`fk_tag`,`fk_puzzle`),
|
||||
KEY `fk_puzzle` (`fk_puzzle`),
|
||||
CONSTRAINT `containsTags_ibfk_1` FOREIGN KEY (`fk_tag`) REFERENCES `tags` (`id_tag`),
|
||||
CONSTRAINT `containsTags_ibfk_2` FOREIGN KEY (`fk_puzzle`) REFERENCES `puzzles` (`id_puzzle`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||
|
||||
INSERT INTO chapters (id_chapter, name) VALUES (1, 'chapter 1');
|
||||
INSERT INTO players (pseudo, email, passwd,firstname,lastname) VALUES ('userTest', 'test@peerat.dev', '$argon2id$v=19$m=15360,t=2,p=1$$cAQwfs30Bf2rQGj86bpz7i59TlsuOFPiXeNpLlVu4AY', 'a','b')
|
BIN
jakarta.activation-api-2.1.2.jar
Normal file
BIN
jakarta.activation-api-2.1.2.jar
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
jose4j-0.9.3.jar
Normal file
BIN
jose4j-0.9.3.jar
Normal file
Binary file not shown.
BIN
json-simple-1.1.1.jar
Normal file
BIN
json-simple-1.1.1.jar
Normal file
Binary file not shown.
BIN
password4j-1.6.3.jar
Normal file
BIN
password4j-1.6.3.jar
Normal file
Binary file not shown.
|
@ -1,6 +0,0 @@
|
|||
/chapter/0
|
||||
GET
|
||||
Authorization: Bearer <token:test@peerat.dev>
|
||||
|
||||
|
||||
400
|
|
@ -1,8 +0,0 @@
|
|||
/chapters
|
||||
GET
|
||||
Authorization: Bearer <token:test@peerat.dev>
|
||||
|
||||
|
||||
200
|
||||
|
||||
[]
|
|
@ -1,8 +0,0 @@
|
|||
/leaderboard/
|
||||
GET
|
||||
Authorization: Bearer <token:test@peerat.dev>
|
||||
|
||||
|
||||
200
|
||||
|
||||
[]
|
|
@ -1,6 +0,0 @@
|
|||
/puzzle/0
|
||||
GET
|
||||
Authorization: Bearer <token:test@peerat.dev>
|
||||
|
||||
|
||||
400
|
|
@ -1,6 +0,0 @@
|
|||
/admin/chapter/0
|
||||
GET
|
||||
Authorization: Bearer <token:test@peerat.dev>
|
||||
|
||||
|
||||
401
|
BIN
slf4j-api-2.0.6.jar
Normal file
BIN
slf4j-api-2.0.6.jar
Normal file
Binary file not shown.
|
@ -11,8 +11,6 @@ import dev.peerat.backend.utils.Mail;
|
|||
|
||||
public class Configuration {
|
||||
|
||||
private boolean prod;
|
||||
|
||||
private String db_host;
|
||||
private int db_port;
|
||||
private String db_user;
|
||||
|
@ -33,52 +31,34 @@ public class Configuration {
|
|||
|
||||
private int groupJoinMinutes;
|
||||
private String groupQuitMinutes;
|
||||
private int groupMaxPlayers;
|
||||
|
||||
private String mailUsername;
|
||||
private String mailPassword;
|
||||
private String mailSmtpHost;
|
||||
private int mailSmtpPort;
|
||||
private int mailSmptPort;
|
||||
private String mailFromAddress;
|
||||
|
||||
private String git_token;
|
||||
|
||||
private String jwt_key;
|
||||
|
||||
private String sql_folder;
|
||||
|
||||
private int event_chapter;
|
||||
|
||||
private File _file;
|
||||
private File file;
|
||||
|
||||
public Configuration(String path) {
|
||||
this._file = new File(path);
|
||||
System.out.println("Config on " + _file.getAbsolutePath());
|
||||
this.file = new File(path);
|
||||
System.out.println("Config on " + file.getAbsolutePath());
|
||||
}
|
||||
|
||||
public <T> Configuration addDefaultValue(String name, T value) throws Exception{
|
||||
if(value == null) throw new IllegalArgumentException("Value cannot be null");
|
||||
Field field = getClass().getDeclaredField(name);
|
||||
field.setAccessible(true);
|
||||
field.set(this, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Configuration load() throws Exception{
|
||||
if(!this._file.exists()) return this;
|
||||
BufferedReader reader = new BufferedReader(new FileReader(this._file));
|
||||
public void load() throws Exception {
|
||||
if (!this.file.exists())
|
||||
return;
|
||||
BufferedReader reader = new BufferedReader(new FileReader(this.file));
|
||||
String line;
|
||||
while((line = reader.readLine()) != null){
|
||||
int index = line.indexOf('=');
|
||||
String key = line.substring(0, index);
|
||||
String value = line.substring(index+1);
|
||||
Field field = getClass().getDeclaredField(key);
|
||||
if(field == null) continue;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
String[] split = line.split("=");
|
||||
Field field = getClass().getDeclaredField(split[0]);
|
||||
if (field == null)
|
||||
continue;
|
||||
field.setAccessible(true);
|
||||
injectValue(field, value);
|
||||
injectValue(field, split[1]);
|
||||
}
|
||||
reader.close();
|
||||
return this;
|
||||
}
|
||||
|
||||
private void injectValue(Field field, String value) throws IllegalAccessException {
|
||||
|
@ -121,72 +101,72 @@ public class Configuration {
|
|||
}
|
||||
|
||||
public void save() throws Exception {
|
||||
if (!_file.exists()) {
|
||||
File parent = _file.getParentFile();
|
||||
if(!parent.exists()) parent.mkdirs();
|
||||
_file.createNewFile();
|
||||
if (!file.exists()) {
|
||||
File parent = file.getParentFile();
|
||||
if (!parent.exists())
|
||||
parent.mkdirs();
|
||||
file.createNewFile();
|
||||
}
|
||||
Field[] fields = getClass().getDeclaredFields();
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(_file));
|
||||
for(Field field : fields){
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
|
||||
for (Field field : fields) {
|
||||
field.setAccessible(true);
|
||||
if(field.getName().startsWith("_")) continue;
|
||||
if (field.getName().startsWith("_"))
|
||||
continue;
|
||||
Object value = field.get(this);
|
||||
writer.write(field.getName() + "=" + value+"\n");
|
||||
writer.write(field.getName() + "=" + value);
|
||||
}
|
||||
writer.flush();
|
||||
writer.close();
|
||||
}
|
||||
|
||||
public boolean isProduction(){
|
||||
return this.prod;
|
||||
}
|
||||
|
||||
public String getDbHost(){
|
||||
public String getDbHost() {
|
||||
return this.db_host;
|
||||
}
|
||||
|
||||
public int getDbPort(){
|
||||
public int getDbPort() {
|
||||
return this.db_port;
|
||||
}
|
||||
|
||||
public String getDbUser(){
|
||||
public String getDbUser() {
|
||||
return this.db_user;
|
||||
}
|
||||
|
||||
public String getDbDatabase(){
|
||||
public String getDbDatabase() {
|
||||
return this.db_database;
|
||||
}
|
||||
|
||||
public String getDbPassword(){
|
||||
public String getDbPassword() {
|
||||
return this.db_password;
|
||||
}
|
||||
|
||||
public String getSslKeystore(){
|
||||
public String getSslKeystore() {
|
||||
return this.ssl_keystore;
|
||||
}
|
||||
|
||||
public String getTokenIssuer(){
|
||||
public String getTokenIssuer() {
|
||||
return this.token_issuer;
|
||||
}
|
||||
|
||||
public int getTokenExpiration(){
|
||||
public int getTokenExpiration() {
|
||||
return this.token_expiration;
|
||||
}
|
||||
|
||||
public String getSslKeystorePasswd(){
|
||||
public String getSslKeystorePasswd() {
|
||||
return this.ssl_keystorePasswd;
|
||||
}
|
||||
|
||||
public int getTcpPort(){
|
||||
public int getTcpPort() {
|
||||
return this.tcp_port;
|
||||
}
|
||||
|
||||
public boolean useSsl(){
|
||||
public boolean useSsl() {
|
||||
return this.use_ssl;
|
||||
}
|
||||
|
||||
public String getUsersFiles(){
|
||||
public String getUsersFiles() {
|
||||
if (users_files == null || users_files.trim().isEmpty())
|
||||
users_files = "/tmp/users_files";
|
||||
return users_files;
|
||||
}
|
||||
|
||||
|
@ -202,36 +182,12 @@ public class Configuration {
|
|||
return this.groupQuitMinutes;
|
||||
}
|
||||
|
||||
public int getGroupMaxPlayers(){
|
||||
return this.groupMaxPlayers;
|
||||
}
|
||||
|
||||
public Mail getMail(){
|
||||
return new Mail(
|
||||
this.mailUsername,
|
||||
this.mailPassword,
|
||||
this.mailSmtpHost,
|
||||
this.mailSmtpPort,
|
||||
this.mailSmptPort,
|
||||
this.mailFromAddress);
|
||||
}
|
||||
|
||||
public String getGitToken(){
|
||||
return this.git_token;
|
||||
}
|
||||
|
||||
public String getJwtKey(){
|
||||
return this.jwt_key;
|
||||
}
|
||||
|
||||
public void setJwtKey(String key){
|
||||
this.jwt_key = key;
|
||||
}
|
||||
|
||||
public String getSqlFolder(){
|
||||
return this.sql_folder;
|
||||
}
|
||||
|
||||
public int getEventChapter(){
|
||||
return this.event_chapter;
|
||||
}
|
||||
}
|
|
@ -2,181 +2,113 @@ package dev.peerat.backend;
|
|||
|
||||
import static dev.peerat.framework.RequestType.OPTIONS;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.bonus.extract.RouteExtracter;
|
||||
import dev.peerat.backend.model.Completion;
|
||||
import dev.peerat.backend.model.Group;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.ConnectionManager;
|
||||
import dev.peerat.backend.model.Player;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.backend.routes.BadgeDetails;
|
||||
import dev.peerat.backend.routes.ChapterElement;
|
||||
import dev.peerat.backend.routes.ChapterList;
|
||||
import dev.peerat.backend.routes.DynamicLeaderboard;
|
||||
import dev.peerat.backend.routes.Leaderboard;
|
||||
import dev.peerat.backend.routes.Login;
|
||||
import dev.peerat.backend.routes.MailConfirmation;
|
||||
import dev.peerat.backend.routes.PlayerDetails;
|
||||
import dev.peerat.backend.routes.PuzzleElement;
|
||||
import dev.peerat.backend.routes.PuzzleResponse;
|
||||
import dev.peerat.backend.routes.Register;
|
||||
import dev.peerat.backend.routes.Result;
|
||||
import dev.peerat.backend.routes.groups.GroupCreate;
|
||||
import dev.peerat.backend.routes.groups.GroupJoin;
|
||||
import dev.peerat.backend.routes.groups.GroupList;
|
||||
import dev.peerat.backend.routes.groups.GroupQuit;
|
||||
import dev.peerat.backend.utils.Mail;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.DependencyInjector;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Locker;
|
||||
import dev.peerat.framework.Locker.Key;
|
||||
import dev.peerat.framework.RequestType;
|
||||
import dev.peerat.framework.Response;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.RouteInterceptor;
|
||||
import dev.peerat.framework.Router;
|
||||
import dev.peerat.framework.auth.Authenticator;
|
||||
import dev.peerat.framework.auth.JwtAuthenticator;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
import dev.peerat.framework.utils.json.JsonParser;
|
||||
|
||||
public class Main{
|
||||
|
||||
private static Router<PeerAtUser> ACCESS_ROUTER;
|
||||
|
||||
public static void main(String[] args) throws Exception{
|
||||
Configuration config = new Configuration("config.txt").addDefaultValue("users_files", "/tmp/users_files");
|
||||
Configuration config = new Configuration("config.txt");
|
||||
config.load();
|
||||
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
|
||||
DatabaseRepository repo = new DatabaseRepository(new ConnectionManager(config), config);
|
||||
DatabaseRepository repo = new DatabaseRepository(config);
|
||||
Router<PeerAtUser> router = new Router<PeerAtUser>()
|
||||
.activeReOrdering().
|
||||
addDefaultHeaders(RequestType.GET, "Access-Control-Allow-Origin: *").
|
||||
addDefaultHeaders(RequestType.POST, "Access-Control-Allow-Origin: *").
|
||||
addDefaultHeaders(RequestType.OPTIONS,
|
||||
"Access-Control-Allow-Origin: *",
|
||||
"Access-Control-Allow-Methods: *",
|
||||
"Access-Control-Allow-Headers: *");
|
||||
ACCESS_ROUTER = router;
|
||||
.configureJwt(
|
||||
(builder) -> builder.setExpectedIssuer(config.getTokenIssuer()),
|
||||
(claims) -> {
|
||||
claims.setIssuer(config.getTokenIssuer()); // who creates the token and signs it
|
||||
claims.setExpirationTimeMinutesInTheFuture(config.getTokenExpiration());
|
||||
},
|
||||
(claims) -> new PeerAtUser(claims));
|
||||
|
||||
Authenticator<PeerAtUser> auth;
|
||||
if(config.getJwtKey() != null){
|
||||
JsonParser parser = new JsonParser();
|
||||
JsonMap json = parser.parse(config.getJwtKey());
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
for(Entry<String, Object> entry : json.entries()) params.put(entry.getKey(), entry.getValue());
|
||||
auth = new JwtAuthenticator<PeerAtUser>().configure(
|
||||
(builder) -> builder.setExpectedIssuer(config.getTokenIssuer()),
|
||||
(claims) -> {
|
||||
claims.setIssuer(config.getTokenIssuer());
|
||||
claims.setExpirationTimeMinutesInTheFuture(config.getTokenExpiration());
|
||||
},
|
||||
(claims) -> new PeerAtUser(claims),
|
||||
params);
|
||||
}else{
|
||||
auth = new JwtAuthenticator<PeerAtUser>().configure(
|
||||
(builder) -> builder.setExpectedIssuer(config.getTokenIssuer()),
|
||||
(claims) -> {
|
||||
claims.setIssuer(config.getTokenIssuer());
|
||||
claims.setExpirationTimeMinutesInTheFuture(config.getTokenExpiration());
|
||||
},
|
||||
(claims) -> new PeerAtUser(claims));
|
||||
JsonMap json = new JsonMap();
|
||||
for(Entry<String, Object> entry : ((JwtAuthenticator<PeerAtUser>)auth).exportKey().entrySet()){
|
||||
json.set(entry.getKey(), entry.getValue());
|
||||
}
|
||||
config.setJwtKey(json.toString());
|
||||
config.save();
|
||||
}
|
||||
router.setAuthenticator(auth);
|
||||
router.addDefaultHeaders(RequestType.GET, "Access-Control-Allow-Origin: *");
|
||||
router.addDefaultHeaders(RequestType.POST, "Access-Control-Allow-Origin: *");
|
||||
|
||||
router.setDefault((matcher, context, reader, writer) -> {
|
||||
context.response(404);
|
||||
writer.write("404 not Found.\n");
|
||||
writer.flush();
|
||||
writer.close();
|
||||
});
|
||||
|
||||
router.register(new Response(){
|
||||
@Route(path = "^(.*)$", type = OPTIONS)
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
context.response(200);
|
||||
context.response(200,
|
||||
"Access-Control-Allow-Origin: *",
|
||||
"Access-Control-Allow-Methods: *",
|
||||
"Access-Control-Allow-Headers: *");
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
initRoutes(router, repo, config);
|
||||
|
||||
router.addInterceptor(new RouteInterceptor(){
|
||||
@Override
|
||||
public boolean intercept(Matcher matcher, Context context, HttpReader reader, HttpWriter writer, Method method){
|
||||
if(method.getDeclaringClass().getPackage().getName().contains(".admins")){
|
||||
try {
|
||||
Group group = repo.getGroupRepository().getPlayerGroup(context.<PeerAtUser>getUser().getId(), 1);
|
||||
if(!group.getName().equalsIgnoreCase("Quarter-Master - Battles PAC x CEI")){
|
||||
context.response(401);
|
||||
return false;
|
||||
}
|
||||
}catch(Exception ex){
|
||||
ex.printStackTrace();
|
||||
try{
|
||||
context.response(401);
|
||||
}catch(Exception e){}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
new Thread(new Runnable(){
|
||||
public void run(){
|
||||
Key key = new Key();
|
||||
|
||||
Locker<Context> locker = router.getLogger();
|
||||
|
||||
locker.init(key);
|
||||
try {
|
||||
while(true){
|
||||
locker.lock(key);
|
||||
Context instance = locker.getValue(key);
|
||||
if(instance == null) continue;
|
||||
System.out.println("["+((instance.isLogged()) ? repo.getPlayerRepository().getPlayer(instance.<PeerAtUser>getUser().getId()).getPseudo() : "?")+"] "+instance.getType()+" "+instance.getPath()+" -> "+instance.getResponseCode());
|
||||
}
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
locker.remove(key);
|
||||
}
|
||||
}).start();
|
||||
|
||||
new Thread(new Runnable(){
|
||||
public void run(){
|
||||
Key key = new Key();
|
||||
|
||||
Locker<Throwable> locker = router.getExceptionLogger();
|
||||
|
||||
locker.init(key);
|
||||
try {
|
||||
while(true){
|
||||
locker.lock(key);
|
||||
locker.getValue(key).printStackTrace();
|
||||
}
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
locker.remove(key);
|
||||
}
|
||||
}).start();
|
||||
// RouteExtracter extracter = new RouteExtracter(router);
|
||||
// extracter.extract();
|
||||
|
||||
if(config.useSsl()) router.configureSSL(config.getSslKeystore(), config.getSslKeystorePasswd());
|
||||
router.listen(config.getTcpPort(), config.useSsl());
|
||||
}
|
||||
|
||||
private static void initRoutes(Router<PeerAtUser> router, DatabaseRepository repo, Configuration config) throws Exception{
|
||||
Predicate<PeerAtUser> isAdmin = (user) -> {
|
||||
try {
|
||||
Group group = repo.getGroupRepository().getPlayerGroup(user.getId(), 1);
|
||||
return group.getName().equalsIgnoreCase("Quarter-Master - Battles PAC x CEI");
|
||||
}catch(Exception ex){}
|
||||
return false;
|
||||
};
|
||||
private static void initRoutes(Router<PeerAtUser> router, DatabaseRepository repo, Configuration config){
|
||||
final Map<Integer, Player> playersWaiting = new HashMap<>();
|
||||
router.register(new ChapterElement(repo));
|
||||
router.register(new ChapterList(repo));
|
||||
router.register(new PuzzleElement(repo));
|
||||
router.register(new Register(repo, router, config.getUsersFiles(), playersWaiting));
|
||||
router.register(new MailConfirmation(repo, router, config.getUsersFiles(), playersWaiting));
|
||||
router.register(new Login(repo, router));
|
||||
router.register(new Result(repo));
|
||||
router.register(new Leaderboard(repo));
|
||||
router.register(new PlayerDetails(repo));
|
||||
router.register(new BadgeDetails(repo));
|
||||
|
||||
router.registerPackages("dev.peerat.backend.routes",new DependencyInjector()
|
||||
.of(repo, router, config, new RouteExtracter(router), config.getMail(), isAdmin)
|
||||
.of("waitting", new HashMap<>())
|
||||
.of("leaderboard", new Locker<>())
|
||||
.of("groups", new Locker<>())
|
||||
.of("groupMessages", new Locker<>()));
|
||||
Locker<Group> groupLock = new Locker<>();
|
||||
router.register(new GroupCreate(repo, groupLock, config.getGroupJoinMinutes()));
|
||||
|
||||
DynamicLeaderboard dlb = new DynamicLeaderboard(repo);
|
||||
router.register(dlb);
|
||||
|
||||
Locker<Completion> leaderboard = dlb.getLocker();
|
||||
|
||||
router.register(new PuzzleResponse(repo, config.getUsersFiles(), leaderboard));
|
||||
router.register(new GroupList(repo));
|
||||
router.register(new GroupJoin(repo, config.getGroupJoinMinutes(), config.getGroupQuitMinutes(), leaderboard));
|
||||
router.register(new GroupQuit(repo, config.getGroupJoinMinutes(), leaderboard));
|
||||
|
||||
// Bot bot = new Bot(config, repo, groupLock);
|
||||
// bot.start();
|
||||
|
|
|
@ -38,7 +38,7 @@ public class Bot extends Thread{
|
|||
|
||||
locker.init(key);
|
||||
|
||||
List<Group> groups = this.repo.getGroupRepository().getAllGroups();
|
||||
List<Group> groups = this.repo.getAllGroups();
|
||||
for(Group group : groups){
|
||||
Integer chapter = group.getLinkToChapter();
|
||||
// Integer puzzle = group.getLinkToPuzzle();
|
||||
|
|
|
@ -2,130 +2,48 @@ package dev.peerat.backend.bonus.extract;
|
|||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.framework.RequestType;
|
||||
import dev.peerat.framework.Response;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.RouteMapper;
|
||||
import dev.peerat.framework.Router;
|
||||
import dev.peerat.framework.utils.json.JsonArray;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
//A noter que le but est d'extraire des informations sans modifier le code source,
|
||||
//comme les tests unitaire, ici je veux générer un élément textuel qui me donnera l'état des routes,
|
||||
//je ne vais donc pas modifier la classe router pour donner un accès au route.
|
||||
public class RouteExtracter {
|
||||
|
||||
private Router<PeerAtUser> router;
|
||||
private Router router;
|
||||
|
||||
public RouteExtracter(Router<PeerAtUser> router){
|
||||
public RouteExtracter(Router router){
|
||||
this.router = router;
|
||||
}
|
||||
|
||||
public void extract() throws Exception{
|
||||
RouteMapper[] mappers = getField(Router.class, router, "mappers");
|
||||
for(RequestType type : RequestType.values()){
|
||||
RouteMapper mapper = mappers[type.ordinal()];
|
||||
Response[] responses = getField(RouteMapper.class, mapper, "responses");
|
||||
Route[] routes = getField(RouteMapper.class, mapper, "routes");
|
||||
synchronized (responses){
|
||||
for(int i = 0; i < responses.length; i++){
|
||||
Route route = routes[i];
|
||||
System.out.println("["+type+"] ("+route.needLogin()+") "+route.path());
|
||||
}
|
||||
Field field = Router.class.getDeclaredField("responses");
|
||||
field.setAccessible(true);
|
||||
Map<RequestType, Map<Response, Route>> responses = (Map<RequestType, Map<Response, Route>>) field.get(this.router);
|
||||
for(Entry<RequestType, Map<Response, Route>> types : responses.entrySet()){
|
||||
for(Entry<Response, Route> routes : types.getValue().entrySet()){
|
||||
System.out.println("["+types.getKey()+"] ("+routes.getValue().needLogin()+") "+routes.getValue().path());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void extractDoc() throws Exception{
|
||||
RouteMapper[] mappers = getField(Router.class, router, "mappers");
|
||||
for(RequestType type : RequestType.values()){
|
||||
RouteMapper mapper = mappers[type.ordinal()];
|
||||
Response[] responses = getField(RouteMapper.class, mapper, "responses");
|
||||
synchronized (responses){
|
||||
for(int i = 0; i < responses.length; i++){
|
||||
Response response = responses[i];
|
||||
Method method = response.getClass().getDeclaredMethod("exec",
|
||||
Response.class.getDeclaredMethods()[0].getParameterTypes());
|
||||
for(RouteDoc doc : method.getDeclaredAnnotationsByType(RouteDoc.class)){
|
||||
System.out.println(doc.path()+((doc.path().isEmpty() ? "":"\n"))+" ["+doc.responseCode()+"] "+doc.responseDescription());
|
||||
}
|
||||
Field field = Router.class.getDeclaredField("responses");
|
||||
field.setAccessible(true);
|
||||
Map<RequestType, Map<Response, Route>> responses = (Map<RequestType, Map<Response, Route>>) field.get(this.router);
|
||||
for(Map<Response, Route> route : responses.values()){
|
||||
for(Response response : route.keySet()){
|
||||
Method method = response.getClass().getDeclaredMethod("exec",
|
||||
Response.class.getDeclaredMethods()[0].getParameterTypes());
|
||||
for(RouteDoc doc : method.getDeclaredAnnotationsByType(RouteDoc.class)){
|
||||
System.out.println(doc.path()+((doc.path().isEmpty() ? "":"\n"))+" ["+doc.responseCode()+"] "+doc.responseDescription());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JsonMap swagger(String host) throws Exception{
|
||||
JsonMap result = new JsonMap();
|
||||
result.set("swagger","2.0");
|
||||
JsonMap info = new JsonMap();
|
||||
info.set("title", "Peer-at-code backend api routes");
|
||||
info.set("description", "Using Peer-at Code Framework");
|
||||
result.set("info", info);
|
||||
result.set("host", host);
|
||||
result.set("basePath","/");
|
||||
|
||||
List<Response> routes = new ArrayList<>();
|
||||
RouteMapper[] mappers = getField(Router.class, router, "mappers");
|
||||
for(RequestType type : RequestType.values()){
|
||||
RouteMapper mapper = mappers[type.ordinal()];
|
||||
Response[] responses = getField(RouteMapper.class, mapper, "responses");
|
||||
synchronized (responses){
|
||||
routes.addAll(Arrays.asList(responses));
|
||||
}
|
||||
}
|
||||
|
||||
Set<String> packages = new HashSet<>();
|
||||
for(Response response : routes){
|
||||
String name = response.getClass().getPackage().getName();
|
||||
name = name.substring(name.lastIndexOf('.')+1, name.length());
|
||||
packages.add(name);
|
||||
}
|
||||
JsonArray tags = new JsonArray();
|
||||
for(String tag : packages){
|
||||
JsonMap current = new JsonMap();
|
||||
current.set("name", tag);
|
||||
tags.add(current);
|
||||
}
|
||||
result.set("tags", tags);
|
||||
JsonArray schemes = new JsonArray();
|
||||
schemes.add("https");
|
||||
result.set("schemes", schemes);
|
||||
|
||||
JsonMap paths = new JsonMap();
|
||||
|
||||
for(Response response : routes){
|
||||
Method method = response.getClass().getDeclaredMethod("exec",
|
||||
Response.class.getDeclaredMethods()[0].getParameterTypes());
|
||||
Route route = method.getDeclaredAnnotation(Route.class);
|
||||
RouteDoc[] docs = method.getDeclaredAnnotationsByType(RouteDoc.class);
|
||||
if(docs.length < 1) continue;
|
||||
RouteDoc base = docs[0];
|
||||
JsonMap current = new JsonMap();
|
||||
JsonMap data = new JsonMap();
|
||||
JsonArray tag = new JsonArray();
|
||||
String pack = response.getClass().getPackage().getName();
|
||||
pack = pack.substring(pack.lastIndexOf('.')+1, pack.length());
|
||||
tag.add(pack);
|
||||
data.set("tags", tag);
|
||||
|
||||
|
||||
current.set(route.type().toString().toLowerCase(), data);
|
||||
|
||||
paths.set(base.path(), current);
|
||||
}
|
||||
|
||||
result.set("paths", paths);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private <E> E getField(Class<?> clazz, Object instance, String name) throws Exception{
|
||||
Field field = clazz.getDeclaredField(name);
|
||||
field.setAccessible(true);
|
||||
return (E) field.get(instance);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package dev.peerat.backend.model;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.mapping.Treasure;
|
||||
|
||||
@Treasure
|
||||
public class Badge {
|
||||
|
||||
private String name;
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package dev.peerat.backend.model;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
public class Chapter {
|
||||
|
@ -35,13 +34,6 @@ public class Chapter {
|
|||
this.puzzles = puzzles;
|
||||
}
|
||||
|
||||
public boolean hasStarted(){
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
boolean show = true;
|
||||
if(startDate != null) show &= now.isAfter(startDate.toLocalDateTime());
|
||||
return show;
|
||||
}
|
||||
|
||||
public Timestamp getStartDate() {
|
||||
return startDate;
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ public class Completion{
|
|||
private int score;
|
||||
private String puzzleName;
|
||||
|
||||
public Completion(int playerId, int puzzleId, int tries, String fileName, int score){
|
||||
public Completion(int playerId, int puzzleId, int tries, String fileName, int score) {
|
||||
this(playerId, puzzleId, tries, score, fileName, null, null, null, null);
|
||||
}
|
||||
public Completion(int playerId, int puzzleId, int tries, String fileName, int score, String puzzleName) {
|
||||
|
@ -22,13 +22,13 @@ public class Completion{
|
|||
this(playerId, puzzleId, 0, 0, fileName, file, response, currentPuzzle, null);
|
||||
}
|
||||
|
||||
public Completion(int initTries, int initScore){
|
||||
public Completion(int initTries, int initScore) {
|
||||
// For group leaderboard
|
||||
this(-1, -1, initTries, initScore, null, null, null, null, null);
|
||||
}
|
||||
|
||||
public Completion(int playerId, int puzzleId, int tries, int score, String fileName, byte[] file, byte[] response,
|
||||
Puzzle currentPuzzle, String initPuzzleName){
|
||||
Puzzle currentPuzzle, String initPuzzleName) {
|
||||
this.playerId = playerId;
|
||||
this.puzzleId = puzzleId;
|
||||
this.fileName = fileName;
|
||||
|
@ -40,7 +40,7 @@ public class Completion{
|
|||
this.score = score;
|
||||
|
||||
if (currentPuzzle != null)
|
||||
addTry(currentPuzzle, response, 0);
|
||||
addTry(currentPuzzle, response);
|
||||
|
||||
puzzleName = initPuzzleName;
|
||||
}
|
||||
|
@ -62,17 +62,12 @@ public class Completion{
|
|||
return tries;
|
||||
}
|
||||
|
||||
public void addTry(Puzzle currentPuzzle, byte[] response, int chapter){
|
||||
public void addTry(Puzzle currentPuzzle, byte[] response) {
|
||||
if (score <= 0){
|
||||
tries++;
|
||||
if (response != null && Arrays.equals(currentPuzzle.getSoluce(), response)){
|
||||
if (response != null && Arrays.equals(currentPuzzle.getSoluce(), response)) {
|
||||
if (tries > 1) { // Loose 5% each try with a minimum of 1 for score
|
||||
if(chapter < 4){
|
||||
score = currentPuzzle.getScoreMax();
|
||||
}else{
|
||||
score = (int) Math.ceil(currentPuzzle.getScoreMax() * (1 - ((tries - 1) / 20.)));
|
||||
}
|
||||
// score = currentPuzzle.getScoreMax();
|
||||
score = (int) Math.ceil(currentPuzzle.getScoreMax() * (1 - ((tries - 1) / 20.)));
|
||||
if (score < 1)
|
||||
score = 1;
|
||||
} else
|
||||
|
|
|
@ -4,41 +4,36 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.jose4j.json.internal.json_simple.JSONArray;
|
||||
import org.jose4j.json.internal.json_simple.JSONObject;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.mapping.SeaBottle;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
import be.jeffcheasey88.peeratcode.mapping.Treasure;
|
||||
|
||||
@Treasure
|
||||
public class Group implements Comparable<Group> {
|
||||
private String name;
|
||||
private Integer linkToChapter;
|
||||
// private Integer linkToPuzzle;
|
||||
private List<Player> players;
|
||||
private int playerCount;
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return "Group[name="+name+", chapter="+linkToChapter+"]";
|
||||
}
|
||||
|
||||
public Group(JsonMap json){
|
||||
public Group(JSONObject json){
|
||||
this.name = (String) json.get("name");
|
||||
if (json.has("chapter"))
|
||||
if (json.containsKey("chapter"))
|
||||
this.linkToChapter = ((Number) json.get("chapter")).intValue();
|
||||
// if (json.has("puzzle"))
|
||||
// if (json.containsKey("puzzle"))
|
||||
// this.linkToPuzzle = ((Number) json.get("puzzle")).intValue();
|
||||
}
|
||||
|
||||
public Group(String name, Integer initChap, Integer initPuzz, int playerCount) {
|
||||
public Group(String name, Integer initChap, Integer initPuzz) {
|
||||
this.name = name;
|
||||
this.linkToChapter = initChap;
|
||||
// this.linkToPuzzle = initPuzz;
|
||||
this.playerCount = playerCount;
|
||||
}
|
||||
|
||||
public int getPlayerCount(){
|
||||
return this.playerCount;
|
||||
}
|
||||
|
||||
@SeaBottle
|
||||
|
@ -119,7 +114,6 @@ public class Group implements Comparable<Group> {
|
|||
}
|
||||
groupJSON.put("players", groupsPlayerJSON);
|
||||
}
|
||||
groupJSON.put("playerCount", this.playerCount);
|
||||
return groupJSON;
|
||||
}
|
||||
|
||||
|
@ -139,7 +133,7 @@ public class Group implements Comparable<Group> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public int hashCode(){
|
||||
public int hashCode() {
|
||||
return Objects.hash(name);
|
||||
}
|
||||
|
||||
|
|
|
@ -5,8 +5,8 @@ import java.util.HashSet;
|
|||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
import dev.peerat.framework.utils.json.JsonArray;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class Player implements Comparable<Player> {
|
||||
private String pseudo;
|
||||
|
@ -42,10 +42,6 @@ public class Player implements Comparable<Player> {
|
|||
this.email = email;
|
||||
}
|
||||
|
||||
public void setPseudo(String pseudo){
|
||||
this.pseudo = pseudo;
|
||||
}
|
||||
|
||||
public String getPseudo() {
|
||||
return this.pseudo;
|
||||
}
|
||||
|
@ -70,9 +66,9 @@ public class Player implements Comparable<Player> {
|
|||
return groups;
|
||||
}
|
||||
|
||||
public JsonArray getJsonGroups() {
|
||||
public JSONArray getJsonGroups() {
|
||||
if (groups != null) {
|
||||
JsonArray groupsJSON = new JsonArray();
|
||||
JSONArray groupsJSON = new JSONArray();
|
||||
for (Group group : groups) {
|
||||
groupsJSON.add(group.toJson());
|
||||
}
|
||||
|
@ -121,13 +117,13 @@ public class Player implements Comparable<Player> {
|
|||
return completions.size();
|
||||
}
|
||||
|
||||
public JsonArray getJsonCompletions() {
|
||||
JsonArray completionsJSON = new JsonArray();
|
||||
public JSONArray getJsonCompletions() {
|
||||
JSONArray completionsJSON = new JSONArray();
|
||||
for (Completion completion : completions) {
|
||||
JsonMap completionJSON = new JsonMap();
|
||||
completionJSON.set("puzzleName", completion.getPuzzleName());
|
||||
completionJSON.set("tries", completion.getTries());
|
||||
completionJSON.set("score", completion.getScore());
|
||||
JSONObject completionJSON = new JSONObject();
|
||||
completionJSON.put("puzzleName", completion.getPuzzleName());
|
||||
completionJSON.put("tries", completion.getTries());
|
||||
completionJSON.put("score", completion.getScore());
|
||||
completionsJSON.add(completionJSON);
|
||||
}
|
||||
return completionsJSON;
|
||||
|
@ -145,17 +141,17 @@ public class Player implements Comparable<Player> {
|
|||
return badges;
|
||||
}
|
||||
|
||||
public JsonArray getJsonBadges() {
|
||||
public JSONArray getJsonBadges() {
|
||||
if (badges == null)
|
||||
return null;
|
||||
JsonArray badgesJSON = new JsonArray();
|
||||
JSONArray badgesJSON = new JSONArray();
|
||||
for (Badge badge : badges) {
|
||||
JsonMap badgeJSON = new JsonMap();
|
||||
badgeJSON.set("name", badge.getName());
|
||||
JSONObject badgeJSON = new JSONObject();
|
||||
badgeJSON.put("name", badge.getName());
|
||||
byte[] logo = badge.getLogo();
|
||||
if (logo != null)
|
||||
badgeJSON.set("logo", Base64.getEncoder().encodeToString(logo));
|
||||
badgeJSON.set("level", badge.getLevel());
|
||||
badgeJSON.put("logo", Base64.getEncoder().encodeToString(logo));
|
||||
badgeJSON.put("level", badge.getLevel());
|
||||
badgesJSON.add(badgeJSON);
|
||||
}
|
||||
return badgesJSON;
|
||||
|
@ -189,7 +185,7 @@ public class Player implements Comparable<Player> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public int hashCode(){
|
||||
public int hashCode() {
|
||||
return Objects.hash(email, pseudo);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
package dev.peerat.backend.model;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import dev.peerat.framework.utils.json.JsonArray;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class Puzzle {
|
||||
|
||||
|
@ -19,10 +17,9 @@ public class Puzzle {
|
|||
private int scoreMax;
|
||||
private Set<String> tags;
|
||||
private int depend;
|
||||
private Timestamp startDate;
|
||||
|
||||
public Puzzle(int id, String name, String content, byte[] soluce, String verify, int scoreMax, String tags,
|
||||
int depend, Timestamp startDate) {
|
||||
int depend) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.content = content;
|
||||
|
@ -31,7 +28,6 @@ public class Puzzle {
|
|||
this.scoreMax = scoreMax;
|
||||
setTags(tags);
|
||||
this.depend = depend;
|
||||
this.startDate = startDate;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
|
@ -63,13 +59,13 @@ public class Puzzle {
|
|||
*
|
||||
* @return DEATH
|
||||
*/
|
||||
public JsonArray getJsonTags() {
|
||||
public JSONArray getJsonTags() {
|
||||
if (tags == null)
|
||||
return null;
|
||||
JsonArray tagsJSON = new JsonArray();
|
||||
JSONArray tagsJSON = new JSONArray();
|
||||
for (String tag : tags) {
|
||||
JsonMap tagJSON = new JsonMap();
|
||||
tagJSON.set("name", tag);
|
||||
JSONObject tagJSON = new JSONObject();
|
||||
tagJSON.put("name", tag);
|
||||
tagsJSON.add(tagJSON);
|
||||
}
|
||||
return tagsJSON;
|
||||
|
@ -86,13 +82,6 @@ public class Puzzle {
|
|||
return this.depend;
|
||||
}
|
||||
|
||||
public boolean hasStarted(){
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
boolean show = true;
|
||||
if(startDate != null) show &= now.isAfter(startDate.toLocalDateTime());
|
||||
return show;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object) {
|
||||
if (this == object)
|
||||
|
|
|
@ -1,30 +0,0 @@
|
|||
package dev.peerat.backend.model;
|
||||
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class Tag{
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public Tag(int id, String name){
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId(){
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public String getName(){
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public JsonMap toJson(){
|
||||
JsonMap result = new JsonMap();
|
||||
result.set("id", id);
|
||||
result.set("name", name);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,114 +0,0 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.model.Badge;
|
||||
import dev.peerat.backend.model.Chapter;
|
||||
import dev.peerat.backend.model.Completion;
|
||||
import dev.peerat.backend.model.Group;
|
||||
import dev.peerat.backend.model.Player;
|
||||
import dev.peerat.backend.model.Puzzle;
|
||||
|
||||
public class BaseDatabaseQuery{
|
||||
|
||||
private ConnectionManager con;
|
||||
private Configuration config;
|
||||
|
||||
public BaseDatabaseQuery(ConnectionManager con){
|
||||
this.con = con;
|
||||
}
|
||||
|
||||
public Connection ensureConnection() throws SQLException{
|
||||
return this.con.ensureConnection();
|
||||
}
|
||||
|
||||
public PreparedStatement prepare(String request) throws SQLException{
|
||||
return this.con.ensureConnection().prepareStatement(request);
|
||||
}
|
||||
|
||||
public Puzzle makePuzzle(ResultSet puzzleResult) throws SQLException {
|
||||
return new Puzzle(puzzleResult.getInt("id_puzzle"), puzzleResult.getString("name"),
|
||||
puzzleResult.getString("content"), puzzleResult.getBytes("soluce"), puzzleResult.getString("verify"),
|
||||
puzzleResult.getInt("score_max"), puzzleResult.getString("tags"),
|
||||
hasColumn(puzzleResult, "origin") ? puzzleResult.getInt("origin") : -1,
|
||||
puzzleResult.getTimestamp("start_date"));
|
||||
}
|
||||
|
||||
public Chapter makeChapter(ResultSet chapterResult) throws SQLException {
|
||||
return new Chapter(chapterResult.getInt("id_chapter"), chapterResult.getString("name"),
|
||||
chapterResult.getTimestamp("start_date"), chapterResult.getTimestamp("end_date"));
|
||||
}
|
||||
|
||||
public Completion makeCompletion(ResultSet completionResult) throws SQLException {
|
||||
String fileName = null;
|
||||
if (hasColumn(completionResult, "fileName"))
|
||||
fileName = completionResult.getString("fileName");
|
||||
String puzzleName = null;
|
||||
if (hasColumn(completionResult, "name"))
|
||||
puzzleName = completionResult.getString("name");
|
||||
|
||||
return new Completion(completionResult.getInt("fk_player"), completionResult.getInt("fk_puzzle"), completionResult.getInt("tries"),
|
||||
fileName, completionResult.getInt("score"), puzzleName);
|
||||
}
|
||||
|
||||
public Player makePlayer(ResultSet playerResult, int id) throws SQLException {
|
||||
Player p = new Player(playerResult.getString("pseudo"), playerResult.getString("email"),
|
||||
playerResult.getString("firstName"), playerResult.getString("lastName"));
|
||||
if (hasColumn(playerResult, "avatar")) {
|
||||
p.setAvatar(playerResult.getBytes("avatar"));
|
||||
}
|
||||
if (hasColumn(playerResult, "score")) {
|
||||
p.addCompletion(new Completion(playerResult.getInt("tries"), playerResult.getInt("score")));
|
||||
for (int ct = 1; ct < playerResult.getInt("completions"); ct++)
|
||||
{ // TODO refactor for V3
|
||||
p.addCompletion(new Completion(0, 0));
|
||||
}
|
||||
}
|
||||
if (hasColumn(playerResult, "name")) {
|
||||
// Manage groups
|
||||
String groupName = playerResult.getString("name");
|
||||
if (groupName != null) {
|
||||
p.addGroup(makeGroup(playerResult));
|
||||
}
|
||||
}
|
||||
|
||||
// ADD rank
|
||||
PreparedStatement completionsStmt = con.ensureConnection().prepareStatement(DatabasePlayerRepository.GET_PLAYER_RANK());
|
||||
completionsStmt.setInt(1, id);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
while (result.next()) {
|
||||
p.setRank(result.getInt("rank"));
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
public Group makeGroup(ResultSet result) throws SQLException {
|
||||
return new Group(result.getString("name"), result.getInt("fk_chapter"), result.getInt("fk_puzzle"), ((hasColumn(result, "countPlayer")) ? result.getInt("countPlayer") : 0));
|
||||
}
|
||||
|
||||
public Player makeGroupPlayer(ResultSet result) throws SQLException {
|
||||
return new Player(result.getString("pseudo"), result.getInt("score"), result.getInt("tries"));
|
||||
}
|
||||
|
||||
public Badge makeBadge(ResultSet rs) throws SQLException {
|
||||
return new Badge(rs.getString("name"), rs.getBytes("logo"), rs.getInt("level"));
|
||||
}
|
||||
|
||||
public boolean hasColumn(ResultSet rs, String columnName) throws SQLException {
|
||||
// Found on StackOverflow
|
||||
ResultSetMetaData rsmd = rs.getMetaData();
|
||||
int columns = rsmd.getColumnCount();
|
||||
for (int x = 1; x <= columns; x++) {
|
||||
if (columnName.equals(rsmd.getColumnName(x)))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
|
||||
public class ConnectionManager {
|
||||
|
||||
private Connection con;
|
||||
private Configuration config;
|
||||
|
||||
public ConnectionManager(Configuration config){
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public Connection ensureConnection() throws SQLException {
|
||||
if (con == null || (!con.isValid(5))) {
|
||||
this.con = DriverManager.getConnection(
|
||||
"jdbc:mysql://" + config.getDbHost() + ":" + config.getDbPort() + "/" + config.getDbDatabase() + "",
|
||||
config.getDbUser(), config.getDbPassword());
|
||||
}
|
||||
return this.con;
|
||||
}
|
||||
}
|
|
@ -1,194 +0,0 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.model.Chapter;
|
||||
import dev.peerat.backend.model.Puzzle;
|
||||
import dev.peerat.backend.model.Tag;
|
||||
|
||||
public class DatabaseAdminRepository extends BaseDatabaseQuery{
|
||||
|
||||
private static enum Query{
|
||||
|
||||
//ADMIN
|
||||
ADD_CHAPTER("INSERT INTO chapters (name, start_date, end_date) VALUES (?,?,?)"),
|
||||
DELETE_CHAPTER("DELETE FROM chapters WHERE id_chapter = ?"),
|
||||
EDIT_CHAPTER("UPDATE chapters SET name = ?, start_date = ?, end_date = ? WHERE id_chapter = ?"),
|
||||
GET_CHAPTER("SELECT * FROM chapters WHERE id_chapter = ?"),
|
||||
|
||||
ADD_PUZZLE("INSERT INTO puzzles (name, content, soluce, verify, score_max, fk_chapter) VALUES (?,?,?,?,?,?)"),
|
||||
DELETE_PUZZLE("DELETE FROM puzzles WHERE id_puzzle = ?"),
|
||||
EDIT_PUZZLE("UPDATE puzzles SET name = ?, content = ?, soluce = ?, verify = ?, score_max = ?, fk_chapter = ? WHERE id_puzzle = ?"),
|
||||
GET_PUZZLE("SELECT p.*, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE p.id_puzzle = ? GROUP BY p.id_puzzle"),
|
||||
GET_PUZZLES("SELECT p.*, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag GROUP BY p.id_puzzle"),
|
||||
|
||||
ADD_TAG("INSERT INTO tags (name) VALUES (?)"),
|
||||
DELETE_TAG("DELETE FROM tags WHERE id_tag = ?"),
|
||||
EDIT_TAG("UPDATE tags SET name = ? WHERE id_tag = ?"),
|
||||
GET_TAGS("SELECT * FROM tags");
|
||||
|
||||
private String request;
|
||||
|
||||
Query(Query parent, String request) {
|
||||
this.request = parent.request + request;
|
||||
}
|
||||
|
||||
Query(String request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||
return base.prepare(this.request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.request;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Configuration config;
|
||||
|
||||
public DatabaseAdminRepository(ConnectionManager con, Configuration config){
|
||||
super(con);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
//ADMIN
|
||||
public Chapter getAdminChapter(int id){
|
||||
try {
|
||||
PreparedStatement chapterStmt = Query.GET_CHAPTER.prepare(this);
|
||||
chapterStmt.setInt(1, id);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
if (chapterResult.next()) {
|
||||
Chapter chapter = makeChapter(chapterResult);
|
||||
return chapter;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Puzzle getAdminPuzzle(int id){
|
||||
try {
|
||||
PreparedStatement chapterStmt = Query.GET_PUZZLE.prepare(this);
|
||||
chapterStmt.setInt(1, id);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
if (chapterResult.next()) {
|
||||
return makePuzzle(chapterResult);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Puzzle> getAdminPuzzles(){
|
||||
try {
|
||||
PreparedStatement chapterStmt = Query.GET_PUZZLES.prepare(this);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
List<Puzzle> list = new ArrayList<>();
|
||||
while(chapterResult.next()){
|
||||
list.add(makePuzzle(chapterResult));
|
||||
}
|
||||
return list;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Tag> getAdminTags(){
|
||||
try {
|
||||
PreparedStatement chapterStmt = Query.GET_TAGS.prepare(this);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
List<Tag> list = new ArrayList<>();
|
||||
while(chapterResult.next()){
|
||||
list.add(new Tag(chapterResult.getInt("id_tag"), chapterResult.getString("name")));
|
||||
}
|
||||
return list;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean adminAddChapter(Chapter chapter) throws SQLException {
|
||||
PreparedStatement statement = Query.ADD_CHAPTER.prepare(this);
|
||||
statement.setString(1, chapter.getName());
|
||||
statement.setTimestamp(2, chapter.getStartDate());
|
||||
statement.setTimestamp(3, chapter.getEndDate());
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminAddPuzzle(Puzzle puzzle, int chapter) throws SQLException {
|
||||
PreparedStatement statement = Query.ADD_PUZZLE.prepare(this);
|
||||
statement.setString(1, puzzle.getName());
|
||||
statement.setString(2, puzzle.getContent());
|
||||
statement.setBytes(3, puzzle.getSoluce());
|
||||
statement.setString(4, "");
|
||||
statement.setInt(5, puzzle.getScoreMax());
|
||||
statement.setInt(6, chapter);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminAddTag(String name) throws SQLException {
|
||||
PreparedStatement statement = Query.ADD_TAG.prepare(this);
|
||||
statement.setString(1, name);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminUpdateChapter(int id, Chapter chapter) throws SQLException {
|
||||
PreparedStatement statement = Query.EDIT_CHAPTER.prepare(this);
|
||||
statement.setString(1, chapter.getName());
|
||||
statement.setTimestamp(2, chapter.getStartDate());
|
||||
statement.setTimestamp(3, chapter.getEndDate());
|
||||
statement.setInt(4, id);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminUpdatePuzzle(int id, Puzzle puzzle, int chapter) throws SQLException {
|
||||
PreparedStatement statement = Query.EDIT_PUZZLE.prepare(this);
|
||||
statement.setString(1, puzzle.getName());
|
||||
statement.setString(2, puzzle.getContent());
|
||||
statement.setBytes(3, puzzle.getSoluce());
|
||||
statement.setString(4, "");
|
||||
statement.setInt(5, puzzle.getScoreMax());
|
||||
statement.setInt(6, chapter);
|
||||
statement.setInt(7, id);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminUpdateTag(Tag tag) throws SQLException {
|
||||
PreparedStatement statement = Query.EDIT_TAG.prepare(this);
|
||||
statement.setString(1, tag.getName());
|
||||
statement.setInt(2, tag.getId());
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminDeleteChapter(int id) throws SQLException {
|
||||
PreparedStatement statement = Query.DELETE_CHAPTER.prepare(this);
|
||||
statement.setInt(1, id);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminDeletePuzzle(int id) throws SQLException {
|
||||
PreparedStatement statement = Query.DELETE_PUZZLE.prepare(this);
|
||||
statement.setInt(1, id);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
public boolean adminDeleteTag(int id) throws SQLException {
|
||||
PreparedStatement statement = Query.DELETE_TAG.prepare(this);
|
||||
statement.setInt(1, id);
|
||||
return (statement.executeUpdate() >= 0);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,167 +0,0 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
import com.password4j.Password;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
|
||||
public class DatabaseAuthRepository extends BaseDatabaseQuery{
|
||||
|
||||
private static enum Query{
|
||||
|
||||
// REGISTER
|
||||
CHECK_PSEUDO_AVAILABLE_QUERY("SELECT * FROM players WHERE pseudo = ?"),
|
||||
CHECK_EMAIL_AVAILABLE_QUERY("SELECT * FROM players WHERE email = ?"),
|
||||
REGISTER_QUERY(
|
||||
"INSERT INTO players (pseudo, email, passwd, firstname, lastname, description, avatar) VALUES (?, ?, ?, ?, ?, ?, ?)"),
|
||||
REGISTER_PLAYER_IN_EXISTING_GROUP(
|
||||
"INSERT INTO containsGroups (fk_player, fk_group) VALUES (?, (SELECT id_group FROM groups WHERE name = ?));"),
|
||||
|
||||
// LOGIN
|
||||
CHECK_PASSWORD("SELECT id_player, passwd FROM players WHERE pseudo=?");
|
||||
|
||||
private String request;
|
||||
|
||||
Query(Query parent, String request) {
|
||||
this.request = parent.request + request;
|
||||
}
|
||||
|
||||
Query(String request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||
return base.prepare(this.request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.request;
|
||||
}
|
||||
}
|
||||
|
||||
private Configuration config;
|
||||
|
||||
public DatabaseAuthRepository(ConnectionManager con, Configuration config){
|
||||
super(con);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a pseudo is available
|
||||
*
|
||||
* @param pseudo The pseudo to check
|
||||
* @return True if the pseudo is available, false if it's already taken
|
||||
*/
|
||||
public boolean checkPseudoAvailability(String pseudo) {
|
||||
return checkAvailability(pseudo, Query.CHECK_PSEUDO_AVAILABLE_QUERY.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an email is available
|
||||
*
|
||||
* @param email The email to check
|
||||
* @return True if the email is available, false if it's already taken
|
||||
*/
|
||||
public boolean checkEmailAvailability(String email) {
|
||||
return checkAvailability(email, Query.CHECK_EMAIL_AVAILABLE_QUERY.toString());
|
||||
}
|
||||
|
||||
private boolean checkAvailability(String queriedString, String correspondingQuery) {
|
||||
try {
|
||||
PreparedStatement statement = prepare(correspondingQuery);
|
||||
statement.setString(1, queriedString);
|
||||
ResultSet result = statement.executeQuery();
|
||||
return !result.next();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new user
|
||||
*
|
||||
* @param pseudo The pseudo of the user
|
||||
* @param email The email of the user
|
||||
* @param password The password of the user
|
||||
* @param firstname The firstname of the user
|
||||
* @param lastname The lastname of the user
|
||||
* @param description The description of the user
|
||||
* @param sgroup The group of the user
|
||||
* @param avatar The avatar of the user
|
||||
* @return True if the user was registered, false if an error occurred
|
||||
*/
|
||||
public int register(String pseudo, String email, String password, String firstname, String lastname,
|
||||
String description, String sgroup, String avatar) {
|
||||
try {
|
||||
String pass = Password.hash(password).withArgon2().getResult();
|
||||
System.out.println("pass("+pass.length()+") "+pass);
|
||||
Connection con = ensureConnection();
|
||||
con.setAutoCommit(false);
|
||||
try (PreparedStatement playerStatement = con.prepareStatement(Query.REGISTER_QUERY.toString(),
|
||||
Statement.RETURN_GENERATED_KEYS)) {
|
||||
playerStatement.setString(1, pseudo);
|
||||
playerStatement.setString(2, email);
|
||||
playerStatement.setString(3, Password.hash(password).withArgon2().getResult());
|
||||
playerStatement.setString(4, firstname);
|
||||
playerStatement.setString(5, lastname);
|
||||
playerStatement.setString(6, description);
|
||||
playerStatement.setString(7, avatar);
|
||||
if (playerStatement.executeUpdate() == 1) {
|
||||
ResultSet inserted = playerStatement.getGeneratedKeys();
|
||||
if (inserted.next()) {
|
||||
int newPlayerId = inserted.getInt(1);
|
||||
if (!sgroup.isEmpty()) {
|
||||
try (PreparedStatement containsGroupsStatement = con
|
||||
.prepareStatement(Query.REGISTER_PLAYER_IN_EXISTING_GROUP.toString())) {
|
||||
containsGroupsStatement.setInt(1, newPlayerId);
|
||||
containsGroupsStatement.setString(2, sgroup);
|
||||
containsGroupsStatement.executeUpdate();
|
||||
}
|
||||
}
|
||||
con.commit();
|
||||
con.setAutoCommit(true);
|
||||
return newPlayerId;
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
con.rollback();
|
||||
con.setAutoCommit(true);
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Login a user
|
||||
*
|
||||
* @param username The username of the user
|
||||
* @param password The password of the user
|
||||
* @return id the id of the user, -1 if not login successefuly
|
||||
*/
|
||||
public int login(String username, String password) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = prepare(Query.CHECK_PASSWORD.toString());
|
||||
statement.setString(1, username);
|
||||
ResultSet result = statement.executeQuery();
|
||||
if (result.next()) {
|
||||
String hashedPassword = result.getString("passwd");
|
||||
if (Password.check(password, hashedPassword).withArgon2())
|
||||
return result.getInt("id_player");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,69 +0,0 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.model.Badge;
|
||||
import dev.peerat.backend.model.Puzzle;
|
||||
|
||||
public class DatabaseBadgeRepository extends BaseDatabaseQuery{
|
||||
|
||||
public static String GET_BADGES_OF_PLAYER(){
|
||||
return Query.GET_BADGES_OF_PLAYER.request;
|
||||
}
|
||||
|
||||
private static enum Query{
|
||||
|
||||
// BADGES
|
||||
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 = ?");
|
||||
|
||||
|
||||
private String request;
|
||||
|
||||
Query(Query parent, String request) {
|
||||
this.request = parent.request + request;
|
||||
}
|
||||
|
||||
Query(String request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||
return base.prepare(this.request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.request;
|
||||
}
|
||||
}
|
||||
|
||||
private Configuration config;
|
||||
|
||||
public DatabaseBadgeRepository(ConnectionManager con, Configuration config){
|
||||
super(con);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public Badge getBadge(int badgeId) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement completionsStmt = Query.GET_BADGE.prepare(this);
|
||||
completionsStmt.setInt(1, badgeId);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
if (result.next()) {
|
||||
return makeBadge(result);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,122 +0,0 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.model.Chapter;
|
||||
import dev.peerat.backend.model.Puzzle;
|
||||
|
||||
public class DatabaseChapterRepository extends BaseDatabaseQuery{
|
||||
|
||||
private static enum Query{
|
||||
|
||||
// CHAPTERS
|
||||
SPECIFIC_CHAPTER_QUERY("SELECT * FROM chapters WHERE id_chapter = ?"),
|
||||
CHAPTER_FROM_PUZZLE("SELECT c.*\r\n"
|
||||
+ "FROM chapters c\r\n"
|
||||
+ "JOIN puzzles p ON p.fk_chapter = c.id_chapter\r\n"
|
||||
+ "WHERE p.id_puzzle = ?"),
|
||||
ALL_CHAPTERS_QUERY("SELECT * FROM chapters WHERE id_chapter > 0");
|
||||
|
||||
|
||||
private String request;
|
||||
|
||||
Query(Query parent, String request) {
|
||||
this.request = parent.request + request;
|
||||
}
|
||||
|
||||
Query(String request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||
return base.prepare(this.request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.request;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Configuration config;
|
||||
private DatabasePuzzleRepository puzzleRepo;
|
||||
|
||||
public DatabaseChapterRepository(ConnectionManager con, Configuration config, DatabasePuzzleRepository puzzleRepo){
|
||||
super(con);
|
||||
this.config = config;
|
||||
this.puzzleRepo = puzzleRepo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific chapter
|
||||
*
|
||||
* @param id The id of the chapter
|
||||
* @return The chapter or null if an error occurred
|
||||
*/
|
||||
public Chapter getChapter(int id) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement chapterStmt = Query.SPECIFIC_CHAPTER_QUERY.prepare(this);
|
||||
chapterStmt.setInt(1, id);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
if (chapterResult.next()) {
|
||||
Chapter chapter = makeChapter(chapterResult);
|
||||
List<Puzzle> puzzles = puzzleRepo.getPuzzlesInChapter(id);
|
||||
chapter.setPuzzles(puzzles);
|
||||
return chapter;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Chapter getChapter(Puzzle puzzle){
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement chapterStmt = Query.CHAPTER_FROM_PUZZLE.prepare(this);
|
||||
chapterStmt.setInt(1, puzzle.getId());
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
if (chapterResult.next()) {
|
||||
Chapter chapter = makeChapter(chapterResult);
|
||||
List<Puzzle> puzzles = puzzleRepo.getPuzzlesInChapter(chapter.getId());
|
||||
chapter.setPuzzles(puzzles);
|
||||
return chapter;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all chapters in the database
|
||||
*
|
||||
* @return List of all chapters or null if an error occurred
|
||||
*/
|
||||
public List<Chapter> getAllChapters() {
|
||||
try {
|
||||
List<Chapter> chapterList = new ArrayList<>();
|
||||
ensureConnection();
|
||||
PreparedStatement chapterStmt = Query.ALL_CHAPTERS_QUERY.prepare(this);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
while (chapterResult.next()) {
|
||||
Chapter chapter = makeChapter(chapterResult);
|
||||
chapter.setPuzzles(puzzleRepo.getPuzzlesInChapter(chapter.getId()));
|
||||
chapterList.add(chapter);
|
||||
}
|
||||
return chapterList;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,162 +0,0 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.model.Completion;
|
||||
import dev.peerat.backend.model.Puzzle;
|
||||
|
||||
public class DatabaseCompletionRepository extends BaseDatabaseQuery{
|
||||
|
||||
private static enum Query{
|
||||
|
||||
// COMPLETIONS
|
||||
GET_COMPLETION(
|
||||
"SELECT * FROM completions WHERE fk_puzzle = ? AND fk_player = ?"),
|
||||
GET_COMPLETION_GROUP("SELECT c.*\r\n"
|
||||
+ "FROM completions c\r\n"
|
||||
+ "WHERE c.fk_puzzle = ? AND c.fk_player IN\r\n"
|
||||
+ "(select f.fk_player FROM containsGroups cgs JOIN containsGroups f ON f.fk_group = cgs.fk_group JOIN groups g ON g.id_group = cgs.fk_group WHERE g.fk_chapter = 12 AND cgs.fk_player = ?)"),
|
||||
INSERT_COMPLETION(
|
||||
"INSERT INTO completions (fk_puzzle, fk_player, tries, code, fileName, score) values (?, ?, ?, ?, ?, ?)"),
|
||||
UPDATE_COMPLETION(
|
||||
"UPDATE completions SET tries = ?, score = ?, fk_player = ? WHERE fk_puzzle = ? AND fk_player = ?"),
|
||||
SCORE("SELECT score FROM completions WHERE fk_player = ? AND fk_puzzle = ?"),
|
||||
SCORE_GROUP("SELECT c.score\r\n"
|
||||
+ "FROM completions c\r\n"
|
||||
+ "WHERE c.fk_puzzle = ? AND c.fk_player IN\r\n"
|
||||
+ "(select f.fk_player FROM containsGroups cgs JOIN containsGroups f ON f.fk_group = cgs.fk_group JOIN groups g ON g.id_group = cgs.fk_group WHERE g.fk_chapter = 12 AND cgs.fk_player = ?)");
|
||||
|
||||
private String request;
|
||||
|
||||
Query(Query parent, String request) {
|
||||
this.request = parent.request + request;
|
||||
}
|
||||
|
||||
Query(String request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||
return base.prepare(this.request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.request;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Configuration config;
|
||||
private DatabaseChapterRepository chapterRepo;
|
||||
|
||||
public DatabaseCompletionRepository(ConnectionManager con, Configuration config, DatabaseChapterRepository chapterRepo){
|
||||
super(con);
|
||||
this.config = config;
|
||||
this.chapterRepo = chapterRepo;
|
||||
}
|
||||
|
||||
public Completion getCompletionGroup(int user, int puzzle) {
|
||||
try {
|
||||
PreparedStatement stmt = Query.GET_COMPLETION_GROUP.prepare(this);
|
||||
stmt.setInt(1, puzzle);
|
||||
stmt.setInt(2, user);
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if (result.next())
|
||||
return makeCompletion(result);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return getCompletion(user, puzzle);
|
||||
}
|
||||
|
||||
public Completion getCompletion(int playerId, int puzzleId) {
|
||||
try {
|
||||
PreparedStatement completionsStmt = Query.GET_COMPLETION.prepare(this);
|
||||
completionsStmt.setInt(1, puzzleId);
|
||||
completionsStmt.setInt(2, playerId);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
if (result.next()) {
|
||||
return makeCompletion(result);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Completion insertOrUpdatePuzzleResponse(int puzzleId, int userId, String fileName, byte[] code,
|
||||
byte[] response, Puzzle currentPuzzle){
|
||||
try {
|
||||
ensureConnection();
|
||||
Completion completion = getCompletionGroup(userId, puzzleId);
|
||||
if (completion == null){
|
||||
System.out.println("Completion is null");
|
||||
completion = new Completion(userId, puzzleId, fileName, code, response, currentPuzzle);
|
||||
insertCompletion(completion);
|
||||
} else {
|
||||
System.out.println(completion);
|
||||
completion.addTry(currentPuzzle, response, chapterRepo.getChapter(currentPuzzle).getId());
|
||||
int lastUserId = completion.getPlayerId();
|
||||
completion.updatePlayer(userId);
|
||||
updateCompletion(completion, lastUserId);
|
||||
}
|
||||
return completion;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void insertCompletion(Completion newCompletion) throws SQLException {
|
||||
PreparedStatement statement = Query.INSERT_COMPLETION.prepare(this);
|
||||
statement.setInt(1, newCompletion.getPuzzleId());
|
||||
statement.setInt(2, newCompletion.getPlayerId());
|
||||
statement.setInt(3, newCompletion.getTries());
|
||||
statement.setBytes(4, newCompletion.getCode());
|
||||
statement.setString(5, newCompletion.getFileName());
|
||||
statement.setInt(6, newCompletion.getScore());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
|
||||
|
||||
private void updateCompletion(Completion completionToUpdate, int user) throws SQLException{
|
||||
System.out.println("update "+completionToUpdate);
|
||||
PreparedStatement statement = Query.UPDATE_COMPLETION.prepare(this);
|
||||
statement.setInt(1, completionToUpdate.getTries());
|
||||
statement.setInt(2, completionToUpdate.getScore());
|
||||
statement.setInt(3, completionToUpdate.getPlayerId());
|
||||
statement.setInt(4, completionToUpdate.getPuzzleId());
|
||||
statement.setInt(5, user);
|
||||
statement.executeUpdate();
|
||||
}
|
||||
|
||||
public int getScore(int user, int puzzle) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement stmt = Query.SCORE_GROUP.prepare(this);
|
||||
stmt.setInt(1, puzzle);
|
||||
stmt.setInt(2, user);
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if (result.next())
|
||||
return result.getInt("score");
|
||||
|
||||
stmt = Query.SCORE.prepare(this);
|
||||
stmt.setInt(1, user);
|
||||
stmt.setInt(2, puzzle);
|
||||
|
||||
result = stmt.executeQuery();
|
||||
if (result.next())
|
||||
return result.getInt("score");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
|
@ -1,166 +0,0 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.model.Group;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
|
||||
public class DatabaseGroupRepository extends BaseDatabaseQuery{
|
||||
|
||||
private static enum Query{
|
||||
|
||||
// GROUPS
|
||||
ALL_GROUPS("SELECT * FROM groups"),
|
||||
ALL_GROUPS_BY_CHAPTER("select g.*, count(cg.fk_player) as countPlayer from groups g left join containsGroups cg on cg.fk_group = g.id_group where g.fk_chapter = ? group by g.id_group"),
|
||||
GET_GROUP_FOR_PLAYER("SELECT g.* FROM groups g JOIN containsGroups cg ON cg.fk_group = g.id_group WHERE cg.fk_player = ? AND g.fk_chapter = ?"), // AND g.fk_puzzle = ?
|
||||
GET_GROUP_ID_BY_DATA("SELECT id_group FROM groups WHERE name = ? AND (fk_chapter = ?)"), // OR fk_puzzle = ?
|
||||
GET_GROUP_USERS_COUNT("SELECT count(*) as howmany FROM containsGroups WHERE fk_group = ?"),
|
||||
INSERT_GROUP("INSERT INTO groups (name, fk_chapter) VALUES (?,?)"),
|
||||
INSERT_PLAYER_IN_GROUP("INSERT INTO containsGroups (fk_player, fk_group) VALUES (?,?)"),
|
||||
LEAVE_GROUP("DELETE FROM containsGroups WHERE fk_player = ? AND fk_group = ?");
|
||||
|
||||
private String request;
|
||||
|
||||
Query(Query parent, String request) {
|
||||
this.request = parent.request + request;
|
||||
}
|
||||
|
||||
Query(String request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||
return base.prepare(this.request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.request;
|
||||
}
|
||||
}
|
||||
|
||||
private Configuration config;
|
||||
|
||||
public DatabaseGroupRepository(ConnectionManager con, Configuration config){
|
||||
super(con);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public List<Group> getAllGroups() {
|
||||
try {
|
||||
ensureConnection();
|
||||
List<Group> list = new ArrayList<>();
|
||||
PreparedStatement stmt = Query.ALL_GROUPS.prepare(this);
|
||||
ResultSet groupResult = stmt.executeQuery();
|
||||
while (groupResult.next())
|
||||
list.add(makeGroup(groupResult));
|
||||
return list;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Group> getAllGroupsByChapter(int chapter){
|
||||
try {
|
||||
ensureConnection();
|
||||
List<Group> list = new ArrayList<>();
|
||||
PreparedStatement stmt = Query.ALL_GROUPS_BY_CHAPTER.prepare(this);
|
||||
stmt.setInt(1, chapter);
|
||||
ResultSet groupResult = stmt.executeQuery();
|
||||
while (groupResult.next())
|
||||
list.add(makeGroup(groupResult));
|
||||
return list;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean insertGroup(Group group, PeerAtUser creator) throws SQLException {
|
||||
Integer groupId = getGroupId(group);
|
||||
if (groupId == null)
|
||||
ensureConnection();
|
||||
PreparedStatement statement = Query.INSERT_GROUP.prepare(this);
|
||||
statement.setString(1, group.getName());
|
||||
statement.setObject(2, group.getLinkToChapter());
|
||||
// statement.setObject(3, group.getLinkToPuzzle());
|
||||
if (statement.executeUpdate() >= 0)
|
||||
return insertUserInGroup(group, creator);
|
||||
return false;
|
||||
}
|
||||
|
||||
public Group getPlayerGroup(int user, Integer chapter) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement stmt = Query.GET_GROUP_FOR_PLAYER.prepare(this);
|
||||
stmt.setInt(1, user);
|
||||
stmt.setObject(2, chapter);
|
||||
// stmt.setObject(3, puzzle);
|
||||
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if (result.next())
|
||||
return makeGroup(result);
|
||||
return null;
|
||||
}
|
||||
|
||||
public Integer getGroupId(Group group) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement stmt = Query.GET_GROUP_ID_BY_DATA.prepare(this);
|
||||
stmt.setString(1, group.getName());
|
||||
stmt.setObject(2, group.getLinkToChapter());
|
||||
// stmt.setObject(3, group.getLinkToPuzzle());
|
||||
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if (result.next())
|
||||
return result.getInt("id_group");
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean insertUserInGroup(Group group, PeerAtUser user) throws SQLException{
|
||||
Integer id = getGroupId(group);
|
||||
if(id != null){
|
||||
int howmany = numberInGroup(id);
|
||||
System.out.println("join group, already have "+howmany);
|
||||
if(howmany > config.getGroupMaxPlayers()) return false;
|
||||
}
|
||||
Group alreadyInGroup = getPlayerGroup(user.getId(), group.getLinkToChapter());
|
||||
if (id != null && alreadyInGroup == null) {
|
||||
PreparedStatement stmt = Query.INSERT_PLAYER_IN_GROUP.prepare(this);
|
||||
|
||||
stmt.setInt(1, user.getId());
|
||||
stmt.setInt(2, id);
|
||||
|
||||
return stmt.executeUpdate() >= 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int numberInGroup(int group) throws SQLException{
|
||||
PreparedStatement stmt = Query.GET_GROUP_USERS_COUNT.prepare(this);
|
||||
stmt.setInt(1, group);
|
||||
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if(result.next()) return result.getInt("howmany");
|
||||
return 0;
|
||||
}
|
||||
|
||||
public boolean leaveGroup(Group group, PeerAtUser user) throws SQLException {
|
||||
Integer id = getGroupId(group);
|
||||
if (id != null) {
|
||||
PreparedStatement stmt = Query.LEAVE_GROUP.prepare(this);
|
||||
|
||||
stmt.setInt(1, user.getId());
|
||||
stmt.setInt(2, id);
|
||||
|
||||
return stmt.executeUpdate() >= 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,102 +0,0 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.model.Group;
|
||||
import dev.peerat.backend.model.Player;
|
||||
|
||||
public class DatabaseLeaderboardRepository extends BaseDatabaseQuery{
|
||||
|
||||
private static enum Query{
|
||||
|
||||
// LEADERBOARD
|
||||
ALL_PLAYERS_FOR_LEADERBOARD(
|
||||
"select p.*, scores.*, g.* from players p ,(SELECT fk_player, SUM(c.score) AS score, COUNT(c.id_completion) AS completions, SUM(c.tries) AS tries, rank() over(ORDER BY score DESC) AS rank FROM completions c LEFT JOIN puzzles puzz on puzz.id_puzzle = c.fk_puzzle LEFT JOIN chapters chap on chap.id_chapter = puzz.fk_chapter WHERE chap.id_chapter > 1 GROUP BY c.fk_player) AS scores LEFT JOIN containsGroups cg ON scores.fk_player = cg.fk_player LEFT JOIN groups g ON cg.fk_group = g.id_group WHERE p.id_player = scores.fk_player ORDER BY g.fk_chapter"),
|
||||
ALL_GROUP_FOR_CHAPTER_LEADERBOARD(
|
||||
"SELECT g.*, pl.pseudo, co.score, co.tries FROM groups g LEFT JOIN containsGroups cg ON g.id_group = cg.fk_group LEFT JOIN players pl ON cg.fk_player = pl.id_player LEFT JOIN completions co ON pl.id_player = co.fk_player WHERE cg.fk_player IS NOT NULL AND fk_chapter = ? AND (co.fk_puzzle IN (SELECT id_puzzle FROM puzzles puz WHERE puz.fk_chapter = g.fk_chapter) OR co.score IS NULL);");
|
||||
|
||||
private String request;
|
||||
|
||||
Query(Query parent, String request) {
|
||||
this.request = parent.request + request;
|
||||
}
|
||||
|
||||
Query(String request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||
return base.prepare(this.request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.request;
|
||||
}
|
||||
}
|
||||
|
||||
private Configuration config;
|
||||
|
||||
public DatabaseLeaderboardRepository(ConnectionManager con, Configuration config){
|
||||
super(con);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public SortedSet<Player> getAllPlayerForLeaderboard() {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement playersStmt = Query.ALL_PLAYERS_FOR_LEADERBOARD.prepare(this);
|
||||
ResultSet result = playersStmt.executeQuery();
|
||||
ArrayList<Player> players = new ArrayList<Player>();
|
||||
Player tmpPlayer;
|
||||
while (result.next()) {
|
||||
tmpPlayer = makePlayer(result, result.getInt("id_player"));
|
||||
if (!players.contains(tmpPlayer)) {
|
||||
players.add(tmpPlayer);
|
||||
} else {
|
||||
players.get(players.indexOf(tmpPlayer)).addGroup(makeGroup(result));
|
||||
}
|
||||
}
|
||||
return new TreeSet<Player>(players);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public SortedSet<Group> getAllGroupForChapterLeaderboard(int chapterId){
|
||||
try{
|
||||
ensureConnection();
|
||||
PreparedStatement groupsStmt = Query.ALL_GROUP_FOR_CHAPTER_LEADERBOARD.prepare(this);
|
||||
groupsStmt.setInt(1, chapterId);
|
||||
ResultSet result = groupsStmt.executeQuery();
|
||||
List<Group> groups = new ArrayList<Group>();
|
||||
Group tmpGroup;
|
||||
while (result.next()) {
|
||||
tmpGroup = makeGroup(result);
|
||||
if (tmpGroup != null) {
|
||||
int gPosition = groups.indexOf(tmpGroup);
|
||||
if (gPosition < 0) {
|
||||
tmpGroup.addPlayer(makeGroupPlayer(result));
|
||||
groups.add(tmpGroup);
|
||||
} else {
|
||||
groups.get(gPosition).addPlayer(makeGroupPlayer(result));
|
||||
}
|
||||
}
|
||||
}
|
||||
return new TreeSet<Group>(groups);
|
||||
}catch(SQLException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,191 +0,0 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.password4j.Password;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.model.Player;
|
||||
import dev.peerat.backend.model.Puzzle;
|
||||
|
||||
public class DatabasePlayerRepository extends BaseDatabaseQuery{
|
||||
|
||||
public static String GET_PLAYER_RANK(){
|
||||
return Query.GET_PLAYER_RANK.request;
|
||||
}
|
||||
|
||||
private static enum Query{
|
||||
|
||||
// PLAYERS
|
||||
GET_PLAYER_SIMPLE("SELECT pseudo, email, firstname, lastname, description FROM players WHERE id_player = ?"),
|
||||
GET_PLAYER_EMAIL("SELECT id_player FROM players WHERE email = ?"),
|
||||
GET_PLAYER_PSEUDO("SELECT * FROM players WHERE pseudo = ?"),
|
||||
GET_PLAYER_DETAILS("SELECT p.*, g.*\r\n"
|
||||
+ "FROM players p\r\n"
|
||||
+ "LEFT OUTER JOIN containsGroups cg ON p.id_player = cg.fk_player\r\n"
|
||||
+ "LEFT OUTER JOIN groups g ON cg.fk_group = g.id_group\r\n"
|
||||
+ "LEFT OUTER JOIN completions c on p.id_player = c.fk_player\r\n"
|
||||
+ "WHERE "),
|
||||
GET_PLAYER_DETAILS_BY_ID(GET_PLAYER_DETAILS, " p.id_player = ? GROUP BY g.name ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
||||
GET_PLAYER_DETAILS_BY_PSEUDO(GET_PLAYER_DETAILS, "p.pseudo = ? GROUP BY g.name ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
||||
GET_PLAYER_COMPLETIONS("select c.*, p.name from completions c left join puzzles p on c.fk_puzzle = p.id_puzzle where fk_player = ?;"),
|
||||
GET_PLAYER_RANK("SELECT * FROM (SELECT fk_player, RANK() OVER(ORDER BY SUM(score) DESC) rank FROM completions c LEFT JOIN puzzles puzz on puzz.id_puzzle = c.fk_puzzle LEFT JOIN chapters chap on chap.id_chapter = puzz.fk_chapter LEFT JOIN players p ON p.id_player = c.fk_player WHERE chap.id_chapter > 1 GROUP BY fk_player ORDER BY rank) AS ranks WHERE ranks.fk_player = ?;"),
|
||||
UPDATE_PLAYER_INFO("UPDATE players SET pseudo = ?, email = ?, firstname = ?, lastname = ? WHERE id_player = ?"),
|
||||
UPDATE_PLAYER_PASSWORD("UPDATE players SET passwd = ? WHERE id_player = ?");
|
||||
private String request;
|
||||
|
||||
Query(Query parent, String request) {
|
||||
this.request = parent.request + request;
|
||||
}
|
||||
|
||||
Query(String request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||
return base.prepare(this.request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.request;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Configuration config;
|
||||
|
||||
public DatabasePlayerRepository(ConnectionManager con, Configuration config){
|
||||
super(con);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public Player getPlayer(int idPlayer) {
|
||||
try {
|
||||
PreparedStatement completionsStmt = Query.GET_PLAYER_SIMPLE.prepare(this);
|
||||
completionsStmt.setInt(1, idPlayer);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
if (result.next()) {
|
||||
return makePlayer(result, idPlayer);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getPlayerId(String email){
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement completionsStmt = Query.GET_PLAYER_EMAIL.prepare(this);
|
||||
completionsStmt.setString(1, email);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
if (result.next()) {
|
||||
return result.getInt("id_player");
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public boolean updatePseudo(int id, Player player, String pseudo){
|
||||
try{
|
||||
PreparedStatement statment = Query.GET_PLAYER_PSEUDO.prepare(this);
|
||||
statment.setString(1, pseudo);
|
||||
ResultSet result = statment.executeQuery();
|
||||
if(result.next()) return false;
|
||||
statment = Query.UPDATE_PLAYER_INFO.prepare(this);
|
||||
statment.setString(1, pseudo);
|
||||
statment.setString(2, player.getEmail());
|
||||
statment.setString(3, player.getFirstname());
|
||||
statment.setString(4, player.getLastname());
|
||||
statment.setInt(5, id);
|
||||
return statment.executeUpdate() > 0;
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void updateProfile(int id, Player player, String lastname, String firstname){
|
||||
try{
|
||||
PreparedStatement statment = Query.UPDATE_PLAYER_INFO.prepare(this);
|
||||
statment.setString(1, player.getPseudo());
|
||||
statment.setString(2, player.getEmail());
|
||||
statment.setString(3, firstname);
|
||||
statment.setString(4, lastname);
|
||||
statment.setInt(5, id);
|
||||
statment.executeUpdate();
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void updatePassword(int id, String password){
|
||||
try{
|
||||
PreparedStatement statment = Query.UPDATE_PLAYER_PASSWORD.prepare(this);
|
||||
statment.setString(1, Password.hash(password).withArgon2().getResult());
|
||||
statment.setInt(2, id);
|
||||
statment.executeUpdate();
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public Player getPlayerDetails(int idPlayer) {
|
||||
return getPlayerDetails(idPlayer, null);
|
||||
}
|
||||
|
||||
public Player getPlayerDetails(String pseudoPlayer) {
|
||||
return getPlayerDetails(-1, pseudoPlayer);
|
||||
}
|
||||
|
||||
private Player getPlayerDetails(int id, String pseudo) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement completionsStmt;
|
||||
if (pseudo != null) {
|
||||
completionsStmt = Query.GET_PLAYER_DETAILS_BY_PSEUDO.prepare(this);
|
||||
completionsStmt.setString(1, pseudo);
|
||||
} else {
|
||||
completionsStmt = Query.GET_PLAYER_DETAILS_BY_ID.prepare(this);
|
||||
completionsStmt.setInt(1, id);
|
||||
}
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
Player player = null;
|
||||
while (result.next()) {
|
||||
if (player == null) {
|
||||
id = result.getInt("id_player");
|
||||
player = makePlayer(result, id);
|
||||
completionsStmt = prepare(DatabaseBadgeRepository.GET_BADGES_OF_PLAYER());
|
||||
completionsStmt.setInt(1, id);
|
||||
ResultSet resultBadges = completionsStmt.executeQuery();
|
||||
while (resultBadges.next()) {
|
||||
player.addBadge(makeBadge(resultBadges));
|
||||
}
|
||||
} else {
|
||||
player.addGroup(makeGroup(result));
|
||||
}
|
||||
}
|
||||
// ADD completions
|
||||
completionsStmt = Query.GET_PLAYER_COMPLETIONS.prepare(this);
|
||||
completionsStmt.setInt(1, id);
|
||||
result = completionsStmt.executeQuery();
|
||||
while (result.next()) {
|
||||
player.addCompletion(makeCompletion(result));
|
||||
}
|
||||
|
||||
return player;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,78 +0,0 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.model.Puzzle;
|
||||
|
||||
public class DatabasePuzzleRepository extends BaseDatabaseQuery{
|
||||
|
||||
private static enum Query{
|
||||
|
||||
SPECIFIC_PUZZLE_QUERY(
|
||||
"SELECT p.*, np.origin, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN nextPart np ON p.id_puzzle = np.next LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE p.id_puzzle = ? GROUP BY p.id_puzzle"),
|
||||
PUZZLES_IN_CHAPTER_QUERY(
|
||||
"SELECT p.*, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE fk_chapter = ? GROUP BY p.id_puzzle");
|
||||
|
||||
private String request;
|
||||
|
||||
Query(Query parent, String request) {
|
||||
this.request = parent.request + request;
|
||||
}
|
||||
|
||||
Query(String request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||
return base.prepare(this.request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.request;
|
||||
}
|
||||
}
|
||||
|
||||
private Configuration config;
|
||||
|
||||
public DatabasePuzzleRepository(ConnectionManager con, Configuration config){
|
||||
super(con);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public List<Puzzle> getPuzzlesInChapter(int id) throws SQLException {
|
||||
List<Puzzle> puzzles = new ArrayList<>();
|
||||
ensureConnection();
|
||||
PreparedStatement puzzleStmt = Query.PUZZLES_IN_CHAPTER_QUERY.prepare(this);
|
||||
puzzleStmt.setInt(1, id);
|
||||
ResultSet puzzleResult = puzzleStmt.executeQuery();
|
||||
while (puzzleResult.next()) {
|
||||
puzzles.add(makePuzzle(puzzleResult));
|
||||
}
|
||||
return puzzles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific puzzle
|
||||
*
|
||||
* @param id The id of the puzzle
|
||||
* @return The puzzle or null if an error occurred
|
||||
*/
|
||||
public Puzzle getPuzzle(int id) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement puzzleStmt = Query.SPECIFIC_PUZZLE_QUERY.prepare(this);
|
||||
puzzleStmt.setInt(1, id);
|
||||
ResultSet puzzleResult = puzzleStmt.executeQuery();
|
||||
if (puzzleResult.next()) {
|
||||
return makePuzzle(puzzleResult);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -5,6 +5,84 @@ import java.sql.PreparedStatement;
|
|||
import java.sql.SQLException;
|
||||
|
||||
public enum DatabaseQuery {
|
||||
// PUZZLES
|
||||
SPECIFIC_PUZZLE_QUERY(
|
||||
"SELECT p.*, np.origin, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN nextPart np ON p.id_puzzle = np.next LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE p.id_puzzle = ? GROUP BY p.id_puzzle"),
|
||||
PUZZLES_IN_CHAPTER_QUERY(
|
||||
"SELECT p.*, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE fk_chapter = ? GROUP BY p.id_puzzle"),
|
||||
|
||||
// CHAPTERS
|
||||
SPECIFIC_CHAPTER_QUERY("SELECT * FROM chapters WHERE id_chapter = ?"),
|
||||
CHAPTER_FROM_PUZZLE("SELECT c.*\r\n"
|
||||
+ "FROM chapters c\r\n"
|
||||
+ "JOIN puzzles p ON p.fk_chapter = c.id_chapter\r\n"
|
||||
+ "WHERE p.id_puzzle = ?"),
|
||||
ALL_CHAPTERS_QUERY("SELECT * FROM chapters WHERE id_chapter > 0"),
|
||||
|
||||
// GROUPS
|
||||
ALL_GROUPS("SELECT * FROM groups"),
|
||||
GET_GROUP_FOR_PLAYER("SELECT g.* FROM groups g JOIN containsGroups cg ON cg.fk_group = g.id_group WHERE cg.fk_player = ? AND g.fk_chapter = ?"), // AND g.fk_puzzle = ?
|
||||
GET_GROUP_ID_BY_DATA("SELECT id_group FROM groups WHERE name = ? AND (fk_chapter = ?)"), // OR fk_puzzle = ?
|
||||
INSERT_GROUP("INSERT INTO groups (name, fk_chapter) VALUES (?,?)"),
|
||||
INSERT_PLAYER_IN_GROUP("INSERT INTO containsGroups (fk_player, fk_group) VALUES (?,?)"),
|
||||
LEAVE_GROUP("DELETE FROM containsGroups WHERE fk_player = ? AND fk_group = ?"),
|
||||
|
||||
// LEADERBOARD
|
||||
ALL_PLAYERS_FOR_LEADERBOARD(
|
||||
"select p.*, scores.*, g.* from players p ,(SELECT fk_player, SUM(c.score) AS score, COUNT(c.id_completion) AS completions, SUM(c.tries) AS tries, rank() over(ORDER BY score DESC) AS rank FROM completions c GROUP BY c.fk_player) AS scores LEFT JOIN containsGroups cg ON scores.fk_player = cg.fk_player LEFT JOIN groups g ON cg.fk_group = g.id_group WHERE p.id_player = scores.fk_player ORDER BY g.fk_chapter"),
|
||||
ALL_GROUP_FOR_CHAPTER_LEADERBOARD(
|
||||
"SELECT g.*, pl.pseudo, co.score, co.tries FROM groups g LEFT JOIN containsGroups cg ON g.id_group = cg.fk_group LEFT JOIN players pl ON cg.fk_player = pl.id_player LEFT JOIN completions co ON pl.id_player = co.fk_player WHERE cg.fk_player IS NOT NULL AND fk_chapter = ? AND (co.fk_puzzle IN (SELECT id_puzzle FROM puzzles puz WHERE puz.fk_chapter = g.fk_chapter) OR co.score IS NULL);"),
|
||||
|
||||
// REGISTER
|
||||
CHECK_PSEUDO_AVAILABLE_QUERY("SELECT * FROM players WHERE pseudo = ?"),
|
||||
CHECK_EMAIL_AVAILABLE_QUERY("SELECT * FROM players WHERE email = ?"),
|
||||
REGISTER_QUERY(
|
||||
"INSERT INTO players (pseudo, email, passwd, firstname, lastname, description, avatar) VALUES (?, ?, ?, ?, ?, ?, ?)"),
|
||||
REGISTER_PLAYER_IN_EXISTING_GROUP(
|
||||
"INSERT INTO containsGroups (fk_player, fk_group) VALUES (?, (SELECT id_group FROM groups WHERE name = ?));"),
|
||||
|
||||
// LOGIN
|
||||
CHECK_PASSWORD("SELECT id_player, passwd FROM players WHERE pseudo=?"),
|
||||
|
||||
// COMPLETIONS
|
||||
GET_COMPLETION(
|
||||
"SELECT * FROM completions WHERE fk_puzzle = ? AND fk_player = ?"),
|
||||
GET_COMPLETION_GROUP("SELECT c.*\r\n"
|
||||
+ "FROM completions c\r\n"
|
||||
+ "JOIN containsGroups cG on c.fk_player = cG.fk_player\r\n"
|
||||
+ "JOIN containsGroups cGs on cGs.fk_group = cG.fk_group\r\n"
|
||||
+ "JOIN groups g on cG.fk_group = g.id_group\r\n"
|
||||
+ "JOIN puzzles p on p.id_puzzle = c.fk_puzzle\r\n"
|
||||
+ "WHERE cGs.fk_player = ? AND p.id_puzzle = ?"),
|
||||
INSERT_COMPLETION(
|
||||
"INSERT INTO completions (fk_puzzle, fk_player, tries, code, fileName, score) values (?, ?, ?, ?, ?, ?)"),
|
||||
UPDATE_COMPLETION(
|
||||
"UPDATE completions SET tries = ?, score = ?, fk_player = ? WHERE fk_puzzle = ? AND fk_player = ?"),
|
||||
SCORE("SELECT score FROM completions WHERE fk_player = ? AND fk_puzzle = ?"),
|
||||
SCORE_GROUP("SELECT c.score\r\n"
|
||||
+ "FROM completions c\r\n"
|
||||
+ "JOIN containsGroups cG on c.fk_player = cG.fk_player\r\n"
|
||||
+ "JOIN containsGroups cGs on cGs.fk_group = cG.fk_group\r\n"
|
||||
+ "JOIN groups g on cG.fk_group = g.id_group\r\n"
|
||||
+ "JOIN puzzles p on p.id_puzzle = c.fk_puzzle\r\n"
|
||||
+ "WHERE cGs.fk_player = ? AND p.id_puzzle = ?"),
|
||||
|
||||
// PLAYERS
|
||||
GET_PLAYER_SIMPLE("SELECT pseudo, email, firstname, lastname, description FROM players WHERE id_player = ?"),
|
||||
GET_PLAYER_DETAILS("SELECT p.*, g.*\r\n"
|
||||
+ "FROM players p\r\n"
|
||||
+ "LEFT OUTER JOIN containsGroups cg ON p.id_player = cg.fk_player\r\n"
|
||||
+ "LEFT OUTER JOIN groups g ON cg.fk_group = g.id_group\r\n"
|
||||
+ "LEFT OUTER JOIN completions c on p.id_player = c.fk_player\r\n"
|
||||
+ "WHERE "),
|
||||
GET_PLAYER_DETAILS_BY_ID(GET_PLAYER_DETAILS, " p.id_player = ? GROUP BY g.name ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
||||
GET_PLAYER_DETAILS_BY_PSEUDO(GET_PLAYER_DETAILS, "p.pseudo = ? GROUP BY g.name ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
||||
GET_PLAYER_COMPLETIONS("select c.*, p.name from completions c left join puzzles p on c.fk_puzzle = p.id_puzzle where fk_player = ?;"),
|
||||
GET_PLAYER_RANK("SELECT * FROM (SELECT fk_player, RANK() OVER(ORDER BY SUM(score) DESC) rank FROM completions c LEFT JOIN players p ON p.id_player = c.fk_player GROUP BY fk_player ORDER BY rank) AS ranks WHERE ranks.fk_player = ?;"),
|
||||
|
||||
// BADGES
|
||||
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 = ?"),
|
||||
|
||||
//TRIGGER
|
||||
FIRST_TRY("CREATE OR REPLACE TRIGGER FirstTry\r\n"
|
||||
|
|
|
@ -1,239 +1,698 @@
|
|||
package dev.peerat.backend.repository;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.reflect.Field;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardWatchEventKinds;
|
||||
import java.nio.file.WatchEvent;
|
||||
import java.nio.file.WatchKey;
|
||||
import java.nio.file.WatchService;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import com.password4j.Hash;
|
||||
import com.password4j.Password;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.model.Badge;
|
||||
import dev.peerat.backend.model.Chapter;
|
||||
import dev.peerat.backend.model.Completion;
|
||||
import dev.peerat.backend.model.Group;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.model.Player;
|
||||
import dev.peerat.backend.model.Puzzle;
|
||||
|
||||
public class DatabaseRepository extends BaseDatabaseQuery{
|
||||
public class DatabaseRepository {
|
||||
|
||||
private Connection con; //refractor chain
|
||||
private Connection con;
|
||||
private Configuration config;
|
||||
|
||||
private DatabasePuzzleRepository puzzleRepo;
|
||||
private DatabaseChapterRepository chapterRepo;
|
||||
private DatabaseAdminRepository adminRepo;
|
||||
private DatabaseAuthRepository authRepo;
|
||||
private DatabaseBadgeRepository badgeRepo;
|
||||
private DatabaseCompletionRepository completionRepo;
|
||||
private DatabaseGroupRepository groupRepo;
|
||||
private DatabaseLeaderboardRepository leaderboardRepo;
|
||||
private DatabasePlayerRepository playerRepo;
|
||||
|
||||
public DatabaseRepository(ConnectionManager con, Configuration config) throws Exception {
|
||||
super(con);
|
||||
public DatabaseRepository(Configuration config) {
|
||||
this.config = config;
|
||||
}
|
||||
// testTrigger();
|
||||
// }
|
||||
//
|
||||
// private void testTrigger(){
|
||||
// try {
|
||||
// ensureConnection();
|
||||
// }catch(Exception e){
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// System.out.println("connection ensured");
|
||||
//
|
||||
// try {
|
||||
// PreparedStatement log = this.con.prepareStatement("DROP TABLE mycustomlog;");
|
||||
// log.execute();
|
||||
// }catch(Exception e){
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// System.out.println("log dropped");
|
||||
//
|
||||
// try {
|
||||
// PreparedStatement log = this.con.prepareStatement("CREATE TABLE mycustomlog(\r\n"
|
||||
// + " message VARCHAR(255),\r\n"
|
||||
// + " primary key (message)\r\n"
|
||||
// + ");");
|
||||
// log.execute();
|
||||
// }catch(Exception e){
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// System.out.println("log created");
|
||||
//
|
||||
// try {
|
||||
// System.out.println(DatabaseQuery.FIRST_TRY.toString());
|
||||
// DatabaseQuery.FIRST_TRY.prepare(this.con).execute();
|
||||
// }catch(Exception e){
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
//
|
||||
// System.out.println("trigger inserted");
|
||||
//
|
||||
// try {
|
||||
// insertCompletion(new Completion(1, 1, 1, null, 1));
|
||||
// } catch (SQLException e1) {
|
||||
// e1.printStackTrace();
|
||||
// }
|
||||
//
|
||||
// try {
|
||||
// showLog();
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// System.out.println("------------------------------");
|
||||
// }
|
||||
//
|
||||
// private void showLog() throws Exception{
|
||||
// ensureConnection();
|
||||
//
|
||||
// PreparedStatement stmt = this.con.prepareStatement("SELECT * FROM mycustomlog");
|
||||
// ResultSet result = stmt.executeQuery();
|
||||
// while(result.next()){
|
||||
// System.out.println("[LOG] "+result.getString("message"));
|
||||
// }
|
||||
// }
|
||||
|
||||
this.puzzleRepo = new DatabasePuzzleRepository(con, config);
|
||||
this.chapterRepo = new DatabaseChapterRepository(con, config, puzzleRepo);
|
||||
this.adminRepo = new DatabaseAdminRepository(con, config);
|
||||
this.authRepo = new DatabaseAuthRepository(con, config);
|
||||
this.badgeRepo = new DatabaseBadgeRepository(con, config);
|
||||
this.completionRepo = new DatabaseCompletionRepository(con, config, chapterRepo);
|
||||
this.groupRepo = new DatabaseGroupRepository(con, config);
|
||||
this.leaderboardRepo = new DatabaseLeaderboardRepository(con, config);
|
||||
this.playerRepo = new DatabasePlayerRepository(con, config);
|
||||
|
||||
loadConfig(config);
|
||||
private void ensureConnection() throws SQLException {
|
||||
if (con == null || (!con.isValid(5))) {
|
||||
this.con = DriverManager.getConnection(
|
||||
"jdbc:mysql://" + config.getDbHost() + ":" + config.getDbPort() + "/" + config.getDbDatabase() + "",
|
||||
config.getDbUser(), config.getDbPassword());
|
||||
}
|
||||
}
|
||||
|
||||
private void loadConfig(Configuration config) throws Exception{
|
||||
String name = DatabaseRepository.class.getPackage().getName();
|
||||
InputStream stream = ClassLoader.getSystemClassLoader().getResourceAsStream(name.replace(".", "/"));
|
||||
File folder = new File(config.getSqlFolder());
|
||||
try{
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
|
||||
String line;
|
||||
while((line = reader.readLine()) != null){
|
||||
if(line.endsWith(".class")){
|
||||
Class<?> clazz = Class.forName(name+"."+line.substring(0, line.length()-6));
|
||||
if(BaseDatabaseQuery.class.isAssignableFrom(clazz)){
|
||||
for(Class<?> subClazz : clazz.getDeclaredClasses()){
|
||||
if(subClazz.isEnum()){
|
||||
configure(subClazz, folder);
|
||||
}
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
private Puzzle makePuzzle(ResultSet puzzleResult) throws SQLException {
|
||||
return new Puzzle(puzzleResult.getInt("id_puzzle"), puzzleResult.getString("name"),
|
||||
puzzleResult.getString("content"), puzzleResult.getBytes("soluce"), puzzleResult.getString("verify"),
|
||||
puzzleResult.getInt("score_max"), puzzleResult.getString("tags"),
|
||||
hasColumn(puzzleResult, "origin") ? puzzleResult.getInt("origin") : -1);
|
||||
}
|
||||
|
||||
private Chapter makeChapter(ResultSet chapterResult) throws SQLException {
|
||||
return new Chapter(chapterResult.getInt("id_chapter"), chapterResult.getString("name"),
|
||||
chapterResult.getTimestamp("start_date"), chapterResult.getTimestamp("end_date"));
|
||||
}
|
||||
|
||||
private Completion makeCompletion(ResultSet completionResult) throws SQLException {
|
||||
String fileName = null;
|
||||
if (hasColumn(completionResult, "fileName"))
|
||||
fileName = completionResult.getString("fileName");
|
||||
String puzzleName = null;
|
||||
if (hasColumn(completionResult, "name"))
|
||||
puzzleName = completionResult.getString("name");
|
||||
|
||||
return new Completion(completionResult.getInt("fk_player"), completionResult.getInt("fk_puzzle"), completionResult.getInt("tries"),
|
||||
fileName, completionResult.getInt("score"), puzzleName);
|
||||
}
|
||||
|
||||
private Player makePlayer(ResultSet playerResult, int id) throws SQLException {
|
||||
Player p = new Player(playerResult.getString("pseudo"), playerResult.getString("email"),
|
||||
playerResult.getString("firstName"), playerResult.getString("lastName"));
|
||||
if (hasColumn(playerResult, "avatar")) {
|
||||
p.setAvatar(playerResult.getBytes("avatar"));
|
||||
}
|
||||
if (hasColumn(playerResult, "score")) {
|
||||
p.addCompletion(new Completion(playerResult.getInt("tries"), playerResult.getInt("score")));
|
||||
for (int ct = 1; ct < playerResult.getInt("completions"); ct++)
|
||||
{ // TODO refactor for V3
|
||||
p.addCompletion(new Completion(0, 0));
|
||||
}
|
||||
reader.close();
|
||||
}catch(Exception e){
|
||||
System.err.println("Failed to read "+name);
|
||||
}
|
||||
// Manage groups
|
||||
String groupName = playerResult.getString("name");
|
||||
if (groupName != null) {
|
||||
p.addGroup(makeGroup(playerResult));
|
||||
}
|
||||
|
||||
// ADD rank
|
||||
PreparedStatement completionsStmt = DatabaseQuery.GET_PLAYER_RANK.prepare(con);
|
||||
completionsStmt.setInt(1, id);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
while (result.next()) {
|
||||
p.setRank(result.getInt("rank"));
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
private Group makeGroup(ResultSet result) throws SQLException {
|
||||
return new Group(result.getString("name"), result.getInt("fk_chapter"), result.getInt("fk_puzzle"));
|
||||
}
|
||||
|
||||
private Player makeGroupPlayer(ResultSet result) throws SQLException {
|
||||
return new Player(result.getString("pseudo"), result.getInt("score"), result.getInt("tries"));
|
||||
}
|
||||
|
||||
private Badge makeBadge(ResultSet rs) throws SQLException {
|
||||
return new Badge(rs.getString("name"), rs.getBytes("logo"), rs.getInt("level"));
|
||||
}
|
||||
|
||||
private boolean hasColumn(ResultSet rs, String columnName) throws SQLException {
|
||||
// Found on StackOverflow
|
||||
ResultSetMetaData rsmd = rs.getMetaData();
|
||||
int columns = rsmd.getColumnCount();
|
||||
for (int x = 1; x <= columns; x++) {
|
||||
if (columnName.equals(rsmd.getColumnName(x)))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private List<Puzzle> getPuzzlesInChapter(int id) throws SQLException {
|
||||
List<Puzzle> puzzles = new ArrayList<>();
|
||||
ensureConnection();
|
||||
PreparedStatement puzzleStmt = DatabaseQuery.PUZZLES_IN_CHAPTER_QUERY.prepare(this.con);
|
||||
puzzleStmt.setInt(1, id);
|
||||
ResultSet puzzleResult = puzzleStmt.executeQuery();
|
||||
while (puzzleResult.next()) {
|
||||
puzzles.add(makePuzzle(puzzleResult));
|
||||
}
|
||||
return puzzles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific puzzle
|
||||
*
|
||||
* @param id The id of the puzzle
|
||||
* @return The puzzle or null if an error occurred
|
||||
*/
|
||||
public Puzzle getPuzzle(int id) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement puzzleStmt = DatabaseQuery.SPECIFIC_PUZZLE_QUERY.prepare(this.con);
|
||||
puzzleStmt.setInt(1, id);
|
||||
ResultSet puzzleResult = puzzleStmt.executeQuery();
|
||||
if (puzzleResult.next()) {
|
||||
return makePuzzle(puzzleResult);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
new Thread(() -> {
|
||||
try{
|
||||
WatchService watchService = FileSystems.getDefault().newWatchService();
|
||||
Path path = folder.toPath();
|
||||
path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
|
||||
while (true) {
|
||||
WatchKey key = watchService.take();
|
||||
for(WatchEvent<?> event : key.pollEvents()){
|
||||
Path edited = (Path) event.context();
|
||||
String targetClazz = edited.toFile().getName().split("\\.")[0];
|
||||
Class<?> clazz = Class.forName(name+"."+targetClazz);
|
||||
if(BaseDatabaseQuery.class.isAssignableFrom(clazz)){
|
||||
for(Class<?> subClazz : clazz.getDeclaredClasses()){
|
||||
if(subClazz.isEnum()){
|
||||
bind(subClazz, new File(folder, edited.toFile().getName()));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getScore(int user, int puzzle) {
|
||||
try {
|
||||
ensureConnection();
|
||||
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);
|
||||
|
||||
result = stmt.executeQuery();
|
||||
if (result.next())
|
||||
return result.getInt("score");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public Completion getCompletionGroup(int user, int puzzle) {
|
||||
try {
|
||||
PreparedStatement stmt = DatabaseQuery.GET_COMPLETION_GROUP.prepare(this.con);
|
||||
stmt.setInt(1, user);
|
||||
stmt.setInt(2, puzzle);
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if (result.next())
|
||||
return makeCompletion(result);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return getCompletion(user, puzzle);
|
||||
}
|
||||
|
||||
public Completion getCompletion(int playerId, int puzzleId) {
|
||||
try {
|
||||
PreparedStatement completionsStmt = DatabaseQuery.GET_COMPLETION.prepare(this.con);
|
||||
completionsStmt.setInt(1, puzzleId);
|
||||
completionsStmt.setInt(2, playerId);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
if (result.next()) {
|
||||
return makeCompletion(result);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Player getPlayer(int idPlayer) {
|
||||
try {
|
||||
PreparedStatement completionsStmt = DatabaseQuery.GET_PLAYER_SIMPLE.prepare(this.con);
|
||||
completionsStmt.setInt(1, idPlayer);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
if (result.next()) {
|
||||
return makePlayer(result, idPlayer);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Player getPlayerDetails(int idPlayer) {
|
||||
return getPlayerDetails(idPlayer, null);
|
||||
}
|
||||
|
||||
public Player getPlayerDetails(String pseudoPlayer) {
|
||||
return getPlayerDetails(-1, pseudoPlayer);
|
||||
}
|
||||
|
||||
private Player getPlayerDetails(int id, String pseudo) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement completionsStmt;
|
||||
if (pseudo != null) {
|
||||
completionsStmt = DatabaseQuery.GET_PLAYER_DETAILS_BY_PSEUDO.prepare(this.con);
|
||||
completionsStmt.setString(1, pseudo);
|
||||
} else {
|
||||
completionsStmt = DatabaseQuery.GET_PLAYER_DETAILS_BY_ID.prepare(this.con);
|
||||
completionsStmt.setInt(1, id);
|
||||
}
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
Player player = null;
|
||||
while (result.next()) {
|
||||
if (player == null) {
|
||||
id = result.getInt("id_player");
|
||||
player = makePlayer(result, id);
|
||||
completionsStmt = DatabaseQuery.GET_BADGES_OF_PLAYER.prepare(this.con);
|
||||
completionsStmt.setInt(1, id);
|
||||
ResultSet resultBadges = completionsStmt.executeQuery();
|
||||
while (resultBadges.next()) {
|
||||
player.addBadge(makeBadge(resultBadges));
|
||||
}
|
||||
} else {
|
||||
player.addGroup(makeGroup(result));
|
||||
}
|
||||
}
|
||||
// ADD completions
|
||||
completionsStmt = DatabaseQuery.GET_PLAYER_COMPLETIONS.prepare(con);
|
||||
completionsStmt.setInt(1, id);
|
||||
result = completionsStmt.executeQuery();
|
||||
while (result.next()) {
|
||||
player.addCompletion(makeCompletion(result));
|
||||
}
|
||||
|
||||
return player;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public SortedSet<Player> getAllPlayerForLeaderboard() {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement playersStmt = DatabaseQuery.ALL_PLAYERS_FOR_LEADERBOARD.prepare(this.con);
|
||||
ResultSet result = playersStmt.executeQuery();
|
||||
ArrayList<Player> players = new ArrayList<Player>();
|
||||
Player tmpPlayer;
|
||||
while (result.next()) {
|
||||
tmpPlayer = makePlayer(result, result.getInt("id_player"));
|
||||
if (!players.contains(tmpPlayer)) {
|
||||
players.add(tmpPlayer);
|
||||
} else {
|
||||
players.get(players.indexOf(tmpPlayer)).addGroup(makeGroup(result));
|
||||
}
|
||||
}
|
||||
return new TreeSet<Player>(players);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public SortedSet<Group> getAllGroupForChapterLeaderboard(int chapterId){
|
||||
try{
|
||||
ensureConnection();
|
||||
PreparedStatement groupsStmt = DatabaseQuery.ALL_GROUP_FOR_CHAPTER_LEADERBOARD.prepare(this.con);
|
||||
groupsStmt.setInt(1, chapterId);
|
||||
ResultSet result = groupsStmt.executeQuery();
|
||||
List<Group> groups = new ArrayList<Group>();
|
||||
Group tmpGroup;
|
||||
while (result.next()) {
|
||||
tmpGroup = makeGroup(result);
|
||||
if (tmpGroup != null) {
|
||||
int gPosition = groups.indexOf(tmpGroup);
|
||||
if (gPosition < 0) {
|
||||
tmpGroup.addPlayer(makeGroupPlayer(result));
|
||||
groups.add(tmpGroup);
|
||||
} else {
|
||||
groups.get(gPosition).addPlayer(makeGroupPlayer(result));
|
||||
}
|
||||
}
|
||||
}
|
||||
return new TreeSet<Group>(groups);
|
||||
}catch(SQLException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Badge getBadge(int badgeId) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement completionsStmt = DatabaseQuery.GET_BADGE.prepare(this.con);
|
||||
completionsStmt.setInt(1, badgeId);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
if (result.next()) {
|
||||
return makeBadge(result);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific chapter
|
||||
*
|
||||
* @param id The id of the chapter
|
||||
* @return The chapter or null if an error occurred
|
||||
*/
|
||||
public Chapter getChapter(int id) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement chapterStmt = DatabaseQuery.SPECIFIC_CHAPTER_QUERY.prepare(this.con);
|
||||
chapterStmt.setInt(1, id);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
if (chapterResult.next()) {
|
||||
Chapter chapter = makeChapter(chapterResult);
|
||||
List<Puzzle> puzzles = getPuzzlesInChapter(id);
|
||||
chapter.setPuzzles(puzzles);
|
||||
return chapter;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Chapter getChapter(Puzzle puzzle){
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement chapterStmt = DatabaseQuery.CHAPTER_FROM_PUZZLE.prepare(this.con);
|
||||
chapterStmt.setInt(1, puzzle.getId());
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
if (chapterResult.next()) {
|
||||
Chapter chapter = makeChapter(chapterResult);
|
||||
List<Puzzle> puzzles = getPuzzlesInChapter(chapter.getId());
|
||||
chapter.setPuzzles(puzzles);
|
||||
return chapter;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all chapters in the database
|
||||
*
|
||||
* @return List of all chapters or null if an error occurred
|
||||
*/
|
||||
public List<Chapter> getAllChapters() {
|
||||
try {
|
||||
List<Chapter> chapterList = new ArrayList<>();
|
||||
ensureConnection();
|
||||
PreparedStatement chapterStmt = DatabaseQuery.ALL_CHAPTERS_QUERY.prepare(this.con);
|
||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||
while (chapterResult.next()) {
|
||||
Chapter chapter = makeChapter(chapterResult);
|
||||
chapter.setPuzzles(getPuzzlesInChapter(chapter.getId()));
|
||||
chapterList.add(chapter);
|
||||
}
|
||||
return chapterList;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Group> getAllGroups() {
|
||||
try {
|
||||
ensureConnection();
|
||||
List<Group> list = new ArrayList<>();
|
||||
PreparedStatement stmt = DatabaseQuery.ALL_GROUPS.prepare(this.con);
|
||||
ResultSet groupResult = stmt.executeQuery();
|
||||
while (groupResult.next())
|
||||
list.add(makeGroup(groupResult));
|
||||
return list;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a pseudo is available
|
||||
*
|
||||
* @param pseudo The pseudo to check
|
||||
* @return True if the pseudo is available, false if it's already taken
|
||||
*/
|
||||
public boolean checkPseudoAvailability(String pseudo) {
|
||||
return checkAvailability(pseudo, DatabaseQuery.CHECK_PSEUDO_AVAILABLE_QUERY.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an email is available
|
||||
*
|
||||
* @param email The email to check
|
||||
* @return True if the email is available, false if it's already taken
|
||||
*/
|
||||
public boolean checkEmailAvailability(String email) {
|
||||
return checkAvailability(email, DatabaseQuery.CHECK_EMAIL_AVAILABLE_QUERY.toString());
|
||||
}
|
||||
|
||||
private boolean checkAvailability(String queriedString, String correspondingQuery) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = con.prepareStatement(correspondingQuery);
|
||||
statement.setString(1, queriedString);
|
||||
ResultSet result = statement.executeQuery();
|
||||
return !result.next();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new user
|
||||
*
|
||||
* @param pseudo The pseudo of the user
|
||||
* @param email The email of the user
|
||||
* @param password The password of the user
|
||||
* @param firstname The firstname of the user
|
||||
* @param lastname The lastname of the user
|
||||
* @param description The description of the user
|
||||
* @param sgroup The group of the user
|
||||
* @param avatar The avatar of the user
|
||||
* @return True if the user was registered, false if an error occurred
|
||||
*/
|
||||
public int register(String pseudo, String email, String password, String firstname, String lastname,
|
||||
String description, String sgroup, String avatar) {
|
||||
Hash hash = Password.hash(password).withArgon2();
|
||||
try {
|
||||
ensureConnection();
|
||||
con.setAutoCommit(false);
|
||||
try (PreparedStatement playerStatement = con.prepareStatement(DatabaseQuery.REGISTER_QUERY.toString(),
|
||||
Statement.RETURN_GENERATED_KEYS)) {
|
||||
playerStatement.setString(1, pseudo);
|
||||
playerStatement.setString(2, email);
|
||||
playerStatement.setString(3, hash.getResult());
|
||||
playerStatement.setString(4, firstname);
|
||||
playerStatement.setString(5, lastname);
|
||||
playerStatement.setString(6, description);
|
||||
playerStatement.setString(7, avatar);
|
||||
if (playerStatement.executeUpdate() == 1) {
|
||||
ResultSet inserted = playerStatement.getGeneratedKeys();
|
||||
if (inserted.next()) {
|
||||
int newPlayerId = inserted.getInt(1);
|
||||
if (!sgroup.isEmpty()) {
|
||||
try (PreparedStatement containsGroupsStatement = con
|
||||
.prepareStatement(DatabaseQuery.REGISTER_PLAYER_IN_EXISTING_GROUP.toString())) {
|
||||
containsGroupsStatement.setInt(1, newPlayerId);
|
||||
containsGroupsStatement.setString(2, sgroup);
|
||||
containsGroupsStatement.executeUpdate();
|
||||
}
|
||||
}
|
||||
}
|
||||
key.reset();
|
||||
con.commit();
|
||||
con.setAutoCommit(true);
|
||||
return newPlayerId;
|
||||
}
|
||||
}
|
||||
}catch(Exception ex){
|
||||
ex.printStackTrace();
|
||||
} catch (SQLException e) {
|
||||
con.rollback();
|
||||
con.setAutoCommit(true);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}).start();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private void configure(Class<?> clazz, File folder) throws Exception{
|
||||
String name = clazz.getName().split("\\$")[0];
|
||||
String[] split = name.split("\\.");
|
||||
File file = new File(folder, split[split.length-1]+".txt");
|
||||
if(file.exists()){
|
||||
bind(clazz, file);
|
||||
}else{
|
||||
File parent = file.getParentFile();
|
||||
if(!parent.exists()) parent.mkdirs();
|
||||
file.createNewFile();
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
|
||||
for(Object obj : clazz.getEnumConstants()){
|
||||
Enum instance = (Enum) obj;
|
||||
writer.write(instance.name()+"="+instance.toString().replace("\n", " ").replace("\r", " ")+"\n");
|
||||
/**
|
||||
* Login a user
|
||||
*
|
||||
* @param username The username of the user
|
||||
* @param password The password of the user
|
||||
* @return id the id of the user, -1 if not login successefuly
|
||||
*/
|
||||
public int login(String username, String password) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement statement = con.prepareStatement(DatabaseQuery.CHECK_PASSWORD.toString());
|
||||
DatabaseQuery.PUZZLES_IN_CHAPTER_QUERY.prepare(this.con);
|
||||
statement.setString(1, username);
|
||||
ResultSet result = statement.executeQuery();
|
||||
if (result.next()) {
|
||||
String hashedPassword = result.getString("passwd");
|
||||
if (Password.check(password, hashedPassword).withArgon2())
|
||||
return result.getInt("id_player");
|
||||
}
|
||||
writer.flush();
|
||||
writer.close();
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private void bind(Class<?> clazz, File file) throws Exception{
|
||||
Map<String, String> map = new HashMap<>();
|
||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||
String line;
|
||||
while((line = reader.readLine()) != null){
|
||||
int index = line.indexOf('=');
|
||||
String key = line.substring(0, index);
|
||||
String value = line.substring(index+1);
|
||||
map.put(key, value);
|
||||
}
|
||||
reader.close();
|
||||
|
||||
for(Object obj : clazz.getEnumConstants()){
|
||||
Enum instance = (Enum) obj;
|
||||
String value = map.get(instance.name());
|
||||
Field field = obj.getClass().getDeclaredField("request");
|
||||
field.setAccessible(true);
|
||||
field.set(instance, value);
|
||||
public Completion insertOrUpdatePuzzleResponse(int puzzleId, int userId, String fileName, byte[] code,
|
||||
byte[] response, Puzzle currentPuzzle){
|
||||
try {
|
||||
ensureConnection();
|
||||
Completion completion = getCompletionGroup(userId, puzzleId);
|
||||
if (completion == null){
|
||||
System.out.println("Completion is null");
|
||||
completion = new Completion(userId, puzzleId, fileName, code, response, currentPuzzle);
|
||||
insertCompletion(completion);
|
||||
} else {
|
||||
System.out.println(completion);
|
||||
completion.addTry(currentPuzzle, response);
|
||||
int lastUserId = completion.getPlayerId();
|
||||
completion.updatePlayer(userId);
|
||||
updateCompletion(completion, lastUserId);
|
||||
}
|
||||
return completion;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public DatabasePuzzleRepository getPuzzleRepository(){
|
||||
return this.puzzleRepo;
|
||||
private void insertCompletion(Completion newCompletion) throws SQLException {
|
||||
PreparedStatement statement = DatabaseQuery.INSERT_COMPLETION.prepare(this.con);
|
||||
statement.setInt(1, newCompletion.getPuzzleId());
|
||||
statement.setInt(2, newCompletion.getPlayerId());
|
||||
statement.setInt(3, newCompletion.getTries());
|
||||
statement.setBytes(4, newCompletion.getCode());
|
||||
statement.setString(5, newCompletion.getFileName());
|
||||
statement.setInt(6, newCompletion.getScore());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
|
||||
public DatabaseChapterRepository getChapterRepository(){
|
||||
return this.chapterRepo;
|
||||
public boolean insertGroup(Group group, PeerAtUser creator) throws SQLException {
|
||||
Integer groupId = getGroupId(group);
|
||||
if (groupId == null)
|
||||
ensureConnection();
|
||||
PreparedStatement statement = DatabaseQuery.INSERT_GROUP.prepare(this.con);
|
||||
statement.setString(1, group.getName());
|
||||
statement.setObject(2, group.getLinkToChapter());
|
||||
// statement.setObject(3, group.getLinkToPuzzle());
|
||||
if (statement.executeUpdate() >= 0)
|
||||
return insertUserInGroup(group, creator);
|
||||
return false;
|
||||
}
|
||||
|
||||
public DatabaseAdminRepository getAdminRepository(){
|
||||
return this.adminRepo;
|
||||
}
|
||||
|
||||
public DatabaseAuthRepository getAuthRepository(){
|
||||
return this.authRepo;
|
||||
}
|
||||
|
||||
public DatabaseBadgeRepository getBadgeRepository(){
|
||||
return this.badgeRepo;
|
||||
}
|
||||
|
||||
public DatabaseCompletionRepository getCompletionRepository(){
|
||||
return this.completionRepo;
|
||||
}
|
||||
|
||||
public DatabaseGroupRepository getGroupRepository(){
|
||||
return this.groupRepo;
|
||||
}
|
||||
|
||||
public DatabaseLeaderboardRepository getLeaderboardRepository(){
|
||||
return this.leaderboardRepo;
|
||||
}
|
||||
|
||||
public DatabasePlayerRepository getPlayerRepository(){
|
||||
return this.playerRepo;
|
||||
}
|
||||
|
||||
|
||||
public void hotfix() throws Exception{
|
||||
public Group getPlayerGroup(int user, Integer chapter) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement stmt = DatabaseQuery.GET_GROUP_FOR_PLAYER.prepare(this.con);
|
||||
stmt.setInt(1, user);
|
||||
stmt.setObject(2, chapter);
|
||||
// stmt.setObject(3, puzzle);
|
||||
|
||||
PreparedStatement stmt = con.prepareStatement("SELECT c.*, g.* FROM completions c JOIN puzzles p ON c.fk_puzzle = p.id_puzzle JOIN containsGroups cg ON cg.fk_player = c.fk_player JOIN groups g ON g.id_group = cg.fk_group WHERE g.fk_chapter = 12 AND p.fk_chapter = 12;");
|
||||
ResultSet results = stmt.executeQuery();
|
||||
List<Completionz> list = new ArrayList<>();
|
||||
while(results.next()){
|
||||
Completionz complete = new Completionz(results);
|
||||
list.add(complete);
|
||||
}
|
||||
Map<Integer, Map<Integer, Completionz>> map = new HashMap<>();
|
||||
for(Completionz comp : list){
|
||||
Map<Integer,Completionz> puz = map.get(comp.puzzle);
|
||||
if(puz == null){
|
||||
puz = new HashMap<>();
|
||||
map.put(comp.puzzle, puz);
|
||||
}
|
||||
Completionz c = puz.get(comp.groups);
|
||||
if(c == null){
|
||||
puz.put(comp.groups, comp);
|
||||
}else{
|
||||
if(comp.score >= c.score){
|
||||
puz.put(comp.groups, comp);
|
||||
System.out.println("remove compl "+c.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if (result.next())
|
||||
return makeGroup(result);
|
||||
return null;
|
||||
}
|
||||
|
||||
private static class Completionz{
|
||||
|
||||
public int id;
|
||||
public int puzzle;
|
||||
public int player;
|
||||
public int tries;
|
||||
public int score;
|
||||
public int groups;
|
||||
|
||||
public Completionz(ResultSet result) throws Exception{
|
||||
id = result.getInt("id_completion");
|
||||
puzzle = result.getInt("fk_puzzle");
|
||||
player = result.getInt("fk_player");
|
||||
tries = result.getInt("tries");
|
||||
score = result.getInt("score");
|
||||
groups = result.getInt("id_group");
|
||||
System.out.println(id);
|
||||
}
|
||||
public Integer getGroupId(Group group) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement stmt = DatabaseQuery.GET_GROUP_ID_BY_DATA.prepare(this.con);
|
||||
stmt.setString(1, group.getName());
|
||||
stmt.setObject(2, group.getLinkToChapter());
|
||||
// stmt.setObject(3, group.getLinkToPuzzle());
|
||||
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if (result.next())
|
||||
return result.getInt("id_group");
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean insertUserInGroup(Group group, PeerAtUser user) throws SQLException {
|
||||
Integer id = getGroupId(group);
|
||||
Group alreadyInGroup = getPlayerGroup(user.getId(), group.getLinkToChapter());
|
||||
if (id != null && alreadyInGroup == null) {
|
||||
PreparedStatement stmt = DatabaseQuery.INSERT_PLAYER_IN_GROUP.prepare(this.con);
|
||||
|
||||
stmt.setInt(1, user.getId());
|
||||
stmt.setInt(2, id);
|
||||
|
||||
return stmt.executeUpdate() >= 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean leaveGroup(Group group, PeerAtUser user) throws SQLException {
|
||||
Integer id = getGroupId(group);
|
||||
if (id != null) {
|
||||
PreparedStatement stmt = DatabaseQuery.LEAVE_GROUP.prepare(this.con);
|
||||
|
||||
stmt.setInt(1, user.getId());
|
||||
stmt.setInt(2, id);
|
||||
|
||||
return stmt.executeUpdate() >= 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void updateCompletion(Completion completionToUpdate, int user) throws SQLException{
|
||||
System.out.println("update "+completionToUpdate);
|
||||
PreparedStatement statement = DatabaseQuery.UPDATE_COMPLETION.prepare(this.con);
|
||||
statement.setInt(1, completionToUpdate.getTries());
|
||||
statement.setInt(2, completionToUpdate.getScore());
|
||||
statement.setInt(3, completionToUpdate.getPlayerId());
|
||||
statement.setInt(4, completionToUpdate.getPuzzleId());
|
||||
statement.setInt(5, user);
|
||||
statement.executeUpdate();
|
||||
}
|
||||
}
|
|
@ -3,23 +3,23 @@ package dev.peerat.backend.routes;
|
|||
import java.util.Base64;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.Badge;
|
||||
import dev.peerat.backend.repository.DatabaseBadgeRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Response;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class BadgeDetails implements Response {
|
||||
|
||||
private final DatabaseBadgeRepository databaseRepo;
|
||||
private final DatabaseRepository databaseRepo;
|
||||
|
||||
public BadgeDetails(DatabaseRepository databaseRepo) {
|
||||
this.databaseRepo = databaseRepo.getBadgeRepository();
|
||||
this.databaseRepo = databaseRepo;
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/badge/<id>", responseCode = 200, responseDescription = "JSON contenant les informations du badge")
|
||||
|
@ -30,14 +30,15 @@ public class BadgeDetails implements Response {
|
|||
if (matcher.groupCount() > 0) {
|
||||
int badgeId = Integer.parseInt(matcher.group(1));
|
||||
Badge badge = databaseRepo.getBadge(badgeId);
|
||||
JsonMap badgeJSON = new JsonMap();
|
||||
if(badge != null){
|
||||
badgeJSON.set("name", badge.getName());
|
||||
if(badge.getLogo() != null) badgeJSON.set("logo", Base64.getEncoder().encodeToString(badge.getLogo()));
|
||||
badgeJSON.set("level", badge.getLevel());
|
||||
JSONObject badgeJSON = new JSONObject();
|
||||
if (badge != null) {
|
||||
badgeJSON.put("name", badge.getName());
|
||||
if (badge.getLogo() != null)
|
||||
badgeJSON.put("logo", Base64.getEncoder().encodeToString(badge.getLogo()));
|
||||
badgeJSON.put("level", badge.getLevel());
|
||||
}
|
||||
context.response(200);
|
||||
writer.write(badgeJSON.toString().replace("\\", ""));
|
||||
writer.write(badgeJSON.toJSONString().replace("\\", ""));
|
||||
} else {
|
||||
context.response(400);
|
||||
}
|
||||
|
|
|
@ -2,30 +2,26 @@ package dev.peerat.backend.routes;
|
|||
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.Chapter;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.model.Puzzle;
|
||||
import dev.peerat.backend.repository.DatabaseChapterRepository;
|
||||
import dev.peerat.backend.repository.DatabaseCompletionRepository;
|
||||
import dev.peerat.backend.repository.DatabaseGroupRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Response;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.utils.json.JsonArray;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class ChapterElement implements Response {
|
||||
|
||||
private final DatabaseChapterRepository chapterRepo;
|
||||
private final DatabaseCompletionRepository completionRepo;
|
||||
private final DatabaseRepository databaseRepo;
|
||||
|
||||
public ChapterElement(DatabaseRepository repo) {
|
||||
this.chapterRepo = repo.getChapterRepository();
|
||||
this.completionRepo = repo.getCompletionRepository();
|
||||
public ChapterElement(DatabaseRepository databaseRepo) {
|
||||
this.databaseRepo = databaseRepo;
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/chapter/<id>", responseCode = 200, responseDescription = "JSON contenant les informations du chapitre demander")
|
||||
|
@ -33,33 +29,30 @@ public class ChapterElement implements Response {
|
|||
|
||||
@Route(path = "^\\/chapter\\/([0-9]+)$", needLogin = true)
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
Chapter chapter = chapterRepo.getChapter(Integer.parseInt(matcher.group(1)));
|
||||
Chapter chapter = databaseRepo.getChapter(Integer.parseInt(matcher.group(1)));
|
||||
if (chapter != null){
|
||||
JsonMap chapterJSON = new JsonMap();
|
||||
chapterJSON.set("id", chapter.getId());
|
||||
chapterJSON.set("name", chapter.getName());
|
||||
boolean show = chapter.hasStarted();
|
||||
chapterJSON.set("show", show);
|
||||
if(chapter.getStartDate() != null) chapterJSON.set("start", chapter.getStartDate().toString());
|
||||
if(chapter.getEndDate() != null) chapterJSON.set("end", chapter.getEndDate().toString());
|
||||
JSONObject chapterJSON = new JSONObject();
|
||||
chapterJSON.put("id", chapter.getId());
|
||||
chapterJSON.put("name", chapter.getName());
|
||||
if (chapter.getStartDate() != null)
|
||||
chapterJSON.put("startDate", chapter.getStartDate().toString());
|
||||
if (chapter.getEndDate() != null)
|
||||
chapterJSON.put("endDate", chapter.getEndDate().toString());
|
||||
JSONArray puzzles = new JSONArray();
|
||||
PeerAtUser user = context.getUser();
|
||||
if(show){
|
||||
JsonArray puzzles = new JsonArray();
|
||||
for (Puzzle puzzle : chapter.getPuzzles()){
|
||||
JsonMap puzzleJSON = new JsonMap();
|
||||
puzzleJSON.set("id", puzzle.getId());
|
||||
puzzleJSON.set("name", puzzle.getName());
|
||||
puzzleJSON.set("scoreMax", puzzle.getScoreMax());
|
||||
if (puzzle.getTags() != null) puzzleJSON.set("tags", puzzle.getJsonTags());
|
||||
int score = this.completionRepo.getScore(user.getId(), puzzle.getId());
|
||||
if(score >= 0) puzzleJSON.set("score", score);
|
||||
puzzleJSON.set("show", puzzle.hasStarted());
|
||||
puzzles.add(puzzleJSON);
|
||||
}
|
||||
chapterJSON.set("puzzles", puzzles);
|
||||
for (Puzzle puzzle : chapter.getPuzzles()){
|
||||
JSONObject puzzleJSON = new JSONObject();
|
||||
puzzleJSON.put("id", puzzle.getId());
|
||||
puzzleJSON.put("name", puzzle.getName());
|
||||
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);
|
||||
context.response(200);
|
||||
writer.write(chapterJSON.toString());
|
||||
writer.write(chapterJSON.toJSONString());
|
||||
} else {
|
||||
context.response(400);
|
||||
}
|
||||
|
|
|
@ -3,24 +3,24 @@ package dev.peerat.backend.routes;
|
|||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.Chapter;
|
||||
import dev.peerat.backend.repository.DatabaseChapterRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Response;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.utils.json.JsonArray;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class ChapterList implements Response {
|
||||
|
||||
private final DatabaseChapterRepository databaseRepo;
|
||||
private final DatabaseRepository databaseRepo;
|
||||
|
||||
public ChapterList(DatabaseRepository databaseRepo) {
|
||||
this.databaseRepo = databaseRepo.getChapterRepository();
|
||||
this.databaseRepo = databaseRepo;
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/chapters", responseCode = 200, responseDescription = "JSON contenant les informations des chapitres")
|
||||
|
@ -30,18 +30,19 @@ public class ChapterList implements Response {
|
|||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
List<Chapter> allChapters = databaseRepo.getAllChapters();
|
||||
if (allChapters != null) {
|
||||
JsonArray chaptersJSON = new JsonArray();
|
||||
JSONArray chaptersJSON = new JSONArray();
|
||||
for (Chapter chapter : allChapters) {
|
||||
JsonMap chapterJSON = new JsonMap();
|
||||
chapterJSON.set("id", chapter.getId());
|
||||
chapterJSON.set("name", chapter.getName());
|
||||
chapterJSON.set("show", chapter.hasStarted());
|
||||
if(chapter.getStartDate() != null) chapterJSON.set("start", chapter.getStartDate().toString());
|
||||
if(chapter.getEndDate() != null) chapterJSON.set("end", chapter.getEndDate().toString());
|
||||
JSONObject chapterJSON = new JSONObject();
|
||||
chapterJSON.put("id", chapter.getId());
|
||||
chapterJSON.put("name", chapter.getName());
|
||||
if (chapter.getStartDate() != null)
|
||||
chapterJSON.put("startDate", chapter.getStartDate().toString());
|
||||
if (chapter.getEndDate() != null)
|
||||
chapterJSON.put("endDate", chapter.getEndDate().toString());
|
||||
chaptersJSON.add(chapterJSON);
|
||||
}
|
||||
context.response(200);
|
||||
writer.write(chaptersJSON.toString());
|
||||
writer.write(chaptersJSON.toJSONString());
|
||||
} else {
|
||||
context.response(400);
|
||||
}
|
||||
|
|
|
@ -8,7 +8,6 @@ import dev.peerat.backend.repository.DatabaseRepository;
|
|||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Injection;
|
||||
import dev.peerat.framework.Locker;
|
||||
import dev.peerat.framework.Locker.Key;
|
||||
import dev.peerat.framework.Route;
|
||||
|
@ -17,9 +16,9 @@ public class DynamicLeaderboard extends Leaderboard{
|
|||
|
||||
private Locker<Completion> locker;
|
||||
|
||||
public DynamicLeaderboard(DatabaseRepository databaseRepo, @Injection("leaderboard") Locker<Completion> locker){
|
||||
public DynamicLeaderboard(DatabaseRepository databaseRepo){
|
||||
super(databaseRepo);
|
||||
this.locker = locker;
|
||||
this.locker = new Locker<>();
|
||||
}
|
||||
|
||||
public Locker<Completion> getLocker(){
|
||||
|
|
|
@ -1,65 +0,0 @@
|
|||
package dev.peerat.backend.routes;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Injection;
|
||||
import dev.peerat.framework.Locker;
|
||||
import dev.peerat.framework.Locker.Key;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class EventSSE{
|
||||
|
||||
private Locker<GroupMessage> locker;
|
||||
|
||||
public EventSSE(@Injection("groupMessages") Locker<GroupMessage> locker){
|
||||
this.locker = locker;
|
||||
}
|
||||
|
||||
@Route(path = "^/group/event/$", websocket = true)
|
||||
public void connect(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
String group = reader.<JsonMap>readJson().get("group");
|
||||
|
||||
Key key = new Key();
|
||||
|
||||
locker.init(key);
|
||||
try {
|
||||
while(true){
|
||||
locker.lock(key);
|
||||
GroupMessage message = locker.getValue(key);
|
||||
if(message.getGroup() == null || message.getGroup().equals(group)){
|
||||
JsonMap send = new JsonMap();
|
||||
send.set("message", message.getMessage());
|
||||
writer.write(send.toString()+"\n");
|
||||
writer.flush();
|
||||
}
|
||||
}
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
locker.remove(key);
|
||||
}
|
||||
|
||||
public static class GroupMessage{
|
||||
|
||||
private String group;
|
||||
private String message;
|
||||
|
||||
public GroupMessage(String group, String message){
|
||||
this.group = group;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getGroup(){
|
||||
return this.group;
|
||||
}
|
||||
|
||||
public String getMessage(){
|
||||
return this.message;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -4,6 +4,9 @@ import java.io.IOException;
|
|||
import java.util.SortedSet;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.Chapter;
|
||||
import dev.peerat.backend.model.Group;
|
||||
|
@ -14,8 +17,6 @@ import dev.peerat.framework.HttpReader;
|
|||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Response;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.utils.json.JsonArray;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class Leaderboard implements Response {
|
||||
|
||||
|
@ -38,15 +39,15 @@ public class Leaderboard implements Response {
|
|||
}
|
||||
|
||||
public final void groupsLeaderboard(int chapterId, HttpWriter writer) throws IOException {
|
||||
Chapter chInfo = databaseRepo.getChapterRepository().getChapter(chapterId);
|
||||
Chapter chInfo = databaseRepo.getChapter(chapterId);
|
||||
|
||||
SortedSet<Group> allGroupsForChapter = databaseRepo.getLeaderboardRepository().getAllGroupForChapterLeaderboard(chapterId);
|
||||
JsonMap leaderboardJSON = new JsonMap();
|
||||
SortedSet<Group> allGroupsForChapter = databaseRepo.getAllGroupForChapterLeaderboard(chapterId);
|
||||
JSONObject leaderboardJSON = new JSONObject();
|
||||
if (chInfo.getStartDate() != null)
|
||||
leaderboardJSON.set("start_date", chInfo.getStartDate().toString());
|
||||
leaderboardJSON.put("start_date", chInfo.getStartDate().toString());
|
||||
if (chInfo.getEndDate() != null)
|
||||
leaderboardJSON.set("end_date", chInfo.getEndDate().toString());
|
||||
JsonArray groupsJSON = new JsonArray();
|
||||
leaderboardJSON.put("end_date", chInfo.getEndDate().toString());
|
||||
JSONArray groupsJSON = new JSONArray();
|
||||
if (allGroupsForChapter != null) {
|
||||
int rank = 1;
|
||||
int sameRankCount = 1;
|
||||
|
@ -64,28 +65,28 @@ public class Leaderboard implements Response {
|
|||
previousGroup = g;
|
||||
}
|
||||
}
|
||||
leaderboardJSON.set("groups", groupsJSON);
|
||||
writer.write(leaderboardJSON.toString());
|
||||
leaderboardJSON.put("groups", groupsJSON);
|
||||
writer.write(leaderboardJSON.toJSONString().replace("\\", ""));
|
||||
}
|
||||
|
||||
public final void playersLeaderboard(HttpWriter writer) throws IOException {
|
||||
SortedSet<Player> allPlayers = databaseRepo.getLeaderboardRepository().getAllPlayerForLeaderboard();
|
||||
JsonArray playersJSON = new JsonArray();
|
||||
SortedSet<Player> allPlayers = databaseRepo.getAllPlayerForLeaderboard();
|
||||
JSONArray playersJSON = new JSONArray();
|
||||
if (allPlayers != null) {
|
||||
for (Player player : allPlayers) {
|
||||
JsonMap playerJSON = new JsonMap();
|
||||
playerJSON.set("pseudo", player.getPseudo());
|
||||
JSONObject playerJSON = new JSONObject();
|
||||
playerJSON.put("pseudo", player.getPseudo());
|
||||
if (player.getGroups() != null)
|
||||
playerJSON.set("groups", player.getJsonGroups());
|
||||
playerJSON.put("groups", player.getJsonGroups());
|
||||
// if (player.getAvatar() != null)
|
||||
// playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
|
||||
playerJSON.set("rank", player.getRank());
|
||||
playerJSON.set("score", player.getTotalScore());
|
||||
playerJSON.set("completions", player.getTotalCompletion());
|
||||
playerJSON.set("tries", player.getTotalTries());
|
||||
playerJSON.put("rank", player.getRank());
|
||||
playerJSON.put("score", player.getTotalScore());
|
||||
playerJSON.put("completions", player.getTotalCompletion());
|
||||
playerJSON.put("tries", player.getTotalTries());
|
||||
playersJSON.add(playerJSON);
|
||||
}
|
||||
}
|
||||
writer.write(playersJSON.toString());
|
||||
writer.write(playersJSON.toJSONString().replace("\\", ""));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,28 +1,28 @@
|
|||
package dev.peerat.backend.routes.users;
|
||||
package dev.peerat.backend.routes;
|
||||
|
||||
import static dev.peerat.framework.RequestType.POST;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.DatabaseAuthRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.backend.utils.FormResponse;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Response;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.Router;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class Login extends FormResponse{
|
||||
public class Login implements Response {
|
||||
|
||||
private DatabaseAuthRepository repo;
|
||||
private DatabaseRepository databaseRepo;
|
||||
private Router<PeerAtUser> router;
|
||||
|
||||
public Login(DatabaseRepository databaseRepo, Router<PeerAtUser> router){
|
||||
this.repo = databaseRepo.getAuthRepository();
|
||||
public Login(DatabaseRepository databaseRepo, Router<PeerAtUser> router) {
|
||||
this.databaseRepo = databaseRepo;
|
||||
this.router = router;
|
||||
}
|
||||
|
||||
|
@ -32,23 +32,23 @@ public class Login extends FormResponse{
|
|||
|
||||
@Route(path = "^\\/login$", type = POST)
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
if(context.isLogged()){
|
||||
if (context.getUser() != null) {
|
||||
context.response(403);
|
||||
return;
|
||||
}
|
||||
JsonMap json = json(reader);
|
||||
if(!areValids("pseudo", "passwd")){
|
||||
context.response(400);
|
||||
return;
|
||||
}
|
||||
int id;
|
||||
if((id = repo.login(json.get("pseudo"), json.get("passwd"))) >= 0){
|
||||
context.response(200,
|
||||
"Access-Control-Expose-Headers: Authorization",
|
||||
"Authorization: Bearer " + this.router.createAuthUser(new PeerAtUser(id)));
|
||||
}else{
|
||||
context.response(400);
|
||||
JSONObject informations = reader.readJson();
|
||||
if (informations != null) {
|
||||
String pseudo = (String) informations.get("pseudo");
|
||||
String password = (String) informations.get("passwd");
|
||||
int id;
|
||||
if ((id = databaseRepo.login(pseudo, password)) >= 0) {
|
||||
context.response(200,
|
||||
"Access-Control-Expose-Headers: Authorization",
|
||||
"Authorization: Bearer " + this.router.createAuthUser(new PeerAtUser(id)));
|
||||
return;
|
||||
}
|
||||
}
|
||||
context.response(400);
|
||||
}
|
||||
|
||||
}
|
96
src/dev/peerat/backend/routes/MailConfirmation.java
Normal file
96
src/dev/peerat/backend/routes/MailConfirmation.java
Normal file
|
@ -0,0 +1,96 @@
|
|||
package dev.peerat.backend.routes;
|
||||
|
||||
import static dev.peerat.framework.RequestType.POST;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.model.Player;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Response;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.Router;
|
||||
|
||||
public class MailConfirmation implements Response {
|
||||
|
||||
private DatabaseRepository databaseRepo;
|
||||
private Router<PeerAtUser> router;
|
||||
private String usersFilesPath;
|
||||
private Map<Player, Integer> playersWaiting;
|
||||
|
||||
public MailConfirmation(DatabaseRepository databaseRepo, Router<PeerAtUser> router, String initUsersFilesPath,
|
||||
Map<Player, Integer> playersWaiting) {
|
||||
this.databaseRepo = databaseRepo;
|
||||
this.router = router;
|
||||
usersFilesPath = initUsersFilesPath;
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/confirmation", responseCode = 200, responseDescription = "L'utilisateur est inscrit")
|
||||
@RouteDoc(responseCode = 403, responseDescription = "L'utilisateur est connecté")
|
||||
@RouteDoc(responseCode = 400, responseDescription = "Aucune données fournie / données invalide")
|
||||
|
||||
@Route(path = "^\\/confirmation$", type = POST)
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
if (context.getUser() != null) {
|
||||
context.response(403);
|
||||
return;
|
||||
}
|
||||
JSONObject informations = reader.readJson();
|
||||
if (informations != null) {
|
||||
boolean allNecessaryFieldsFilled = informations.containsKey("email") && informations.containsKey("code")
|
||||
&& informations.containsKey("passwd");
|
||||
if (!allNecessaryFieldsFilled) {
|
||||
context.response(400);
|
||||
return;
|
||||
}
|
||||
String email = (String) informations.get("email");
|
||||
String password = (String) informations.get("passwd");
|
||||
int code = (int) informations.get("code");
|
||||
|
||||
Player newPlayer = getPlayerFromEmail(email);
|
||||
if (newPlayer != null && code == playersWaiting.get(newPlayer)) {
|
||||
String pseudo = newPlayer.getPseudo();
|
||||
int id;
|
||||
if ((id = databaseRepo.register(pseudo, email, password, newPlayer.getFirstname(), newPlayer.getLastname(), "", "", "")) >= 0) {
|
||||
context.response(200, "Access-Control-Expose-Headers: Authorization",
|
||||
"Authorization: Bearer " + this.router.createAuthUser(new PeerAtUser(id)));
|
||||
createFolderToSaveSourceCode(pseudo);
|
||||
return;
|
||||
} else {
|
||||
context.response(400);
|
||||
JSONObject error = new JSONObject();
|
||||
error.put("username_valid", pseudo);
|
||||
error.put("email_valid", email);
|
||||
writer.write(error.toJSONString());
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
context.response(400);
|
||||
}
|
||||
|
||||
private void createFolderToSaveSourceCode(String pseudo) throws IOException {
|
||||
|
||||
Files.createDirectories(Paths.get(String.format("%s/%s", usersFilesPath, pseudo)));
|
||||
}
|
||||
|
||||
private Player getPlayerFromEmail(String email) {
|
||||
Player toMatch = new Player(email);
|
||||
for (Player p: playersWaiting.keySet()) {
|
||||
if (p.equals(toMatch)) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,25 +1,26 @@
|
|||
package dev.peerat.backend.routes;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.model.Player;
|
||||
import dev.peerat.backend.repository.DatabasePlayerRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Response;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class PlayerDetails implements Response {
|
||||
|
||||
private final DatabasePlayerRepository databaseRepo;
|
||||
private final DatabaseRepository databaseRepo;
|
||||
|
||||
public PlayerDetails(DatabaseRepository databaseRepo) {
|
||||
this.databaseRepo = databaseRepo.getPlayerRepository();
|
||||
this.databaseRepo = databaseRepo;
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/player/{id}", responseCode = 200, responseDescription = "JSON contenant les informations de l'utilisateur")
|
||||
|
@ -34,26 +35,26 @@ public class PlayerDetails implements Response {
|
|||
PeerAtUser user = context.getUser();
|
||||
player = databaseRepo.getPlayerDetails(user.getId());
|
||||
}
|
||||
JsonMap playerJSON = new JsonMap();
|
||||
JSONObject playerJSON = new JSONObject();
|
||||
if (player != null) {
|
||||
playerJSON.set("pseudo", player.getPseudo());
|
||||
playerJSON.set("email", player.getEmail());
|
||||
playerJSON.set("firstname", player.getFirstname());
|
||||
playerJSON.set("lastname", player.getLastname());
|
||||
playerJSON.set("description", player.getDescription());
|
||||
playerJSON.put("pseudo", player.getPseudo());
|
||||
playerJSON.put("email", player.getEmail());
|
||||
playerJSON.put("firstname", player.getFirstname());
|
||||
playerJSON.put("lastname", player.getLastname());
|
||||
playerJSON.put("description", player.getDescription());
|
||||
if (player.getGroups() != null)
|
||||
playerJSON.set("groups", player.getJsonGroups());
|
||||
playerJSON.set("rank", player.getRank());
|
||||
playerJSON.set("score", player.getTotalScore());
|
||||
playerJSON.set("completions", player.getTotalCompletion());
|
||||
playerJSON.set("completionsList", player.getJsonCompletions());
|
||||
playerJSON.set("tries", player.getTotalTries());
|
||||
playerJSON.put("groups", player.getJsonGroups());
|
||||
playerJSON.put("rank", player.getRank());
|
||||
playerJSON.put("score", player.getTotalScore());
|
||||
playerJSON.put("completions", player.getTotalCompletion());
|
||||
playerJSON.put("completionsList", player.getJsonCompletions());
|
||||
playerJSON.put("tries", player.getTotalTries());
|
||||
if (player.getBadges() != null)
|
||||
playerJSON.set("badges", player.getJsonBadges());
|
||||
// if (player.getAvatar() != null)
|
||||
// playerJSON.set("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
|
||||
playerJSON.put("badges", player.getJsonBadges());
|
||||
if (player.getAvatar() != null)
|
||||
playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
|
||||
context.response(200);
|
||||
writer.write(playerJSON.toString());
|
||||
writer.write(playerJSON.toJSONString().replace("\\", ""));
|
||||
} else {
|
||||
context.response(400);
|
||||
}
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
package dev.peerat.backend.routes;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.Chapter;
|
||||
import dev.peerat.backend.model.Completion;
|
||||
|
@ -15,16 +16,13 @@ import dev.peerat.framework.HttpReader;
|
|||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Response;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class PuzzleElement implements Response {
|
||||
|
||||
private final DatabaseRepository databaseRepo;
|
||||
private Predicate<PeerAtUser> isAdmin;
|
||||
|
||||
public PuzzleElement(DatabaseRepository databaseRepo, Predicate<PeerAtUser> isAdmin){
|
||||
public PuzzleElement(DatabaseRepository databaseRepo) {
|
||||
this.databaseRepo = databaseRepo;
|
||||
this.isAdmin = isAdmin;
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/puzzle/<id>", responseCode = 200, responseDescription = "JSON contenant les informations du puzzle")
|
||||
|
@ -33,10 +31,10 @@ public class PuzzleElement implements Response {
|
|||
|
||||
@Route(path = "^\\/puzzle\\/([0-9]+)$", needLogin = true)
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
Puzzle puzzle = databaseRepo.getPuzzleRepository().getPuzzle(extractId(matcher));
|
||||
Puzzle puzzle = databaseRepo.getPuzzle(extractId(matcher));
|
||||
if (puzzle != null){
|
||||
Chapter chapter = this.databaseRepo.getChapterRepository().getChapter(puzzle);
|
||||
if(chapter.getStartDate() != null && !isAdmin.test(context.getUser())){
|
||||
Chapter chapter = this.databaseRepo.getChapter(puzzle);
|
||||
if(chapter.getStartDate() != null){
|
||||
if(LocalDateTime.now().isBefore(chapter.getStartDate().toLocalDateTime())){
|
||||
context.response(423);
|
||||
return;
|
||||
|
@ -51,20 +49,20 @@ public class PuzzleElement implements Response {
|
|||
|
||||
PeerAtUser user = context.getUser();
|
||||
|
||||
JsonMap puzzleJSON = new JsonMap();
|
||||
puzzleJSON.set("id", puzzle.getId());
|
||||
puzzleJSON.set("name", puzzle.getName());
|
||||
puzzleJSON.set("content", puzzle.getContent());
|
||||
puzzleJSON.set("scoreMax", puzzle.getScoreMax());
|
||||
if(puzzle.getTags() != null) puzzleJSON.set("tags", puzzle.getJsonTags());
|
||||
Completion completion = this.databaseRepo.getCompletionRepository().getCompletionGroup(user.getId(), puzzle.getId());
|
||||
JSONObject puzzleJSON = new JSONObject();
|
||||
puzzleJSON.put("id", puzzle.getId());
|
||||
puzzleJSON.put("name", puzzle.getName());
|
||||
puzzleJSON.put("content", puzzle.getContent());
|
||||
puzzleJSON.put("scoreMax", puzzle.getScoreMax());
|
||||
if(puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getJsonTags());
|
||||
Completion completion = this.databaseRepo.getCompletionGroup(user.getId(), puzzle.getId());
|
||||
if(completion != null && completion.getScore() >= 0){
|
||||
puzzleJSON.set("score", completion.getScore());
|
||||
puzzleJSON.set("tries", completion.getTries());
|
||||
puzzleJSON.put("score", completion.getScore());
|
||||
puzzleJSON.put("tries", completion.getTries());
|
||||
}
|
||||
if(puzzle.getDepend() > 0) puzzleJSON.set("depend", puzzle.getDepend());
|
||||
if(puzzle.getDepend() > 0) puzzleJSON.put("depend", puzzle.getDepend());
|
||||
context.response(200, "Content-Type: application/json");
|
||||
writer.write(puzzleJSON.toString());
|
||||
writer.write(puzzleJSON.toJSONString());
|
||||
}
|
||||
else {
|
||||
context.response(400);
|
||||
|
|
|
@ -9,10 +9,10 @@ import java.nio.file.Paths;
|
|||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.Chapter;
|
||||
import dev.peerat.backend.model.Completion;
|
||||
|
@ -24,24 +24,20 @@ import dev.peerat.backend.repository.DatabaseRepository;
|
|||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Injection;
|
||||
import dev.peerat.framework.Locker;
|
||||
import dev.peerat.framework.Response;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class PuzzleResponse implements Response {
|
||||
private final DatabaseRepository databaseRepo;
|
||||
private final String usersFilesPath;
|
||||
|
||||
private final Locker<Completion> leaderboard;
|
||||
private Predicate<PeerAtUser> isAdmin;
|
||||
|
||||
public PuzzleResponse(DatabaseRepository databaseRepo, Configuration config, @Injection("leaderboard") Locker<Completion> locker, Predicate<PeerAtUser> isAdmin){
|
||||
public PuzzleResponse(DatabaseRepository databaseRepo, String initUsersFilesPath, Locker<Completion> locker){
|
||||
this.databaseRepo = databaseRepo;
|
||||
usersFilesPath = config.getUsersFiles();
|
||||
usersFilesPath = initUsersFilesPath;
|
||||
this.leaderboard = locker;
|
||||
this.isAdmin = isAdmin;
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/puzzleResponse/<id>", responseCode = 200, responseDescription = "Bonne réponse, json contenant les points + tries")
|
||||
|
@ -60,36 +56,33 @@ public class PuzzleResponse implements Response {
|
|||
PeerAtUser user = context.getUser();
|
||||
|
||||
//saveSourceCode(received, databaseRepo.getPlayer(user.getId()));
|
||||
JsonMap responseJSON = new JsonMap();
|
||||
if(this.databaseRepo.getCompletionRepository().getScore(user.getId(), received.getPuzzleId()) > 0){
|
||||
JSONObject responseJSON = new JSONObject();
|
||||
if(this.databaseRepo.getScore(user.getId(), received.getPuzzleId()) > 0){
|
||||
context.response(403);
|
||||
return;
|
||||
}
|
||||
|
||||
Puzzle currentPuzzle = databaseRepo.getPuzzleRepository().getPuzzle(received.getPuzzleId());
|
||||
if(!currentPuzzle.hasStarted()){
|
||||
context.response(423);
|
||||
return;
|
||||
Puzzle currentPuzzle = databaseRepo.getPuzzle(received.getPuzzleId());
|
||||
Chapter chapter = this.databaseRepo.getChapter(currentPuzzle);
|
||||
if(chapter.getStartDate() != null){
|
||||
if(LocalDateTime.now().isBefore(chapter.getStartDate().toLocalDateTime())){
|
||||
context.response(423);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Chapter chapter = this.databaseRepo.getChapterRepository().getChapter(currentPuzzle);
|
||||
if(!chapter.hasStarted()){
|
||||
context.response(423);
|
||||
return;
|
||||
}
|
||||
if(chapter.getEndDate() != null && !isAdmin.test(context.getUser())){
|
||||
if(chapter.getEndDate() != null){
|
||||
if(LocalDateTime.now().isAfter(chapter.getEndDate().toLocalDateTime())){
|
||||
if(Arrays.equals(currentPuzzle.getSoluce(), received.getResponse())){
|
||||
context.response(200, "Content-Type: application/json");
|
||||
JsonMap theoSaisPasJusteRecevoir200 = new JsonMap();
|
||||
theoSaisPasJusteRecevoir200.set("success", true);
|
||||
writer.write(theoSaisPasJusteRecevoir200.toString());
|
||||
JSONObject theoSaisPasJusteRecevoir200 = new JSONObject();
|
||||
theoSaisPasJusteRecevoir200.put("success", true);
|
||||
writer.write(theoSaisPasJusteRecevoir200.toJSONString());
|
||||
return;
|
||||
}
|
||||
context.response(423);
|
||||
return;
|
||||
}
|
||||
Group group = this.databaseRepo.getGroupRepository().getPlayerGroup(user.getId(), chapter.getId());
|
||||
Group group = this.databaseRepo.getPlayerGroup(user.getId(), chapter.getId());
|
||||
if(group == null){
|
||||
context.response(423);
|
||||
return;
|
||||
|
@ -97,7 +90,7 @@ public class PuzzleResponse implements Response {
|
|||
}
|
||||
|
||||
|
||||
Completion completion = databaseRepo.getCompletionRepository().insertOrUpdatePuzzleResponse(received.getPuzzleId(), user.getId(),
|
||||
Completion completion = databaseRepo.insertOrUpdatePuzzleResponse(received.getPuzzleId(), user.getId(),
|
||||
received.getFileName(), received.getSourceCode(), received.getResponse(), currentPuzzle);
|
||||
if(completion == null){
|
||||
context.response(400);
|
||||
|
@ -105,14 +98,14 @@ public class PuzzleResponse implements Response {
|
|||
}
|
||||
if(completion.getScore() > 0){
|
||||
context.response(200, "Content-Type: application/json");
|
||||
responseJSON.set("score", completion.getScore());
|
||||
responseJSON.set("tries", completion.getTries());
|
||||
responseJSON.put("score", completion.getScore());
|
||||
responseJSON.put("tries", completion.getTries());
|
||||
|
||||
}else{
|
||||
context.response(406, "Content-Type: application/json");
|
||||
responseJSON.set("tries", completion.getTries());
|
||||
responseJSON.put("tries", completion.getTries());
|
||||
}
|
||||
writer.write(responseJSON.toString());
|
||||
writer.write(responseJSON.toJSONString());
|
||||
writer.flush();
|
||||
|
||||
leaderboard.setValue(completion);
|
||||
|
@ -142,11 +135,8 @@ class ReceivedResponse {
|
|||
puzzleId = Integer.parseInt(matcher.group(1));
|
||||
|
||||
List<String> multiPartData = reader.readMultiPartData();
|
||||
if (multiPartData != null && multiPartData.size() > 0){
|
||||
if (multiPartData != null && multiPartData.size() > 0) {
|
||||
this.response = multiPartData.get(0).getBytes();
|
||||
System.out.println("Puzzle response:");
|
||||
System.out.println(Arrays.toString(response));
|
||||
System.out.println(new String(response));
|
||||
if (multiPartData.size() == 3) {
|
||||
this.fileName = multiPartData.get(1);
|
||||
this.sourceCode = multiPartData.get(2).getBytes();
|
||||
|
|
89
src/dev/peerat/backend/routes/Register.java
Normal file
89
src/dev/peerat/backend/routes/Register.java
Normal file
|
@ -0,0 +1,89 @@
|
|||
package dev.peerat.backend.routes;
|
||||
|
||||
import static dev.peerat.framework.RequestType.POST;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.model.Player;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Response;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.Router;
|
||||
|
||||
public class Register implements Response {
|
||||
|
||||
private DatabaseRepository databaseRepo;
|
||||
private Router<PeerAtUser> router;
|
||||
private String usersFilesPath;
|
||||
private Map<Player, Integer> playersWaiting;
|
||||
|
||||
public Register(DatabaseRepository databaseRepo, Router<PeerAtUser> router, String initUsersFilesPath,
|
||||
Map<Player, Integer> playersWaiting) {
|
||||
this.databaseRepo = databaseRepo;
|
||||
this.router = router;
|
||||
usersFilesPath = initUsersFilesPath;
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/register", responseCode = 200, responseDescription = "L'utilisateur est inscrit")
|
||||
@RouteDoc(responseCode = 403, responseDescription = "L'utilisateur est connecté")
|
||||
@RouteDoc(responseCode = 400, responseDescription = "Aucune données fournie / données invalide")
|
||||
|
||||
@Route(path = "^\\/register$", type = POST)
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
if (context.getUser() != null) {
|
||||
context.response(403);
|
||||
return;
|
||||
}
|
||||
JSONObject informations = reader.readJson();
|
||||
if (informations != null) {
|
||||
boolean allNecessaryFieldsFilled = informations.containsKey("pseudo") && informations.containsKey("email")
|
||||
&& informations.containsKey("firstname") && informations.containsKey("lastname");
|
||||
if (!allNecessaryFieldsFilled) {
|
||||
context.response(400);
|
||||
return;
|
||||
}
|
||||
String pseudo = (String) informations.get("pseudo");
|
||||
String email = (String) informations.get("email");
|
||||
String firstname = (String) informations.get("firstname");
|
||||
String lastname = (String) informations.get("lastname");
|
||||
|
||||
boolean pseudoAvailable = databaseRepo.checkPseudoAvailability(pseudo);
|
||||
boolean emailAvailable = databaseRepo.checkEmailAvailability(email);
|
||||
if (pseudoAvailable && emailAvailable) {
|
||||
Player player = new Player(pseudo, email, firstname, lastname);
|
||||
playersWaiting.put(player, codeGenerator());
|
||||
context.response(200);
|
||||
} else {
|
||||
context.response(400);
|
||||
JSONObject error = new JSONObject();
|
||||
error.put("username_valid", pseudoAvailable);
|
||||
error.put("email_valid", emailAvailable);
|
||||
writer.write(error.toJSONString());
|
||||
|
||||
}
|
||||
return;
|
||||
}
|
||||
context.response(400);
|
||||
}
|
||||
|
||||
private int codeGenerator() {
|
||||
int min = 1000;
|
||||
int max = 9999;
|
||||
return new Random().nextInt((max-min)) + min;
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -4,7 +4,6 @@ import java.util.regex.Matcher;
|
|||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.DatabaseCompletionRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
|
@ -14,10 +13,10 @@ import dev.peerat.framework.Route;
|
|||
|
||||
public class Result implements Response {
|
||||
|
||||
private DatabaseCompletionRepository repo;
|
||||
private DatabaseRepository repo;
|
||||
|
||||
public Result(DatabaseRepository repo) {
|
||||
this.repo = repo.getCompletionRepository();
|
||||
this.repo = repo;
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/result/<id>", responseCode = 200, responseDescription = "Le score")
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
package dev.peerat.backend.routes;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.bonus.extract.RouteExtracter;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Injection;
|
||||
import dev.peerat.framework.Response;
|
||||
import dev.peerat.framework.Route;
|
||||
|
||||
public class Swagger implements Response{
|
||||
|
||||
private String json;
|
||||
|
||||
public Swagger(RouteExtracter extracter, Configuration config){
|
||||
try{
|
||||
// this.json = extracter.swagger(config.getTokenIssuer()).toString();
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
json = "{}";
|
||||
}
|
||||
}
|
||||
|
||||
@Route(path = "^/swagger$")
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
context.response(200);
|
||||
writer.write(json);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,70 +0,0 @@
|
|||
package dev.peerat.backend.routes.admins;
|
||||
|
||||
import static dev.peerat.framework.RequestType.DELETE;
|
||||
import static dev.peerat.framework.RequestType.POST;
|
||||
import static dev.peerat.framework.RequestType.PUT;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.model.Chapter;
|
||||
import dev.peerat.backend.repository.DatabaseAdminRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class ChapterController{
|
||||
|
||||
private DatabaseAdminRepository repo;
|
||||
|
||||
public ChapterController(DatabaseRepository repo){
|
||||
this.repo = repo.getAdminRepository();
|
||||
}
|
||||
|
||||
@Route(path = "^/admin/chapter/$", type = POST, needLogin = true)
|
||||
public void add(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
JsonMap json = reader.readJson();
|
||||
Chapter chapter = new Chapter(-1, json.get("name"), Timestamp.valueOf(json.<String>get("start")), Timestamp.valueOf(json.<String>get("end")));
|
||||
if(repo.adminAddChapter(chapter)){
|
||||
context.response(200);
|
||||
}else{
|
||||
context.response(501);
|
||||
}
|
||||
}
|
||||
|
||||
@Route(path = "^/admin/chapter/(\\d+)$", type = DELETE, needLogin = true)
|
||||
public void delte(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
if(this.repo.adminDeleteChapter(Integer.parseInt(matcher.group(1)))){
|
||||
context.response(200);
|
||||
}else{
|
||||
context.response(501);
|
||||
}
|
||||
}
|
||||
|
||||
@Route(path = "^/admin/chapter/(\\d+)$", type = PUT, needLogin = true)
|
||||
public void edit(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
JsonMap json = reader.readJson();
|
||||
Chapter chapter = new Chapter(-1, json.get("name"), Timestamp.valueOf(json.<String>get("start")), Timestamp.valueOf(json.<String>get("end")));
|
||||
if(repo.adminUpdateChapter(Integer.parseInt(matcher.group(1)), chapter)){
|
||||
context.response(200);
|
||||
}else{
|
||||
context.response(501);
|
||||
}
|
||||
}
|
||||
|
||||
@Route(path = "^/admin/chapter/(\\d+)$", needLogin = true)
|
||||
public void get(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
Chapter chapter = this.repo.getAdminChapter(Integer.parseInt(matcher.group(1)));
|
||||
JsonMap json = new JsonMap();
|
||||
json.set("name", chapter.getName());
|
||||
json.set("start", chapter.getStartDate());
|
||||
json.set("end", chapter.getEndDate());
|
||||
|
||||
context.response(200);
|
||||
writer.write(json.toString()+"\n");
|
||||
}
|
||||
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
package dev.peerat.backend.routes.admins;
|
||||
|
||||
import dev.peerat.backend.routes.EventSSE.GroupMessage;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.Injection;
|
||||
import dev.peerat.framework.Locker;
|
||||
import dev.peerat.framework.RequestType;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class GroupController {
|
||||
|
||||
private Locker<GroupMessage> locker;
|
||||
|
||||
public GroupController(@Injection("groupMessages") Locker<GroupMessage> locker){
|
||||
this.locker = locker;
|
||||
}
|
||||
|
||||
@Route(path = "^/admin/event/$", type = RequestType.POST ,needLogin = true)
|
||||
public void send(Context context, HttpReader reader) throws Exception{
|
||||
JsonMap json = reader.readJson();
|
||||
if(!json.has("message")){
|
||||
context.response(400);
|
||||
return;
|
||||
}
|
||||
|
||||
locker.setValue(new GroupMessage(json.get("group"), json.get("message")));
|
||||
context.response(200);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,78 +0,0 @@
|
|||
package dev.peerat.backend.routes.admins;
|
||||
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.DatabaseAdminRepository;
|
||||
import dev.peerat.backend.repository.DatabasePlayerRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Locker;
|
||||
import dev.peerat.framework.Locker.Key;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.Router;
|
||||
import dev.peerat.framework.utils.json.JsonArray;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class LogController {
|
||||
|
||||
private Locker<Context> contextLocker;
|
||||
private Locker<Throwable> exceptionLocker;
|
||||
private DatabaseAdminRepository repo;
|
||||
private DatabasePlayerRepository playerRepo;
|
||||
|
||||
public LogController(Router<PeerAtUser> router, DatabaseRepository repo){
|
||||
this.contextLocker = router.getLogger();
|
||||
this.exceptionLocker = router.getExceptionLogger();
|
||||
this.repo = repo.getAdminRepository();
|
||||
this.playerRepo = repo.getPlayerRepository();
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/admin/logs", responseCode = 200, responseDescription = "L'utilisateur peux voir les logs en directe")
|
||||
@RouteDoc(responseCode = 401, responseDescription = "L'utilisateur n'a pas accès à cette ressource")
|
||||
|
||||
@Route(path = "^/admin/logs$", needLogin = true, websocket = true)
|
||||
public void logs(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
log(reader, writer, this.contextLocker, (json, instance) -> {
|
||||
json.set("logged", instance.isLogged());
|
||||
if(instance.isLogged()) json.set("pseudo", playerRepo.getPlayer(instance.<PeerAtUser>getUser().getId()).getPseudo());
|
||||
json.set("path", instance.getPath());
|
||||
json.set("type", instance.getType().toString());
|
||||
json.set("code", instance.getResponseCode());
|
||||
});
|
||||
}
|
||||
|
||||
@Route(path = "^/admin/exceptions$", needLogin = true, websocket = true)
|
||||
public void exceptions(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
log(reader, writer, this.exceptionLocker, (json, exception) -> {
|
||||
json.set("type", exception.getClass().getSimpleName());
|
||||
json.set("message", exception.getMessage());
|
||||
JsonArray trace = new JsonArray();
|
||||
for(StackTraceElement element : exception.getStackTrace()) trace.add(element.toString());
|
||||
json.set("trace", trace);
|
||||
});
|
||||
}
|
||||
|
||||
public <T> void log(HttpReader reader, HttpWriter writer, Locker<T> locker, BiConsumer<JsonMap, T> consumer){
|
||||
Key key = new Key();
|
||||
|
||||
locker.init(key);
|
||||
try {
|
||||
while(!reader.isClosed()){
|
||||
locker.lock(key);
|
||||
T instance = locker.getValue(key);
|
||||
JsonMap json = new JsonMap();
|
||||
consumer.accept(json, instance);
|
||||
|
||||
writer.write(json.toString());
|
||||
writer.flush();
|
||||
}
|
||||
}catch(Exception e){}
|
||||
locker.remove(key);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,89 +0,0 @@
|
|||
package dev.peerat.backend.routes.admins;
|
||||
|
||||
import static dev.peerat.framework.RequestType.DELETE;
|
||||
import static dev.peerat.framework.RequestType.POST;
|
||||
import static dev.peerat.framework.RequestType.PUT;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.model.Puzzle;
|
||||
import dev.peerat.backend.repository.DatabaseAdminRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.utils.json.JsonArray;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class PuzzleController {
|
||||
|
||||
private DatabaseAdminRepository repo;
|
||||
|
||||
public PuzzleController(DatabaseRepository repo){
|
||||
this.repo = repo.getAdminRepository();
|
||||
}
|
||||
|
||||
@Route(path = "^/admin/puzzle/$", type = POST, needLogin = true)
|
||||
public void add(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
JsonMap json = reader.readJson();
|
||||
Puzzle puzzle = new Puzzle(-1, json.get("name"), json.get("content"), json.<String>get("soluce").getBytes(), null, json.<Long>get("scoreMax").intValue(), null, -1, null);
|
||||
if(repo.adminAddPuzzle(puzzle, json.<Long>get("chapter").intValue())){
|
||||
context.response(200);
|
||||
}else{
|
||||
context.response(501);
|
||||
}
|
||||
}
|
||||
|
||||
@Route(path = "^/admin/puzzle/(\\d+)$", type = DELETE, needLogin = true)
|
||||
public void delete(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
if(this.repo.adminDeletePuzzle(Integer.parseInt(matcher.group(1)))){
|
||||
context.response(200);
|
||||
}else{
|
||||
context.response(501);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Route(path = "^/admin/puzzle/(\\d+)$", type = PUT, needLogin = true)
|
||||
public void edit(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
JsonMap json = reader.readJson();
|
||||
Puzzle puzzle = new Puzzle(-1, json.get("name"), json.get("content"), json.<String>get("soluce").getBytes(), null, json.<Long>get("scoreMax").intValue(), null, -1, null);
|
||||
if(repo.adminUpdatePuzzle(Integer.parseInt(matcher.group(1)), puzzle, json.<Long>get("chapter").intValue())){
|
||||
context.response(200);
|
||||
}else{
|
||||
context.response(501);
|
||||
}
|
||||
}
|
||||
|
||||
@Route(path = "^/admin/puzzle/(\\d+)$", needLogin = true)
|
||||
public void get(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
Puzzle puzzle = this.repo.getAdminPuzzle(Integer.parseInt(matcher.group(1)));
|
||||
JsonMap json = new JsonMap();
|
||||
json.set("name", puzzle.getName());
|
||||
json.set("content", puzzle.getContent());
|
||||
json.set("soluce", new String(puzzle.getSoluce()));
|
||||
json.set("score_max", puzzle.getScoreMax());
|
||||
|
||||
context.response(200);
|
||||
writer.write(json.toString()+"\n");
|
||||
}
|
||||
|
||||
@Route(path = "^/admin/puzzles/$", needLogin = true)
|
||||
public void getAll(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
List<Puzzle> puzzles = repo.getAdminPuzzles();
|
||||
JsonArray array = new JsonArray();
|
||||
for (Puzzle puzzle : puzzles){
|
||||
JsonMap puzzleJSON = new JsonMap();
|
||||
puzzleJSON.set("id", puzzle.getId());
|
||||
puzzleJSON.set("name", puzzle.getName());
|
||||
puzzleJSON.set("scoreMax", puzzle.getScoreMax());
|
||||
if(puzzle.getTags() != null) puzzleJSON.set("tags", puzzle.getJsonTags());
|
||||
puzzleJSON.set("show", puzzle.hasStarted());
|
||||
array.add(puzzleJSON);
|
||||
}
|
||||
context.response(200);
|
||||
writer.write(array.toString());
|
||||
}
|
||||
}
|
|
@ -1,68 +0,0 @@
|
|||
package dev.peerat.backend.routes.admins;
|
||||
|
||||
import static dev.peerat.framework.RequestType.DELETE;
|
||||
import static dev.peerat.framework.RequestType.POST;
|
||||
import static dev.peerat.framework.RequestType.PUT;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.model.Tag;
|
||||
import dev.peerat.backend.repository.DatabaseAdminRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.utils.json.JsonArray;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class TagController {
|
||||
|
||||
private DatabaseAdminRepository repo;
|
||||
|
||||
public TagController(DatabaseRepository repo){
|
||||
this.repo = repo.getAdminRepository();
|
||||
}
|
||||
|
||||
@Route(path = "^/admin/tag/$", type = POST, needLogin = true)
|
||||
public void add(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
JsonMap json = reader.readJson();
|
||||
if(repo.adminAddTag(json.get("name"))){
|
||||
context.response(200);
|
||||
}else{
|
||||
context.response(501);
|
||||
}
|
||||
}
|
||||
|
||||
@Route(path = "^/admin/tag/(\\d+)$", type = DELETE, needLogin = true)
|
||||
public void delete(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
if(this.repo.adminDeleteTag(Integer.parseInt(matcher.group(1)))){
|
||||
context.response(200);
|
||||
}else{
|
||||
context.response(501);
|
||||
}
|
||||
}
|
||||
|
||||
@Route(path = "^/admin/tag/(\\d+)$", type = PUT, needLogin = true)
|
||||
public void edit(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
JsonMap json = reader.readJson();
|
||||
if(repo.adminUpdateTag(new Tag(Integer.parseInt(matcher.group(1)), json.get("name")))){
|
||||
context.response(200);
|
||||
}else{
|
||||
context.response(501);
|
||||
}
|
||||
}
|
||||
|
||||
@Route(path = "^/admin/tag/$", needLogin = true)
|
||||
public void get(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
List<Tag> list = repo.getAdminTags();
|
||||
JsonArray json = new JsonArray();
|
||||
for(Tag tag : list) json.add(tag.toJson());
|
||||
context.response(200);
|
||||
writer.write(json.toString());
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
package dev.peerat.backend.routes.admins;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.model.Completion;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Injection;
|
||||
import dev.peerat.framework.Locker;
|
||||
import dev.peerat.framework.Response;
|
||||
import dev.peerat.framework.Route;
|
||||
|
||||
public class WebHookLeaderboard implements Response{
|
||||
|
||||
private Locker<Completion> locker;
|
||||
|
||||
public WebHookLeaderboard(@Injection("leaderboard") Locker<Completion> locker){
|
||||
this.locker = locker;
|
||||
}
|
||||
|
||||
@Route(path = "^/admin/webhook/leaderboard/$", needLogin = true)
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
locker.setValue(new Completion(0, 0));
|
||||
context.response(200);
|
||||
}
|
||||
|
||||
}
|
|
@ -5,37 +5,28 @@ import static dev.peerat.framework.RequestType.POST;
|
|||
import java.time.LocalDateTime;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.Chapter;
|
||||
import dev.peerat.backend.model.Group;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.DatabaseChapterRepository;
|
||||
import dev.peerat.backend.repository.DatabaseGroupRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.backend.utils.FormResponse;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Injection;
|
||||
import dev.peerat.framework.Locker;
|
||||
import dev.peerat.framework.Response;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class GroupCreate extends FormResponse {
|
||||
public class GroupCreate implements Response {
|
||||
|
||||
private Locker<Group> locker;
|
||||
private DatabaseGroupRepository repo;
|
||||
private DatabaseChapterRepository chapterRepo;
|
||||
private DatabaseRepository repo;
|
||||
private int groupDelay;
|
||||
|
||||
public GroupCreate(DatabaseRepository repo, @Injection("groups") Locker<Group> locker, Configuration config){
|
||||
this.repo = repo.getGroupRepository();
|
||||
this.chapterRepo = repo.getChapterRepository();
|
||||
public GroupCreate(DatabaseRepository repo, Locker<Group> locker, int groupDelay){
|
||||
this.repo = repo;
|
||||
this.locker = locker;
|
||||
this.groupDelay = config.getGroupJoinMinutes();
|
||||
|
||||
validator("name", "[a-zA-Z0-9&|!?{}\\[\\]%/*\\-+=:;,_#@ ]{3,100}");
|
||||
this.groupDelay = groupDelay;
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/groupCreate", responseCode = 200, responseDescription = "Le groupe a été créé")
|
||||
|
@ -44,12 +35,7 @@ public class GroupCreate extends FormResponse {
|
|||
|
||||
@Route(path = "^\\/groupCreate$", type = POST, needLogin = true)
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
JsonMap json = json(reader);
|
||||
if(!areValids("name")){
|
||||
context.response(403);
|
||||
return;
|
||||
}
|
||||
Group newGroup = new Group(json);
|
||||
Group newGroup = new Group(reader.readJson());
|
||||
PeerAtUser user = context.getUser();
|
||||
|
||||
if (this.repo.getPlayerGroup(user.getId(), newGroup.getLinkToChapter()) == null) {
|
||||
|
@ -59,7 +45,7 @@ public class GroupCreate extends FormResponse {
|
|||
return;
|
||||
}catch(NullPointerException e){
|
||||
if(newGroup.getLinkToChapter() != null){
|
||||
Chapter chapter = this.chapterRepo.getChapter(newGroup.getLinkToChapter());
|
||||
Chapter chapter = this.repo.getChapter(newGroup.getLinkToChapter());
|
||||
if(chapter.getStartDate() != null){
|
||||
LocalDateTime start = chapter.getStartDate().toLocalDateTime().plusMinutes(this.groupDelay);
|
||||
if(LocalDateTime.now().isAfter(start)){
|
||||
|
|
|
@ -5,37 +5,31 @@ import static dev.peerat.framework.RequestType.POST;
|
|||
import java.time.LocalDateTime;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.Chapter;
|
||||
import dev.peerat.backend.model.Completion;
|
||||
import dev.peerat.backend.model.Group;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.DatabaseChapterRepository;
|
||||
import dev.peerat.backend.repository.DatabaseGroupRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Injection;
|
||||
import dev.peerat.framework.Locker;
|
||||
import dev.peerat.framework.Response;
|
||||
import dev.peerat.framework.Route;
|
||||
|
||||
public class GroupJoin implements Response{
|
||||
|
||||
private DatabaseChapterRepository chapterRepo;
|
||||
private DatabaseGroupRepository groupRepo;
|
||||
private DatabaseRepository repo;
|
||||
private int groupDelay;
|
||||
private String waitTime;
|
||||
|
||||
private final Locker<Completion> leaderboard;
|
||||
|
||||
public GroupJoin(DatabaseRepository repo, Configuration config, @Injection("leaderboard") Locker<Completion> locker){
|
||||
this.chapterRepo = repo.getChapterRepository();
|
||||
this.groupRepo = repo.getGroupRepository();
|
||||
this.groupDelay = config.getGroupJoinMinutes();
|
||||
this.waitTime = config.getGroupQuitMinutes();
|
||||
public GroupJoin(DatabaseRepository repo, int groupDelay, String waitTime, Locker<Completion> locker){
|
||||
this.repo = repo;
|
||||
this.groupDelay = groupDelay;
|
||||
this.waitTime = waitTime;
|
||||
this.leaderboard = locker;
|
||||
}
|
||||
|
||||
|
@ -49,7 +43,7 @@ public class GroupJoin implements Response{
|
|||
Group group = new Group(reader.readJson());
|
||||
PeerAtUser user = context.getUser();
|
||||
|
||||
Group userGroup = this.groupRepo.getPlayerGroup(user.getId(), group.getLinkToChapter());
|
||||
Group userGroup = this.repo.getPlayerGroup(user.getId(), group.getLinkToChapter());
|
||||
if(group.equals(userGroup)){
|
||||
context.response(403);
|
||||
return;
|
||||
|
@ -62,7 +56,7 @@ public class GroupJoin implements Response{
|
|||
}
|
||||
|
||||
if(group.getLinkToChapter() != null){
|
||||
Chapter chapter = this.chapterRepo.getChapter(group.getLinkToChapter());
|
||||
Chapter chapter = this.repo.getChapter(group.getLinkToChapter());
|
||||
if(chapter.getStartDate() != null){
|
||||
LocalDateTime start = chapter.getStartDate().toLocalDateTime().plusMinutes(this.groupDelay);
|
||||
if(LocalDateTime.now().isAfter(start)){
|
||||
|
@ -72,7 +66,7 @@ public class GroupJoin implements Response{
|
|||
}
|
||||
}
|
||||
|
||||
if (this.groupRepo.insertUserInGroup(group, user)) {
|
||||
if (this.repo.insertUserInGroup(group, user)) {
|
||||
context.response(200);
|
||||
|
||||
leaderboard.setValue(new Completion(0, 0, 0, null, 0));
|
||||
|
|
|
@ -2,42 +2,33 @@ package dev.peerat.backend.routes.groups;
|
|||
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.Group;
|
||||
import dev.peerat.backend.repository.DatabaseGroupRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Response;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.utils.json.JsonArray;
|
||||
|
||||
public class GroupList implements Response {
|
||||
|
||||
private DatabaseGroupRepository repo;
|
||||
private DatabaseRepository repo;
|
||||
|
||||
public GroupList(DatabaseRepository repo){
|
||||
this.repo = repo.getGroupRepository();
|
||||
public GroupList(DatabaseRepository repo) {
|
||||
this.repo = repo;
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/groups", responseCode = 200, responseDescription = "JSON avec la liste des groups")
|
||||
|
||||
@Route(path = "^\\/groups\\/?(\\d+)?$", needLogin = true)
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
String param = matcher.group(1);
|
||||
if(param == null){
|
||||
JsonArray result = new JsonArray();
|
||||
for(Group group : this.repo.getAllGroups()) result.add(group.toJson());
|
||||
context.response(200);
|
||||
writer.write(result.toString());
|
||||
return;
|
||||
}
|
||||
int chapter = Integer.parseInt(param);
|
||||
JsonArray result = new JsonArray();
|
||||
for(Group group : this.repo.getAllGroupsByChapter(chapter)) result.add(group.toJson());
|
||||
@Route(path = "^\\/groups$", needLogin = true)
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
context.response(200);
|
||||
writer.write(result.toString());
|
||||
JSONArray result = new JSONArray();
|
||||
for(Group group : this.repo.getAllGroups()) result.add(group.toJson());
|
||||
writer.write(result.toJSONString());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -5,35 +5,29 @@ import static dev.peerat.framework.RequestType.POST;
|
|||
import java.time.LocalDateTime;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.Chapter;
|
||||
import dev.peerat.backend.model.Completion;
|
||||
import dev.peerat.backend.model.Group;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.DatabaseChapterRepository;
|
||||
import dev.peerat.backend.repository.DatabaseGroupRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Injection;
|
||||
import dev.peerat.framework.Locker;
|
||||
import dev.peerat.framework.Response;
|
||||
import dev.peerat.framework.Route;
|
||||
|
||||
public class GroupQuit implements Response{
|
||||
|
||||
private DatabaseChapterRepository chapterRepo;
|
||||
private DatabaseGroupRepository groupRepo;
|
||||
private DatabaseRepository repo;
|
||||
private int groupDelay;
|
||||
|
||||
private final Locker<Completion> leaderboard;
|
||||
|
||||
public GroupQuit(DatabaseRepository repo, Configuration config, @Injection("leaderboard") Locker<Completion> locker){
|
||||
this.chapterRepo = repo.getChapterRepository();
|
||||
this.groupRepo = repo.getGroupRepository();
|
||||
this.groupDelay = config.getGroupJoinMinutes();
|
||||
public GroupQuit(DatabaseRepository repo, int groupDelay, Locker<Completion> locker){
|
||||
this.repo = repo;
|
||||
this.groupDelay = groupDelay;
|
||||
|
||||
this.leaderboard = locker;
|
||||
}
|
||||
|
@ -47,14 +41,14 @@ public class GroupQuit implements Response{
|
|||
Group group = new Group(reader.readJson());
|
||||
PeerAtUser user = context.getUser();
|
||||
|
||||
Group userGroup = this.groupRepo.getPlayerGroup(user.getId(), group.getLinkToChapter());
|
||||
Group userGroup = this.repo.getPlayerGroup(user.getId(), group.getLinkToChapter());
|
||||
if(!group.equals(userGroup)){
|
||||
context.response(403);
|
||||
return;
|
||||
}
|
||||
|
||||
if(group.getLinkToChapter() != null){
|
||||
Chapter chapter = this.chapterRepo.getChapter(group.getLinkToChapter());
|
||||
Chapter chapter = this.repo.getChapter(group.getLinkToChapter());
|
||||
if(chapter.getStartDate() != null){
|
||||
LocalDateTime start = chapter.getStartDate().toLocalDateTime().plusMinutes(this.groupDelay);
|
||||
if(LocalDateTime.now().isAfter(start)){
|
||||
|
@ -64,7 +58,7 @@ public class GroupQuit implements Response{
|
|||
}
|
||||
}
|
||||
|
||||
if (this.groupRepo.leaveGroup(group, user)) {
|
||||
if (this.repo.leaveGroup(group, user)) {
|
||||
context.response(200);
|
||||
|
||||
leaderboard.setValue(new Completion(0, 0, 0, null, 0));
|
||||
|
|
|
@ -1,36 +0,0 @@
|
|||
package dev.peerat.backend.routes.users;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.DatabasePlayerRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.RequestType;
|
||||
import dev.peerat.framework.Response;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class ChangePassword implements Response{
|
||||
|
||||
private DatabasePlayerRepository repo;
|
||||
|
||||
public ChangePassword(DatabaseRepository repo){
|
||||
this.repo = repo.getPlayerRepository();
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/user/cpw", responseCode = 200, responseDescription = "L'utilisateur a mis à jours sont mots de passe")
|
||||
@RouteDoc(responseCode = 400, responseDescription = "L'utilisateur a envoyer un mots de passe invalide")
|
||||
|
||||
@Route(path = "^/user/cpw$", type = RequestType.POST, needLogin = true)
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
String password = reader.<JsonMap>readJson().get("password");
|
||||
|
||||
repo.updatePassword(context.<PeerAtUser>getUser().getId(), password);
|
||||
context.response(200);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,124 +0,0 @@
|
|||
package dev.peerat.backend.routes.users;
|
||||
|
||||
import static dev.peerat.framework.RequestType.POST;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.DatabasePlayerRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.backend.utils.FormResponse;
|
||||
import dev.peerat.backend.utils.Mail;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.Router;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class ForgotPassword extends FormResponse{
|
||||
|
||||
private Router<PeerAtUser> router;
|
||||
private DatabasePlayerRepository repo;
|
||||
private Mail mail;
|
||||
private Map<String, String> codes;
|
||||
private List<Random> randoms;
|
||||
|
||||
public ForgotPassword(Router<PeerAtUser> router, DatabaseRepository repo, Mail mail){
|
||||
this.router = router;
|
||||
this.repo = repo.getPlayerRepository();
|
||||
this.mail = mail;
|
||||
this.codes = new HashMap<>();
|
||||
|
||||
this.randoms = new ArrayList<>();
|
||||
|
||||
Random random = new Random();
|
||||
int randoms = random.nextInt(10)+3;
|
||||
for(int i = 0; i < randoms; i++) this.randoms.add(new SecureRandom());
|
||||
|
||||
try {
|
||||
Constructor<?> constructor = UUID.class.getDeclaredConstructor(byte[].class);
|
||||
constructor.setAccessible(true);
|
||||
this.uuidBuilder = constructor;
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Route(path = "^/user/fpw$", type = POST)
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
if(context.isLogged()){
|
||||
context.response(403);
|
||||
return;
|
||||
}
|
||||
|
||||
JsonMap json = json(reader);
|
||||
if(!areValids("email")){
|
||||
context.response(400);
|
||||
return;
|
||||
}
|
||||
|
||||
String email = json.get("email");
|
||||
|
||||
int player = repo.getPlayerId(email);
|
||||
if(player < 0){
|
||||
context.response(400);
|
||||
return;
|
||||
}
|
||||
|
||||
if(hasFields("code") && areValids("password")){
|
||||
String checkCode = codes.get(email);
|
||||
if(checkCode == null){
|
||||
context.response(400);
|
||||
return;
|
||||
}
|
||||
|
||||
String code = json.<String>get("code");
|
||||
String password = json.get("password");
|
||||
|
||||
if(checkCode.equals(code)){
|
||||
codes.remove(email);
|
||||
|
||||
repo.updatePassword(player, password);
|
||||
context.response(200,
|
||||
"Access-Control-Expose-Headers: Authorization",
|
||||
"Authorization: Bearer " + this.router.createAuthUser(new PeerAtUser(player)));
|
||||
}else{
|
||||
context.response(400);
|
||||
}
|
||||
}else{
|
||||
String code = codeGenerator();
|
||||
codes.put(email, code);
|
||||
mail.send(email, "Forgot your Peer @ Code password ?", "Your check code is "+code+" !");
|
||||
context.response(200);
|
||||
}
|
||||
}
|
||||
|
||||
private Constructor<?> uuidBuilder;
|
||||
private int[] start = {4, 9, 14, 19};
|
||||
|
||||
private String codeGenerator() throws Exception{
|
||||
Random random = new Random();
|
||||
Random target = this.randoms.get(random.nextInt(this.randoms.size()));
|
||||
byte[] arrayOfByte = new byte[16];
|
||||
target.nextBytes(arrayOfByte);
|
||||
arrayOfByte[6] = (byte)(arrayOfByte[6] & 0xF);
|
||||
arrayOfByte[6] = (byte)(arrayOfByte[6] | 0x40);
|
||||
arrayOfByte[8] = (byte)(arrayOfByte[8] & 0x3F);
|
||||
arrayOfByte[8] = (byte)(arrayOfByte[8] | 0x80);
|
||||
String uuid = uuidBuilder.newInstance(arrayOfByte).toString();
|
||||
target = this.randoms.get(random.nextInt(this.randoms.size()));
|
||||
|
||||
int start = this.start[target.nextInt(this.start.length)];
|
||||
return uuid.substring(start, start+9);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,230 +0,0 @@
|
|||
package dev.peerat.backend.routes.users;
|
||||
|
||||
import static dev.peerat.framework.RequestType.POST;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.net.URL;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Base64.Encoder;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
|
||||
import org.jose4j.json.internal.json_simple.JSONAware;
|
||||
import org.jose4j.json.internal.json_simple.JSONObject;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.DatabaseAuthRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.backend.utils.FormResponse;
|
||||
import dev.peerat.backend.utils.Mail;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Injection;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.Router;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class MailConfirmation extends FormResponse{
|
||||
|
||||
private DatabaseAuthRepository databaseRepo;
|
||||
private Router<PeerAtUser> router;
|
||||
private String usersFilesPath;
|
||||
private KeyPairGenerator generator;
|
||||
private Encoder encoder;
|
||||
private String gitToken;
|
||||
private Map<String, String> playersWaiting;
|
||||
private Mail mail;
|
||||
private List<Random> randoms;
|
||||
|
||||
public MailConfirmation(
|
||||
DatabaseRepository databaseRepo,
|
||||
Router<PeerAtUser> router,
|
||||
Configuration config,
|
||||
@Injection("waitting") Map<String, String> playersWaiting,
|
||||
Mail mail) throws NoSuchAlgorithmException{
|
||||
|
||||
this.databaseRepo = databaseRepo.getAuthRepository();
|
||||
this.router = router;
|
||||
this.usersFilesPath = config.getUsersFiles();
|
||||
this.gitToken = config.getGitToken();
|
||||
this.playersWaiting = playersWaiting;
|
||||
this.mail = mail;
|
||||
|
||||
generator = KeyPairGenerator.getInstance("RSA");
|
||||
generator.initialize(4096);
|
||||
encoder = Base64.getEncoder();
|
||||
|
||||
this.randoms = new ArrayList<>();
|
||||
|
||||
Random random = new Random();
|
||||
int randoms = random.nextInt(10)+3;
|
||||
for(int i = 0; i < randoms; i++) this.randoms.add(new SecureRandom());
|
||||
|
||||
try {
|
||||
Constructor<?> constructor = UUID.class.getDeclaredConstructor(byte[].class);
|
||||
constructor.setAccessible(true);
|
||||
this.uuidBuilder = constructor;
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
validator("pseudo", "[a-zA-Z0-9&|!?{}\\[\\]%/*\\-+=:;,_#@ ]{3,100}");
|
||||
validator("firstname", "^(?>[A-Za-zÀ-ÖØ-öø-ÿ]+ ?)+$");
|
||||
validator("lastname", "^(?>[A-Za-zÀ-ÖØ-öø-ÿ]+ ?)+$");
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/confirmation", responseCode = 200, responseDescription = "L'utilisateur est inscrit")
|
||||
@RouteDoc(responseCode = 403, responseDescription = "L'utilisateur est connecté")
|
||||
@RouteDoc(responseCode = 400, responseDescription = "Aucune données fournie / données invalide")
|
||||
|
||||
@Route(path = "^\\/confirmation$", type = POST)
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
if(context.isLogged()){
|
||||
context.response(403);
|
||||
return;
|
||||
}
|
||||
JsonMap json = json(reader);
|
||||
if((!areValids("email","pseudo","firstname","lastname","passwd")) || (!hasFields("code"))){
|
||||
context.response(400);
|
||||
return;
|
||||
}
|
||||
String email = json.get("email");
|
||||
String code = json.<String>get("code");
|
||||
String pseudo = json.get("pseudo");
|
||||
String firstname = json.get("firstname");
|
||||
String lastname = json.get("lastname");
|
||||
String password = json.get("passwd");
|
||||
|
||||
String checkCode = playersWaiting.get(email);
|
||||
if(checkCode == null){
|
||||
context.response(400);
|
||||
return;
|
||||
}
|
||||
|
||||
boolean pseudoAvailable = databaseRepo.checkPseudoAvailability(pseudo);
|
||||
boolean emailAvailable = databaseRepo.checkEmailAvailability(email);
|
||||
if(pseudoAvailable && emailAvailable){
|
||||
if(code.equals(checkCode)){
|
||||
playersWaiting.remove(email);
|
||||
int id = databaseRepo.register(pseudo, email, password, firstname, lastname, "", "", "");
|
||||
if(id >= 0){
|
||||
// createFolderToSaveSourceCode(pseudo);
|
||||
// generateGitKey(email, pseudo, password);
|
||||
context.response(200,
|
||||
"Access-Control-Expose-Headers: Authorization",
|
||||
"Authorization: Bearer " + this.router.createAuthUser(new PeerAtUser(id)));
|
||||
}else{
|
||||
context.response(400);
|
||||
JsonMap error = new JsonMap();
|
||||
error.set("username_valid", pseudo);
|
||||
error.set("email_valid", email);
|
||||
writer.write(error.toString());
|
||||
String ncode = codeGenerator();
|
||||
playersWaiting.put(email, ncode);
|
||||
mail.send(email, "Welcome @ Peer @ Code", "Your check code is "+ncode+" !");
|
||||
}
|
||||
}else{
|
||||
context.response(400);
|
||||
}
|
||||
}else{
|
||||
context.response(400);
|
||||
JsonMap error = new JsonMap();
|
||||
error.set("username_valid", pseudo);
|
||||
error.set("email_valid", email);
|
||||
writer.write(error.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private Constructor<?> uuidBuilder;
|
||||
private int[] start = {4, 9, 14, 19};
|
||||
|
||||
private String codeGenerator() throws Exception{
|
||||
Random random = new Random();
|
||||
Random target = this.randoms.get(random.nextInt(this.randoms.size()));
|
||||
byte[] arrayOfByte = new byte[16];
|
||||
target.nextBytes(arrayOfByte);
|
||||
arrayOfByte[6] = (byte)(arrayOfByte[6] & 0xF);
|
||||
arrayOfByte[6] = (byte)(arrayOfByte[6] | 0x40);
|
||||
arrayOfByte[8] = (byte)(arrayOfByte[8] & 0x3F);
|
||||
arrayOfByte[8] = (byte)(arrayOfByte[8] | 0x80);
|
||||
String uuid = uuidBuilder.newInstance(arrayOfByte).toString();
|
||||
target = this.randoms.get(random.nextInt(this.randoms.size()));
|
||||
|
||||
int start = this.start[target.nextInt(this.start.length)];
|
||||
return uuid.substring(start, start+9);
|
||||
}
|
||||
|
||||
private void createFolderToSaveSourceCode(String pseudo) throws IOException {
|
||||
|
||||
Files.createDirectories(Paths.get(String.format("%s/%s", usersFilesPath, pseudo)));
|
||||
}
|
||||
|
||||
private static byte[] PREFIX = new byte[] {0,0,0,7,(byte)'s',(byte)'s',(byte)'h',(byte)'-',(byte)'r',(byte)'s',(byte)'a'};
|
||||
|
||||
private String generateGitKey(String email, String pseudo, String password) throws Exception{
|
||||
KeyPair pair = generator.generateKeyPair(); //doit être unique ???
|
||||
|
||||
JSONObject createUser = new JSONObject();
|
||||
createUser.put("email", email);
|
||||
createUser.put("username", pseudo);
|
||||
createUser.put("password", password);
|
||||
post("https://git-users.peerat.dev/api/v1/admin/users/", createUser);
|
||||
|
||||
JSONObject sendKey = new JSONObject();
|
||||
RSAPublicKey pub = (RSAPublicKey) pair.getPublic();
|
||||
byte[] exponent = pub.getPublicExponent().toByteArray();
|
||||
byte[] modulus = pub.getModulus().toByteArray();
|
||||
byte[] key = new byte[19+exponent.length+modulus.length];
|
||||
System.arraycopy(PREFIX, 0, key, 0, 11);
|
||||
byte[] exLength = ByteBuffer.allocate(4).putInt(exponent.length).array();
|
||||
byte[] modLength = ByteBuffer.allocate(4).putInt(modulus.length).array();
|
||||
System.arraycopy(exLength, 0, key, 11, 4);
|
||||
System.arraycopy(exponent, 0, key, 15, exponent.length);
|
||||
System.arraycopy(modLength, 0, key, 15+exponent.length, 4);
|
||||
System.arraycopy(modulus, 0, key, 19+exponent.length, modulus.length);
|
||||
|
||||
sendKey.put("key", "ssh-rsa "+new String(encoder.encode(key)));
|
||||
sendKey.put("read_only", false);
|
||||
sendKey.put("title", "peer_at_code_auto_push_key_"+pseudo);
|
||||
post("https://git-users.peerat.dev/api/v1/admin/users/"+pseudo+"/keys", sendKey);
|
||||
|
||||
return new String(encoder.encode(pair.getPrivate().getEncoded()));
|
||||
}
|
||||
|
||||
private void post(String url, JSONAware json) throws Exception{
|
||||
HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type","application/json");
|
||||
connection.setRequestProperty("Authorization","Bearer "+this.gitToken);
|
||||
connection.setDoInput(true);
|
||||
connection.setDoOutput(true);
|
||||
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
|
||||
writer.write(json.toJSONString());
|
||||
writer.flush();
|
||||
writer.close();
|
||||
|
||||
int response = connection.getResponseCode();
|
||||
if(response != 201) throw new Exception("Call to "+url+" failed with response code "+response);
|
||||
}
|
||||
}
|
|
@ -1,64 +0,0 @@
|
|||
package dev.peerat.backend.routes.users;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.model.Player;
|
||||
import dev.peerat.backend.repository.DatabasePlayerRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.backend.utils.FormResponse;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.RequestType;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class ProfileSettings extends FormResponse{
|
||||
|
||||
private DatabasePlayerRepository repo;
|
||||
|
||||
public ProfileSettings(DatabaseRepository repo){
|
||||
this.repo = repo.getPlayerRepository();
|
||||
|
||||
validator("pseudo", "[a-zA-Z0-9&|!?{}\\[\\]%/*\\-+=:;,_#@ ]{3,100}");
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/user/settings", responseCode = 200, responseDescription = "L'utilisateur a mis à jours sont profile")
|
||||
@RouteDoc(responseCode = 400, responseDescription = "L'utilisateur a envoyer une donnée unique, déjà utilisée")
|
||||
|
||||
@Route(path = "^/user/settings$", type = RequestType.POST, needLogin = true)
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
JsonMap json = json(reader);
|
||||
|
||||
String pseudo = json.get("pseudo");
|
||||
String email = json.get("email");
|
||||
String firstname = json.get("firstname");
|
||||
String lastname = json.get("lastname");
|
||||
Player player = repo.getPlayer(context.<PeerAtUser>getUser().getId());
|
||||
|
||||
if(!areValids("pseudo", "firstname", "lastname")){
|
||||
context.response(400);
|
||||
return;
|
||||
}
|
||||
|
||||
if(!player.getPseudo().equals(pseudo)){
|
||||
if(!repo.updatePseudo(context.<PeerAtUser>getUser().getId(), player, pseudo)){
|
||||
context.response(400);
|
||||
return;
|
||||
}
|
||||
player.setPseudo(pseudo);
|
||||
}
|
||||
|
||||
if(!player.getEmail().equals(email)){
|
||||
}
|
||||
|
||||
if((!player.getFirstname().equals(firstname)) || (!player.getLastname().equals(lastname))){
|
||||
repo.updateProfile(context.<PeerAtUser>getUser().getId(), player, lastname, firstname);
|
||||
}
|
||||
|
||||
context.response(200);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,106 +0,0 @@
|
|||
package dev.peerat.backend.routes.users;
|
||||
|
||||
import static dev.peerat.framework.RequestType.POST;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||
import dev.peerat.backend.repository.DatabaseAuthRepository;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.backend.utils.FormResponse;
|
||||
import dev.peerat.backend.utils.Mail;
|
||||
import dev.peerat.framework.Context;
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.Injection;
|
||||
import dev.peerat.framework.Route;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class Register extends FormResponse{
|
||||
|
||||
private DatabaseAuthRepository databaseRepo;
|
||||
private Map<String, String> playersWaiting;
|
||||
private Mail mail;
|
||||
private String host;
|
||||
private List<Random> randoms;
|
||||
|
||||
public Register(DatabaseRepository databaseRepo, @Injection("waitting") Map<String, String> playersWaiting, Mail mail, Configuration config){
|
||||
this.databaseRepo = databaseRepo.getAuthRepository();
|
||||
this.playersWaiting = playersWaiting;
|
||||
this.mail = mail;
|
||||
this.host = config.getTokenIssuer();
|
||||
this.randoms = new ArrayList<>();
|
||||
|
||||
Random random = new Random();
|
||||
int randoms = random.nextInt(10)+3;
|
||||
for(int i = 0; i < randoms; i++) this.randoms.add(new SecureRandom());
|
||||
|
||||
try {
|
||||
Constructor<?> constructor = UUID.class.getDeclaredConstructor(byte[].class);
|
||||
constructor.setAccessible(true);
|
||||
this.uuidBuilder = constructor;
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/register", responseCode = 200, responseDescription = "L'utilisateur est inscrit")
|
||||
@RouteDoc(responseCode = 403, responseDescription = "L'utilisateur est connecté")
|
||||
@RouteDoc(responseCode = 400, responseDescription = "Aucune données fournie / données invalide")
|
||||
|
||||
@Route(path = "^\\/register$", type = POST)
|
||||
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
if (context.isLogged()){
|
||||
context.response(403);
|
||||
return;
|
||||
}
|
||||
JsonMap json = json(reader);
|
||||
if(!areValids("email")){
|
||||
context.response(400);
|
||||
return;
|
||||
}
|
||||
|
||||
String email = json.get("email");
|
||||
|
||||
boolean emailAvailable = databaseRepo.checkEmailAvailability(email);
|
||||
if(emailAvailable){
|
||||
String code = codeGenerator();
|
||||
playersWaiting.put(email, code);
|
||||
mail.send(email, "Welcome @ Peer @ Code", "Your check code is "+code+" !");
|
||||
context.response(200);
|
||||
}else{
|
||||
context.response(400);
|
||||
JsonMap error = new JsonMap();
|
||||
error.set("email_valid", emailAvailable);
|
||||
writer.write(error.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private Constructor<?> uuidBuilder;
|
||||
private int[] start = {4, 9, 14, 19};
|
||||
|
||||
private String codeGenerator() throws Exception{
|
||||
Random random = new Random();
|
||||
Random target = this.randoms.get(random.nextInt(this.randoms.size()));
|
||||
byte[] arrayOfByte = new byte[16];
|
||||
target.nextBytes(arrayOfByte);
|
||||
arrayOfByte[6] = (byte)(arrayOfByte[6] & 0xF);
|
||||
arrayOfByte[6] = (byte)(arrayOfByte[6] | 0x40);
|
||||
arrayOfByte[8] = (byte)(arrayOfByte[8] & 0x3F);
|
||||
arrayOfByte[8] = (byte)(arrayOfByte[8] | 0x80);
|
||||
String uuid = uuidBuilder.newInstance(arrayOfByte).toString();
|
||||
target = this.randoms.get(random.nextInt(this.randoms.size()));
|
||||
|
||||
int start = this.start[target.nextInt(this.start.length)];
|
||||
return uuid.substring(start, start+9);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
package dev.peerat.backend.utils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.Response;
|
||||
import dev.peerat.framework.utils.json.Json;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public abstract class FormResponse implements Response{
|
||||
|
||||
private Json json;
|
||||
private Map<String, Pattern> checker;
|
||||
|
||||
public FormResponse(){
|
||||
this.checker = new HashMap<>();
|
||||
}
|
||||
|
||||
public void validator(String key, String regex){
|
||||
this.checker.put(key, Pattern.compile(regex, Pattern.CASE_INSENSITIVE));
|
||||
}
|
||||
|
||||
public <T extends Json> T json(HttpReader reader) throws Exception{
|
||||
return (T) (this.json = reader.readJson());
|
||||
}
|
||||
|
||||
public boolean hasFields(String... fields){
|
||||
JsonMap map = (JsonMap)json;
|
||||
for(String field : fields){
|
||||
if(!map.has(field)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean areValids(String... fields){
|
||||
JsonMap map = (JsonMap)json;
|
||||
for(String field : fields){
|
||||
String value = (String) map.get(field);
|
||||
if(value == null) return false;
|
||||
if(value.isEmpty()) return false;
|
||||
Pattern pattern = checker.get(field);
|
||||
if(pattern == null) continue;
|
||||
if(!pattern.matcher(value).matches()) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -24,6 +24,7 @@ public class Mail {
|
|||
props.put("mail.smtp.starttls.enable", "true");
|
||||
|
||||
Authenticator auth = new Authenticator() {
|
||||
//override the getPasswordAuthentication method
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(initUsername, initPassword);
|
||||
}
|
||||
|
@ -32,7 +33,7 @@ public class Mail {
|
|||
fromAddress = initFromAddress;
|
||||
}
|
||||
|
||||
public void send(String toAddress, String subject, String text){
|
||||
public void send(String toAddress, String subject, String text) {
|
||||
try
|
||||
{
|
||||
MimeMessage msg = new MimeMessage(session);
|
||||
|
@ -41,8 +42,8 @@ public class Mail {
|
|||
msg.addHeader("format", "flowed");
|
||||
msg.addHeader("Content-Transfer-Encoding", "8bit");
|
||||
|
||||
msg.setFrom(new InternetAddress(fromAddress, "Peer-at Code"));
|
||||
msg.setReplyTo(InternetAddress.parse(fromAddress, false));
|
||||
msg.setFrom(new InternetAddress("ping@peerat.dev", "NoReply-JD"));
|
||||
msg.setReplyTo(InternetAddress.parse("ping@peerat.dev", false));
|
||||
msg.setSubject(subject, "UTF-8");
|
||||
msg.setText(text, "UTF-8");
|
||||
msg.setSentDate(new Date());
|
||||
|
|
|
@ -1,64 +0,0 @@
|
|||
package dev.peerat.backend;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
|
||||
public class TestDatabaseRepository extends DatabaseRepository{
|
||||
|
||||
private Connection con;
|
||||
private String schem;
|
||||
|
||||
public TestDatabaseRepository(Configuration config, File databaseSchem){
|
||||
super(config);
|
||||
|
||||
try{
|
||||
schem = "";
|
||||
BufferedReader reader = new BufferedReader(new FileReader(databaseSchem));
|
||||
String line;
|
||||
while((line = reader.readLine()) != null) schem+=line;
|
||||
reader.close();
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void init(){
|
||||
try {
|
||||
Method method = DatabaseRepository.class.getDeclaredMethod("ensureConnection");
|
||||
method.setAccessible(true);
|
||||
method.invoke(this);
|
||||
|
||||
Field field = DatabaseRepository.class.getDeclaredField("con");
|
||||
field.setAccessible(true);
|
||||
this.con = (Connection) field.get(this);
|
||||
}catch(Exception e){
|
||||
e.getCause().printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void close(){
|
||||
try {
|
||||
this.con.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void reset(){
|
||||
try{
|
||||
String[] split = schem.split(";");
|
||||
for(String statment : split){
|
||||
this.con.prepareStatement(statment).execute();
|
||||
}
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
package dev.peerat.backend.routes;
|
||||
|
||||
public interface DatabaseSeeder{
|
||||
|
||||
void setup() throws Exception;
|
||||
|
||||
}
|
|
@ -9,7 +9,7 @@ import org.junit.jupiter.api.TestInstance;
|
|||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||
|
||||
import dev.peerat.backend.Main;
|
||||
import dev.peerat.backend.WebClient;
|
||||
import dev.peerat.backend.webclient.WebClient;
|
||||
|
||||
@TestInstance(Lifecycle.PER_CLASS)
|
||||
class PlayerDetailsTests {
|
||||
|
|
|
@ -2,6 +2,7 @@ package dev.peerat.backend.routes;
|
|||
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
@ -9,8 +10,7 @@ import org.junit.jupiter.api.TestInstance;
|
|||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||
|
||||
import dev.peerat.backend.Main;
|
||||
import dev.peerat.backend.WebClient;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
import dev.peerat.backend.webclient.WebClient;
|
||||
|
||||
@TestInstance(Lifecycle.PER_CLASS)
|
||||
public class ScoreTests {
|
||||
|
@ -42,14 +42,14 @@ public class ScoreTests {
|
|||
@Test
|
||||
void testOnDeployed(){
|
||||
try{
|
||||
JsonMap group = new JsonMap();
|
||||
group.set("name", "GroupTest");
|
||||
group.set("chapter", 2);
|
||||
JSONObject group = new JSONObject();
|
||||
group.put("name", "GroupTest");
|
||||
group.put("chapter", 2);
|
||||
|
||||
client.register("Test1", "Test2", "Test3@Test7.be", "Test4", "Test5", "Test6");
|
||||
client.assertResponseCode(200);
|
||||
|
||||
client.route("/groupCreate", "POST", group.toString());
|
||||
client.route("/groupCreate", "POST", group.toJSONString());
|
||||
client.assertResponseCode(200);
|
||||
|
||||
client.sendHeaders.add("content-type: ah;boundary=----WebKitFormBoundaryNUjiLBAMuX2dhxU7");
|
||||
|
@ -60,7 +60,7 @@ public class ScoreTests {
|
|||
client.disconnect();
|
||||
|
||||
client.auth("JeffCheasey88", "TheoPueDesPieds");
|
||||
client.route("/groupJoin", "POST", group.toString());
|
||||
client.route("/groupJoin", "POST", group.toJSONString());
|
||||
client.assertResponseCode(200);
|
||||
|
||||
client.sendHeaders.add("content-type: ah;boundary=----WebKitFormBoundaryNUjiLBAMuX2dhxU7");
|
||||
|
|
|
@ -1,162 +0,0 @@
|
|||
package dev.peerat.backend.routes;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.opentest4j.AssertionFailedError;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.Main;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Locker;
|
||||
import dev.peerat.framework.Locker.Key;
|
||||
import dev.peerat.framework.RequestType;
|
||||
import dev.peerat.framework.Router;
|
||||
|
||||
public class StateTestCase{
|
||||
|
||||
@Test
|
||||
void main() throws Exception{
|
||||
Locker<Boolean> locker = new Locker<>();
|
||||
Thread server = new Thread(new Runnable(){
|
||||
@Override
|
||||
public void run(){
|
||||
try {
|
||||
locker.setValue(true);
|
||||
Main.main(null);
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
Key key = new Key();
|
||||
locker.init(key);
|
||||
server.start();
|
||||
Thread.sleep(3000);
|
||||
locker.lock(key);
|
||||
locker.unlock(key);
|
||||
|
||||
Configuration config = new Configuration("config.txt");
|
||||
config.load();
|
||||
|
||||
DatabaseRepository repo = new DatabaseRepository(config);
|
||||
|
||||
Field field = Main.class.getDeclaredField("ACCESS_ROUTER");
|
||||
field.setAccessible(true);
|
||||
Router<PeerAtUser> router = (Router<PeerAtUser>) field.get(null);
|
||||
Function<String, String> tokenProvider = (email) -> {
|
||||
try{
|
||||
return router.createAuthUser(new PeerAtUser(repo.getPlayerId(email)));
|
||||
}catch(Exception ex){
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
File dir = new File("./route-test/");
|
||||
for(File cases : dir.listFiles()){
|
||||
if(!cases.isDirectory()) continue;
|
||||
List<File> responses = new ArrayList<>();
|
||||
File dbsetup = null;
|
||||
for(File file : cases.listFiles()){
|
||||
if(file.getName().endsWith(".class")) dbsetup = file; else responses.add(file);
|
||||
}
|
||||
// Class<?> clazz = new URLClassLoader(new URL[]{cases.toURI().toURL()}).loadClass(dbsetup.getAbsolutePath().substring(cases.getAbsolutePath().length()+1).replace("/", ".").replace("\\", ".").replace(".class", ""));
|
||||
// DatabaseSeeder seeder = (DatabaseSeeder) clazz.newInstance();
|
||||
// seeder.setup();
|
||||
for(File file : responses){
|
||||
StateCase state = new StateCase(file, tokenProvider);
|
||||
state.execute("127.0.0.1", config.getTcpPort());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class StateCase{
|
||||
|
||||
private static final Pattern TOKEN_PATTERN = Pattern.compile("^(.*)<token:(.*)>(.*)$");
|
||||
|
||||
private String path;
|
||||
private RequestType type;
|
||||
private List<String> headers;
|
||||
private String payload;
|
||||
|
||||
private int responseCode;
|
||||
private List<String> responseHeaders;
|
||||
private String responsePayload;
|
||||
|
||||
public StateCase(File file, Function<String, String> tokenProvider) throws Exception{
|
||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||
String line;
|
||||
String payloadBuffer;
|
||||
this.headers = new ArrayList<>();
|
||||
this.responseHeaders = new ArrayList<>();
|
||||
|
||||
this.path = reader.readLine();
|
||||
this.type = RequestType.valueOf(reader.readLine());
|
||||
|
||||
while(((line = reader.readLine() )!= null) && (!line.isEmpty())) headers.add(line);
|
||||
|
||||
payloadBuffer = "";
|
||||
while(((line = reader.readLine() )!= null) && (!line.isEmpty())) payloadBuffer+=line;
|
||||
this.payload = payloadBuffer;
|
||||
|
||||
this.responseCode = Integer.parseInt(reader.readLine());
|
||||
while(((line = reader.readLine() )!= null) && (!line.isEmpty())) responseHeaders.add(line);
|
||||
|
||||
payloadBuffer = "";
|
||||
while(((line = reader.readLine() )!= null) && (!line.isEmpty())) payloadBuffer+=line;
|
||||
this.responsePayload = payloadBuffer;
|
||||
|
||||
reader.close();
|
||||
|
||||
this.headers = Arrays.asList(this.headers.stream().<String>map((header) -> {
|
||||
Matcher matcher = TOKEN_PATTERN.matcher(header);
|
||||
return matcher.matches() ? matcher.group(1)+tokenProvider.apply(matcher.group(2))+matcher.group(3) : header;
|
||||
}).toArray((size) -> new String[size]));
|
||||
}
|
||||
|
||||
public void execute(String host, int port) throws Exception{
|
||||
Socket socket = new Socket(host, port);
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||
writer.write(this.type.toString()+" "+path+" HTTP/1.1\n");
|
||||
for(String header : headers) writer.write(header+"\n");
|
||||
writer.write("\n");
|
||||
if(payload != null) writer.write(payload+"\n");
|
||||
writer.flush();
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
int responseCode;
|
||||
Set<String> responseHeaders = new HashSet<>();
|
||||
String responseBody = "";
|
||||
|
||||
String line;
|
||||
responseCode = Integer.parseInt(reader.readLine().split("\\s+")[1]);
|
||||
while(((line = reader.readLine() )!= null) && (!line.isEmpty())) responseHeaders.add(line);
|
||||
|
||||
while(((line = reader.readLine() )!= null) && (!line.isEmpty())) responseBody+=line;
|
||||
|
||||
socket.close();
|
||||
|
||||
if(this.responseCode != responseCode) throw new AssertionFailedError("Excepted "+this.responseCode+" but have "+responseCode);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -2,6 +2,7 @@ package dev.peerat.backend.routes;
|
|||
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
@ -9,8 +10,7 @@ import org.junit.jupiter.api.TestInstance;
|
|||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||
|
||||
import dev.peerat.backend.Main;
|
||||
import dev.peerat.backend.WebClient;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
import dev.peerat.backend.webclient.WebClient;
|
||||
|
||||
@TestInstance(Lifecycle.PER_CLASS)
|
||||
public class TmpRoutesTests {
|
||||
|
@ -42,12 +42,12 @@ public class TmpRoutesTests {
|
|||
@Test
|
||||
void testOnDeployed(){
|
||||
try {
|
||||
JsonMap group = new JsonMap();
|
||||
group.set("name", "MyTest");
|
||||
group.set("chapter", 1);
|
||||
JSONObject group = new JSONObject();
|
||||
group.put("name", "MyTest");
|
||||
group.put("chapter", 1);
|
||||
|
||||
client.auth("JeffCheasey88", "TheoPueDesPieds");
|
||||
client.route("/groupCreate", "POST", group.toString());
|
||||
client.route("/groupCreate", "POST", group.toJSONString());
|
||||
|
||||
client.assertResponseCode(200);
|
||||
}catch(Exception e){
|
||||
|
|
|
@ -9,7 +9,7 @@ import org.junit.jupiter.api.TestInstance;
|
|||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||
|
||||
import dev.peerat.backend.Main;
|
||||
import dev.peerat.backend.WebClient;
|
||||
import dev.peerat.backend.webclient.WebClient;
|
||||
|
||||
@TestInstance(Lifecycle.PER_CLASS)
|
||||
public class TriggerTests {
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
package dev.peerat.backend.routes.states;
|
||||
|
||||
import dev.peerat.backend.routes.DatabaseSeeder;
|
||||
|
||||
public class AllEmptyTests implements DatabaseSeeder{
|
||||
|
||||
@Override
|
||||
public void setup() throws Exception{}
|
||||
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
package dev.peerat.backend.userstories;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.TestDatabaseRepository;
|
||||
|
||||
public class BaseUserStoriesTest {
|
||||
|
||||
private Configuration config;
|
||||
private TestDatabaseRepository repo;
|
||||
|
||||
public BaseUserStoriesTest(){}
|
||||
|
||||
public void init() throws Exception{
|
||||
this.config = new Configuration("config-test.txt");
|
||||
this.repo = new TestDatabaseRepository(config, new File("database-schem.sql"));
|
||||
|
||||
this.config.load();
|
||||
}
|
||||
|
||||
public Configuration getConfig(){
|
||||
return this.config;
|
||||
}
|
||||
|
||||
public TestDatabaseRepository getRepository(){
|
||||
return this.repo;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,83 +0,0 @@
|
|||
package dev.peerat.backend.userstories;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||
|
||||
import dev.peerat.backend.Main;
|
||||
import dev.peerat.backend.WebClient;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
@TestInstance(Lifecycle.PER_METHOD)
|
||||
public class GroupTests extends BaseUserStoriesTest{
|
||||
|
||||
private Thread server;
|
||||
private WebClient client;
|
||||
|
||||
@BeforeEach
|
||||
public void init() throws Exception{
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
|
||||
super.init();
|
||||
getRepository().init();
|
||||
getRepository().reset();
|
||||
|
||||
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);
|
||||
|
||||
// System.out.println(Password.hash("password").withArgon2().getResult());
|
||||
|
||||
try {
|
||||
client.auth("userTest", "password");
|
||||
client.assertResponseCode(200);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void stop(){
|
||||
server.interrupt();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createNormalGroup() throws Exception{
|
||||
JsonMap json = new JsonMap();
|
||||
json.set("name", "Group_test");
|
||||
json.set("chapter", 1);
|
||||
client.route("/groupCreate", "POST", json.toString());
|
||||
client.assertResponseCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void leaveNormalGroup() throws Exception{
|
||||
createNormalGroup();
|
||||
JsonMap json = new JsonMap();
|
||||
json.set("name", "Group_test");
|
||||
json.set("chapter", 1);
|
||||
client.route("/groupQuit", "POST", json.toString());
|
||||
client.assertResponseCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void joinNormalGroup() throws Exception{
|
||||
leaveNormalGroup();
|
||||
JsonMap json = new JsonMap();
|
||||
json.set("name", "Group_test");
|
||||
json.set("chapter", 1);
|
||||
client.route("/groupJoin", "POST", json.toString());
|
||||
client.assertResponseCode(200);
|
||||
}
|
||||
}
|
|
@ -1,79 +0,0 @@
|
|||
package dev.peerat.backend.userstories;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||
|
||||
import dev.peerat.backend.Main;
|
||||
import dev.peerat.backend.WebClient;
|
||||
|
||||
@TestInstance(Lifecycle.PER_METHOD)
|
||||
public class LoginTests extends BaseUserStoriesTest{
|
||||
|
||||
private Thread server;
|
||||
private WebClient client;
|
||||
|
||||
@BeforeEach
|
||||
public void init() throws Exception{
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
|
||||
super.init();
|
||||
getRepository().init();
|
||||
getRepository().reset();
|
||||
|
||||
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);
|
||||
|
||||
try {
|
||||
client.register("user", "password", "mail@peerat.dev", "firstname", "lastname", "description");
|
||||
client.assertResponseCode(200);
|
||||
client.disconnect();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void stop(){
|
||||
server.interrupt();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void normalLogin() throws Exception{
|
||||
client.auth("user", "password");
|
||||
client.assertResponseCode(200);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wrongPassword() throws Exception{
|
||||
client.auth("user", "password1");
|
||||
client.assertResponseCode(400);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wrongUsername() throws Exception{
|
||||
client.auth("user1", "password");
|
||||
client.assertResponseCode(400);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void alreadyLoggedin() throws Exception{
|
||||
client.auth("user", "password");
|
||||
client.assertResponseCode(200);
|
||||
client.auth("user", "password");
|
||||
client.assertResponseCode(403);
|
||||
}
|
||||
}
|
|
@ -1,89 +0,0 @@
|
|||
package dev.peerat.backend.userstories;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||
|
||||
import dev.peerat.backend.Main;
|
||||
import dev.peerat.backend.WebClient;
|
||||
|
||||
@TestInstance(Lifecycle.PER_METHOD)
|
||||
public class RegisterTests extends BaseUserStoriesTest{
|
||||
|
||||
private Thread server;
|
||||
private WebClient client;
|
||||
|
||||
@BeforeEach
|
||||
public void init() throws Exception{
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
|
||||
super.init();
|
||||
getRepository().init();
|
||||
getRepository().reset();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void stop(){
|
||||
server.interrupt();
|
||||
getRepository().close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void normalRegister() throws Exception{
|
||||
client.register("test", "test", "test@peerat.dev", "te", "st", "my desc");
|
||||
client.assertResponseCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pseudoAlreadyUse() throws Exception{
|
||||
client.register("test", "test", "test@peerat.dev", "te", "st", "my desc");
|
||||
client.assertResponseCode(200);
|
||||
client.disconnect();
|
||||
client.register("test", "test", "test1@peerat.dev", "te", "st", "my desc");
|
||||
client.assertResponseCode(400);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emailAlreadyUse() throws Exception{
|
||||
client.register("test", "test", "test@peerat.dev", "te", "st", "my desc");
|
||||
client.assertResponseCode(200);
|
||||
client.disconnect();
|
||||
client.register("test1", "test", "test@peerat.dev", "te", "st", "my desc");
|
||||
client.assertResponseCode(400);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyField() throws Exception{
|
||||
client.register("","","",",","","");
|
||||
client.assertResponseCode(400);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lostField() throws Exception{
|
||||
client.route("/register", "POST", "{}");
|
||||
client.assertResponseCode(400);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void alreadyLoggedin() throws Exception{
|
||||
client.register("test", "test", "test@peerat.dev", "te", "st", "my desc");
|
||||
client.assertResponseCode(200);
|
||||
client.register("test1", "test", "test@peerat.dev", "te", "st", "my desc");
|
||||
client.assertResponseCode(403);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package dev.peerat.backend;
|
||||
package dev.peerat.backend.webclient;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
|
@ -9,9 +9,10 @@ import java.util.List;
|
|||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import dev.peerat.framework.HttpReader;
|
||||
import dev.peerat.framework.HttpWriter;
|
||||
import dev.peerat.framework.utils.json.JsonMap;
|
||||
|
||||
public class WebClient {
|
||||
|
||||
|
@ -48,10 +49,10 @@ public class WebClient {
|
|||
}
|
||||
|
||||
public void auth(String user, String password) throws Exception{
|
||||
JsonMap login = new JsonMap();
|
||||
login.set("pseudo", user);
|
||||
login.set("passwd", password);
|
||||
route("/login", "POST", login.toString());
|
||||
JSONObject login = new JSONObject();
|
||||
login.put("pseudo", user);
|
||||
login.put("passwd", password);
|
||||
route("/login", "POST", login.toJSONString());
|
||||
System.out.println("["+host+"] /login "+login);
|
||||
|
||||
for(String line : this.headers){
|
||||
|
@ -66,17 +67,17 @@ public class WebClient {
|
|||
}
|
||||
|
||||
public void register(String user, String password, String email, String firstname, String lastname, String description) throws Exception{
|
||||
JsonMap register = new JsonMap();
|
||||
register.set("pseudo", user);
|
||||
register.set("passwd", password);
|
||||
register.set("email", email);
|
||||
register.set("firstname", firstname);
|
||||
register.set("lastname", lastname);
|
||||
register.set("description", description);
|
||||
register.set("sgroup", "");
|
||||
register.set("avatar", "");
|
||||
JSONObject register = new JSONObject();
|
||||
register.put("pseudo", user);
|
||||
register.put("passwd", password);
|
||||
register.put("email", email);
|
||||
register.put("firstname", firstname);
|
||||
register.put("lastname", lastname);
|
||||
register.put("description", description);
|
||||
register.put("sgroup", "");
|
||||
register.put("avatar", "");
|
||||
|
||||
route("/register", "POST", register.toString());
|
||||
route("/register", "POST", register.toJSONString());
|
||||
System.out.println("["+host+"] /register "+register);
|
||||
for(String line : this.headers){
|
||||
Matcher matcher = AUTORIZATION.matcher(line);
|
||||
|
@ -98,10 +99,6 @@ public class WebClient {
|
|||
this.writer.write(type+" "+route+" HTTP/1.1\n");
|
||||
if(this.token != null) this.writer.write("Authorization: Bearer "+this.token+"\n");
|
||||
for(String send : this.sendHeaders) this.writer.write(send+"\n");
|
||||
int length = 0;
|
||||
for(String value : content) length+=value.length();
|
||||
length+=content.length-1;
|
||||
this.writer.write("Content-length: "+length+"\n");
|
||||
|
||||
this.writer.write("\n");
|
||||
for(String value : content) this.writer.write(value+"\n");
|
Loading…
Add table
Reference in a new issue