Compare commits
11 commits
Author | SHA1 | Date | |
---|---|---|---|
27a15a7ff0 | |||
03e64abf9c | |||
4a499e3540 | |||
4df8f2a37b | |||
24dc025507 | |||
9e2a4735d3 | |||
e3b18ace64 | |||
ebabcf4d2c | |||
14b12634f8 | |||
5fde1c1747 | |||
c720d1e542 |
37 changed files with 934 additions and 1008 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -2,6 +2,7 @@
|
||||||
bin/
|
bin/
|
||||||
.project
|
.project
|
||||||
config.txt
|
config.txt
|
||||||
|
config-test.txt
|
||||||
dist/
|
dist/
|
||||||
testApi/
|
testApi/
|
||||||
.apt_generated/*
|
.apt_generated/*
|
||||||
|
|
126
database-schem.sql
Normal file
126
database-schem.sql
Normal file
|
@ -0,0 +1,126 @@
|
||||||
|
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;
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ import java.io.FileReader;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
public class Configuration {
|
public class Configuration{
|
||||||
|
|
||||||
private String db_host;
|
private String db_host;
|
||||||
private int db_port;
|
private int db_port;
|
||||||
|
@ -30,32 +30,32 @@ public class Configuration {
|
||||||
private int groupJoinMinutes;
|
private int groupJoinMinutes;
|
||||||
private String groupQuitMinutes;
|
private String groupQuitMinutes;
|
||||||
|
|
||||||
private File file;
|
private String git_token;
|
||||||
|
|
||||||
public Configuration(String path) {
|
private File _file;
|
||||||
this.file = new File(path);
|
|
||||||
System.out.println("Config on " + file.getAbsolutePath());
|
public Configuration(String path){
|
||||||
|
this._file = new File(path);
|
||||||
|
System.out.println("Config on " + _file.getAbsolutePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void load() throws Exception {
|
public void load() throws Exception{
|
||||||
if (!this.file.exists())
|
if(!this._file.exists()) return;
|
||||||
return;
|
BufferedReader reader = new BufferedReader(new FileReader(this._file));
|
||||||
BufferedReader reader = new BufferedReader(new FileReader(this.file));
|
|
||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null) {
|
while((line = reader.readLine()) != null){
|
||||||
String[] split = line.split("=");
|
String[] split = line.split("=");
|
||||||
Field field = getClass().getDeclaredField(split[0]);
|
Field field = getClass().getDeclaredField(split[0]);
|
||||||
if (field == null)
|
if(field == null) continue;
|
||||||
continue;
|
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
injectValue(field, split[1]);
|
injectValue(field, split[1]);
|
||||||
}
|
}
|
||||||
reader.close();
|
reader.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void injectValue(Field field, String value) throws IllegalAccessException {
|
private void injectValue(Field field, String value) throws IllegalAccessException{
|
||||||
if (field.getType().isPrimitive()) {
|
if(field.getType().isPrimitive()){
|
||||||
switch (field.getType().getName()) {
|
switch(field.getType().getName()){
|
||||||
case "boolean":
|
case "boolean":
|
||||||
field.setBoolean(this, Boolean.parseBoolean(value));
|
field.setBoolean(this, Boolean.parseBoolean(value));
|
||||||
break;
|
break;
|
||||||
|
@ -85,26 +85,24 @@ public class Configuration {
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (field.getType().equals(String.class)) {
|
if(field.getType().equals(String.class)){
|
||||||
field.set(this, value);
|
field.set(this, value);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
throw new IllegalArgumentException(value);
|
throw new IllegalArgumentException(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save() throws Exception {
|
public void save() throws Exception{
|
||||||
if (!file.exists()) {
|
if(!_file.exists()){
|
||||||
File parent = file.getParentFile();
|
File parent = _file.getParentFile();
|
||||||
if (!parent.exists())
|
if(!parent.exists()) parent.mkdirs();
|
||||||
parent.mkdirs();
|
_file.createNewFile();
|
||||||
file.createNewFile();
|
|
||||||
}
|
}
|
||||||
Field[] fields = getClass().getDeclaredFields();
|
Field[] fields = getClass().getDeclaredFields();
|
||||||
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
|
BufferedWriter writer = new BufferedWriter(new FileWriter(_file));
|
||||||
for (Field field : fields) {
|
for(Field field : fields){
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
if (field.getName().startsWith("_"))
|
if(field.getName().startsWith("_")) continue;
|
||||||
continue;
|
|
||||||
Object value = field.get(this);
|
Object value = field.get(this);
|
||||||
writer.write(field.getName() + "=" + value);
|
writer.write(field.getName() + "=" + value);
|
||||||
}
|
}
|
||||||
|
@ -112,53 +110,52 @@ public class Configuration {
|
||||||
writer.close();
|
writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDbHost() {
|
public String getDbHost(){
|
||||||
return this.db_host;
|
return this.db_host;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getDbPort() {
|
public int getDbPort(){
|
||||||
return this.db_port;
|
return this.db_port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDbUser() {
|
public String getDbUser(){
|
||||||
return this.db_user;
|
return this.db_user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDbDatabase() {
|
public String getDbDatabase(){
|
||||||
return this.db_database;
|
return this.db_database;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDbPassword() {
|
public String getDbPassword(){
|
||||||
return this.db_password;
|
return this.db_password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSslKeystore() {
|
public String getSslKeystore(){
|
||||||
return this.ssl_keystore;
|
return this.ssl_keystore;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTokenIssuer() {
|
public String getTokenIssuer(){
|
||||||
return this.token_issuer;
|
return this.token_issuer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTokenExpiration() {
|
public int getTokenExpiration(){
|
||||||
return this.token_expiration;
|
return this.token_expiration;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSslKeystorePasswd() {
|
public String getSslKeystorePasswd(){
|
||||||
return this.ssl_keystorePasswd;
|
return this.ssl_keystorePasswd;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTcpPort() {
|
public int getTcpPort(){
|
||||||
return this.tcp_port;
|
return this.tcp_port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean useSsl() {
|
public boolean useSsl(){
|
||||||
return this.use_ssl;
|
return this.use_ssl;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUsersFiles() {
|
public String getUsersFiles(){
|
||||||
if (users_files == null || users_files.trim().isEmpty())
|
if(users_files == null || users_files.trim().isEmpty()) users_files = "/tmp/users_files";
|
||||||
users_files = "/tmp/users_files";
|
|
||||||
return users_files;
|
return users_files;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,4 +170,8 @@ public class Configuration {
|
||||||
public String getGroupQuitMinutes(){
|
public String getGroupQuitMinutes(){
|
||||||
return this.groupQuitMinutes;
|
return this.groupQuitMinutes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getGitToken(){
|
||||||
|
return this.git_token;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -26,16 +26,20 @@ import be.jeffcheasey88.peeratcode.routes.ChapterElement;
|
||||||
import be.jeffcheasey88.peeratcode.routes.ChapterList;
|
import be.jeffcheasey88.peeratcode.routes.ChapterList;
|
||||||
import be.jeffcheasey88.peeratcode.routes.DynamicLeaderboard;
|
import be.jeffcheasey88.peeratcode.routes.DynamicLeaderboard;
|
||||||
import be.jeffcheasey88.peeratcode.routes.Leaderboard;
|
import be.jeffcheasey88.peeratcode.routes.Leaderboard;
|
||||||
import be.jeffcheasey88.peeratcode.routes.Login;
|
|
||||||
import be.jeffcheasey88.peeratcode.routes.PlayerDetails;
|
import be.jeffcheasey88.peeratcode.routes.PlayerDetails;
|
||||||
import be.jeffcheasey88.peeratcode.routes.PuzzleElement;
|
import be.jeffcheasey88.peeratcode.routes.PuzzleElement;
|
||||||
import be.jeffcheasey88.peeratcode.routes.PuzzleResponse;
|
import be.jeffcheasey88.peeratcode.routes.PuzzleResponse;
|
||||||
import be.jeffcheasey88.peeratcode.routes.Register;
|
|
||||||
import be.jeffcheasey88.peeratcode.routes.Result;
|
import be.jeffcheasey88.peeratcode.routes.Result;
|
||||||
|
import be.jeffcheasey88.peeratcode.routes.admins.DynamicLogs;
|
||||||
import be.jeffcheasey88.peeratcode.routes.groups.GroupCreate;
|
import be.jeffcheasey88.peeratcode.routes.groups.GroupCreate;
|
||||||
import be.jeffcheasey88.peeratcode.routes.groups.GroupJoin;
|
import be.jeffcheasey88.peeratcode.routes.groups.GroupJoin;
|
||||||
import be.jeffcheasey88.peeratcode.routes.groups.GroupList;
|
import be.jeffcheasey88.peeratcode.routes.groups.GroupList;
|
||||||
import be.jeffcheasey88.peeratcode.routes.groups.GroupQuit;
|
import be.jeffcheasey88.peeratcode.routes.groups.GroupQuit;
|
||||||
|
import be.jeffcheasey88.peeratcode.routes.users.ChangePassword;
|
||||||
|
import be.jeffcheasey88.peeratcode.routes.users.ForgotPassword;
|
||||||
|
import be.jeffcheasey88.peeratcode.routes.users.Login;
|
||||||
|
import be.jeffcheasey88.peeratcode.routes.users.ProfileSettings;
|
||||||
|
import be.jeffcheasey88.peeratcode.routes.users.Register;
|
||||||
|
|
||||||
public class Main{
|
public class Main{
|
||||||
public static void main(String[] args) throws Exception{
|
public static void main(String[] args) throws Exception{
|
||||||
|
@ -50,8 +54,6 @@ public class Main{
|
||||||
router.setDefault((matcher, user, reader, writer) -> {
|
router.setDefault((matcher, user, reader, writer) -> {
|
||||||
writer.response(404, "Access-Control-Allow-Origin: *");
|
writer.response(404, "Access-Control-Allow-Origin: *");
|
||||||
writer.write("404 not Found.\n");
|
writer.write("404 not Found.\n");
|
||||||
writer.flush();
|
|
||||||
writer.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
router.register(new Response(){
|
router.register(new Response(){
|
||||||
|
@ -72,30 +74,35 @@ public class Main{
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void initRoutes(Router router, Configuration config){
|
private static void initRoutes(Router router, Configuration config){
|
||||||
router.register(new ChapterElement(router.getDataBase()));
|
DatabaseRepository repo = router.getDataBase();
|
||||||
router.register(new ChapterList(router.getDataBase()));
|
router.register(new Register(repo, router, config.getUsersFiles(), config.getGitToken()));
|
||||||
router.register(new PuzzleElement(router.getDataBase()));
|
router.register(new Login(repo, router));
|
||||||
router.register(new Register(router.getDataBase(), router, config.getUsersFiles()));
|
router.register(new ProfileSettings(repo));
|
||||||
router.register(new Login(router.getDataBase(), router));
|
router.register(new ChangePassword(repo));
|
||||||
router.register(new Result(router.getDataBase()));
|
router.register(new ForgotPassword());
|
||||||
router.register(new Leaderboard(router.getDataBase()));
|
|
||||||
router.register(new PlayerDetails(router.getDataBase()));
|
router.register(new DynamicLogs(repo, new Locker<>())); //to change
|
||||||
router.register(new BadgeDetails(router.getDataBase()));
|
|
||||||
|
router.register(new ChapterElement(repo));
|
||||||
|
router.register(new ChapterList(repo));
|
||||||
|
router.register(new PuzzleElement(repo));
|
||||||
|
router.register(new Result(repo));
|
||||||
|
router.register(new Leaderboard(repo));
|
||||||
|
router.register(new PlayerDetails(repo));
|
||||||
|
router.register(new BadgeDetails(repo));
|
||||||
|
|
||||||
Locker<Group> groupLock = new Locker<>();
|
Locker<Group> groupLock = new Locker<>();
|
||||||
router.register(new GroupCreate(router.getDataBase(), groupLock, config.getGroupJoinMinutes()));
|
Locker<Completion> leaderboard = new Locker<>();
|
||||||
|
|
||||||
DynamicLeaderboard dlb = new DynamicLeaderboard(router.getDataBase());
|
router.register(new DynamicLeaderboard(repo, leaderboard));
|
||||||
router.register(dlb);
|
router.register(new PuzzleResponse(repo, config.getUsersFiles(), leaderboard));
|
||||||
|
|
||||||
Locker<Completion> leaderboard = dlb.getLocker();
|
router.register(new GroupCreate(repo, groupLock, config.getGroupJoinMinutes()));
|
||||||
|
router.register(new GroupList(repo));
|
||||||
|
router.register(new GroupJoin(repo, config.getGroupJoinMinutes(), config.getGroupQuitMinutes(), leaderboard));
|
||||||
|
router.register(new GroupQuit(repo, config.getGroupJoinMinutes(), leaderboard));
|
||||||
|
|
||||||
router.register(new PuzzleResponse(router.getDataBase(), config.getUsersFiles(), leaderboard));
|
// Bot bot = new Bot(config, repo, groupLock);
|
||||||
router.register(new GroupList(router.getDataBase()));
|
|
||||||
router.register(new GroupJoin(router.getDataBase(), config.getGroupJoinMinutes(), config.getGroupQuitMinutes(), leaderboard));
|
|
||||||
router.register(new GroupQuit(router.getDataBase(), config.getGroupJoinMinutes(), leaderboard));
|
|
||||||
|
|
||||||
// Bot bot = new Bot(config, router.getDataBase(), groupLock);
|
|
||||||
// bot.start();
|
// bot.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package be.jeffcheasey88.peeratcode.model;
|
package be.jeffcheasey88.peeratcode.model;
|
||||||
|
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Chapter {
|
public class Chapter {
|
||||||
|
@ -34,6 +35,14 @@ public class Chapter {
|
||||||
this.puzzles = puzzles;
|
this.puzzles = puzzles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isInCurrentTime(){
|
||||||
|
LocalDateTime now = LocalDateTime.now();
|
||||||
|
boolean show = true;
|
||||||
|
if(startDate != null) show &= now.isAfter(startDate.toLocalDateTime());
|
||||||
|
if(endDate != null) show &= now.isBefore(endDate.toLocalDateTime());
|
||||||
|
return show;
|
||||||
|
}
|
||||||
|
|
||||||
public Timestamp getStartDate() {
|
public Timestamp getStartDate() {
|
||||||
return startDate;
|
return startDate;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ import java.util.Set;
|
||||||
import org.json.simple.JSONArray;
|
import org.json.simple.JSONArray;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
public class Player implements Comparable<Player> {
|
public class Player implements Comparable<Player>{
|
||||||
private String pseudo;
|
private String pseudo;
|
||||||
private String email;
|
private String email;
|
||||||
private String firstname;
|
private String firstname;
|
||||||
|
@ -37,6 +37,10 @@ public class Player implements Comparable<Player> {
|
||||||
email = ""; // TO make compareTo and equals works as usual
|
email = ""; // TO make compareTo and equals works as usual
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setPseudo(String pseudo){
|
||||||
|
this.pseudo = pseudo;
|
||||||
|
}
|
||||||
|
|
||||||
public String getPseudo() {
|
public String getPseudo() {
|
||||||
return this.pseudo;
|
return this.pseudo;
|
||||||
}
|
}
|
||||||
|
@ -180,8 +184,8 @@ public class Player implements Comparable<Player> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode(){
|
||||||
return Objects.hash(email, pseudo);
|
return Objects.hash(email);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -193,6 +197,6 @@ public class Player implements Comparable<Player> {
|
||||||
if (getClass() != obj.getClass())
|
if (getClass() != obj.getClass())
|
||||||
return false;
|
return false;
|
||||||
Player other = (Player) obj;
|
Player other = (Player) obj;
|
||||||
return Objects.equals(email, other.email) && Objects.equals(pseudo, other.pseudo);
|
return Objects.equals(email, other.email);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,110 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.parser.java;
|
|
||||||
|
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
public class Class {
|
|
||||||
|
|
||||||
private static Pattern PATTERN = Pattern.compile("^(\\s*([^\\{]*)\\{(.*)\\})\\s*$");
|
|
||||||
|
|
||||||
private int modifier;
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
private List<Variable> vars;
|
|
||||||
private List<Function> functions;
|
|
||||||
|
|
||||||
public Class(){}
|
|
||||||
|
|
||||||
public int parse(String content) throws Exception{
|
|
||||||
Matcher matcher = PATTERN.matcher(content);
|
|
||||||
matcher.matches();
|
|
||||||
|
|
||||||
String[] split = matcher.group(2).split("\\s+");
|
|
||||||
for(int i = 0; i < split.length-1; i++){
|
|
||||||
this.modifier+=JavaParser.getModifier(split[i]);
|
|
||||||
}
|
|
||||||
this.name = split[split.length-1];
|
|
||||||
|
|
||||||
this.vars = new ArrayList<>();
|
|
||||||
this.functions = new ArrayList<>();
|
|
||||||
|
|
||||||
content = matcher.group(3);
|
|
||||||
Pattern empty = Pattern.compile("^\\s*$");
|
|
||||||
while(!(empty.matcher(content).matches())){
|
|
||||||
int quotes = indexOf(content,";");
|
|
||||||
int braces = indexOf(content,"\\{");
|
|
||||||
int equals = indexOf(content,"=");
|
|
||||||
if(quotes < braces && quotes < equals){
|
|
||||||
boolean quote = false;
|
|
||||||
Variable last = null;
|
|
||||||
do {
|
|
||||||
Variable variable = (last == null) ? new Variable() : new Variable(last.getModifier(), last.getType());
|
|
||||||
int index = variable.parse(content);
|
|
||||||
this.vars.add(variable);
|
|
||||||
content = content.substring(index);
|
|
||||||
quote = content.startsWith(",");
|
|
||||||
if(quote) {
|
|
||||||
content = content.substring(1);
|
|
||||||
last = variable;
|
|
||||||
}
|
|
||||||
}while(quote);
|
|
||||||
}else if(equals < braces){
|
|
||||||
//variable with value
|
|
||||||
boolean quote = false;
|
|
||||||
Variable last = null;
|
|
||||||
do {
|
|
||||||
Variable variable = (last == null) ? new Variable() : new Variable(last.getModifier(), last.getType());
|
|
||||||
int index = variable.parse(content);
|
|
||||||
this.vars.add(variable);
|
|
||||||
content = content.substring(index);
|
|
||||||
quote = content.startsWith(",");
|
|
||||||
if(quote) {
|
|
||||||
content = content.substring(1);
|
|
||||||
last = variable;
|
|
||||||
}else if(indexOf(content, "=") < indexOf(content, ";")){
|
|
||||||
Operation operation = new Operation();
|
|
||||||
index = operation.parse(content);
|
|
||||||
content = content.substring(index);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}while(quote);
|
|
||||||
}else{
|
|
||||||
System.out.println("Function "+content);
|
|
||||||
Function func = new Function();
|
|
||||||
int index = func.parse(content);
|
|
||||||
this.functions.add(func);
|
|
||||||
content = content.substring(index);
|
|
||||||
System.out.println("End "+content);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return matcher.group(1).length();
|
|
||||||
}
|
|
||||||
|
|
||||||
private int indexOf(String value, String target){
|
|
||||||
return value.split(target)[0].length();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getModifier(){
|
|
||||||
return this.modifier;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName(){
|
|
||||||
return this.name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Variable> getVariables(){
|
|
||||||
return this.vars;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void show(){
|
|
||||||
System.out.println(Modifier.toString(modifier)+" "+this.name+"{");
|
|
||||||
for(Variable v : this.vars) v.show(1);
|
|
||||||
System.out.println();
|
|
||||||
for(Function f : this.functions) f.show(1);
|
|
||||||
System.out.println("}");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,94 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.parser.java;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
public class CleanerPool {
|
|
||||||
|
|
||||||
private static String CONSTANT_REPLACER_STRING = "$STRING_STATEMENT_CONSTANT_";
|
|
||||||
private static String CONSTANT_REPLACER_CHAR = "$CHAR_STATEMENT_CONSTANT_";
|
|
||||||
private static String CONSTANT_REPLACER_GENERIC = "$GENERIC_STATEMENT_CONSTANT_";
|
|
||||||
|
|
||||||
private List<String> constants;
|
|
||||||
|
|
||||||
private CleanerPool(){
|
|
||||||
this.constants = new ArrayList<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String clean(String statement){
|
|
||||||
char[] chars = statement.toCharArray();
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
for(int i = 0; i < chars.length; i++){
|
|
||||||
char current = chars[i];
|
|
||||||
if(current== '"'){
|
|
||||||
int constantPos = this.constants.size();
|
|
||||||
String constant = cutConstant(chars, i);
|
|
||||||
i+=constant.length()+1;
|
|
||||||
builder.append(CONSTANT_REPLACER_STRING+constantPos);
|
|
||||||
this.constants.add(constant);
|
|
||||||
}else{
|
|
||||||
builder.append(current);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for(String s : constants){
|
|
||||||
System.out.println("CONSTANT="+s);
|
|
||||||
}
|
|
||||||
return builder.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isConstant(String region){
|
|
||||||
return region.startsWith(CONSTANT_REPLACER_STRING);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getConstant(String replacer){
|
|
||||||
if(!replacer.startsWith(CONSTANT_REPLACER_STRING)) return null;
|
|
||||||
return this.constants.get(Integer.parseInt(replacer.replace(CONSTANT_REPLACER_STRING,"")));
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> getConstants(){
|
|
||||||
return this.constants;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Pattern parenthesisPattern = Pattern.compile("^\\$SQL_STATEMENT_PARENTHESIS_([0-9]*$)");
|
|
||||||
|
|
||||||
public boolean isParenthesis(String region){
|
|
||||||
return parenthesisPattern.matcher(region).matches();
|
|
||||||
}
|
|
||||||
|
|
||||||
private String cutConstant(char[] chars, int pos){
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
for(int i = pos+1; i < chars.length; i++){
|
|
||||||
char current = chars[i];
|
|
||||||
if(current == '"'){
|
|
||||||
if(current == '\\'){ //toChange
|
|
||||||
builder.append(current);
|
|
||||||
}else break;
|
|
||||||
}else{
|
|
||||||
builder.append(current);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return builder.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static interface Cutter{
|
|
||||||
|
|
||||||
String execute(String value, List<String> constants, String pattern);
|
|
||||||
|
|
||||||
default String cutOpenable(String value, List<String> constants, String pattern, char open, char close){
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
|
|
||||||
for(char c : value.toCharArray()){
|
|
||||||
if(c == open){
|
|
||||||
|
|
||||||
}else if(c == close){
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return builder.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,85 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.parser.java;
|
|
||||||
|
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
public class Function {
|
|
||||||
|
|
||||||
private static Pattern PATTERN = Pattern.compile("^(\\s*([^(]*)\\(([^)]*)\\)\\s*([^{]*)\\{)(.*)$");
|
|
||||||
|
|
||||||
private int modifier;
|
|
||||||
private String name;
|
|
||||||
private String exceptions;
|
|
||||||
private String parameters;
|
|
||||||
|
|
||||||
private List<Function> functions;
|
|
||||||
private List<Operation> operations;
|
|
||||||
|
|
||||||
public Function(){
|
|
||||||
this.functions = new ArrayList<>();
|
|
||||||
this.operations = new ArrayList<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int parse(String content) throws Exception{
|
|
||||||
Matcher matcher = PATTERN.matcher(content);
|
|
||||||
matcher.matches();
|
|
||||||
|
|
||||||
String[] split = matcher.group(2).split("\\s+");
|
|
||||||
for(int i = 0; i < split.length-2; i++){
|
|
||||||
this.modifier+=JavaParser.getModifier(split[i]);
|
|
||||||
}
|
|
||||||
this.name = split[split.length-1];
|
|
||||||
this.parameters = matcher.group(3);
|
|
||||||
this.exceptions = matcher.group(4);
|
|
||||||
|
|
||||||
String body = matcher.group(5);
|
|
||||||
int offset = 0;
|
|
||||||
int index = 0;
|
|
||||||
do {
|
|
||||||
int end = body.indexOf('}');
|
|
||||||
int braces = body.indexOf('{');
|
|
||||||
int quotes = body.indexOf(';');
|
|
||||||
|
|
||||||
if((end < 0) || (end < braces && end < quotes)){
|
|
||||||
if(end > 0) offset+=end;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(braces < 0 && quotes < 0){
|
|
||||||
if(end > 0) offset+=end;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(braces >= 0 && braces < quotes){
|
|
||||||
Function func = new Function();
|
|
||||||
index = func.parse(body.substring(0, end+1));
|
|
||||||
this.functions.add(func);
|
|
||||||
}else{
|
|
||||||
Operation op = new Operation();
|
|
||||||
index = op.parse(body.substring(0, end+1));
|
|
||||||
this.operations.add(op);
|
|
||||||
}
|
|
||||||
offset+=index+1;
|
|
||||||
body = body.substring(index);
|
|
||||||
}while(offset > -1);
|
|
||||||
return matcher.group(1).length()+offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void show(int tab){
|
|
||||||
String start = "";
|
|
||||||
for(int i = 0; i < tab; i++) start+="\t";
|
|
||||||
System.out.println(start+Modifier.toString(modifier)+" "+name+"("+parameters+") "+exceptions+" {");
|
|
||||||
for(Operation o : this.operations) o.show(tab+1);
|
|
||||||
System.out.println();
|
|
||||||
for(Function f : this.functions) f.show(tab+1);
|
|
||||||
System.out.println(start+"}");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString(){
|
|
||||||
return "Function[name="+name+",param="+parameters+",exception="+exceptions+"]";
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,28 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.parser.java;
|
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
public class Import {
|
|
||||||
|
|
||||||
private static Pattern PATTERN = Pattern.compile("^\\s*(import\\s+([^;]*);).*$");
|
|
||||||
|
|
||||||
public static boolean isImport(String content){
|
|
||||||
return PATTERN.matcher(content).matches();
|
|
||||||
}
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
public Import(){}
|
|
||||||
|
|
||||||
public int parse(String content) throws Exception{
|
|
||||||
Matcher matcher = PATTERN.matcher(content);
|
|
||||||
matcher.matches();
|
|
||||||
this.name = matcher.group(2);
|
|
||||||
return matcher.group(1).length();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName(){
|
|
||||||
return this.name;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,96 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.parser.java;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileReader;
|
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class JavaParser {
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\be\\jeffcheasey88\\peeratcode\\parser\\java\\Import.java");
|
|
||||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
|
||||||
|
|
||||||
JavaParser parser = new JavaParser(reader);
|
|
||||||
parser.parse();
|
|
||||||
System.out.println("SHOW-----------------");
|
|
||||||
parser.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
private Package pack;
|
|
||||||
private List<Import> imports;
|
|
||||||
private Class clazz;
|
|
||||||
|
|
||||||
private BufferedReader reader;
|
|
||||||
|
|
||||||
public JavaParser(BufferedReader reader){
|
|
||||||
this.reader = reader;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void parse() throws Exception{
|
|
||||||
String content = "";
|
|
||||||
int index;
|
|
||||||
|
|
||||||
String line;
|
|
||||||
while((line = reader.readLine()) != null) content+=line;
|
|
||||||
|
|
||||||
// content = CleanerPool.getterToDelete.clean(content);
|
|
||||||
|
|
||||||
this.pack = new Package();
|
|
||||||
index = this.pack.parse(content);
|
|
||||||
content = content.substring(index);
|
|
||||||
|
|
||||||
this.imports = new ArrayList<>();
|
|
||||||
while(Import.isImport(content)){
|
|
||||||
Import imp = new Import();
|
|
||||||
index = imp.parse(content);
|
|
||||||
this.imports.add(imp);
|
|
||||||
content = content.substring(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.clazz = new Class();
|
|
||||||
index = this.clazz.parse(content);
|
|
||||||
content = content.substring(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Package getPackage(){
|
|
||||||
return this.pack;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Import> getImports(){
|
|
||||||
return this.imports;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Class getClazz(){
|
|
||||||
return this.clazz;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void show(){
|
|
||||||
System.out.println("package "+this.pack.getName()+";");
|
|
||||||
System.out.println();
|
|
||||||
for(Import i : this.imports) System.out.println("import "+i.getName()+";");
|
|
||||||
System.out.println();
|
|
||||||
this.clazz.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int getModifier(String modifier){
|
|
||||||
switch(modifier){
|
|
||||||
case "public": return Modifier.PUBLIC;
|
|
||||||
case "private": return Modifier.PRIVATE;
|
|
||||||
case "protected": return Modifier.PROTECTED;
|
|
||||||
case "static": return Modifier.STATIC;
|
|
||||||
case "final": return Modifier.FINAL;
|
|
||||||
case "synchronized": return Modifier.SYNCHRONIZED;
|
|
||||||
case "volatile": return Modifier.VOLATILE;
|
|
||||||
case "transient": return Modifier.TRANSIENT;
|
|
||||||
case "native": return Modifier.NATIVE;
|
|
||||||
case "abstract": return Modifier.ABSTRACT;
|
|
||||||
case "strictfp": return Modifier.STRICT;
|
|
||||||
default: break;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,29 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.parser.java;
|
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
public class Operation {
|
|
||||||
|
|
||||||
private static Pattern VARIABLE_PATTERN = Pattern.compile("^(\\s*([^;]*)).*$");
|
|
||||||
|
|
||||||
private String tmp;
|
|
||||||
|
|
||||||
public Operation(){}
|
|
||||||
|
|
||||||
public int parse(String content) throws Exception{
|
|
||||||
Matcher matcher = VARIABLE_PATTERN.matcher(content);
|
|
||||||
if(matcher.matches()){
|
|
||||||
this.tmp = matcher.group(2);
|
|
||||||
return matcher.group(1).length()+1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void show(int tab){
|
|
||||||
String start = "";
|
|
||||||
for(int i = 0; i < tab; i++) start+="\t";
|
|
||||||
System.out.println(start+tmp+";");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,24 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.parser.java;
|
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
public class Package {
|
|
||||||
|
|
||||||
private static Pattern PATTERN = Pattern.compile("^(\\s*package\\s+([^;]*);).*$");
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
public Package(){}
|
|
||||||
|
|
||||||
public int parse(String content) throws Exception{
|
|
||||||
Matcher matcher = PATTERN.matcher(content);
|
|
||||||
matcher.matches();
|
|
||||||
this.name = matcher.group(2);
|
|
||||||
return matcher.group(1).length();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName(){
|
|
||||||
return this.name;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,127 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.parser.java;
|
|
||||||
|
|
||||||
import java.lang.reflect.Modifier;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
public class Variable {
|
|
||||||
|
|
||||||
private static Pattern PATTERN = Pattern.compile("^(\\s*)(.*)$");
|
|
||||||
|
|
||||||
private int modifier;
|
|
||||||
private String name;
|
|
||||||
private String type;
|
|
||||||
private Variable value;
|
|
||||||
|
|
||||||
public Variable(){}
|
|
||||||
|
|
||||||
public Variable(int modifier, String type){
|
|
||||||
this.modifier = modifier;
|
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int parse(String content) throws Exception{
|
|
||||||
Matcher matcher = PATTERN.matcher(content);
|
|
||||||
matcher.matches();
|
|
||||||
|
|
||||||
int offset = matcher.group(1).length();
|
|
||||||
|
|
||||||
String body = matcher.group(2);
|
|
||||||
int equals = indexOf(body, "=");
|
|
||||||
int quote = indexOf(body,",");
|
|
||||||
int quotes = indexOf(body, ";");
|
|
||||||
int min = Math.min(quote, quotes);
|
|
||||||
body = body.substring(0, min);
|
|
||||||
|
|
||||||
if(equals < quote && equals < quotes){
|
|
||||||
assigment(body);
|
|
||||||
}else{
|
|
||||||
onlyDefine(body);
|
|
||||||
}
|
|
||||||
|
|
||||||
return offset+min;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void assigment(String content){
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onlyDefine(String content){
|
|
||||||
content = generiqueTypes(content);
|
|
||||||
System.out.println(content);
|
|
||||||
String[] values = content.split("\\s+");
|
|
||||||
for(String value : values){
|
|
||||||
int modifier = JavaParser.getModifier(value);
|
|
||||||
if(modifier > 0){
|
|
||||||
this.modifier+=modifier;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if(this.type == null){
|
|
||||||
this.type = value;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
this.name = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private String generiqueTypes(String content){
|
|
||||||
System.out.println(content);
|
|
||||||
String result = "";
|
|
||||||
int opened = 0;
|
|
||||||
for(char c : content.toCharArray()){
|
|
||||||
if(c == '<') opened++;
|
|
||||||
else if(c == '>') opened--;
|
|
||||||
|
|
||||||
if(opened > 0){
|
|
||||||
if(Character.isWhitespace(c)) continue;
|
|
||||||
}
|
|
||||||
result+=c;
|
|
||||||
}
|
|
||||||
result = result.replaceAll("(>\\s*)", "> ").replace("> >", ">>");
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int indexOf(String value, String target){
|
|
||||||
return value.split(target)[0].length();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getModifier(){
|
|
||||||
return this.modifier;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName(){
|
|
||||||
return this.name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getType(){
|
|
||||||
return this.type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Variable getValue(){
|
|
||||||
return this.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void show(int tab){
|
|
||||||
String start = "";
|
|
||||||
for(int i = 0; i < tab; i++) start+="\t";
|
|
||||||
System.out.println(start+Modifier.toString(modifier)+" "+type+" "+name+(value == null ? ";":"="+value+";"));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Value extends Variable{
|
|
||||||
|
|
||||||
private String value;
|
|
||||||
|
|
||||||
public Value(String value){
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String value(){
|
|
||||||
return this.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString(){
|
|
||||||
return this.value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -69,6 +69,7 @@ public enum DatabaseQuery {
|
||||||
|
|
||||||
// PLAYERS
|
// PLAYERS
|
||||||
GET_PLAYER_SIMPLE("SELECT pseudo, email, firstname, lastname, description FROM players WHERE id_player = ?"),
|
GET_PLAYER_SIMPLE("SELECT pseudo, email, firstname, lastname, description FROM players WHERE id_player = ?"),
|
||||||
|
GET_PLAYER_PSEUDO("SELECT * FROM players WHERE pseudo = ?"),
|
||||||
GET_PLAYER_DETAILS("SELECT p.*, g.*\r\n"
|
GET_PLAYER_DETAILS("SELECT p.*, g.*\r\n"
|
||||||
+ "FROM players p\r\n"
|
+ "FROM players p\r\n"
|
||||||
+ "LEFT OUTER JOIN containsGroups cg ON p.id_player = cg.fk_player\r\n"
|
+ "LEFT OUTER JOIN containsGroups cg ON p.id_player = cg.fk_player\r\n"
|
||||||
|
@ -80,6 +81,9 @@ public enum DatabaseQuery {
|
||||||
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_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 = ?;"),
|
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 = ?;"),
|
||||||
|
|
||||||
|
UPDATE_PLAYER_INFO("UPDATE players SET pseudo = ?, email = ?, first_name = ?, last_name = ? WHERE id_player = ?"),
|
||||||
|
UPDATE_PLAYER_PASSWORD("UPDATE players SET passwd = ? WHERE id_player = ?"),
|
||||||
|
|
||||||
// BADGES
|
// BADGES
|
||||||
GET_BADGE("SELECT * FROM badges WHERE id_badge = ?"), GET_BADGES_OF_PLAYER(
|
GET_BADGE("SELECT * FROM badges WHERE id_badge = ?"), GET_BADGES_OF_PLAYER(
|
||||||
"SELECT * FROM badges b LEFT JOIN containsBadges cb ON cb.fk_badge = b.id_badge WHERE cb.fk_player = ?"),
|
"SELECT * FROM badges b LEFT JOIN containsBadges cb ON cb.fk_badge = b.id_badge WHERE cb.fk_player = ?"),
|
||||||
|
|
|
@ -32,71 +32,10 @@ public class DatabaseRepository {
|
||||||
public DatabaseRepository(Configuration config) {
|
public DatabaseRepository(Configuration config) {
|
||||||
this.config = 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"));
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
private void ensureConnection() throws SQLException {
|
private void ensureConnection() throws SQLException {
|
||||||
if (con == null || (!con.isValid(5))) {
|
if (con == null || (!con.isValid(5))){
|
||||||
|
System.out.println("connecting "+config.getDbHost()+" on "+config.getDbDatabase());
|
||||||
this.con = DriverManager.getConnection(
|
this.con = DriverManager.getConnection(
|
||||||
"jdbc:mysql://" + config.getDbHost() + ":" + config.getDbPort() + "/" + config.getDbDatabase() + "",
|
"jdbc:mysql://" + config.getDbHost() + ":" + config.getDbPort() + "/" + config.getDbDatabase() + "",
|
||||||
config.getDbUser(), config.getDbPassword());
|
config.getDbUser(), config.getDbPassword());
|
||||||
|
@ -279,6 +218,50 @@ public class DatabaseRepository {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean updatePseudo(int id, Player player, String pseudo){
|
||||||
|
try{
|
||||||
|
PreparedStatement statment = DatabaseQuery.GET_PLAYER_PSEUDO.prepare(this.con);
|
||||||
|
statment.setString(1, pseudo);
|
||||||
|
ResultSet result = statment.executeQuery();
|
||||||
|
if(result.next()) return false;
|
||||||
|
statment = DatabaseQuery.UPDATE_PLAYER_INFO.prepare(this.con);
|
||||||
|
statment.setString(1, player.getPseudo());
|
||||||
|
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 = DatabaseQuery.UPDATE_PLAYER_INFO.prepare(this.con);
|
||||||
|
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 = DatabaseQuery.UPDATE_PLAYER_PASSWORD.prepare(this.con);
|
||||||
|
statment.setString(1, Password.hash(password).withArgon2().getResult());
|
||||||
|
statment.setInt(2, id);
|
||||||
|
statment.executeUpdate();
|
||||||
|
}catch(Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Player getPlayerDetails(int idPlayer) {
|
public Player getPlayerDetails(int idPlayer) {
|
||||||
return getPlayerDetails(idPlayer, null);
|
return getPlayerDetails(idPlayer, null);
|
||||||
}
|
}
|
||||||
|
@ -521,7 +504,6 @@ public class DatabaseRepository {
|
||||||
*/
|
*/
|
||||||
public int register(String pseudo, String email, String password, String firstname, String lastname,
|
public int register(String pseudo, String email, String password, String firstname, String lastname,
|
||||||
String description, String sgroup, String avatar) {
|
String description, String sgroup, String avatar) {
|
||||||
Hash hash = Password.hash(password).withArgon2();
|
|
||||||
try {
|
try {
|
||||||
ensureConnection();
|
ensureConnection();
|
||||||
con.setAutoCommit(false);
|
con.setAutoCommit(false);
|
||||||
|
@ -529,7 +511,7 @@ public class DatabaseRepository {
|
||||||
Statement.RETURN_GENERATED_KEYS)) {
|
Statement.RETURN_GENERATED_KEYS)) {
|
||||||
playerStatement.setString(1, pseudo);
|
playerStatement.setString(1, pseudo);
|
||||||
playerStatement.setString(2, email);
|
playerStatement.setString(2, email);
|
||||||
playerStatement.setString(3, hash.getResult());
|
playerStatement.setString(3, Password.hash(password).withArgon2().getResult());
|
||||||
playerStatement.setString(4, firstname);
|
playerStatement.setString(4, firstname);
|
||||||
playerStatement.setString(5, lastname);
|
playerStatement.setString(5, lastname);
|
||||||
playerStatement.setString(6, description);
|
playerStatement.setString(6, description);
|
||||||
|
@ -569,7 +551,7 @@ public class DatabaseRepository {
|
||||||
* @param password The password of the user
|
* @param password The password of the user
|
||||||
* @return id the id of the user, -1 if not login successefuly
|
* @return id the id of the user, -1 if not login successefuly
|
||||||
*/
|
*/
|
||||||
public int login(String username, String password) {
|
public int login(String username, String password){
|
||||||
try {
|
try {
|
||||||
ensureConnection();
|
ensureConnection();
|
||||||
PreparedStatement statement = con.prepareStatement(DatabaseQuery.CHECK_PASSWORD.toString());
|
PreparedStatement statement = con.prepareStatement(DatabaseQuery.CHECK_PASSWORD.toString());
|
||||||
|
|
|
@ -33,22 +33,24 @@ public class ChapterElement implements Response {
|
||||||
JSONObject chapterJSON = new JSONObject();
|
JSONObject chapterJSON = new JSONObject();
|
||||||
chapterJSON.put("id", chapter.getId());
|
chapterJSON.put("id", chapter.getId());
|
||||||
chapterJSON.put("name", chapter.getName());
|
chapterJSON.put("name", chapter.getName());
|
||||||
if (chapter.getStartDate() != null)
|
boolean show = chapter.isInCurrentTime();
|
||||||
chapterJSON.put("startDate", chapter.getStartDate().toString());
|
chapterJSON.put("show", show);
|
||||||
if (chapter.getEndDate() != null)
|
|
||||||
chapterJSON.put("endDate", chapter.getEndDate().toString());
|
if(show){
|
||||||
JSONArray puzzles = new JSONArray();
|
JSONArray puzzles = new JSONArray();
|
||||||
for (Puzzle puzzle : chapter.getPuzzles()){
|
for (Puzzle puzzle : chapter.getPuzzles()){
|
||||||
JSONObject puzzleJSON = new JSONObject();
|
JSONObject puzzleJSON = new JSONObject();
|
||||||
puzzleJSON.put("id", puzzle.getId());
|
puzzleJSON.put("id", puzzle.getId());
|
||||||
puzzleJSON.put("name", puzzle.getName());
|
puzzleJSON.put("name", puzzle.getName());
|
||||||
puzzleJSON.put("scoreMax", puzzle.getScoreMax());
|
puzzleJSON.put("scoreMax", puzzle.getScoreMax());
|
||||||
if (puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getJsonTags());
|
if (puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getJsonTags());
|
||||||
int score = this.databaseRepo.getScore(user.getId(), puzzle.getId());
|
int score = this.databaseRepo.getScore(user.getId(), puzzle.getId());
|
||||||
if(score >= 0) puzzleJSON.put("score", score);
|
if(score >= 0) puzzleJSON.put("score", score);
|
||||||
puzzles.add(puzzleJSON);
|
puzzles.add(puzzleJSON);
|
||||||
|
}
|
||||||
|
chapterJSON.put("puzzles", puzzles);
|
||||||
}
|
}
|
||||||
chapterJSON.put("puzzles", puzzles);
|
|
||||||
writer.response(200, "Access-Control-Allow-Origin: *");
|
writer.response(200, "Access-Control-Allow-Origin: *");
|
||||||
writer.write(chapterJSON.toJSONString());
|
writer.write(chapterJSON.toJSONString());
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -27,23 +27,20 @@ public class ChapterList implements Response {
|
||||||
@RouteDoc(responseCode = 400, responseDescription = "Aucun chapitre trouver")
|
@RouteDoc(responseCode = 400, responseDescription = "Aucun chapitre trouver")
|
||||||
|
|
||||||
@Route(path = "^\\/chapters$", needLogin = true)
|
@Route(path = "^\\/chapters$", needLogin = true)
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
List<Chapter> allChapters = databaseRepo.getAllChapters();
|
List<Chapter> allChapters = databaseRepo.getAllChapters();
|
||||||
if (allChapters != null) {
|
if(allChapters != null){
|
||||||
JSONArray chaptersJSON = new JSONArray();
|
JSONArray chaptersJSON = new JSONArray();
|
||||||
for (Chapter chapter : allChapters) {
|
for (Chapter chapter : allChapters) {
|
||||||
JSONObject chapterJSON = new JSONObject();
|
JSONObject chapterJSON = new JSONObject();
|
||||||
chapterJSON.put("id", chapter.getId());
|
chapterJSON.put("id", chapter.getId());
|
||||||
chapterJSON.put("name", chapter.getName());
|
chapterJSON.put("name", chapter.getName());
|
||||||
if (chapter.getStartDate() != null)
|
chapterJSON.put("show", chapter.isInCurrentTime());
|
||||||
chapterJSON.put("startDate", chapter.getStartDate().toString());
|
|
||||||
if (chapter.getEndDate() != null)
|
|
||||||
chapterJSON.put("endDate", chapter.getEndDate().toString());
|
|
||||||
chaptersJSON.add(chapterJSON);
|
chaptersJSON.add(chapterJSON);
|
||||||
}
|
}
|
||||||
writer.response(200, "Access-Control-Allow-Origin: *");
|
writer.response(200, "Access-Control-Allow-Origin: *");
|
||||||
writer.write(chaptersJSON.toJSONString());
|
writer.write(chaptersJSON.toJSONString());
|
||||||
} else {
|
}else{
|
||||||
writer.response(400, "Access-Control-Allow-Origin: *");
|
writer.response(400, "Access-Control-Allow-Origin: *");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,13 +16,9 @@ public class DynamicLeaderboard extends Leaderboard{
|
||||||
|
|
||||||
private Locker<Completion> locker;
|
private Locker<Completion> locker;
|
||||||
|
|
||||||
public DynamicLeaderboard(DatabaseRepository databaseRepo){
|
public DynamicLeaderboard(DatabaseRepository databaseRepo, Locker<Completion> locker){
|
||||||
super(databaseRepo);
|
super(databaseRepo);
|
||||||
this.locker = new Locker<>();
|
this.locker = locker;
|
||||||
}
|
|
||||||
|
|
||||||
public Locker<Completion> getLocker(){
|
|
||||||
return this.locker;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@RouteDoc(path = "/rleaderboard/{id}", responseCode = 101, responseDescription = "WebSocket")
|
@RouteDoc(path = "/rleaderboard/{id}", responseCode = 101, responseDescription = "WebSocket")
|
||||||
|
|
|
@ -1,91 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes;
|
|
||||||
|
|
||||||
import static be.jeffcheasey88.peeratcode.framework.RequestType.POST;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
|
|
||||||
import org.json.simple.JSONObject;
|
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc;
|
|
||||||
import be.jeffcheasey88.peeratcode.framework.HttpReader;
|
|
||||||
import be.jeffcheasey88.peeratcode.framework.HttpWriter;
|
|
||||||
import be.jeffcheasey88.peeratcode.framework.Response;
|
|
||||||
import be.jeffcheasey88.peeratcode.framework.Route;
|
|
||||||
import be.jeffcheasey88.peeratcode.framework.Router;
|
|
||||||
import be.jeffcheasey88.peeratcode.framework.User;
|
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
|
||||||
|
|
||||||
public class Register implements Response {
|
|
||||||
|
|
||||||
private DatabaseRepository databaseRepo;
|
|
||||||
private Router router;
|
|
||||||
private String usersFilesPath;
|
|
||||||
|
|
||||||
public Register(DatabaseRepository databaseRepo, Router router, String initUsersFilesPath) {
|
|
||||||
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, User user, HttpReader reader, HttpWriter writer) throws Exception{
|
|
||||||
if (user != null){
|
|
||||||
writer.response(403, "Access-Control-Allow-Origin: *");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
JSONObject informations = reader.readJson();
|
|
||||||
if (informations != null) {
|
|
||||||
boolean allFieldsFilled = informations.containsKey("pseudo") && informations.containsKey("email")
|
|
||||||
&& informations.containsKey("passwd") && informations.containsKey("firstname")
|
|
||||||
&& informations.containsKey("lastname") && informations.containsKey("description")
|
|
||||||
&& informations.containsKey("sgroup") && informations.containsKey("avatar");
|
|
||||||
if (!allFieldsFilled) {
|
|
||||||
writer.response(403, "Access-Control-Allow-Origin: *");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
String pseudo = (String) informations.get("pseudo");
|
|
||||||
String email = (String) informations.get("email");
|
|
||||||
String password = (String) informations.get("passwd");
|
|
||||||
String firstname = (String) informations.get("firstname");
|
|
||||||
String lastname = (String) informations.get("lastname");
|
|
||||||
String description = (String) informations.get("description");
|
|
||||||
String group = (String) informations.get("sgroup");
|
|
||||||
String avatar = (String) informations.get("avatar");
|
|
||||||
|
|
||||||
boolean pseudoAvailable = databaseRepo.checkPseudoAvailability(pseudo);
|
|
||||||
boolean emailAvailable = databaseRepo.checkEmailAvailability(email);
|
|
||||||
if (pseudoAvailable && emailAvailable) {
|
|
||||||
int id;
|
|
||||||
if ((id = databaseRepo.register(pseudo, email, password, firstname, lastname, description, group,
|
|
||||||
avatar)) >= 0) {
|
|
||||||
writer.response(200, "Access-Control-Allow-Origin: *",
|
|
||||||
"Access-Control-Expose-Headers: Authorization",
|
|
||||||
"Authorization: Bearer " + this.router.createAuthUser(id));
|
|
||||||
createFolderToSaveSourceCode(pseudo);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
writer.response(400, "Access-Control-Allow-Origin: *");
|
|
||||||
JSONObject error = new JSONObject();
|
|
||||||
error.put("username_valid", pseudoAvailable);
|
|
||||||
error.put("email_valid", emailAvailable);
|
|
||||||
writer.write(error.toJSONString());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
writer.response(400, "Access-Control-Allow-Origin: *");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void createFolderToSaveSourceCode(String pseudo) throws IOException {
|
|
||||||
|
|
||||||
Files.createDirectories(Paths.get(String.format("%s/%s", usersFilesPath, pseudo)));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
package be.jeffcheasey88.peeratcode.routes.admins;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.HttpReader;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.HttpWriter;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.Locker;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.Response;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.Route;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.User;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.Locker.Key;
|
||||||
|
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||||
|
|
||||||
|
public class DynamicLogs implements Response{
|
||||||
|
|
||||||
|
private Locker<Object> locker; //Context
|
||||||
|
private DatabaseRepository repo;
|
||||||
|
|
||||||
|
public DynamicLogs(DatabaseRepository repo, Locker<Object> locker){
|
||||||
|
this.repo = repo;
|
||||||
|
this.locker = locker;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/logs$", needLogin = true, websocket = true)
|
||||||
|
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
|
//check if admin
|
||||||
|
|
||||||
|
Key key = new Key();
|
||||||
|
|
||||||
|
locker.init(key);
|
||||||
|
try {
|
||||||
|
while(!reader.isClosed()){
|
||||||
|
Object instance = locker.getValue(key);
|
||||||
|
JSONObject json = new JSONObject();
|
||||||
|
writer.write(json.toJSONString());
|
||||||
|
writer.flush();
|
||||||
|
locker.lock(key);
|
||||||
|
}
|
||||||
|
}catch(Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
locker.remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package be.jeffcheasey88.peeratcode.routes.users;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import org.jose4j.json.internal.json_simple.JSONObject;
|
||||||
|
|
||||||
|
import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.HttpReader;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.HttpWriter;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.RequestType;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.Response;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.Route;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.User;
|
||||||
|
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||||
|
|
||||||
|
public class ChangePassword implements Response{
|
||||||
|
|
||||||
|
private DatabaseRepository repo;
|
||||||
|
|
||||||
|
public ChangePassword(DatabaseRepository repo){
|
||||||
|
this.repo = repo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
|
String password = (String) reader.<JSONObject>readJson().get("password");
|
||||||
|
|
||||||
|
repo.updatePassword(user.getId(), password);
|
||||||
|
writer.response(200, "Access-Control-Allow-Origin: *");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
package be.jeffcheasey88.peeratcode.routes.users;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.HttpReader;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.HttpWriter;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.Response;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.Route;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.User;
|
||||||
|
|
||||||
|
public class ForgotPassword implements Response{
|
||||||
|
|
||||||
|
@Route(path = "^/user/fpw$")
|
||||||
|
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
|
if(user != null){
|
||||||
|
writer.response(403, "Access-Control-Allow-Origin: *");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes;
|
package be.jeffcheasey88.peeratcode.routes.users;
|
||||||
|
|
||||||
import static be.jeffcheasey88.peeratcode.framework.RequestType.POST;
|
import static be.jeffcheasey88.peeratcode.framework.RequestType.POST;
|
||||||
|
|
||||||
|
@ -14,13 +14,14 @@ import be.jeffcheasey88.peeratcode.framework.Route;
|
||||||
import be.jeffcheasey88.peeratcode.framework.Router;
|
import be.jeffcheasey88.peeratcode.framework.Router;
|
||||||
import be.jeffcheasey88.peeratcode.framework.User;
|
import be.jeffcheasey88.peeratcode.framework.User;
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||||
|
import be.jeffcheasey88.peeratcode.utils.FormResponse;
|
||||||
|
|
||||||
public class Login implements Response {
|
public class Login extends FormResponse{
|
||||||
|
|
||||||
private DatabaseRepository databaseRepo;
|
private DatabaseRepository databaseRepo;
|
||||||
private Router router;
|
private Router router;
|
||||||
|
|
||||||
public Login(DatabaseRepository databaseRepo, Router router) {
|
public Login(DatabaseRepository databaseRepo, Router router){
|
||||||
this.databaseRepo = databaseRepo;
|
this.databaseRepo = databaseRepo;
|
||||||
this.router = router;
|
this.router = router;
|
||||||
}
|
}
|
||||||
|
@ -31,23 +32,24 @@ public class Login implements Response {
|
||||||
|
|
||||||
@Route(path = "^\\/login$", type = POST)
|
@Route(path = "^\\/login$", type = POST)
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
if (user != null) {
|
if(user != null){
|
||||||
writer.response(403, "Access-Control-Allow-Origin: *");
|
writer.response(403, "Access-Control-Allow-Origin: *");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
JSONObject informations = reader.readJson();
|
JSONObject json = json(reader);
|
||||||
if (informations != null) {
|
if(!areValids("pseudo", "passwd")){
|
||||||
String pseudo = (String) informations.get("pseudo");
|
writer.response(400, "Access-Control-Allow-Origin: *");
|
||||||
String password = (String) informations.get("passwd");
|
return;
|
||||||
int id;
|
}
|
||||||
if ((id = databaseRepo.login(pseudo, password)) >= 0) {
|
int id;
|
||||||
writer.response(200, "Access-Control-Allow-Origin: *",
|
if((id = databaseRepo.login((String)json.get("pseudo"), (String)json.get("passwd"))) >= 0){
|
||||||
"Access-Control-Expose-Headers: Authorization",
|
writer.response(200,
|
||||||
"Authorization: Bearer " + this.router.createAuthUser(id));
|
"Access-Control-Allow-Origin: *",
|
||||||
return;
|
"Access-Control-Expose-Headers: Authorization",
|
||||||
}
|
"Authorization: Bearer " + this.router.createAuthUser(id));
|
||||||
|
}else{
|
||||||
|
writer.response(400,"Access-Control-Allow-Origin: *");
|
||||||
}
|
}
|
||||||
writer.response(400, "Access-Control-Allow-Origin: *");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,57 @@
|
||||||
|
package be.jeffcheasey88.peeratcode.routes.users;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
|
import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.HttpReader;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.HttpWriter;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.RequestType;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.Response;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.Route;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.User;
|
||||||
|
import be.jeffcheasey88.peeratcode.model.Player;
|
||||||
|
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||||
|
|
||||||
|
public class ProfileSettings implements Response{
|
||||||
|
|
||||||
|
private DatabaseRepository repo;
|
||||||
|
|
||||||
|
public ProfileSettings(DatabaseRepository repo){
|
||||||
|
this.repo = repo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
|
JSONObject json = reader.readJson();
|
||||||
|
|
||||||
|
String pseudo = (String) json.get("pseudo");
|
||||||
|
String email = (String) json.get("email");
|
||||||
|
String firstname = (String) json.get("firstname");
|
||||||
|
String lastname = (String) json.get("lastname");
|
||||||
|
Player player = repo.getPlayer(user.getId());
|
||||||
|
|
||||||
|
if(!player.getPseudo().equals(pseudo)){
|
||||||
|
if(!repo.updatePseudo(user.getId(), player, pseudo)){
|
||||||
|
writer.response(400, "Access-Control-Allow-Origin: *");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
player.setPseudo(pseudo);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!player.getEmail().equals(email)){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if((!player.getFirstname().equals(firstname)) || (!player.getLastname().equals(lastname))){
|
||||||
|
repo.updateProfile(user.getId(), player, lastname, firstname);
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.response(200, "Access-Control-Allow-Origin: *");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
143
src/be/jeffcheasey88/peeratcode/routes/users/Register.java
Normal file
143
src/be/jeffcheasey88/peeratcode/routes/users/Register.java
Normal file
|
@ -0,0 +1,143 @@
|
||||||
|
package be.jeffcheasey88.peeratcode.routes.users;
|
||||||
|
|
||||||
|
import static be.jeffcheasey88.peeratcode.framework.RequestType.POST;
|
||||||
|
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.security.KeyPair;
|
||||||
|
import java.security.KeyPairGenerator;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.Base64.Encoder;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import javax.net.ssl.HttpsURLConnection;
|
||||||
|
|
||||||
|
import org.json.simple.JSONAware;
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
|
import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.HttpReader;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.HttpWriter;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.Route;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.Router;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.User;
|
||||||
|
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||||
|
import be.jeffcheasey88.peeratcode.utils.FormResponse;
|
||||||
|
|
||||||
|
public class Register extends FormResponse {
|
||||||
|
|
||||||
|
private DatabaseRepository databaseRepo;
|
||||||
|
private Router router;
|
||||||
|
private String usersFilesPath;
|
||||||
|
private KeyPairGenerator generator;
|
||||||
|
private Encoder encoder;
|
||||||
|
private String gitToken;
|
||||||
|
|
||||||
|
public Register(DatabaseRepository databaseRepo, Router router, String initUsersFilesPath, String gitToken){
|
||||||
|
this.databaseRepo = databaseRepo;
|
||||||
|
this.router = router;
|
||||||
|
this.usersFilesPath = initUsersFilesPath;
|
||||||
|
this.gitToken = gitToken;
|
||||||
|
try {
|
||||||
|
generator = KeyPairGenerator.getInstance("RSA");
|
||||||
|
generator.initialize(2048); //a changer ?
|
||||||
|
} catch (NoSuchAlgorithmException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
encoder = Base64.getEncoder();
|
||||||
|
}
|
||||||
|
|
||||||
|
@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, User user, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
if (user != null){
|
||||||
|
writer.response(403, "Access-Control-Allow-Origin: *");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
JSONObject json = json(reader);
|
||||||
|
if(!areValids("pseudo","email","passwd","firstname","lastname")){
|
||||||
|
writer.response(400, "Access-Control-Allow-Origin: *");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String pseudo = (String) json.get("pseudo");
|
||||||
|
String email = (String) json.get("email");
|
||||||
|
String password = (String) json.get("passwd");
|
||||||
|
String firstname = (String) json.get("firstname");
|
||||||
|
String lastname = (String) json.get("lastname");
|
||||||
|
String description = (String) json.get("description");
|
||||||
|
String group = (String) json.get("sgroup");
|
||||||
|
String avatar = (String) json.get("avatar");
|
||||||
|
|
||||||
|
boolean pseudoAvailable = databaseRepo.checkPseudoAvailability(pseudo);
|
||||||
|
boolean emailAvailable = databaseRepo.checkEmailAvailability(email);
|
||||||
|
if(pseudoAvailable && emailAvailable){
|
||||||
|
int id;
|
||||||
|
if((id = databaseRepo.register(pseudo, email, password, firstname, lastname, description, group,
|
||||||
|
avatar)) >= 0) {
|
||||||
|
writer.response(200,
|
||||||
|
"Access-Control-Allow-Origin: *",
|
||||||
|
"Access-Control-Expose-Headers: Authorization",
|
||||||
|
"Authorization: Bearer " + this.router.createAuthUser(id));
|
||||||
|
createFolderToSaveSourceCode(pseudo);
|
||||||
|
}else{
|
||||||
|
writer.response(400, "Access-Control-Allow-Origin: *");
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
writer.response(400, "Access-Control-Allow-Origin: *");
|
||||||
|
JSONObject error = new JSONObject();
|
||||||
|
error.put("username_valid", pseudoAvailable);
|
||||||
|
error.put("email_valid", emailAvailable);
|
||||||
|
writer.write(error.toJSONString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
sendKey.put("key", new String(encoder.encode(pair.getPrivate().getEncoded()))); //add ssh-rsa au début ?
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createFolderToSaveSourceCode(String pseudo) throws IOException {
|
||||||
|
|
||||||
|
Files.createDirectories(Paths.get(String.format("%s/%s", usersFilesPath, pseudo)));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
49
src/be/jeffcheasey88/peeratcode/utils/FormResponse.java
Normal file
49
src/be/jeffcheasey88/peeratcode/utils/FormResponse.java
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
package be.jeffcheasey88.peeratcode.utils;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import org.json.simple.JSONAware;
|
||||||
|
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.HttpReader;
|
||||||
|
import be.jeffcheasey88.peeratcode.framework.Response;
|
||||||
|
|
||||||
|
public abstract class FormResponse implements Response{
|
||||||
|
|
||||||
|
private JSONAware json;
|
||||||
|
private Map<String, Pattern> checker;
|
||||||
|
|
||||||
|
public FormResponse(){
|
||||||
|
this.checker = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validator(String key, Pattern regex){
|
||||||
|
this.checker.put(key, regex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T extends JSONAware> T json(HttpReader reader) throws Exception{
|
||||||
|
return (T) (this.json = reader.readJson());
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasFields(String... fields){
|
||||||
|
Map<?,?> map = (Map<?,?>)json;
|
||||||
|
for(String field : fields){
|
||||||
|
if(!map.containsKey(field)) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean areValids(String... fields){
|
||||||
|
Map<?,?> map = (Map<?,?>)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;
|
||||||
|
}
|
||||||
|
}
|
64
test/be/jeffcheasey88/peeratcode/TestDatabaseRepository.java
Normal file
64
test/be/jeffcheasey88/peeratcode/TestDatabaseRepository.java
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
package be.jeffcheasey88.peeratcode;
|
||||||
|
|
||||||
|
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 be.jeffcheasey88.peeratcode.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,4 +1,4 @@
|
||||||
package be.jeffcheasey88.peeratcode.webclient;
|
package be.jeffcheasey88.peeratcode;
|
||||||
|
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
|
@ -1,142 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.parser.java;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.parser.java.Variable.Value;
|
|
||||||
|
|
||||||
class VariableTest {
|
|
||||||
|
|
||||||
//int i = 4;
|
|
||||||
//int i,j,k,l=1;
|
|
||||||
//int lm ;
|
|
||||||
//public static int l;
|
|
||||||
//Test<Test>t;
|
|
||||||
//Test<Test,K,L> j = new Test().schedule(p -> { return true;});
|
|
||||||
//int i =j=k=l=4;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void case1(){
|
|
||||||
try {
|
|
||||||
Variable variable = new Variable();
|
|
||||||
variable.parse(" int i = 4 ; ");
|
|
||||||
|
|
||||||
assertEquals(0, variable.getModifier());
|
|
||||||
assertEquals("int", variable.getType());
|
|
||||||
assertEquals("i", variable.getName());
|
|
||||||
assertEquals("4", ((Value)variable.getValue()).value());
|
|
||||||
}catch(Exception e){
|
|
||||||
fail(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void case2(){
|
|
||||||
try {
|
|
||||||
Variable variable = new Variable();
|
|
||||||
variable.parse("public static int l ; ");
|
|
||||||
|
|
||||||
assertEquals(JavaParser.getModifier("public")+JavaParser.getModifier("static"), variable.getModifier());
|
|
||||||
assertEquals("int", variable.getType());
|
|
||||||
assertEquals("l", variable.getName());
|
|
||||||
assertNull(variable.getValue());
|
|
||||||
}catch(Exception e){
|
|
||||||
fail(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void case3(){
|
|
||||||
try {
|
|
||||||
Variable variable = new Variable();
|
|
||||||
variable.parse(" int lm ; ");
|
|
||||||
|
|
||||||
assertEquals(0, variable.getModifier());
|
|
||||||
assertEquals("int", variable.getType());
|
|
||||||
assertEquals("lm", variable.getName());
|
|
||||||
assertNull(variable.getValue());
|
|
||||||
}catch(Exception e){
|
|
||||||
fail(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void case4(){
|
|
||||||
try {
|
|
||||||
Variable variable = new Variable();
|
|
||||||
variable.parse("Testas< List< Map< Test, List< Test >, Test>> >t; ");
|
|
||||||
|
|
||||||
assertEquals(0, variable.getModifier());
|
|
||||||
assertEquals("Testas<List<Map<Test,List<Test>,Test>>>", variable.getType());
|
|
||||||
assertEquals("t", variable.getName());
|
|
||||||
assertNull(variable.getValue());
|
|
||||||
}catch(Exception e){
|
|
||||||
fail(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void case5(){
|
|
||||||
try {
|
|
||||||
Variable variable = new Variable();
|
|
||||||
variable.parse(" int i,j,k,l=1; ");
|
|
||||||
|
|
||||||
assertEquals(0, variable.getModifier());
|
|
||||||
assertEquals("int", variable.getType());
|
|
||||||
assertEquals("i", variable.getName());
|
|
||||||
assertNull(variable.getValue());
|
|
||||||
}catch(Exception e){
|
|
||||||
fail(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void case6(){
|
|
||||||
try {
|
|
||||||
Class clazz = new Class();
|
|
||||||
clazz.parse("public class Test{ int i ,j,k,l=1; } ");
|
|
||||||
|
|
||||||
List<Variable> vars = clazz.getVariables();
|
|
||||||
assertEquals(vars.size(), 4);
|
|
||||||
for(int i = 0; i < 3; i++){
|
|
||||||
Variable v = vars.get(i);
|
|
||||||
assertEquals(0, v.getModifier());
|
|
||||||
assertEquals("int", v.getType());
|
|
||||||
assertEquals((char)('i'+i), v.getName().charAt(0));
|
|
||||||
assertNull(v.getValue());
|
|
||||||
}
|
|
||||||
Variable v = vars.get(3);
|
|
||||||
assertEquals(0, v.getModifier());
|
|
||||||
assertEquals("int", v.getType());
|
|
||||||
assertEquals('l', v.getName().charAt(0));
|
|
||||||
assertEquals("1", ((Value)v.getValue()).value());
|
|
||||||
}catch(Exception e){
|
|
||||||
fail(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void case7(){
|
|
||||||
try {
|
|
||||||
Class clazz = new Class();
|
|
||||||
clazz.parse("public class Test{ int i ,j,k; int l=i=k=l=4; } ");
|
|
||||||
|
|
||||||
List<Variable> vars = clazz.getVariables();
|
|
||||||
assertEquals(vars.size(), 4);
|
|
||||||
for(int i = 0; i < 4; i++){
|
|
||||||
Variable v = vars.get(i);
|
|
||||||
assertEquals(0, v.getModifier());
|
|
||||||
assertEquals("int", v.getType());
|
|
||||||
assertEquals((char)('i'+i), v.getName().charAt(0));
|
|
||||||
}
|
|
||||||
}catch(Exception e){
|
|
||||||
fail(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -9,7 +9,7 @@ import org.junit.jupiter.api.TestInstance;
|
||||||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.Main;
|
import be.jeffcheasey88.peeratcode.Main;
|
||||||
import be.jeffcheasey88.peeratcode.webclient.WebClient;
|
import be.jeffcheasey88.peeratcode.WebClient;
|
||||||
|
|
||||||
@TestInstance(Lifecycle.PER_CLASS)
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
class PlayerDetailsTests {
|
class PlayerDetailsTests {
|
||||||
|
|
|
@ -10,7 +10,7 @@ import org.junit.jupiter.api.TestInstance;
|
||||||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.Main;
|
import be.jeffcheasey88.peeratcode.Main;
|
||||||
import be.jeffcheasey88.peeratcode.webclient.WebClient;
|
import be.jeffcheasey88.peeratcode.WebClient;
|
||||||
|
|
||||||
@TestInstance(Lifecycle.PER_CLASS)
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
public class ScoreTests {
|
public class ScoreTests {
|
||||||
|
|
|
@ -10,7 +10,7 @@ import org.junit.jupiter.api.TestInstance;
|
||||||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.Main;
|
import be.jeffcheasey88.peeratcode.Main;
|
||||||
import be.jeffcheasey88.peeratcode.webclient.WebClient;
|
import be.jeffcheasey88.peeratcode.WebClient;
|
||||||
|
|
||||||
@TestInstance(Lifecycle.PER_CLASS)
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
public class TmpRoutesTests {
|
public class TmpRoutesTests {
|
||||||
|
|
|
@ -9,7 +9,7 @@ import org.junit.jupiter.api.TestInstance;
|
||||||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.Main;
|
import be.jeffcheasey88.peeratcode.Main;
|
||||||
import be.jeffcheasey88.peeratcode.webclient.WebClient;
|
import be.jeffcheasey88.peeratcode.WebClient;
|
||||||
|
|
||||||
@TestInstance(Lifecycle.PER_CLASS)
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
public class TriggerTests {
|
public class TriggerTests {
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
package be.jeffcheasey88.peeratcode.userstories;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import be.jeffcheasey88.peeratcode.Configuration;
|
||||||
|
import be.jeffcheasey88.peeratcode.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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
79
test/be/jeffcheasey88/peeratcode/userstories/LoginTests.java
Normal file
79
test/be/jeffcheasey88/peeratcode/userstories/LoginTests.java
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
package be.jeffcheasey88.peeratcode.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 be.jeffcheasey88.peeratcode.Main;
|
||||||
|
import be.jeffcheasey88.peeratcode.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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,90 @@
|
||||||
|
package be.jeffcheasey88.peeratcode.userstories;
|
||||||
|
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
|
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 be.jeffcheasey88.peeratcode.Main;
|
||||||
|
import be.jeffcheasey88.peeratcode.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", new JSONObject().toJSONString());
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue