Compare commits
157 commits
switch_rep
...
main
Author | SHA1 | Date | |
---|---|---|---|
de8cba207c | |||
a78723825b | |||
2d03ccbe3e | |||
fbb8514afd | |||
e746d6fe2b | |||
51d6d311fb | |||
e4cbca8f1c | |||
5c43347710 | |||
7b40c6edea | |||
7e871f824c | |||
0bfba6d99c | |||
35b97d1a25 | |||
a0cf7989b4 | |||
ed050b17fe | |||
5f02e31507 | |||
758ee3b960 | |||
066d4f5d9f | |||
47510f3d91 | |||
901d7930fa | |||
2a1912b474 | |||
a8985b14b1 | |||
d89a11fba0 | |||
ee9739f5d3 | |||
6550cf03f1 | |||
28cd04406b | |||
e78ef96cfd | |||
b223b6bad4 | |||
c3d81f2020 | |||
1d67dfdfd9 | |||
1012a489ab | |||
e915f6baa2 | |||
9a0966322e | |||
5b8699cf9d | |||
634bcbadf4 | |||
19b3f6391b | |||
50ccad4b24 | |||
88d4bfb8ee | |||
d0bdc316bf | |||
7030adbfba | |||
aef778570e | |||
cb9107bc58 | |||
2560279ef4 | |||
710e3eccb8 | |||
f6d2d7f96f | |||
15f0791777 | |||
f23904d860 | |||
f722a4db22 | |||
700054f586 | |||
94abf83731 | |||
c60dee21b4 | |||
575d679113 | |||
250758c3bf | |||
d089ff237a | |||
a7b87bcf12 | |||
d43ae3b989 | |||
bc73dc6624 | |||
7c3558f5a8 | |||
3b98624df9 | |||
68dd312a42 | |||
8041ddb470 | |||
4f11822c12 | |||
894ed9fb46 | |||
087cf59ae0 | |||
89d6e14c9c | |||
7fb0408b52 | |||
2fc73af4f2 | |||
ac8df18e99 | |||
d321a3e088 | |||
cf6167a12f | |||
bdd5fa4081 | |||
66eb81210c | |||
c6393ef4f3 | |||
93b048ed7d | |||
a0d7848109 | |||
abb46f0915 | |||
798702f677 | |||
a58fd656b5 | |||
092e58b11d | |||
6222d17524 | |||
b20d11b224 | |||
e1a750cbe3 | |||
26514c0961 | |||
33a23af493 | |||
8a138b52d6 | |||
cfd0114590 | |||
b04bce2be6 | |||
3c423c7122 | |||
1ac2fe4a4f | |||
f079e4e617 | |||
75baf14253 | |||
c419013c40 | |||
081930f415 | |||
5b6c122178 | |||
cc24278a3f | |||
cc8ad713a7 | |||
e50034dd00 | |||
cf5695e011 | |||
ecea0a396c | |||
f92462db82 | |||
018a132466 | |||
9e56d83343 | |||
3da54bbe17 | |||
dbfd936d32 | |||
772dd2d23f | |||
fe904774c0 | |||
e5081f6c13 | |||
9ad98cbedf | |||
449cb027b3 | |||
6d017e1d5a | |||
30d221e35a | |||
6c6eba29f8 | |||
e237be36b6 | |||
e185258a54 | |||
0248b7b07c | |||
c3621da722 | |||
5e2d84895d | |||
18ec7fb396 | |||
6470bd8be7 | |||
6824ced972 | |||
62c2a923ae | |||
fa09374178 | |||
2d1c67fc65 | |||
3c7aa052dc | |||
735534ea2d | |||
119e789900 | |||
514452c295 | |||
58d890737d | |||
75277c5b03 | |||
a68bc91459 | |||
f6560a85fd | |||
c4dcbbb977 | |||
12e0bd7bac | |||
76e9c4376a | |||
dbb47bd8c5 | |||
a06eb168e7 | |||
8dbb5e3474 | |||
262875fd62 | |||
26eea355f1 | |||
e2cf4f8542 | |||
1f2e30a7d1 | |||
4f8b45f074 | |||
c2428ebd94 | |||
e1fde6bd5a | |||
e815346b93 | |||
743e230f85 | |||
bff438073a | |||
5161916f54 | |||
3ad9e91365 | |||
ecdf99365b | |||
c80d9ce31d | |||
6b652c8adf | |||
234a4a4795 | |||
bd0460e754 | |||
eeac351b02 | |||
97d8c0b7de | |||
50dacdff15 | |||
5481dbd2cf |
113 changed files with 5222 additions and 2961 deletions
11
.classpath
11
.classpath
|
@ -1,6 +1,10 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<classpath>
|
<classpath>
|
||||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="module" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
<classpathentry kind="src" path="src"/>
|
<classpathentry kind="src" path="src"/>
|
||||||
<classpathentry kind="src" path="test"/>
|
<classpathentry kind="src" path="test"/>
|
||||||
<classpathentry exported="true" kind="lib" path="mysql-connector-java-8.0.28.jar"/>
|
<classpathentry exported="true" kind="lib" path="mysql-connector-java-8.0.28.jar"/>
|
||||||
|
@ -12,6 +16,9 @@
|
||||||
</classpathentry>
|
</classpathentry>
|
||||||
<classpathentry kind="lib" path="Treasure.jar"/>
|
<classpathentry kind="lib" path="Treasure.jar"/>
|
||||||
<classpathentry exported="true" kind="lib" path="JDA-5.0.0-beta.8-withDependencies.jar"/>
|
<classpathentry exported="true" kind="lib" path="JDA-5.0.0-beta.8-withDependencies.jar"/>
|
||||||
<classpathentry exported="true" kind="lib" path="C:/Users/jeffc/eclipse-workspace/peer-at-code-backend/PeerAtCodeFramework.jar"/>
|
<classpathentry exported="true" kind="lib" path="PeerAtCodeFramework.jar"/>
|
||||||
|
<classpathentry exported="true" kind="lib" path="angus-activation-2.0.2.jar"/>
|
||||||
|
<classpathentry exported="true" kind="lib" path="jakarta.activation-api-2.1.3.jar"/>
|
||||||
|
<classpathentry exported="true" kind="lib" path="jakarta.mail-2.0.3.jar"/>
|
||||||
<classpathentry kind="output" path="bin"/>
|
<classpathentry kind="output" path="bin"/>
|
||||||
</classpath>
|
</classpath>
|
||||||
|
|
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -2,6 +2,8 @@
|
||||||
bin/
|
bin/
|
||||||
.project
|
.project
|
||||||
config.txt
|
config.txt
|
||||||
|
config-test.txt
|
||||||
dist/
|
dist/
|
||||||
testApi/
|
testApi/
|
||||||
.apt_generated/*
|
.apt_generated/*
|
||||||
|
/.apt_generated/
|
||||||
|
|
Binary file not shown.
BIN
Treasure.jar
BIN
Treasure.jar
Binary file not shown.
BIN
angus-activation-2.0.2.jar
Normal file
BIN
angus-activation-2.0.2.jar
Normal file
Binary file not shown.
127
database-schem.sql
Normal file
127
database-schem.sql
Normal file
|
@ -0,0 +1,127 @@
|
||||||
|
DROP TABLE IF EXISTS `containsTags`;
|
||||||
|
DROP TABLE IF EXISTS `tags`;
|
||||||
|
DROP TABLE IF EXISTS `containsBadges`;
|
||||||
|
DROP TABLE IF EXISTS `badges`;
|
||||||
|
DROP TABLE IF EXISTS `containsGroups`;
|
||||||
|
DROP TABLE IF EXISTS `nextPart`;
|
||||||
|
DROP TABLE IF EXISTS `groups`;
|
||||||
|
DROP TABLE IF EXISTS `completions`;
|
||||||
|
DROP TABLE IF EXISTS `players`;
|
||||||
|
DROP TABLE IF EXISTS `puzzles`;
|
||||||
|
DROP TABLE IF EXISTS `chapters`;
|
||||||
|
|
||||||
|
CREATE TABLE `players` (
|
||||||
|
`id_player` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
|
`pseudo` varchar(100) NOT NULL,
|
||||||
|
`email` varchar(100) NOT NULL,
|
||||||
|
`passwd` varchar(150) NOT NULL,
|
||||||
|
`firstname` varchar(100) NOT NULL,
|
||||||
|
`lastname` varchar(100) NOT NULL,
|
||||||
|
`description` varchar(200) DEFAULT NULL,
|
||||||
|
`avatar` blob DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`id_player`)
|
||||||
|
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||||
|
|
||||||
|
CREATE TABLE badges (
|
||||||
|
id_badge int(11) NOT NULL AUTO_INCREMENT,
|
||||||
|
name varchar(50) NOT NULL,
|
||||||
|
logo mediumblob DEFAULT NULL,
|
||||||
|
level int(11) DEFAULT 1,
|
||||||
|
PRIMARY KEY (id_badge)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE `chapters` (
|
||||||
|
`id_chapter` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
|
`name` varchar(150) NOT NULL,
|
||||||
|
`start_date` datetime DEFAULT NULL,
|
||||||
|
`end_date` datetime DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`id_chapter`)
|
||||||
|
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||||
|
|
||||||
|
CREATE TABLE `puzzles` (
|
||||||
|
`id_puzzle` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
|
`name` varchar(150) NOT NULL,
|
||||||
|
`content` text NOT NULL,
|
||||||
|
`soluce` blob NOT NULL,
|
||||||
|
`verify` text DEFAULT NULL,
|
||||||
|
`score_max` int(11) NOT NULL,
|
||||||
|
`fk_chapter` int(11) NOT NULL,
|
||||||
|
PRIMARY KEY (`id_puzzle`),
|
||||||
|
KEY `fk_chapter` (`fk_chapter`),
|
||||||
|
CONSTRAINT `puzzles_ibfk_1` FOREIGN KEY (`fk_chapter`) REFERENCES `chapters` (`id_chapter`)
|
||||||
|
) ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||||
|
|
||||||
|
|
||||||
|
CREATE TABLE `groups` (
|
||||||
|
`id_group` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
|
`name` varchar(150) DEFAULT NULL,
|
||||||
|
`fk_chapter` int(11) DEFAULT NULL,
|
||||||
|
`fk_puzzle` int(11) DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`id_group`),
|
||||||
|
KEY `fk_chapter` (`fk_chapter`),
|
||||||
|
KEY `fk_puzzle` (`fk_puzzle`),
|
||||||
|
CONSTRAINT `groups_ibfk_1` FOREIGN KEY (`fk_chapter`) REFERENCES `chapters` (`id_chapter`),
|
||||||
|
CONSTRAINT `groups_ibfk_2` FOREIGN KEY (`fk_puzzle`) REFERENCES `puzzles` (`id_puzzle`)
|
||||||
|
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||||
|
|
||||||
|
|
||||||
|
CREATE TABLE `nextPart` (
|
||||||
|
`origin` int(11) NOT NULL,
|
||||||
|
`next` int(11) NOT NULL,
|
||||||
|
PRIMARY KEY (`origin`,`next`),
|
||||||
|
KEY `next` (`next`),
|
||||||
|
CONSTRAINT `nextPart_ibfk_1` FOREIGN KEY (`origin`) REFERENCES `puzzles` (`id_puzzle`),
|
||||||
|
CONSTRAINT `nextPart_ibfk_2` FOREIGN KEY (`next`) REFERENCES `puzzles` (`id_puzzle`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||||
|
|
||||||
|
|
||||||
|
CREATE TABLE `completions` (
|
||||||
|
`id_completion` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
|
`fk_puzzle` int(11) NOT NULL,
|
||||||
|
`fk_player` int(11) NOT NULL,
|
||||||
|
`tries` int(11) DEFAULT 0,
|
||||||
|
`code` blob DEFAULT NULL,
|
||||||
|
`score` int(11) DEFAULT 0,
|
||||||
|
`fileName` varchar(100) DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`id_completion`),
|
||||||
|
KEY `fk_puzzle` (`fk_puzzle`),
|
||||||
|
KEY `fk_player` (`fk_player`),
|
||||||
|
CONSTRAINT `completions_ibfk_1` FOREIGN KEY (`fk_puzzle`) REFERENCES `puzzles` (`id_puzzle`),
|
||||||
|
CONSTRAINT `completions_ibfk_2` FOREIGN KEY (`fk_player`) REFERENCES `players` (`id_player`)
|
||||||
|
) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||||
|
|
||||||
|
CREATE TABLE `tags` (
|
||||||
|
`id_tag` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
|
`name` varchar(50) NOT NULL,
|
||||||
|
PRIMARY KEY (`id_tag`)
|
||||||
|
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||||
|
|
||||||
|
CREATE TABLE `containsBadges` (
|
||||||
|
`fk_player` int(11) NOT NULL,
|
||||||
|
`fk_badge` int(11) NOT NULL,
|
||||||
|
PRIMARY KEY (`fk_player`,`fk_badge`),
|
||||||
|
KEY `fk_badge` (`fk_badge`),
|
||||||
|
CONSTRAINT `containsBadges_ibfk_1` FOREIGN KEY (`fk_player`) REFERENCES `players` (`id_player`),
|
||||||
|
CONSTRAINT `containsBadges_ibfk_2` FOREIGN KEY (`fk_badge`) REFERENCES `badges` (`id_badge`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||||
|
|
||||||
|
CREATE TABLE `containsGroups` (
|
||||||
|
`fk_player` int(11) NOT NULL,
|
||||||
|
`fk_group` int(11) NOT NULL,
|
||||||
|
PRIMARY KEY (`fk_player`,`fk_group`),
|
||||||
|
KEY `fk_group` (`fk_group`),
|
||||||
|
CONSTRAINT `containsGroups_ibfk_1` FOREIGN KEY (`fk_player`) REFERENCES `players` (`id_player`),
|
||||||
|
CONSTRAINT `containsGroups_ibfk_2` FOREIGN KEY (`fk_group`) REFERENCES `groups` (`id_group`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||||
|
|
||||||
|
CREATE TABLE `containsTags` (
|
||||||
|
`fk_tag` int(11) NOT NULL,
|
||||||
|
`fk_puzzle` int(11) NOT NULL,
|
||||||
|
PRIMARY KEY (`fk_tag`,`fk_puzzle`),
|
||||||
|
KEY `fk_puzzle` (`fk_puzzle`),
|
||||||
|
CONSTRAINT `containsTags_ibfk_1` FOREIGN KEY (`fk_tag`) REFERENCES `tags` (`id_tag`),
|
||||||
|
CONSTRAINT `containsTags_ibfk_2` FOREIGN KEY (`fk_puzzle`) REFERENCES `puzzles` (`id_puzzle`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
|
||||||
|
|
||||||
|
INSERT INTO chapters (id_chapter, name) VALUES (1, 'chapter 1');
|
||||||
|
INSERT INTO players (pseudo, email, passwd,firstname,lastname) VALUES ('userTest', 'test@peerat.dev', '$argon2id$v=19$m=15360,t=2,p=1$$cAQwfs30Bf2rQGj86bpz7i59TlsuOFPiXeNpLlVu4AY', 'a','b')
|
BIN
jakarta.activation-api-2.1.3.jar
Normal file
BIN
jakarta.activation-api-2.1.3.jar
Normal file
Binary file not shown.
BIN
jakarta.mail-2.0.3.jar
Normal file
BIN
jakarta.mail-2.0.3.jar
Normal file
Binary file not shown.
BIN
jose4j-0.9.3.jar
BIN
jose4j-0.9.3.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
6
route-test/all_empty/fetch_chapter.test
Normal file
6
route-test/all_empty/fetch_chapter.test
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
/chapter/0
|
||||||
|
GET
|
||||||
|
Authorization: Bearer <token:test@peerat.dev>
|
||||||
|
|
||||||
|
|
||||||
|
400
|
8
route-test/all_empty/fetch_chapters.test
Normal file
8
route-test/all_empty/fetch_chapters.test
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
/chapters
|
||||||
|
GET
|
||||||
|
Authorization: Bearer <token:test@peerat.dev>
|
||||||
|
|
||||||
|
|
||||||
|
200
|
||||||
|
|
||||||
|
[]
|
8
route-test/all_empty/fetch_leaderboard.test
Normal file
8
route-test/all_empty/fetch_leaderboard.test
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
/leaderboard/
|
||||||
|
GET
|
||||||
|
Authorization: Bearer <token:test@peerat.dev>
|
||||||
|
|
||||||
|
|
||||||
|
200
|
||||||
|
|
||||||
|
[]
|
6
route-test/all_empty/fetch_puzzle.test
Normal file
6
route-test/all_empty/fetch_puzzle.test
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
/puzzle/0
|
||||||
|
GET
|
||||||
|
Authorization: Bearer <token:test@peerat.dev>
|
||||||
|
|
||||||
|
|
||||||
|
400
|
6
route-test/all_empty/try_admin.test
Normal file
6
route-test/all_empty/try_admin.test
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
/admin/chapter/0
|
||||||
|
GET
|
||||||
|
Authorization: Bearer <token:test@peerat.dev>
|
||||||
|
|
||||||
|
|
||||||
|
401
|
Binary file not shown.
|
@ -1,96 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode;
|
|
||||||
|
|
||||||
import static be.jeffcheasey88.peeratcode.framework.RequestType.OPTIONS;
|
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
|
|
||||||
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.Router;
|
|
||||||
import be.jeffcheasey88.peeratcode.framework.User;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Completion;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Group;
|
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
|
||||||
import be.jeffcheasey88.peeratcode.routes.BadgeDetails;
|
|
||||||
import be.jeffcheasey88.peeratcode.routes.ChapterElement;
|
|
||||||
import be.jeffcheasey88.peeratcode.routes.ChapterList;
|
|
||||||
import be.jeffcheasey88.peeratcode.routes.DynamicLeaderboard;
|
|
||||||
import be.jeffcheasey88.peeratcode.routes.Leaderboard;
|
|
||||||
import be.jeffcheasey88.peeratcode.routes.Login;
|
|
||||||
import be.jeffcheasey88.peeratcode.routes.PlayerDetails;
|
|
||||||
import be.jeffcheasey88.peeratcode.routes.PuzzleElement;
|
|
||||||
import be.jeffcheasey88.peeratcode.routes.PuzzleResponse;
|
|
||||||
import be.jeffcheasey88.peeratcode.routes.Register;
|
|
||||||
import be.jeffcheasey88.peeratcode.routes.Result;
|
|
||||||
import be.jeffcheasey88.peeratcode.routes.groups.GroupCreate;
|
|
||||||
import be.jeffcheasey88.peeratcode.routes.groups.GroupJoin;
|
|
||||||
import be.jeffcheasey88.peeratcode.routes.groups.GroupList;
|
|
||||||
import be.jeffcheasey88.peeratcode.routes.groups.GroupQuit;
|
|
||||||
|
|
||||||
public class Main{
|
|
||||||
public static void main(String[] args) throws Exception{
|
|
||||||
Configuration config = new Configuration("config.txt");
|
|
||||||
config.load();
|
|
||||||
|
|
||||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
|
||||||
|
|
||||||
DatabaseRepository repo = new DatabaseRepository(config);
|
|
||||||
Router router = new Router(config.getTokenIssuer(),
|
|
||||||
config.getTokenExpiration());
|
|
||||||
|
|
||||||
router.setDefault((matcher, user, reader, writer) -> {
|
|
||||||
writer.response(404, "Access-Control-Allow-Origin: *");
|
|
||||||
writer.write("404 not Found.\n");
|
|
||||||
writer.flush();
|
|
||||||
writer.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
router.register(new Response(){
|
|
||||||
@Route(path = "^(.*)$", type = OPTIONS)
|
|
||||||
@Override
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
|
||||||
writer.response(200, "Access-Control-Allow-Origin: *",
|
|
||||||
"Access-Control-Allow-Methods: *", "Access-Control-Allow-Headers: *");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
initRoutes(router, repo, config);
|
|
||||||
|
|
||||||
// RouteExtracter extracter = new RouteExtracter(router);
|
|
||||||
// extracter.extract();
|
|
||||||
|
|
||||||
if(config.useSsl()) router.configureSSL(config.getSslKeystore(), config.getSslKeystorePasswd());
|
|
||||||
router.listen(config.getTcpPort(), config.useSsl());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void initRoutes(Router router, DatabaseRepository repo, Configuration config){
|
|
||||||
router.register(new ChapterElement(repo));
|
|
||||||
router.register(new ChapterList(repo));
|
|
||||||
router.register(new PuzzleElement(repo));
|
|
||||||
router.register(new Register(repo, router, config.getUsersFiles()));
|
|
||||||
router.register(new Login(repo, router));
|
|
||||||
router.register(new Result(repo));
|
|
||||||
router.register(new Leaderboard(repo));
|
|
||||||
router.register(new PlayerDetails(repo));
|
|
||||||
router.register(new BadgeDetails(repo));
|
|
||||||
|
|
||||||
Locker<Group> groupLock = new Locker<>();
|
|
||||||
router.register(new GroupCreate(repo, groupLock, config.getGroupJoinMinutes()));
|
|
||||||
|
|
||||||
DynamicLeaderboard dlb = new DynamicLeaderboard(repo);
|
|
||||||
router.register(dlb);
|
|
||||||
|
|
||||||
Locker<Completion> leaderboard = dlb.getLocker();
|
|
||||||
|
|
||||||
router.register(new PuzzleResponse(repo, config.getUsersFiles(), leaderboard));
|
|
||||||
router.register(new GroupList(repo));
|
|
||||||
router.register(new GroupJoin(repo, config.getGroupJoinMinutes(), config.getGroupQuitMinutes(), leaderboard));
|
|
||||||
router.register(new GroupQuit(repo, config.getGroupJoinMinutes(), leaderboard));
|
|
||||||
|
|
||||||
// Bot bot = new Bot(config, repo, groupLock);
|
|
||||||
// bot.start();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,49 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.bonus.extract;
|
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Map.Entry;
|
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.framework.RequestType;
|
|
||||||
import be.jeffcheasey88.peeratcode.framework.Response;
|
|
||||||
import be.jeffcheasey88.peeratcode.framework.Route;
|
|
||||||
import be.jeffcheasey88.peeratcode.framework.Router;
|
|
||||||
|
|
||||||
//A noter que le but est d'extraire des informations sans modifier le code source,
|
|
||||||
//comme les tests unitaire, ici je veux générer un élément textuel qui me donnera l'état des routes,
|
|
||||||
//je ne vais donc pas modifier la classe router pour donner un accès au route.
|
|
||||||
public class RouteExtracter {
|
|
||||||
|
|
||||||
private Router router;
|
|
||||||
|
|
||||||
public RouteExtracter(Router router){
|
|
||||||
this.router = router;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void extract() throws Exception{
|
|
||||||
Field field = Router.class.getDeclaredField("responses");
|
|
||||||
field.setAccessible(true);
|
|
||||||
Map<RequestType, Map<Response, Route>> responses = (Map<RequestType, Map<Response, Route>>) field.get(this.router);
|
|
||||||
for(Entry<RequestType, Map<Response, Route>> types : responses.entrySet()){
|
|
||||||
for(Entry<Response, Route> routes : types.getValue().entrySet()){
|
|
||||||
System.out.println("["+types.getKey()+"] ("+routes.getValue().needLogin()+") "+routes.getValue().path());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void extractDoc() throws Exception{
|
|
||||||
Field field = Router.class.getDeclaredField("responses");
|
|
||||||
field.setAccessible(true);
|
|
||||||
Map<RequestType, Map<Response, Route>> responses = (Map<RequestType, Map<Response, Route>>) field.get(this.router);
|
|
||||||
for(Map<Response, Route> route : responses.values()){
|
|
||||||
for(Response response : route.keySet()){
|
|
||||||
Method method = response.getClass().getDeclaredMethod("exec",
|
|
||||||
Response.class.getDeclaredMethods()[0].getParameterTypes());
|
|
||||||
for(RouteDoc doc : method.getDeclaredAnnotationsByType(RouteDoc.class)){
|
|
||||||
System.out.println(doc.path()+((doc.path().isEmpty() ? "":"\n"))+" ["+doc.responseCode()+"] "+doc.responseDescription());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,259 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.repository;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public enum DatabaseQuery {
|
|
||||||
// PUZZLES
|
|
||||||
SPECIFIC_PUZZLE_QUERY(
|
|
||||||
"SELECT p.*, np.origin, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN nextPart np ON p.id_puzzle = np.next LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE p.id_puzzle = ? GROUP BY p.id_puzzle"),
|
|
||||||
PUZZLES_IN_CHAPTER_QUERY(
|
|
||||||
"SELECT p.*, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE fk_chapter = ? GROUP BY p.id_puzzle"),
|
|
||||||
|
|
||||||
// CHAPTERS
|
|
||||||
SPECIFIC_CHAPTER_QUERY("SELECT * FROM chapters WHERE id_chapter = ?"),
|
|
||||||
CHAPTER_FROM_PUZZLE("SELECT c.*\r\n"
|
|
||||||
+ "FROM chapters c\r\n"
|
|
||||||
+ "JOIN puzzles p ON p.fk_chapter = c.id_chapter\r\n"
|
|
||||||
+ "WHERE p.id_puzzle = ?"),
|
|
||||||
ALL_CHAPTERS_QUERY("SELECT * FROM chapters WHERE id_chapter > 0"),
|
|
||||||
|
|
||||||
// GROUPS
|
|
||||||
ALL_GROUPS("SELECT * FROM groups"),
|
|
||||||
GET_GROUP_FOR_PLAYER("SELECT g.* FROM groups g JOIN containsGroups cg ON cg.fk_group = g.id_group WHERE cg.fk_player = ? AND g.fk_chapter = ?"), // AND g.fk_puzzle = ?
|
|
||||||
GET_GROUP_ID_BY_DATA("SELECT id_group FROM groups WHERE name = ? AND (fk_chapter = ?)"), // OR fk_puzzle = ?
|
|
||||||
INSERT_GROUP("INSERT INTO groups (name, fk_chapter) VALUES (?,?)"),
|
|
||||||
INSERT_PLAYER_IN_GROUP("INSERT INTO containsGroups (fk_player, fk_group) VALUES (?,?)"),
|
|
||||||
LEAVE_GROUP("DELETE FROM containsGroups WHERE fk_player = ? AND fk_group = ?"),
|
|
||||||
|
|
||||||
// LEADERBOARD
|
|
||||||
ALL_PLAYERS_FOR_LEADERBOARD(
|
|
||||||
"select p.*, scores.*, g.* from players p ,(SELECT fk_player, SUM(c.score) AS score, COUNT(c.id_completion) AS completions, SUM(c.tries) AS tries, rank() over(ORDER BY score DESC) AS rank FROM completions c GROUP BY c.fk_player) AS scores LEFT JOIN containsGroups cg ON scores.fk_player = cg.fk_player LEFT JOIN groups g ON cg.fk_group = g.id_group WHERE p.id_player = scores.fk_player ORDER BY g.fk_chapter"),
|
|
||||||
ALL_GROUP_FOR_CHAPTER_LEADERBOARD(
|
|
||||||
"SELECT g.*, pl.pseudo, co.score, co.tries FROM groups g LEFT JOIN containsGroups cg ON g.id_group = cg.fk_group LEFT JOIN players pl ON cg.fk_player = pl.id_player LEFT JOIN completions co ON pl.id_player = co.fk_player WHERE cg.fk_player IS NOT NULL AND fk_chapter = ? AND (co.fk_puzzle IN (SELECT id_puzzle FROM puzzles puz WHERE puz.fk_chapter = g.fk_chapter) OR co.score IS NULL);"),
|
|
||||||
|
|
||||||
// REGISTER
|
|
||||||
CHECK_PSEUDO_AVAILABLE_QUERY("SELECT * FROM players WHERE pseudo = ?"),
|
|
||||||
CHECK_EMAIL_AVAILABLE_QUERY("SELECT * FROM players WHERE email = ?"),
|
|
||||||
REGISTER_QUERY(
|
|
||||||
"INSERT INTO players (pseudo, email, passwd, firstname, lastname, description, avatar) VALUES (?, ?, ?, ?, ?, ?, ?)"),
|
|
||||||
REGISTER_PLAYER_IN_EXISTING_GROUP(
|
|
||||||
"INSERT INTO containsGroups (fk_player, fk_group) VALUES (?, (SELECT id_group FROM groups WHERE name = ?));"),
|
|
||||||
|
|
||||||
// LOGIN
|
|
||||||
CHECK_PASSWORD("SELECT id_player, passwd FROM players WHERE pseudo=?"),
|
|
||||||
|
|
||||||
// COMPLETIONS
|
|
||||||
GET_COMPLETION(
|
|
||||||
"SELECT * FROM completions WHERE fk_puzzle = ? AND fk_player = ?"),
|
|
||||||
GET_COMPLETION_GROUP("SELECT c.*\r\n"
|
|
||||||
+ "FROM completions c\r\n"
|
|
||||||
+ "JOIN containsGroups cG on c.fk_player = cG.fk_player\r\n"
|
|
||||||
+ "JOIN containsGroups cGs on cGs.fk_group = cG.fk_group\r\n"
|
|
||||||
+ "JOIN groups g on cG.fk_group = g.id_group\r\n"
|
|
||||||
+ "JOIN puzzles p on p.id_puzzle = c.fk_puzzle\r\n"
|
|
||||||
+ "WHERE cGs.fk_player = ? AND p.id_puzzle = ?"),
|
|
||||||
INSERT_COMPLETION(
|
|
||||||
"INSERT INTO completions (fk_puzzle, fk_player, tries, code, fileName, score) values (?, ?, ?, ?, ?, ?)"),
|
|
||||||
UPDATE_COMPLETION(
|
|
||||||
"UPDATE completions SET tries = ?, score = ?, fk_player = ? WHERE fk_puzzle = ? AND fk_player = ?"),
|
|
||||||
SCORE("SELECT score FROM completions WHERE fk_player = ? AND fk_puzzle = ?"),
|
|
||||||
SCORE_GROUP("SELECT c.score\r\n"
|
|
||||||
+ "FROM completions c\r\n"
|
|
||||||
+ "JOIN containsGroups cG on c.fk_player = cG.fk_player\r\n"
|
|
||||||
+ "JOIN containsGroups cGs on cGs.fk_group = cG.fk_group\r\n"
|
|
||||||
+ "JOIN groups g on cG.fk_group = g.id_group\r\n"
|
|
||||||
+ "JOIN puzzles p on p.id_puzzle = c.fk_puzzle\r\n"
|
|
||||||
+ "WHERE cGs.fk_player = ? AND p.id_puzzle = ?"),
|
|
||||||
|
|
||||||
// PLAYERS
|
|
||||||
GET_PLAYER_SIMPLE("SELECT pseudo, email, firstname, lastname, description FROM players WHERE id_player = ?"),
|
|
||||||
GET_PLAYER_DETAILS("SELECT p.*, g.*\r\n"
|
|
||||||
+ "FROM players p\r\n"
|
|
||||||
+ "LEFT OUTER JOIN containsGroups cg ON p.id_player = cg.fk_player\r\n"
|
|
||||||
+ "LEFT OUTER JOIN groups g ON cg.fk_group = g.id_group\r\n"
|
|
||||||
+ "LEFT OUTER JOIN completions c on p.id_player = c.fk_player\r\n"
|
|
||||||
+ "WHERE "),
|
|
||||||
GET_PLAYER_DETAILS_BY_ID(GET_PLAYER_DETAILS, " p.id_player = ? GROUP BY g.name ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
|
||||||
GET_PLAYER_DETAILS_BY_PSEUDO(GET_PLAYER_DETAILS, "p.pseudo = ? GROUP BY g.name ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
|
||||||
GET_PLAYER_COMPLETIONS("select c.*, p.name from completions c left join puzzles p on c.fk_puzzle = p.id_puzzle where fk_player = ?;"),
|
|
||||||
GET_PLAYER_RANK("SELECT * FROM (SELECT fk_player, RANK() OVER(ORDER BY SUM(score) DESC) rank FROM completions c LEFT JOIN players p ON p.id_player = c.fk_player GROUP BY fk_player ORDER BY rank) AS ranks WHERE ranks.fk_player = ?;"),
|
|
||||||
|
|
||||||
// BADGES
|
|
||||||
GET_BADGE("SELECT * FROM badges WHERE id_badge = ?"), GET_BADGES_OF_PLAYER(
|
|
||||||
"SELECT * FROM badges b LEFT JOIN containsBadges cb ON cb.fk_badge = b.id_badge WHERE cb.fk_player = ?"),
|
|
||||||
|
|
||||||
//TRIGGER
|
|
||||||
FIRST_TRY("CREATE OR REPLACE TRIGGER FirstTry\r\n"
|
|
||||||
+ "AFTER INSERT\r\n"
|
|
||||||
+ " ON completions FOR EACH ROW\r\n"
|
|
||||||
+ "BEGIN\r\n"
|
|
||||||
+ " DECLARE badge INT;\r\n"
|
|
||||||
+ " DECLARE contain INT;\r\n"
|
|
||||||
+ " \r\n"
|
|
||||||
+ " SELECT id_badge\r\n"
|
|
||||||
+ " INTO @badge\r\n"
|
|
||||||
+ " FROM badges\r\n"
|
|
||||||
+ " WHERE name = 'FirstTry';\r\n"
|
|
||||||
+ " \r\n"
|
|
||||||
+ " IF @badge is not null THEN\r\n"
|
|
||||||
+ " \r\n"
|
|
||||||
+ " SELECT count(*)\r\n"
|
|
||||||
+ " INTO @contain\r\n"
|
|
||||||
+ " FROM containsBadges\r\n"
|
|
||||||
+ " WHERE fk_badge = @badge AND fk_player = NEW.fk_player;\r\n"
|
|
||||||
+ " \r\n"
|
|
||||||
+ " IF (@contain = 0) THEN\r\n"
|
|
||||||
+ " INSERT INTO containsBadges(fk_player, fk_badge) VALUES (NEW.fk_player, @badge);\r\n"
|
|
||||||
+ " END IF;\r\n"
|
|
||||||
+ " END IF;\r\n"
|
|
||||||
+ "END;"),
|
|
||||||
EventParticipation("CREATE OR REPLACE TRIGGER EventParticipation\r\n"
|
|
||||||
+ "AFTER INSERT\r\n"
|
|
||||||
+ " ON completions FOR EACH ROW\r\n"
|
|
||||||
+ "BEGIN\r\n"
|
|
||||||
+ " DECLARE badge INT;\r\n"
|
|
||||||
+ " DECLARE endDate datetime;\r\n"
|
|
||||||
+ " DECLARE player INT;\r\n"
|
|
||||||
+ " DECLARE contain INT;\r\n"
|
|
||||||
+ "\r\n"
|
|
||||||
+ " SELECT id_badge\r\n"
|
|
||||||
+ " INTO badge\r\n"
|
|
||||||
+ " FROM badges\r\n"
|
|
||||||
+ " WHERE name = 'EventParticipation';\r\n"
|
|
||||||
+ "\r\n"
|
|
||||||
+ " IF @badge is not null THEN\r\n"
|
|
||||||
+ "\r\n"
|
|
||||||
+ " SELECT c.end_date, i.fk_player\r\n"
|
|
||||||
+ " INTO endDate, player\r\n"
|
|
||||||
+ " FROM inserted i\r\n"
|
|
||||||
+ " JOIN puzzles p ON i.fk_puzzle = p.id_puzzle\r\n"
|
|
||||||
+ " JOIN chapters c on p.fk_chapter = c.id_chapter;\r\n"
|
|
||||||
+ "\r\n"
|
|
||||||
+ " IF @endDate is not null THEN\r\n"
|
|
||||||
+ "\r\n"
|
|
||||||
+ " SELECT count(*)\r\n"
|
|
||||||
+ " INTO contain\r\n"
|
|
||||||
+ " FROM containsBadges\r\n"
|
|
||||||
+ " WHERE fk_badge = badge AND fk_player = player;\r\n"
|
|
||||||
+ "\r\n"
|
|
||||||
+ " IF (@contain = 0) THEN\r\n"
|
|
||||||
+ " INSERT INTO containsBadges(fk_player, fk_badge) VALUES (@player, @badge);\r\n"
|
|
||||||
+ " END IF;\r\n"
|
|
||||||
+ " END IF;\r\n"
|
|
||||||
+ " END IF;\r\n"
|
|
||||||
+ "END;");
|
|
||||||
|
|
||||||
private String request;
|
|
||||||
|
|
||||||
DatabaseQuery(DatabaseQuery parent, String request) {
|
|
||||||
this.request = parent.request + request;
|
|
||||||
}
|
|
||||||
|
|
||||||
DatabaseQuery(String request) {
|
|
||||||
this.request = request;
|
|
||||||
}
|
|
||||||
|
|
||||||
public PreparedStatement prepare(Connection con) throws SQLException {
|
|
||||||
return con.prepareStatement(this.request);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return this.request;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
*
|
|
||||||
|
|
||||||
SELECT p.*, scores.score, scores.completions, scores.tries, scores.rank, g.*
|
|
||||||
FROM players p,
|
|
||||||
(SELECT fk_player, SUM(c.score) AS score,
|
|
||||||
COUNT(c.id_completion) AS completions,
|
|
||||||
SUM(c.tries) AS tries,
|
|
||||||
rank() over(ORDER BY score DESC) AS rank
|
|
||||||
FROM completions c GROUP BY c.fk_player)
|
|
||||||
AS scores
|
|
||||||
LEFT OUTER JOIN containsGroups cg ON scores.fk_player = cg.fk_player
|
|
||||||
LEFT OUTER JOIN groups g ON cg.fk_group = g.id_group
|
|
||||||
WHERE p.id_player = scores.fk_player AND "),
|
|
||||||
|
|
||||||
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
/* TRIGGER PLACE
|
|
||||||
*
|
|
||||||
|
|
||||||
CREATE OR REPLACE TRIGGER FirstTry
|
|
||||||
AFTER INSERT
|
|
||||||
ON completions FOR EACH ROW
|
|
||||||
BEGIN
|
|
||||||
DECLARE badge INT;
|
|
||||||
DECLARE contain INT;
|
|
||||||
|
|
||||||
SELECT id_badge
|
|
||||||
INTO @badge
|
|
||||||
FROM badges
|
|
||||||
WHERE name = 'FirstTry';
|
|
||||||
|
|
||||||
IF @badge is not null THEN
|
|
||||||
|
|
||||||
SELECT count(*)
|
|
||||||
INTO @contain
|
|
||||||
FROM containsBadges
|
|
||||||
WHERE fk_badge = @badge AND fk_player = NEW.fk_player;
|
|
||||||
|
|
||||||
IF (@contain = 0) THEN
|
|
||||||
|
|
||||||
IF (NEW.score >= 0 AND NEW.tries < 2) THEN
|
|
||||||
INSERT INTO containsBadges(fk_player, fk_badge) VALUES (NEW.fk_player, @badge);
|
|
||||||
END IF;
|
|
||||||
END IF;
|
|
||||||
END IF;
|
|
||||||
END;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
CREATE OR REPLACE TRIGGER EventParticipation
|
|
||||||
AFTER INSERT
|
|
||||||
ON completions FOR EACH ROW
|
|
||||||
BEGIN
|
|
||||||
DECLARE badge INT;
|
|
||||||
DECLARE endDate datetime;
|
|
||||||
DECLARE player INT;
|
|
||||||
DECLARE contain INT;
|
|
||||||
|
|
||||||
SELECT id_badge
|
|
||||||
INTO badge
|
|
||||||
FROM badges
|
|
||||||
WHERE name = 'EventParticipation';
|
|
||||||
|
|
||||||
IF @badge is not null THEN
|
|
||||||
|
|
||||||
SELECT c.end_date, i.fk_player
|
|
||||||
INTO endDate, player
|
|
||||||
FROM inserted i
|
|
||||||
JOIN puzzles p ON i.fk_puzzle = p.id_puzzle
|
|
||||||
JOIN chapters c on p.fk_chapter = c.id_chapter;
|
|
||||||
|
|
||||||
IF @endDate is not null THEN
|
|
||||||
|
|
||||||
SELECT count(*)
|
|
||||||
INTO contain
|
|
||||||
FROM containsBadges
|
|
||||||
WHERE fk_badge = badge AND fk_player = player;
|
|
||||||
|
|
||||||
IF (@contain = 0) THEN
|
|
||||||
INSERT INTO containsBadges(fk_player, fk_badge) VALUES (@player, @badge);
|
|
||||||
END IF;
|
|
||||||
END IF;
|
|
||||||
END IF;
|
|
||||||
END;
|
|
||||||
|
|
||||||
|
|
||||||
*
|
|
||||||
*/
|
|
|
@ -1,699 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.repository;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.ResultSetMetaData;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.SortedSet;
|
|
||||||
import java.util.TreeSet;
|
|
||||||
|
|
||||||
import com.password4j.Hash;
|
|
||||||
import com.password4j.Password;
|
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.Configuration;
|
|
||||||
import be.jeffcheasey88.peeratcode.framework.User;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Badge;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Chapter;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Completion;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Group;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Player;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Puzzle;
|
|
||||||
|
|
||||||
public class DatabaseRepository {
|
|
||||||
|
|
||||||
private Connection con;
|
|
||||||
private Configuration config;
|
|
||||||
|
|
||||||
public DatabaseRepository(Configuration config) {
|
|
||||||
this.config = config;
|
|
||||||
}
|
|
||||||
// testTrigger();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private void testTrigger(){
|
|
||||||
// try {
|
|
||||||
// ensureConnection();
|
|
||||||
// }catch(Exception e){
|
|
||||||
// e.printStackTrace();
|
|
||||||
// }
|
|
||||||
// System.out.println("connection ensured");
|
|
||||||
//
|
|
||||||
// try {
|
|
||||||
// PreparedStatement log = this.con.prepareStatement("DROP TABLE mycustomlog;");
|
|
||||||
// log.execute();
|
|
||||||
// }catch(Exception e){
|
|
||||||
// e.printStackTrace();
|
|
||||||
// }
|
|
||||||
// System.out.println("log dropped");
|
|
||||||
//
|
|
||||||
// try {
|
|
||||||
// PreparedStatement log = this.con.prepareStatement("CREATE TABLE mycustomlog(\r\n"
|
|
||||||
// + " message VARCHAR(255),\r\n"
|
|
||||||
// + " primary key (message)\r\n"
|
|
||||||
// + ");");
|
|
||||||
// log.execute();
|
|
||||||
// }catch(Exception e){
|
|
||||||
// e.printStackTrace();
|
|
||||||
// }
|
|
||||||
// System.out.println("log created");
|
|
||||||
//
|
|
||||||
// try {
|
|
||||||
// System.out.println(DatabaseQuery.FIRST_TRY.toString());
|
|
||||||
// DatabaseQuery.FIRST_TRY.prepare(this.con).execute();
|
|
||||||
// }catch(Exception e){
|
|
||||||
// e.printStackTrace();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// System.out.println("trigger inserted");
|
|
||||||
//
|
|
||||||
// try {
|
|
||||||
// insertCompletion(new Completion(1, 1, 1, null, 1));
|
|
||||||
// } catch (SQLException e1) {
|
|
||||||
// e1.printStackTrace();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// try {
|
|
||||||
// showLog();
|
|
||||||
// } catch (Exception e) {
|
|
||||||
// e.printStackTrace();
|
|
||||||
// }
|
|
||||||
// System.out.println("------------------------------");
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private void showLog() throws Exception{
|
|
||||||
// ensureConnection();
|
|
||||||
//
|
|
||||||
// PreparedStatement stmt = this.con.prepareStatement("SELECT * FROM mycustomlog");
|
|
||||||
// ResultSet result = stmt.executeQuery();
|
|
||||||
// while(result.next()){
|
|
||||||
// System.out.println("[LOG] "+result.getString("message"));
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
private void ensureConnection() throws SQLException {
|
|
||||||
if (con == null || (!con.isValid(5))) {
|
|
||||||
this.con = DriverManager.getConnection(
|
|
||||||
"jdbc:mysql://" + config.getDbHost() + ":" + config.getDbPort() + "/" + config.getDbDatabase() + "",
|
|
||||||
config.getDbUser(), config.getDbPassword());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Puzzle makePuzzle(ResultSet puzzleResult) throws SQLException {
|
|
||||||
return new Puzzle(puzzleResult.getInt("id_puzzle"), puzzleResult.getString("name"),
|
|
||||||
puzzleResult.getString("content"), puzzleResult.getBytes("soluce"), puzzleResult.getString("verify"),
|
|
||||||
puzzleResult.getInt("score_max"), puzzleResult.getString("tags"),
|
|
||||||
hasColumn(puzzleResult, "origin") ? puzzleResult.getInt("origin") : -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Chapter makeChapter(ResultSet chapterResult) throws SQLException {
|
|
||||||
return new Chapter(chapterResult.getInt("id_chapter"), chapterResult.getString("name"),
|
|
||||||
chapterResult.getTimestamp("start_date"), chapterResult.getTimestamp("end_date"));
|
|
||||||
}
|
|
||||||
|
|
||||||
private Completion makeCompletion(ResultSet completionResult) throws SQLException {
|
|
||||||
String fileName = null;
|
|
||||||
if (hasColumn(completionResult, "fileName"))
|
|
||||||
fileName = completionResult.getString("fileName");
|
|
||||||
String puzzleName = null;
|
|
||||||
if (hasColumn(completionResult, "name"))
|
|
||||||
puzzleName = completionResult.getString("name");
|
|
||||||
|
|
||||||
return new Completion(completionResult.getInt("fk_player"), completionResult.getInt("fk_puzzle"), completionResult.getInt("tries"),
|
|
||||||
fileName, completionResult.getInt("score"), puzzleName);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Player makePlayer(ResultSet playerResult, int id) throws SQLException {
|
|
||||||
Player p = new Player(playerResult.getString("pseudo"), playerResult.getString("email"),
|
|
||||||
playerResult.getString("firstName"), playerResult.getString("lastName"),
|
|
||||||
playerResult.getString("description"));
|
|
||||||
if (hasColumn(playerResult, "avatar")) {
|
|
||||||
p.setAvatar(playerResult.getBytes("avatar"));
|
|
||||||
}
|
|
||||||
if (hasColumn(playerResult, "score")) {
|
|
||||||
p.addCompletion(new Completion(playerResult.getInt("tries"), playerResult.getInt("score")));
|
|
||||||
for (int ct = 1; ct < playerResult.getInt("completions"); ct++)
|
|
||||||
{ // TODO refactor for V3
|
|
||||||
p.addCompletion(new Completion(0, 0));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Manage groups
|
|
||||||
String groupName = playerResult.getString("name");
|
|
||||||
if (groupName != null) {
|
|
||||||
p.addGroup(makeGroup(playerResult));
|
|
||||||
}
|
|
||||||
|
|
||||||
// ADD rank
|
|
||||||
PreparedStatement completionsStmt = DatabaseQuery.GET_PLAYER_RANK.prepare(con);
|
|
||||||
completionsStmt.setInt(1, id);
|
|
||||||
ResultSet result = completionsStmt.executeQuery();
|
|
||||||
while (result.next()) {
|
|
||||||
p.setRank(result.getInt("rank"));
|
|
||||||
}
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Group makeGroup(ResultSet result) throws SQLException {
|
|
||||||
return new Group(result.getString("name"), result.getInt("fk_chapter"), result.getInt("fk_puzzle"));
|
|
||||||
}
|
|
||||||
|
|
||||||
private Player makeGroupPlayer(ResultSet result) throws SQLException {
|
|
||||||
return new Player(result.getString("pseudo"), result.getInt("score"), result.getInt("tries"));
|
|
||||||
}
|
|
||||||
|
|
||||||
private Badge makeBadge(ResultSet rs) throws SQLException {
|
|
||||||
return new Badge(rs.getString("name"), rs.getBytes("logo"), rs.getInt("level"));
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean hasColumn(ResultSet rs, String columnName) throws SQLException {
|
|
||||||
// Found on StackOverflow
|
|
||||||
ResultSetMetaData rsmd = rs.getMetaData();
|
|
||||||
int columns = rsmd.getColumnCount();
|
|
||||||
for (int x = 1; x <= columns; x++) {
|
|
||||||
if (columnName.equals(rsmd.getColumnName(x)))
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<Puzzle> getPuzzlesInChapter(int id) throws SQLException {
|
|
||||||
List<Puzzle> puzzles = new ArrayList<>();
|
|
||||||
ensureConnection();
|
|
||||||
PreparedStatement puzzleStmt = DatabaseQuery.PUZZLES_IN_CHAPTER_QUERY.prepare(this.con);
|
|
||||||
puzzleStmt.setInt(1, id);
|
|
||||||
ResultSet puzzleResult = puzzleStmt.executeQuery();
|
|
||||||
while (puzzleResult.next()) {
|
|
||||||
puzzles.add(makePuzzle(puzzleResult));
|
|
||||||
}
|
|
||||||
return puzzles;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a specific puzzle
|
|
||||||
*
|
|
||||||
* @param id The id of the puzzle
|
|
||||||
* @return The puzzle or null if an error occurred
|
|
||||||
*/
|
|
||||||
public Puzzle getPuzzle(int id) {
|
|
||||||
try {
|
|
||||||
ensureConnection();
|
|
||||||
PreparedStatement puzzleStmt = DatabaseQuery.SPECIFIC_PUZZLE_QUERY.prepare(this.con);
|
|
||||||
puzzleStmt.setInt(1, id);
|
|
||||||
ResultSet puzzleResult = puzzleStmt.executeQuery();
|
|
||||||
if (puzzleResult.next()) {
|
|
||||||
return makePuzzle(puzzleResult);
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getScore(int user, int puzzle) {
|
|
||||||
try {
|
|
||||||
ensureConnection();
|
|
||||||
PreparedStatement stmt = DatabaseQuery.SCORE_GROUP.prepare(this.con);
|
|
||||||
stmt.setInt(1, user);
|
|
||||||
stmt.setInt(2, puzzle);
|
|
||||||
ResultSet result = stmt.executeQuery();
|
|
||||||
if (result.next())
|
|
||||||
return result.getInt("score");
|
|
||||||
|
|
||||||
stmt = DatabaseQuery.SCORE.prepare(this.con);
|
|
||||||
stmt.setInt(1, user);
|
|
||||||
stmt.setInt(2, puzzle);
|
|
||||||
|
|
||||||
result = stmt.executeQuery();
|
|
||||||
if (result.next())
|
|
||||||
return result.getInt("score");
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Completion getCompletionGroup(int user, int puzzle) {
|
|
||||||
try {
|
|
||||||
PreparedStatement stmt = DatabaseQuery.GET_COMPLETION_GROUP.prepare(this.con);
|
|
||||||
stmt.setInt(1, user);
|
|
||||||
stmt.setInt(2, puzzle);
|
|
||||||
ResultSet result = stmt.executeQuery();
|
|
||||||
if (result.next())
|
|
||||||
return makeCompletion(result);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return getCompletion(user, puzzle);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Completion getCompletion(int playerId, int puzzleId) {
|
|
||||||
try {
|
|
||||||
PreparedStatement completionsStmt = DatabaseQuery.GET_COMPLETION.prepare(this.con);
|
|
||||||
completionsStmt.setInt(1, puzzleId);
|
|
||||||
completionsStmt.setInt(2, playerId);
|
|
||||||
ResultSet result = completionsStmt.executeQuery();
|
|
||||||
if (result.next()) {
|
|
||||||
return makeCompletion(result);
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Player getPlayer(int idPlayer) {
|
|
||||||
try {
|
|
||||||
PreparedStatement completionsStmt = DatabaseQuery.GET_PLAYER_SIMPLE.prepare(this.con);
|
|
||||||
completionsStmt.setInt(1, idPlayer);
|
|
||||||
ResultSet result = completionsStmt.executeQuery();
|
|
||||||
if (result.next()) {
|
|
||||||
return makePlayer(result, idPlayer);
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Player getPlayerDetails(int idPlayer) {
|
|
||||||
return getPlayerDetails(idPlayer, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Player getPlayerDetails(String pseudoPlayer) {
|
|
||||||
return getPlayerDetails(-1, pseudoPlayer);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Player getPlayerDetails(int id, String pseudo) {
|
|
||||||
try {
|
|
||||||
ensureConnection();
|
|
||||||
PreparedStatement completionsStmt;
|
|
||||||
if (pseudo != null) {
|
|
||||||
completionsStmt = DatabaseQuery.GET_PLAYER_DETAILS_BY_PSEUDO.prepare(this.con);
|
|
||||||
completionsStmt.setString(1, pseudo);
|
|
||||||
} else {
|
|
||||||
completionsStmt = DatabaseQuery.GET_PLAYER_DETAILS_BY_ID.prepare(this.con);
|
|
||||||
completionsStmt.setInt(1, id);
|
|
||||||
}
|
|
||||||
ResultSet result = completionsStmt.executeQuery();
|
|
||||||
Player player = null;
|
|
||||||
while (result.next()) {
|
|
||||||
if (player == null) {
|
|
||||||
id = result.getInt("id_player");
|
|
||||||
player = makePlayer(result, id);
|
|
||||||
completionsStmt = DatabaseQuery.GET_BADGES_OF_PLAYER.prepare(this.con);
|
|
||||||
completionsStmt.setInt(1, id);
|
|
||||||
ResultSet resultBadges = completionsStmt.executeQuery();
|
|
||||||
while (resultBadges.next()) {
|
|
||||||
player.addBadge(makeBadge(resultBadges));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
player.addGroup(makeGroup(result));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// ADD completions
|
|
||||||
completionsStmt = DatabaseQuery.GET_PLAYER_COMPLETIONS.prepare(con);
|
|
||||||
completionsStmt.setInt(1, id);
|
|
||||||
result = completionsStmt.executeQuery();
|
|
||||||
while (result.next()) {
|
|
||||||
player.addCompletion(makeCompletion(result));
|
|
||||||
}
|
|
||||||
|
|
||||||
return player;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SortedSet<Player> getAllPlayerForLeaderboard() {
|
|
||||||
try {
|
|
||||||
ensureConnection();
|
|
||||||
PreparedStatement playersStmt = DatabaseQuery.ALL_PLAYERS_FOR_LEADERBOARD.prepare(this.con);
|
|
||||||
ResultSet result = playersStmt.executeQuery();
|
|
||||||
ArrayList<Player> players = new ArrayList<Player>();
|
|
||||||
Player tmpPlayer;
|
|
||||||
while (result.next()) {
|
|
||||||
tmpPlayer = makePlayer(result, result.getInt("id_player"));
|
|
||||||
if (!players.contains(tmpPlayer)) {
|
|
||||||
players.add(tmpPlayer);
|
|
||||||
} else {
|
|
||||||
players.get(players.indexOf(tmpPlayer)).addGroup(makeGroup(result));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new TreeSet<Player>(players);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SortedSet<Group> getAllGroupForChapterLeaderboard(int chapterId){
|
|
||||||
try{
|
|
||||||
ensureConnection();
|
|
||||||
PreparedStatement groupsStmt = DatabaseQuery.ALL_GROUP_FOR_CHAPTER_LEADERBOARD.prepare(this.con);
|
|
||||||
groupsStmt.setInt(1, chapterId);
|
|
||||||
ResultSet result = groupsStmt.executeQuery();
|
|
||||||
List<Group> groups = new ArrayList<Group>();
|
|
||||||
Group tmpGroup;
|
|
||||||
while (result.next()) {
|
|
||||||
tmpGroup = makeGroup(result);
|
|
||||||
if (tmpGroup != null) {
|
|
||||||
int gPosition = groups.indexOf(tmpGroup);
|
|
||||||
if (gPosition < 0) {
|
|
||||||
tmpGroup.addPlayer(makeGroupPlayer(result));
|
|
||||||
groups.add(tmpGroup);
|
|
||||||
} else {
|
|
||||||
groups.get(gPosition).addPlayer(makeGroupPlayer(result));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new TreeSet<Group>(groups);
|
|
||||||
}catch(SQLException e){
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Badge getBadge(int badgeId) {
|
|
||||||
try {
|
|
||||||
ensureConnection();
|
|
||||||
PreparedStatement completionsStmt = DatabaseQuery.GET_BADGE.prepare(this.con);
|
|
||||||
completionsStmt.setInt(1, badgeId);
|
|
||||||
ResultSet result = completionsStmt.executeQuery();
|
|
||||||
if (result.next()) {
|
|
||||||
return makeBadge(result);
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a specific chapter
|
|
||||||
*
|
|
||||||
* @param id The id of the chapter
|
|
||||||
* @return The chapter or null if an error occurred
|
|
||||||
*/
|
|
||||||
public Chapter getChapter(int id) {
|
|
||||||
try {
|
|
||||||
ensureConnection();
|
|
||||||
PreparedStatement chapterStmt = DatabaseQuery.SPECIFIC_CHAPTER_QUERY.prepare(this.con);
|
|
||||||
chapterStmt.setInt(1, id);
|
|
||||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
|
||||||
if (chapterResult.next()) {
|
|
||||||
Chapter chapter = makeChapter(chapterResult);
|
|
||||||
List<Puzzle> puzzles = getPuzzlesInChapter(id);
|
|
||||||
chapter.setPuzzles(puzzles);
|
|
||||||
return chapter;
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Chapter getChapter(Puzzle puzzle){
|
|
||||||
try {
|
|
||||||
ensureConnection();
|
|
||||||
PreparedStatement chapterStmt = DatabaseQuery.CHAPTER_FROM_PUZZLE.prepare(this.con);
|
|
||||||
chapterStmt.setInt(1, puzzle.getId());
|
|
||||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
|
||||||
if (chapterResult.next()) {
|
|
||||||
Chapter chapter = makeChapter(chapterResult);
|
|
||||||
List<Puzzle> puzzles = getPuzzlesInChapter(chapter.getId());
|
|
||||||
chapter.setPuzzles(puzzles);
|
|
||||||
return chapter;
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get all chapters in the database
|
|
||||||
*
|
|
||||||
* @return List of all chapters or null if an error occurred
|
|
||||||
*/
|
|
||||||
public List<Chapter> getAllChapters() {
|
|
||||||
try {
|
|
||||||
List<Chapter> chapterList = new ArrayList<>();
|
|
||||||
ensureConnection();
|
|
||||||
PreparedStatement chapterStmt = DatabaseQuery.ALL_CHAPTERS_QUERY.prepare(this.con);
|
|
||||||
ResultSet chapterResult = chapterStmt.executeQuery();
|
|
||||||
while (chapterResult.next()) {
|
|
||||||
Chapter chapter = makeChapter(chapterResult);
|
|
||||||
chapter.setPuzzles(getPuzzlesInChapter(chapter.getId()));
|
|
||||||
chapterList.add(chapter);
|
|
||||||
}
|
|
||||||
return chapterList;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Group> getAllGroups() {
|
|
||||||
try {
|
|
||||||
ensureConnection();
|
|
||||||
List<Group> list = new ArrayList<>();
|
|
||||||
PreparedStatement stmt = DatabaseQuery.ALL_GROUPS.prepare(this.con);
|
|
||||||
ResultSet groupResult = stmt.executeQuery();
|
|
||||||
while (groupResult.next())
|
|
||||||
list.add(makeGroup(groupResult));
|
|
||||||
return list;
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if a pseudo is available
|
|
||||||
*
|
|
||||||
* @param pseudo The pseudo to check
|
|
||||||
* @return True if the pseudo is available, false if it's already taken
|
|
||||||
*/
|
|
||||||
public boolean checkPseudoAvailability(String pseudo) {
|
|
||||||
return checkAvailability(pseudo, DatabaseQuery.CHECK_PSEUDO_AVAILABLE_QUERY.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if an email is available
|
|
||||||
*
|
|
||||||
* @param email The email to check
|
|
||||||
* @return True if the email is available, false if it's already taken
|
|
||||||
*/
|
|
||||||
public boolean checkEmailAvailability(String email) {
|
|
||||||
return checkAvailability(email, DatabaseQuery.CHECK_EMAIL_AVAILABLE_QUERY.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean checkAvailability(String queriedString, String correspondingQuery) {
|
|
||||||
try {
|
|
||||||
ensureConnection();
|
|
||||||
PreparedStatement statement = con.prepareStatement(correspondingQuery);
|
|
||||||
statement.setString(1, queriedString);
|
|
||||||
ResultSet result = statement.executeQuery();
|
|
||||||
return !result.next();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Register a new user
|
|
||||||
*
|
|
||||||
* @param pseudo The pseudo of the user
|
|
||||||
* @param email The email of the user
|
|
||||||
* @param password The password of the user
|
|
||||||
* @param firstname The firstname of the user
|
|
||||||
* @param lastname The lastname of the user
|
|
||||||
* @param description The description of the user
|
|
||||||
* @param sgroup The group of the user
|
|
||||||
* @param avatar The avatar of the user
|
|
||||||
* @return True if the user was registered, false if an error occurred
|
|
||||||
*/
|
|
||||||
public int register(String pseudo, String email, String password, String firstname, String lastname,
|
|
||||||
String description, String sgroup, String avatar) {
|
|
||||||
Hash hash = Password.hash(password).withArgon2();
|
|
||||||
try {
|
|
||||||
ensureConnection();
|
|
||||||
con.setAutoCommit(false);
|
|
||||||
try (PreparedStatement playerStatement = con.prepareStatement(DatabaseQuery.REGISTER_QUERY.toString(),
|
|
||||||
Statement.RETURN_GENERATED_KEYS)) {
|
|
||||||
playerStatement.setString(1, pseudo);
|
|
||||||
playerStatement.setString(2, email);
|
|
||||||
playerStatement.setString(3, hash.getResult());
|
|
||||||
playerStatement.setString(4, firstname);
|
|
||||||
playerStatement.setString(5, lastname);
|
|
||||||
playerStatement.setString(6, description);
|
|
||||||
playerStatement.setString(7, avatar);
|
|
||||||
if (playerStatement.executeUpdate() == 1) {
|
|
||||||
ResultSet inserted = playerStatement.getGeneratedKeys();
|
|
||||||
if (inserted.next()) {
|
|
||||||
int newPlayerId = inserted.getInt(1);
|
|
||||||
if (!sgroup.isEmpty()) {
|
|
||||||
try (PreparedStatement containsGroupsStatement = con
|
|
||||||
.prepareStatement(DatabaseQuery.REGISTER_PLAYER_IN_EXISTING_GROUP.toString())) {
|
|
||||||
containsGroupsStatement.setInt(1, newPlayerId);
|
|
||||||
containsGroupsStatement.setString(2, sgroup);
|
|
||||||
containsGroupsStatement.executeUpdate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
con.commit();
|
|
||||||
con.setAutoCommit(true);
|
|
||||||
return newPlayerId;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
con.rollback();
|
|
||||||
con.setAutoCommit(true);
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Login a user
|
|
||||||
*
|
|
||||||
* @param username The username of the user
|
|
||||||
* @param password The password of the user
|
|
||||||
* @return id the id of the user, -1 if not login successefuly
|
|
||||||
*/
|
|
||||||
public int login(String username, String password) {
|
|
||||||
try {
|
|
||||||
ensureConnection();
|
|
||||||
PreparedStatement statement = con.prepareStatement(DatabaseQuery.CHECK_PASSWORD.toString());
|
|
||||||
DatabaseQuery.PUZZLES_IN_CHAPTER_QUERY.prepare(this.con);
|
|
||||||
statement.setString(1, username);
|
|
||||||
ResultSet result = statement.executeQuery();
|
|
||||||
if (result.next()) {
|
|
||||||
String hashedPassword = result.getString("passwd");
|
|
||||||
if (Password.check(password, hashedPassword).withArgon2())
|
|
||||||
return result.getInt("id_player");
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Completion insertOrUpdatePuzzleResponse(int puzzleId, int userId, String fileName, byte[] code,
|
|
||||||
byte[] response, Puzzle currentPuzzle){
|
|
||||||
try {
|
|
||||||
ensureConnection();
|
|
||||||
Completion completion = getCompletionGroup(userId, puzzleId);
|
|
||||||
if (completion == null){
|
|
||||||
System.out.println("Completion is null");
|
|
||||||
completion = new Completion(userId, puzzleId, fileName, code, response, currentPuzzle);
|
|
||||||
insertCompletion(completion);
|
|
||||||
} else {
|
|
||||||
System.out.println(completion);
|
|
||||||
completion.addTry(currentPuzzle, response);
|
|
||||||
int lastUserId = completion.getPlayerId();
|
|
||||||
completion.updatePlayer(userId);
|
|
||||||
updateCompletion(completion, lastUserId);
|
|
||||||
}
|
|
||||||
return completion;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void insertCompletion(Completion newCompletion) throws SQLException {
|
|
||||||
PreparedStatement statement = DatabaseQuery.INSERT_COMPLETION.prepare(this.con);
|
|
||||||
statement.setInt(1, newCompletion.getPuzzleId());
|
|
||||||
statement.setInt(2, newCompletion.getPlayerId());
|
|
||||||
statement.setInt(3, newCompletion.getTries());
|
|
||||||
statement.setBytes(4, newCompletion.getCode());
|
|
||||||
statement.setString(5, newCompletion.getFileName());
|
|
||||||
statement.setInt(6, newCompletion.getScore());
|
|
||||||
statement.executeUpdate();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean insertGroup(Group group, User creator) throws SQLException {
|
|
||||||
Integer groupId = getGroupId(group);
|
|
||||||
if (groupId == null)
|
|
||||||
ensureConnection();
|
|
||||||
PreparedStatement statement = DatabaseQuery.INSERT_GROUP.prepare(this.con);
|
|
||||||
statement.setString(1, group.getName());
|
|
||||||
statement.setObject(2, group.getLinkToChapter());
|
|
||||||
// statement.setObject(3, group.getLinkToPuzzle());
|
|
||||||
if (statement.executeUpdate() >= 0)
|
|
||||||
return insertUserInGroup(group, creator);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Group getPlayerGroup(int user, Integer chapter) throws SQLException {
|
|
||||||
ensureConnection();
|
|
||||||
PreparedStatement stmt = DatabaseQuery.GET_GROUP_FOR_PLAYER.prepare(this.con);
|
|
||||||
stmt.setInt(1, user);
|
|
||||||
stmt.setObject(2, chapter);
|
|
||||||
// stmt.setObject(3, puzzle);
|
|
||||||
|
|
||||||
ResultSet result = stmt.executeQuery();
|
|
||||||
if (result.next())
|
|
||||||
return makeGroup(result);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer getGroupId(Group group) throws SQLException {
|
|
||||||
ensureConnection();
|
|
||||||
PreparedStatement stmt = DatabaseQuery.GET_GROUP_ID_BY_DATA.prepare(this.con);
|
|
||||||
stmt.setString(1, group.getName());
|
|
||||||
stmt.setObject(2, group.getLinkToChapter());
|
|
||||||
// stmt.setObject(3, group.getLinkToPuzzle());
|
|
||||||
|
|
||||||
ResultSet result = stmt.executeQuery();
|
|
||||||
if (result.next())
|
|
||||||
return result.getInt("id_group");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean insertUserInGroup(Group group, User user) throws SQLException {
|
|
||||||
Integer id = getGroupId(group);
|
|
||||||
Group alreadyInGroup = getPlayerGroup(user.getId(), group.getLinkToChapter());
|
|
||||||
if (id != null && alreadyInGroup == null) {
|
|
||||||
PreparedStatement stmt = DatabaseQuery.INSERT_PLAYER_IN_GROUP.prepare(this.con);
|
|
||||||
|
|
||||||
stmt.setInt(1, user.getId());
|
|
||||||
stmt.setInt(2, id);
|
|
||||||
|
|
||||||
return stmt.executeUpdate() >= 0;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean leaveGroup(Group group, User user) throws SQLException {
|
|
||||||
Integer id = getGroupId(group);
|
|
||||||
if (id != null) {
|
|
||||||
PreparedStatement stmt = DatabaseQuery.LEAVE_GROUP.prepare(this.con);
|
|
||||||
|
|
||||||
stmt.setInt(1, user.getId());
|
|
||||||
stmt.setInt(2, id);
|
|
||||||
|
|
||||||
return stmt.executeUpdate() >= 0;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateCompletion(Completion completionToUpdate, int user) throws SQLException{
|
|
||||||
System.out.println("update "+completionToUpdate);
|
|
||||||
PreparedStatement statement = DatabaseQuery.UPDATE_COMPLETION.prepare(this.con);
|
|
||||||
statement.setInt(1, completionToUpdate.getTries());
|
|
||||||
statement.setInt(2, completionToUpdate.getScore());
|
|
||||||
statement.setInt(3, completionToUpdate.getPlayerId());
|
|
||||||
statement.setInt(4, completionToUpdate.getPuzzleId());
|
|
||||||
statement.setInt(5, user);
|
|
||||||
statement.executeUpdate();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,47 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes;
|
|
||||||
|
|
||||||
import java.util.Base64;
|
|
||||||
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.User;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Badge;
|
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
|
||||||
|
|
||||||
public class BadgeDetails implements Response {
|
|
||||||
|
|
||||||
private final DatabaseRepository databaseRepo;
|
|
||||||
|
|
||||||
public BadgeDetails(DatabaseRepository databaseRepo) {
|
|
||||||
this.databaseRepo = databaseRepo;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RouteDoc(path = "/badge/<id>", responseCode = 200, responseDescription = "JSON contenant les informations du badge")
|
|
||||||
@RouteDoc(responseCode = 400, responseDescription = "Aucun id donner")
|
|
||||||
|
|
||||||
@Route(path = "^\\/badge\\/([0-9]+)$", needLogin = true)
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
|
||||||
if (matcher.groupCount() > 0) {
|
|
||||||
int badgeId = Integer.parseInt(matcher.group(1));
|
|
||||||
Badge badge = databaseRepo.getBadge(badgeId);
|
|
||||||
JSONObject badgeJSON = new JSONObject();
|
|
||||||
if (badge != null) {
|
|
||||||
badgeJSON.put("name", badge.getName());
|
|
||||||
if (badge.getLogo() != null)
|
|
||||||
badgeJSON.put("logo", Base64.getEncoder().encodeToString(badge.getLogo()));
|
|
||||||
badgeJSON.put("level", badge.getLevel());
|
|
||||||
}
|
|
||||||
writer.response(200, "Access-Control-Allow-Origin: *");
|
|
||||||
writer.write(badgeJSON.toJSONString().replace("\\", ""));
|
|
||||||
} else {
|
|
||||||
writer.response(400, "Access-Control-Allow-Origin: *");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,59 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes;
|
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
|
|
||||||
import org.json.simple.JSONArray;
|
|
||||||
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.User;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Chapter;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Puzzle;
|
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
|
||||||
|
|
||||||
public class ChapterElement implements Response {
|
|
||||||
|
|
||||||
private final DatabaseRepository databaseRepo;
|
|
||||||
|
|
||||||
public ChapterElement(DatabaseRepository databaseRepo) {
|
|
||||||
this.databaseRepo = databaseRepo;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RouteDoc(path = "/chapter/<id>", responseCode = 200, responseDescription = "JSON contenant les informations du chapitre demander")
|
|
||||||
@RouteDoc(responseCode = 400, responseDescription = "Aucun id donner")
|
|
||||||
|
|
||||||
@Route(path = "^\\/chapter\\/([0-9]+)$", needLogin = true)
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{
|
|
||||||
Chapter chapter = databaseRepo.getChapter(Integer.parseInt(matcher.group(1)));
|
|
||||||
if (chapter != null){
|
|
||||||
JSONObject chapterJSON = new JSONObject();
|
|
||||||
chapterJSON.put("id", chapter.getId());
|
|
||||||
chapterJSON.put("name", chapter.getName());
|
|
||||||
if (chapter.getStartDate() != null)
|
|
||||||
chapterJSON.put("startDate", chapter.getStartDate().toString());
|
|
||||||
if (chapter.getEndDate() != null)
|
|
||||||
chapterJSON.put("endDate", chapter.getEndDate().toString());
|
|
||||||
JSONArray puzzles = new JSONArray();
|
|
||||||
for (Puzzle puzzle : chapter.getPuzzles()){
|
|
||||||
JSONObject puzzleJSON = new JSONObject();
|
|
||||||
puzzleJSON.put("id", puzzle.getId());
|
|
||||||
puzzleJSON.put("name", puzzle.getName());
|
|
||||||
puzzleJSON.put("scoreMax", puzzle.getScoreMax());
|
|
||||||
if (puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getJsonTags());
|
|
||||||
int score = this.databaseRepo.getScore(user.getId(), puzzle.getId());
|
|
||||||
if(score >= 0) puzzleJSON.put("score", score);
|
|
||||||
puzzles.add(puzzleJSON);
|
|
||||||
}
|
|
||||||
chapterJSON.put("puzzles", puzzles);
|
|
||||||
writer.response(200, "Access-Control-Allow-Origin: *");
|
|
||||||
writer.write(chapterJSON.toJSONString());
|
|
||||||
} else {
|
|
||||||
writer.response(400, "Access-Control-Allow-Origin: *");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,51 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
|
|
||||||
import org.json.simple.JSONArray;
|
|
||||||
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.User;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Chapter;
|
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
|
||||||
|
|
||||||
public class ChapterList implements Response {
|
|
||||||
|
|
||||||
private final DatabaseRepository databaseRepo;
|
|
||||||
|
|
||||||
public ChapterList(DatabaseRepository databaseRepo) {
|
|
||||||
this.databaseRepo = databaseRepo;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RouteDoc(path = "/chapters", responseCode = 200, responseDescription = "JSON contenant les informations des chapitres")
|
|
||||||
@RouteDoc(responseCode = 400, responseDescription = "Aucun chapitre trouver")
|
|
||||||
|
|
||||||
@Route(path = "^\\/chapters$", needLogin = true)
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
|
||||||
List<Chapter> allChapters = databaseRepo.getAllChapters();
|
|
||||||
if (allChapters != null) {
|
|
||||||
JSONArray chaptersJSON = new JSONArray();
|
|
||||||
for (Chapter chapter : allChapters) {
|
|
||||||
JSONObject chapterJSON = new JSONObject();
|
|
||||||
chapterJSON.put("id", chapter.getId());
|
|
||||||
chapterJSON.put("name", chapter.getName());
|
|
||||||
if (chapter.getStartDate() != null)
|
|
||||||
chapterJSON.put("startDate", chapter.getStartDate().toString());
|
|
||||||
if (chapter.getEndDate() != null)
|
|
||||||
chapterJSON.put("endDate", chapter.getEndDate().toString());
|
|
||||||
chaptersJSON.add(chapterJSON);
|
|
||||||
}
|
|
||||||
writer.response(200, "Access-Control-Allow-Origin: *");
|
|
||||||
writer.write(chaptersJSON.toJSONString());
|
|
||||||
} else {
|
|
||||||
writer.response(400, "Access-Control-Allow-Origin: *");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,92 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.SortedSet;
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
|
|
||||||
import org.json.simple.JSONArray;
|
|
||||||
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.User;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Chapter;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Group;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Player;
|
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
|
||||||
|
|
||||||
public class Leaderboard implements Response {
|
|
||||||
|
|
||||||
private final DatabaseRepository databaseRepo;
|
|
||||||
|
|
||||||
public Leaderboard(DatabaseRepository databaseRepo) {
|
|
||||||
this.databaseRepo = databaseRepo;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RouteDoc(path = "/leaderboard/{id}", responseCode = 200, responseDescription = "JSON contenant le leaderboard")
|
|
||||||
|
|
||||||
@Route(path = "^\\/leaderboard\\/?(\\d+)?$")
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
|
||||||
writer.response(200, "Access-Control-Allow-Origin: *");
|
|
||||||
if (matcher.group(1) != null) {
|
|
||||||
groupsLeaderboard(Integer.parseInt(matcher.group(1)), writer);
|
|
||||||
} else {
|
|
||||||
playersLeaderboard(writer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void groupsLeaderboard(int chapterId, HttpWriter writer) throws IOException {
|
|
||||||
Chapter chInfo = databaseRepo.getChapter(chapterId);
|
|
||||||
|
|
||||||
SortedSet<Group> allGroupsForChapter = databaseRepo.getAllGroupForChapterLeaderboard(chapterId);
|
|
||||||
JSONObject leaderboardJSON = new JSONObject();
|
|
||||||
if (chInfo.getStartDate() != null)
|
|
||||||
leaderboardJSON.put("start_date", chInfo.getStartDate().toString());
|
|
||||||
if (chInfo.getEndDate() != null)
|
|
||||||
leaderboardJSON.put("end_date", chInfo.getEndDate().toString());
|
|
||||||
JSONArray groupsJSON = new JSONArray();
|
|
||||||
if (allGroupsForChapter != null) {
|
|
||||||
int rank = 1;
|
|
||||||
int sameRankCount = 1;
|
|
||||||
Group previousGroup = null;
|
|
||||||
for (Group g : allGroupsForChapter) {
|
|
||||||
if (previousGroup != null) {
|
|
||||||
if (g.compareTo(previousGroup) == 0) {
|
|
||||||
sameRankCount++;
|
|
||||||
} else {
|
|
||||||
rank = rank + sameRankCount;
|
|
||||||
sameRankCount = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
groupsJSON.add(g.toJson(rank));
|
|
||||||
previousGroup = g;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
leaderboardJSON.put("groups", groupsJSON);
|
|
||||||
writer.write(leaderboardJSON.toJSONString().replace("\\", ""));
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void playersLeaderboard(HttpWriter writer) throws IOException {
|
|
||||||
SortedSet<Player> allPlayers = databaseRepo.getAllPlayerForLeaderboard();
|
|
||||||
JSONArray playersJSON = new JSONArray();
|
|
||||||
if (allPlayers != null) {
|
|
||||||
for (Player player : allPlayers) {
|
|
||||||
JSONObject playerJSON = new JSONObject();
|
|
||||||
playerJSON.put("pseudo", player.getPseudo());
|
|
||||||
if (player.getGroups() != null)
|
|
||||||
playerJSON.put("groups", player.getJsonGroups());
|
|
||||||
// if (player.getAvatar() != null)
|
|
||||||
// playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
|
|
||||||
playerJSON.put("rank", player.getRank());
|
|
||||||
playerJSON.put("score", player.getTotalScore());
|
|
||||||
playerJSON.put("completions", player.getTotalCompletion());
|
|
||||||
playerJSON.put("tries", player.getTotalTries());
|
|
||||||
playersJSON.add(playerJSON);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
writer.write(playersJSON.toJSONString().replace("\\", ""));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,53 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes;
|
|
||||||
|
|
||||||
import static be.jeffcheasey88.peeratcode.framework.RequestType.POST;
|
|
||||||
|
|
||||||
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 Login implements Response {
|
|
||||||
|
|
||||||
private DatabaseRepository databaseRepo;
|
|
||||||
private Router router;
|
|
||||||
|
|
||||||
public Login(DatabaseRepository databaseRepo, Router router) {
|
|
||||||
this.databaseRepo = databaseRepo;
|
|
||||||
this.router = router;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RouteDoc(path = "/login", 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 = "^\\/login$", 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) {
|
|
||||||
String pseudo = (String) informations.get("pseudo");
|
|
||||||
String password = (String) informations.get("passwd");
|
|
||||||
int id;
|
|
||||||
if ((id = databaseRepo.login(pseudo, password)) >= 0) {
|
|
||||||
writer.response(200, "Access-Control-Allow-Origin: *",
|
|
||||||
"Access-Control-Expose-Headers: Authorization",
|
|
||||||
"Authorization: Bearer " + this.router.createAuthUser(id));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
writer.response(400, "Access-Control-Allow-Origin: *");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,61 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes;
|
|
||||||
|
|
||||||
import java.util.Base64;
|
|
||||||
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.User;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Player;
|
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
|
||||||
|
|
||||||
public class PlayerDetails implements Response {
|
|
||||||
|
|
||||||
private final DatabaseRepository databaseRepo;
|
|
||||||
|
|
||||||
public PlayerDetails(DatabaseRepository databaseRepo) {
|
|
||||||
this.databaseRepo = databaseRepo;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RouteDoc(path = "/player/{id}", responseCode = 200, responseDescription = "JSON contenant les informations de l'utilisateur")
|
|
||||||
@RouteDoc(responseCode = 400, responseDescription = "Utilisateur introuvable dans la base de donnée")
|
|
||||||
|
|
||||||
@Route(path = "^\\/player\\/?(.+)?$", needLogin = true)
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
|
||||||
Player player;
|
|
||||||
if (matcher.group(1) != null) {
|
|
||||||
player = databaseRepo.getPlayerDetails(matcher.group(1));
|
|
||||||
} else {
|
|
||||||
player = databaseRepo.getPlayerDetails(user.getId());
|
|
||||||
}
|
|
||||||
JSONObject playerJSON = new JSONObject();
|
|
||||||
if (player != null) {
|
|
||||||
playerJSON.put("pseudo", player.getPseudo());
|
|
||||||
playerJSON.put("email", player.getEmail());
|
|
||||||
playerJSON.put("firstname", player.getFirstname());
|
|
||||||
playerJSON.put("lastname", player.getLastname());
|
|
||||||
playerJSON.put("description", player.getDescription());
|
|
||||||
if (player.getGroups() != null)
|
|
||||||
playerJSON.put("groups", player.getJsonGroups());
|
|
||||||
playerJSON.put("rank", player.getRank());
|
|
||||||
playerJSON.put("score", player.getTotalScore());
|
|
||||||
playerJSON.put("completions", player.getTotalCompletion());
|
|
||||||
playerJSON.put("completionsList", player.getJsonCompletions());
|
|
||||||
playerJSON.put("tries", player.getTotalTries());
|
|
||||||
if (player.getBadges() != null)
|
|
||||||
playerJSON.put("badges", player.getJsonBadges());
|
|
||||||
if (player.getAvatar() != null)
|
|
||||||
playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
|
|
||||||
writer.response(200, "Access-Control-Allow-Origin: *");
|
|
||||||
writer.write(playerJSON.toJSONString().replace("\\", ""));
|
|
||||||
} else {
|
|
||||||
writer.response(400, "Access-Control-Allow-Origin: *");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,72 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes;
|
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
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.User;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Chapter;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Completion;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Puzzle;
|
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
|
||||||
|
|
||||||
public class PuzzleElement implements Response {
|
|
||||||
|
|
||||||
private final DatabaseRepository databaseRepo;
|
|
||||||
|
|
||||||
public PuzzleElement(DatabaseRepository databaseRepo) {
|
|
||||||
this.databaseRepo = databaseRepo;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RouteDoc(path = "/puzzle/<id>", responseCode = 200, responseDescription = "JSON contenant les informations du puzzle")
|
|
||||||
@RouteDoc(responseCode = 400, responseDescription = "puzzle introuvable dans la base de donnée")
|
|
||||||
@RouteDoc(responseCode = 423, responseDescription = "L'utilisateur essaye de voir le puzzle en dehors de l'event")
|
|
||||||
|
|
||||||
@Route(path = "^\\/puzzle\\/([0-9]+)$", needLogin = true)
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
|
||||||
Puzzle puzzle = databaseRepo.getPuzzle(extractId(matcher));
|
|
||||||
if (puzzle != null){
|
|
||||||
Chapter chapter = this.databaseRepo.getChapter(puzzle);
|
|
||||||
if(chapter.getStartDate() != null){
|
|
||||||
if(LocalDateTime.now().isBefore(chapter.getStartDate().toLocalDateTime())){
|
|
||||||
writer.response(423, "Access-Control-Allow-Origin: *");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// if(chapter.getEndDate() != null){
|
|
||||||
// if(LocalDateTime.now().isAfter(chapter.getEndDate().toLocalDateTime())){
|
|
||||||
// writer.response(423, "Access-Control-Allow-Origin: *");
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
JSONObject puzzleJSON = new JSONObject();
|
|
||||||
puzzleJSON.put("id", puzzle.getId());
|
|
||||||
puzzleJSON.put("name", puzzle.getName());
|
|
||||||
puzzleJSON.put("content", puzzle.getContent());
|
|
||||||
puzzleJSON.put("scoreMax", puzzle.getScoreMax());
|
|
||||||
if(puzzle.getTags() != null) puzzleJSON.put("tags", puzzle.getJsonTags());
|
|
||||||
Completion completion = this.databaseRepo.getCompletionGroup(user.getId(), puzzle.getId());
|
|
||||||
if(completion != null && completion.getScore() >= 0){
|
|
||||||
puzzleJSON.put("score", completion.getScore());
|
|
||||||
puzzleJSON.put("tries", completion.getTries());
|
|
||||||
}
|
|
||||||
if(puzzle.getDepend() > 0) puzzleJSON.put("depend", puzzle.getDepend());
|
|
||||||
writer.response(200, "Access-Control-Allow-Origin: *", "Content-Type: application/json");
|
|
||||||
writer.write(puzzleJSON.toJSONString());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
writer.response(400, "Access-Control-Allow-Origin: *");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private int extractId(Matcher matcher) {
|
|
||||||
return Integer.parseInt(matcher.group(1));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,159 +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.Path;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
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.Locker;
|
|
||||||
import be.jeffcheasey88.peeratcode.framework.Response;
|
|
||||||
import be.jeffcheasey88.peeratcode.framework.Route;
|
|
||||||
import be.jeffcheasey88.peeratcode.framework.User;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Chapter;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Completion;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Group;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Player;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Puzzle;
|
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
|
||||||
|
|
||||||
public class PuzzleResponse implements Response {
|
|
||||||
private final DatabaseRepository databaseRepo;
|
|
||||||
private final String usersFilesPath;
|
|
||||||
|
|
||||||
private final Locker<Completion> leaderboard;
|
|
||||||
|
|
||||||
public PuzzleResponse(DatabaseRepository databaseRepo, String initUsersFilesPath, Locker<Completion> locker){
|
|
||||||
this.databaseRepo = databaseRepo;
|
|
||||||
usersFilesPath = initUsersFilesPath;
|
|
||||||
this.leaderboard = locker;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RouteDoc(path = "/puzzleResponse/<id>", responseCode = 200, responseDescription = "Bonne réponse, json contenant les points + tries")
|
|
||||||
@RouteDoc(responseCode = 400, responseDescription = "Pas de réponse")
|
|
||||||
@RouteDoc(responseCode = 403, responseDescription = "Déjà répondu")
|
|
||||||
@RouteDoc(responseCode = 406, responseDescription = "Mauvaise réponse")
|
|
||||||
@RouteDoc(responseCode = 423, responseDescription = "L'utilisateur essaye de répondre en dehors de l'event")
|
|
||||||
|
|
||||||
@Route(path = "^\\/puzzleResponse\\/([0-9]+)$", type = POST, needLogin = true)
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{
|
|
||||||
ReceivedResponse received = new ReceivedResponse(matcher, reader);
|
|
||||||
if (received.getResponse() == null){
|
|
||||||
writer.response(400, "Access-Control-Allow-Origin: *");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
//saveSourceCode(received, databaseRepo.getPlayer(user.getId()));
|
|
||||||
JSONObject responseJSON = new JSONObject();
|
|
||||||
if(this.databaseRepo.getScore(user.getId(), received.getPuzzleId()) > 0){
|
|
||||||
writer.response(403, "Access-Control-Allow-Origin: *");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Puzzle currentPuzzle = databaseRepo.getPuzzle(received.getPuzzleId());
|
|
||||||
Chapter chapter = this.databaseRepo.getChapter(currentPuzzle);
|
|
||||||
if(chapter.getStartDate() != null){
|
|
||||||
if(LocalDateTime.now().isBefore(chapter.getStartDate().toLocalDateTime())){
|
|
||||||
writer.response(423, "Access-Control-Allow-Origin: *");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(chapter.getEndDate() != null){
|
|
||||||
if(LocalDateTime.now().isAfter(chapter.getEndDate().toLocalDateTime())){
|
|
||||||
if(Arrays.equals(currentPuzzle.getSoluce(), received.getResponse())){
|
|
||||||
writer.response(200, "Access-Control-Allow-Origin: *", "Content-Type: application/json");
|
|
||||||
JSONObject theoSaisPasJusteRecevoir200 = new JSONObject();
|
|
||||||
theoSaisPasJusteRecevoir200.put("success", true);
|
|
||||||
writer.write(theoSaisPasJusteRecevoir200.toJSONString());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
writer.response(423, "Access-Control-Allow-Origin: *");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Group group = this.databaseRepo.getPlayerGroup(user.getId(), chapter.getId());
|
|
||||||
if(group == null){
|
|
||||||
writer.response(423, "Access-Control-Allow-Origin: *");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Completion completion = databaseRepo.insertOrUpdatePuzzleResponse(received.getPuzzleId(), user.getId(),
|
|
||||||
received.getFileName(), received.getSourceCode(), received.getResponse(), currentPuzzle);
|
|
||||||
if(completion == null){
|
|
||||||
writer.response(400, "Access-Control-Allow-Origin: *");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if(completion.getScore() > 0){
|
|
||||||
writer.response(200, "Access-Control-Allow-Origin: *", "Content-Type: application/json");
|
|
||||||
responseJSON.put("score", completion.getScore());
|
|
||||||
responseJSON.put("tries", completion.getTries());
|
|
||||||
|
|
||||||
}else{
|
|
||||||
writer.response(406, "Access-Control-Allow-Origin: *", "Content-Type: application/json");
|
|
||||||
responseJSON.put("tries", completion.getTries());
|
|
||||||
}
|
|
||||||
writer.write(responseJSON.toJSONString());
|
|
||||||
writer.flush();
|
|
||||||
|
|
||||||
leaderboard.setValue(completion);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void saveSourceCode(ReceivedResponse received, Player player){
|
|
||||||
try{
|
|
||||||
Path path = Paths.get(String.format("%s/%s/puz%04d-%s", usersFilesPath, player.getPseudo(),
|
|
||||||
received.getPuzzleId(), received.getFileName()));
|
|
||||||
Files.write(path, received.getSourceCode());
|
|
||||||
}catch (IOException e){
|
|
||||||
System.err.println("ERREUR D'ENREGISTREMENT DU CODE POUR PUZZLE_RESPONSE : " + e.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
class ReceivedResponse {
|
|
||||||
|
|
||||||
private int puzzleId;
|
|
||||||
private byte[] response;
|
|
||||||
private String fileName;
|
|
||||||
private byte[] sourceCode;
|
|
||||||
|
|
||||||
public ReceivedResponse(Matcher matcher, HttpReader reader) throws Exception {
|
|
||||||
puzzleId = Integer.parseInt(matcher.group(1));
|
|
||||||
|
|
||||||
List<String> multiPartData = reader.readMultiPartData();
|
|
||||||
if (multiPartData != null && multiPartData.size() > 0) {
|
|
||||||
this.response = multiPartData.get(0).getBytes();
|
|
||||||
if (multiPartData.size() == 3) {
|
|
||||||
this.fileName = multiPartData.get(1);
|
|
||||||
this.sourceCode = multiPartData.get(2).getBytes();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getPuzzleId() {
|
|
||||||
return puzzleId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] getResponse() {
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getFileName() {
|
|
||||||
return fileName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] getSourceCode() {
|
|
||||||
return sourceCode;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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)));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,37 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes;
|
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
|
|
||||||
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.User;
|
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
|
||||||
|
|
||||||
public class Result implements Response {
|
|
||||||
|
|
||||||
private DatabaseRepository repo;
|
|
||||||
|
|
||||||
public Result(DatabaseRepository repo) {
|
|
||||||
this.repo = repo;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RouteDoc(path = "/result/<id>", responseCode = 200, responseDescription = "Le score")
|
|
||||||
@RouteDoc(responseCode = 425, responseDescription = "Puzzle pas compléter")
|
|
||||||
|
|
||||||
@Route(path = "^\\/result\\/(\\d+)$", needLogin = true)
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
|
||||||
int puzzle = Integer.parseInt(matcher.group(1));
|
|
||||||
|
|
||||||
int score = this.repo.getScore(user.getId(), puzzle);
|
|
||||||
if (score < 0) {
|
|
||||||
writer.response(425, "Access-Control-Allow-Origin: *");
|
|
||||||
} else {
|
|
||||||
writer.response(200, "Access-Control-Allow-Origin: *");
|
|
||||||
writer.write(score + "");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,67 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes.groups;
|
|
||||||
|
|
||||||
import static be.jeffcheasey88.peeratcode.framework.RequestType.POST;
|
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc;
|
|
||||||
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.model.Chapter;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Group;
|
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
|
||||||
|
|
||||||
public class GroupCreate implements Response {
|
|
||||||
|
|
||||||
private Locker<Group> locker;
|
|
||||||
private DatabaseRepository repo;
|
|
||||||
private int groupDelay;
|
|
||||||
|
|
||||||
public GroupCreate(DatabaseRepository repo, Locker<Group> locker, int groupDelay){
|
|
||||||
this.repo = repo;
|
|
||||||
this.locker = locker;
|
|
||||||
this.groupDelay = groupDelay;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RouteDoc(path = "/groupCreate", responseCode = 200, responseDescription = "Le groupe a été créé")
|
|
||||||
@RouteDoc(responseCode = 403, responseDescription = "L'utilisateur est déjà dans le groupe / ne peux pas le rejoindre")
|
|
||||||
@RouteDoc(responseCode = 423, responseDescription = "L'utilisateur essaye de crée un groupe après le délai maximum de création de l'event")
|
|
||||||
|
|
||||||
@Route(path = "^\\/groupCreate$", type = POST, needLogin = true)
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{
|
|
||||||
Group newGroup = new Group(reader.readJson());
|
|
||||||
|
|
||||||
if (this.repo.getPlayerGroup(user.getId(), newGroup.getLinkToChapter()) == null) {
|
|
||||||
try {
|
|
||||||
if(this.repo.getGroupId(newGroup) == null) throw new NullPointerException();
|
|
||||||
writer.response(403, "Access-Control-Allow-Origin: *");
|
|
||||||
return;
|
|
||||||
}catch(NullPointerException e){
|
|
||||||
if(newGroup.getLinkToChapter() != null){
|
|
||||||
Chapter chapter = this.repo.getChapter(newGroup.getLinkToChapter());
|
|
||||||
if(chapter.getStartDate() != null){
|
|
||||||
LocalDateTime start = chapter.getStartDate().toLocalDateTime().plusMinutes(this.groupDelay);
|
|
||||||
if(LocalDateTime.now().isAfter(start)){
|
|
||||||
writer.response(423, "Access-Control-Allow-Origin: *");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (this.repo.insertGroup(newGroup, user)) {
|
|
||||||
writer.response(200, "Access-Control-Allow-Origin: *");
|
|
||||||
|
|
||||||
locker.setValue(newGroup);
|
|
||||||
} else {
|
|
||||||
writer.response(403, "Access-Control-Allow-Origin: *");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}else {
|
|
||||||
writer.response(403, "Access-Control-Allow-Origin: *");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,76 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes.groups;
|
|
||||||
|
|
||||||
import static be.jeffcheasey88.peeratcode.framework.RequestType.POST;
|
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc;
|
|
||||||
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.model.Chapter;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Completion;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Group;
|
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
|
||||||
|
|
||||||
public class GroupJoin implements Response{
|
|
||||||
|
|
||||||
private DatabaseRepository repo;
|
|
||||||
private int groupDelay;
|
|
||||||
private String waitTime;
|
|
||||||
|
|
||||||
private final Locker<Completion> leaderboard;
|
|
||||||
|
|
||||||
public GroupJoin(DatabaseRepository repo, int groupDelay, String waitTime, Locker<Completion> locker){
|
|
||||||
this.repo = repo;
|
|
||||||
this.groupDelay = groupDelay;
|
|
||||||
this.waitTime = waitTime;
|
|
||||||
this.leaderboard = locker;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RouteDoc(path = "/groupJoin", responseCode = 200, responseDescription = "L'utilisateur a rejoint le groupe")
|
|
||||||
@RouteDoc(responseCode = 403, responseDescription = "L'utilisateur est déjà dedans / ne peux pas le rejoindre")
|
|
||||||
@RouteDoc(responseCode = 423, responseDescription = "L'utilisateur essaye de rejoindre un groupe après le délai maximum de création de l'event")
|
|
||||||
@RouteDoc(responseCode = 409, responseDescription = "L'utilisateur est un peu débile... ou pas ?")
|
|
||||||
|
|
||||||
@Route(path = "^\\/groupJoin$", type = POST, needLogin = true)
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{
|
|
||||||
Group group = new Group(reader.readJson());
|
|
||||||
|
|
||||||
Group userGroup = this.repo.getPlayerGroup(user.getId(), group.getLinkToChapter());
|
|
||||||
if(group.equals(userGroup)){
|
|
||||||
writer.response(403, "Access-Control-Allow-Origin: *");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(group.getLinkToChapter() == null){
|
|
||||||
writer.response(409, "Access-Control-Allow-Origin: *");
|
|
||||||
writer.write(waitTime);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(group.getLinkToChapter() != null){
|
|
||||||
Chapter chapter = this.repo.getChapter(group.getLinkToChapter());
|
|
||||||
if(chapter.getStartDate() != null){
|
|
||||||
LocalDateTime start = chapter.getStartDate().toLocalDateTime().plusMinutes(this.groupDelay);
|
|
||||||
if(LocalDateTime.now().isAfter(start)){
|
|
||||||
writer.response(423, "Access-Control-Allow-Origin: *");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.repo.insertUserInGroup(group, user)) {
|
|
||||||
writer.response(200, "Access-Control-Allow-Origin: *");
|
|
||||||
|
|
||||||
leaderboard.setValue(new Completion(0, 0, 0, null, 0));
|
|
||||||
} else {
|
|
||||||
writer.response(403, "Access-Control-Allow-Origin: *");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,34 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes.groups;
|
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
|
|
||||||
import org.json.simple.JSONArray;
|
|
||||||
|
|
||||||
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.User;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Group;
|
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
|
||||||
|
|
||||||
public class GroupList implements Response {
|
|
||||||
|
|
||||||
private DatabaseRepository repo;
|
|
||||||
|
|
||||||
public GroupList(DatabaseRepository repo) {
|
|
||||||
this.repo = repo;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RouteDoc(path = "/groups", responseCode = 200, responseDescription = "JSON avec la liste des groups")
|
|
||||||
|
|
||||||
@Route(path = "^\\/groups$", needLogin = true)
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
|
||||||
writer.response(200, "Access-Control-Allow-Origin: *");
|
|
||||||
JSONArray result = new JSONArray();
|
|
||||||
for(Group group : this.repo.getAllGroups()) result.add(group.toJson());
|
|
||||||
writer.write(result.toJSONString());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,68 +0,0 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes.groups;
|
|
||||||
|
|
||||||
import static be.jeffcheasey88.peeratcode.framework.RequestType.POST;
|
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc;
|
|
||||||
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.model.Chapter;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Completion;
|
|
||||||
import be.jeffcheasey88.peeratcode.model.Group;
|
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
|
||||||
|
|
||||||
public class GroupQuit implements Response{
|
|
||||||
|
|
||||||
private DatabaseRepository repo;
|
|
||||||
private int groupDelay;
|
|
||||||
|
|
||||||
private final Locker<Completion> leaderboard;
|
|
||||||
|
|
||||||
public GroupQuit(DatabaseRepository repo, int groupDelay, Locker<Completion> locker){
|
|
||||||
this.repo = repo;
|
|
||||||
this.groupDelay = groupDelay;
|
|
||||||
|
|
||||||
this.leaderboard = locker;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RouteDoc(path = "/groupQuit", responseCode = 200, responseDescription = "L'utilisateur à quitter le groupe")
|
|
||||||
@RouteDoc(responseCode = 403, responseDescription = "L'utilisateur n'est pas dans le groupe / n'a pas pu le quittez")
|
|
||||||
@RouteDoc(responseCode = 423, responseDescription = "L'utilisateur essaye de quitter un groupe après le délai maximum de création de l'event")
|
|
||||||
|
|
||||||
@Route(path = "^\\/groupQuit$", type = POST, needLogin = true)
|
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{
|
|
||||||
Group group = new Group(reader.readJson());
|
|
||||||
|
|
||||||
Group userGroup = this.repo.getPlayerGroup(user.getId(), group.getLinkToChapter());
|
|
||||||
if(!group.equals(userGroup)){
|
|
||||||
writer.response(403, "Access-Control-Allow-Origin: *");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(group.getLinkToChapter() != null){
|
|
||||||
Chapter chapter = this.repo.getChapter(group.getLinkToChapter());
|
|
||||||
if(chapter.getStartDate() != null){
|
|
||||||
LocalDateTime start = chapter.getStartDate().toLocalDateTime().plusMinutes(this.groupDelay);
|
|
||||||
if(LocalDateTime.now().isAfter(start)){
|
|
||||||
writer.response(423, "Access-Control-Allow-Origin: *");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.repo.leaveGroup(group, user)) {
|
|
||||||
writer.response(200, "Access-Control-Allow-Origin: *");
|
|
||||||
|
|
||||||
leaderboard.setValue(new Completion(0, 0, 0, null, 0));
|
|
||||||
} else {
|
|
||||||
writer.response(403, "Access-Control-Allow-Origin: *");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,4 +1,4 @@
|
||||||
package be.jeffcheasey88.peeratcode;
|
package dev.peerat.backend;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
|
@ -7,8 +7,12 @@ import java.io.FileReader;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
|
import dev.peerat.backend.utils.Mail;
|
||||||
|
|
||||||
public class Configuration {
|
public class Configuration {
|
||||||
|
|
||||||
|
private boolean prod;
|
||||||
|
|
||||||
private String db_host;
|
private String db_host;
|
||||||
private int db_port;
|
private int db_port;
|
||||||
private String db_user;
|
private String db_user;
|
||||||
|
@ -29,28 +33,52 @@ public class Configuration {
|
||||||
|
|
||||||
private int groupJoinMinutes;
|
private int groupJoinMinutes;
|
||||||
private String groupQuitMinutes;
|
private String groupQuitMinutes;
|
||||||
|
private int groupMaxPlayers;
|
||||||
|
|
||||||
private File file;
|
private String mailUsername;
|
||||||
|
private String mailPassword;
|
||||||
|
private String mailSmtpHost;
|
||||||
|
private int mailSmtpPort;
|
||||||
|
private String mailFromAddress;
|
||||||
|
|
||||||
|
private String git_token;
|
||||||
|
|
||||||
|
private String jwt_key;
|
||||||
|
|
||||||
|
private String sql_folder;
|
||||||
|
|
||||||
|
private int event_chapter;
|
||||||
|
|
||||||
|
private File _file;
|
||||||
|
|
||||||
public Configuration(String path) {
|
public Configuration(String path) {
|
||||||
this.file = new File(path);
|
this._file = new File(path);
|
||||||
System.out.println("Config on " + file.getAbsolutePath());
|
System.out.println("Config on " + _file.getAbsolutePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void load() throws Exception {
|
public <T> Configuration addDefaultValue(String name, T value) throws Exception{
|
||||||
if (!this.file.exists())
|
if(value == null) throw new IllegalArgumentException("Value cannot be null");
|
||||||
return;
|
Field field = getClass().getDeclaredField(name);
|
||||||
BufferedReader reader = new BufferedReader(new FileReader(this.file));
|
field.setAccessible(true);
|
||||||
|
field.set(this, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Configuration load() throws Exception{
|
||||||
|
if(!this._file.exists()) return this;
|
||||||
|
BufferedReader reader = new BufferedReader(new FileReader(this._file));
|
||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null) {
|
while((line = reader.readLine()) != null){
|
||||||
String[] split = line.split("=");
|
int index = line.indexOf('=');
|
||||||
Field field = getClass().getDeclaredField(split[0]);
|
String key = line.substring(0, index);
|
||||||
if (field == null)
|
String value = line.substring(index+1);
|
||||||
continue;
|
Field field = getClass().getDeclaredField(key);
|
||||||
|
if(field == null) continue;
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
injectValue(field, split[1]);
|
injectValue(field, value);
|
||||||
}
|
}
|
||||||
reader.close();
|
reader.close();
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void injectValue(Field field, String value) throws IllegalAccessException {
|
private void injectValue(Field field, String value) throws IllegalAccessException {
|
||||||
|
@ -93,72 +121,72 @@ public class Configuration {
|
||||||
}
|
}
|
||||||
|
|
||||||
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+"\n");
|
||||||
}
|
}
|
||||||
writer.flush();
|
writer.flush();
|
||||||
writer.close();
|
writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDbHost() {
|
public boolean isProduction(){
|
||||||
|
return this.prod;
|
||||||
|
}
|
||||||
|
|
||||||
|
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())
|
|
||||||
users_files = "/tmp/users_files";
|
|
||||||
return users_files;
|
return users_files;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,4 +201,37 @@ public class Configuration {
|
||||||
public String getGroupQuitMinutes(){
|
public String getGroupQuitMinutes(){
|
||||||
return this.groupQuitMinutes;
|
return this.groupQuitMinutes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getGroupMaxPlayers(){
|
||||||
|
return this.groupMaxPlayers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mail getMail(){
|
||||||
|
return new Mail(
|
||||||
|
this.mailUsername,
|
||||||
|
this.mailPassword,
|
||||||
|
this.mailSmtpHost,
|
||||||
|
this.mailSmtpPort,
|
||||||
|
this.mailFromAddress);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGitToken(){
|
||||||
|
return this.git_token;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getJwtKey(){
|
||||||
|
return this.jwt_key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setJwtKey(String key){
|
||||||
|
this.jwt_key = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSqlFolder(){
|
||||||
|
return this.sql_folder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getEventChapter(){
|
||||||
|
return this.event_chapter;
|
||||||
|
}
|
||||||
}
|
}
|
184
src/dev/peerat/backend/Main.java
Normal file
184
src/dev/peerat/backend/Main.java
Normal file
|
@ -0,0 +1,184 @@
|
||||||
|
package dev.peerat.backend;
|
||||||
|
|
||||||
|
import static dev.peerat.framework.RequestType.OPTIONS;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.bonus.extract.RouteExtracter;
|
||||||
|
import dev.peerat.backend.model.Group;
|
||||||
|
import dev.peerat.backend.model.PeerAtUser;
|
||||||
|
import dev.peerat.backend.repository.ConnectionManager;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.DependencyInjector;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Locker;
|
||||||
|
import dev.peerat.framework.Locker.Key;
|
||||||
|
import dev.peerat.framework.RequestType;
|
||||||
|
import dev.peerat.framework.Response;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.RouteInterceptor;
|
||||||
|
import dev.peerat.framework.Router;
|
||||||
|
import dev.peerat.framework.auth.Authenticator;
|
||||||
|
import dev.peerat.framework.auth.JwtAuthenticator;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
import dev.peerat.framework.utils.json.JsonParser;
|
||||||
|
|
||||||
|
public class Main{
|
||||||
|
|
||||||
|
private static Router<PeerAtUser> ACCESS_ROUTER;
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception{
|
||||||
|
Configuration config = new Configuration("config.txt").addDefaultValue("users_files", "/tmp/users_files");
|
||||||
|
config.load();
|
||||||
|
|
||||||
|
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||||
|
|
||||||
|
DatabaseRepository repo = new DatabaseRepository(new ConnectionManager(config), config);
|
||||||
|
Router<PeerAtUser> router = new Router<PeerAtUser>()
|
||||||
|
.activeReOrdering().
|
||||||
|
addDefaultHeaders(RequestType.GET, "Access-Control-Allow-Origin: *").
|
||||||
|
addDefaultHeaders(RequestType.POST, "Access-Control-Allow-Origin: *").
|
||||||
|
addDefaultHeaders(RequestType.OPTIONS,
|
||||||
|
"Access-Control-Allow-Origin: *",
|
||||||
|
"Access-Control-Allow-Methods: *",
|
||||||
|
"Access-Control-Allow-Headers: *");
|
||||||
|
ACCESS_ROUTER = router;
|
||||||
|
|
||||||
|
Authenticator<PeerAtUser> auth;
|
||||||
|
if(config.getJwtKey() != null){
|
||||||
|
JsonParser parser = new JsonParser();
|
||||||
|
JsonMap json = parser.parse(config.getJwtKey());
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
for(Entry<String, Object> entry : json.entries()) params.put(entry.getKey(), entry.getValue());
|
||||||
|
auth = new JwtAuthenticator<PeerAtUser>().configure(
|
||||||
|
(builder) -> builder.setExpectedIssuer(config.getTokenIssuer()),
|
||||||
|
(claims) -> {
|
||||||
|
claims.setIssuer(config.getTokenIssuer());
|
||||||
|
claims.setExpirationTimeMinutesInTheFuture(config.getTokenExpiration());
|
||||||
|
},
|
||||||
|
(claims) -> new PeerAtUser(claims),
|
||||||
|
params);
|
||||||
|
}else{
|
||||||
|
auth = new JwtAuthenticator<PeerAtUser>().configure(
|
||||||
|
(builder) -> builder.setExpectedIssuer(config.getTokenIssuer()),
|
||||||
|
(claims) -> {
|
||||||
|
claims.setIssuer(config.getTokenIssuer());
|
||||||
|
claims.setExpirationTimeMinutesInTheFuture(config.getTokenExpiration());
|
||||||
|
},
|
||||||
|
(claims) -> new PeerAtUser(claims));
|
||||||
|
JsonMap json = new JsonMap();
|
||||||
|
for(Entry<String, Object> entry : ((JwtAuthenticator<PeerAtUser>)auth).exportKey().entrySet()){
|
||||||
|
json.set(entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
config.setJwtKey(json.toString());
|
||||||
|
config.save();
|
||||||
|
}
|
||||||
|
router.setAuthenticator(auth);
|
||||||
|
|
||||||
|
router.setDefault((matcher, context, reader, writer) -> {
|
||||||
|
context.response(404);
|
||||||
|
writer.write("404 not Found.\n");
|
||||||
|
});
|
||||||
|
|
||||||
|
router.register(new Response(){
|
||||||
|
@Route(path = "^(.*)$", type = OPTIONS)
|
||||||
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
|
context.response(200);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
initRoutes(router, repo, config);
|
||||||
|
|
||||||
|
router.addInterceptor(new RouteInterceptor(){
|
||||||
|
@Override
|
||||||
|
public boolean intercept(Matcher matcher, Context context, HttpReader reader, HttpWriter writer, Method method){
|
||||||
|
if(method.getDeclaringClass().getPackage().getName().contains(".admins")){
|
||||||
|
try {
|
||||||
|
Group group = repo.getGroupRepository().getPlayerGroup(context.<PeerAtUser>getUser().getId(), 1);
|
||||||
|
if(!group.getName().equalsIgnoreCase("Quarter-Master - Battles PAC x CEI")){
|
||||||
|
context.response(401);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}catch(Exception ex){
|
||||||
|
ex.printStackTrace();
|
||||||
|
try{
|
||||||
|
context.response(401);
|
||||||
|
}catch(Exception e){}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
new Thread(new Runnable(){
|
||||||
|
public void run(){
|
||||||
|
Key key = new Key();
|
||||||
|
|
||||||
|
Locker<Context> locker = router.getLogger();
|
||||||
|
|
||||||
|
locker.init(key);
|
||||||
|
try {
|
||||||
|
while(true){
|
||||||
|
locker.lock(key);
|
||||||
|
Context instance = locker.getValue(key);
|
||||||
|
if(instance == null) continue;
|
||||||
|
System.out.println("["+((instance.isLogged()) ? repo.getPlayerRepository().getPlayer(instance.<PeerAtUser>getUser().getId()).getPseudo() : "?")+"] "+instance.getType()+" "+instance.getPath()+" -> "+instance.getResponseCode());
|
||||||
|
}
|
||||||
|
}catch(Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
locker.remove(key);
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
|
||||||
|
new Thread(new Runnable(){
|
||||||
|
public void run(){
|
||||||
|
Key key = new Key();
|
||||||
|
|
||||||
|
Locker<Throwable> locker = router.getExceptionLogger();
|
||||||
|
|
||||||
|
locker.init(key);
|
||||||
|
try {
|
||||||
|
while(true){
|
||||||
|
locker.lock(key);
|
||||||
|
locker.getValue(key).printStackTrace();
|
||||||
|
}
|
||||||
|
}catch(Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
locker.remove(key);
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
|
||||||
|
if(config.useSsl()) router.configureSSL(config.getSslKeystore(), config.getSslKeystorePasswd());
|
||||||
|
router.listen(config.getTcpPort(), config.useSsl());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void initRoutes(Router<PeerAtUser> router, DatabaseRepository repo, Configuration config) throws Exception{
|
||||||
|
Predicate<PeerAtUser> isAdmin = (user) -> {
|
||||||
|
try {
|
||||||
|
Group group = repo.getGroupRepository().getPlayerGroup(user.getId(), 1);
|
||||||
|
return group.getName().equalsIgnoreCase("Quarter-Master - Battles PAC x CEI");
|
||||||
|
}catch(Exception ex){}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
router.registerPackages("dev.peerat.backend.routes",new DependencyInjector()
|
||||||
|
.of(repo, router, config, new RouteExtracter(router), config.getMail(), isAdmin)
|
||||||
|
.of("waitting", new HashMap<>())
|
||||||
|
.of("leaderboard", new Locker<>())
|
||||||
|
.of("groups", new Locker<>())
|
||||||
|
.of("groupMessages", new Locker<>()));
|
||||||
|
|
||||||
|
// Bot bot = new Bot(config, repo, groupLock);
|
||||||
|
// bot.start();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,12 +1,12 @@
|
||||||
package be.jeffcheasey88.peeratcode.bonus.discord;
|
package dev.peerat.backend.bonus.discord;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.Configuration;
|
import dev.peerat.backend.Configuration;
|
||||||
import be.jeffcheasey88.peeratcode.framework.Locker;
|
import dev.peerat.backend.model.Group;
|
||||||
import be.jeffcheasey88.peeratcode.framework.Locker.Key;
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
import be.jeffcheasey88.peeratcode.model.Group;
|
import dev.peerat.framework.Locker;
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
import dev.peerat.framework.Locker.Key;
|
||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
import net.dv8tion.jda.api.JDABuilder;
|
import net.dv8tion.jda.api.JDABuilder;
|
||||||
import net.dv8tion.jda.api.entities.Guild;
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
|
@ -38,7 +38,7 @@ public class Bot extends Thread{
|
||||||
|
|
||||||
locker.init(key);
|
locker.init(key);
|
||||||
|
|
||||||
List<Group> groups = this.repo.getAllGroups();
|
List<Group> groups = this.repo.getGroupRepository().getAllGroups();
|
||||||
for(Group group : groups){
|
for(Group group : groups){
|
||||||
Integer chapter = group.getLinkToChapter();
|
Integer chapter = group.getLinkToChapter();
|
||||||
// Integer puzzle = group.getLinkToPuzzle();
|
// Integer puzzle = group.getLinkToPuzzle();
|
|
@ -1,4 +1,4 @@
|
||||||
package be.jeffcheasey88.peeratcode.bonus.extract;
|
package dev.peerat.backend.bonus.extract;
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
import java.lang.annotation.ElementType;
|
||||||
import java.lang.annotation.Repeatable;
|
import java.lang.annotation.Repeatable;
|
|
@ -1,4 +1,4 @@
|
||||||
package be.jeffcheasey88.peeratcode.bonus.extract;
|
package dev.peerat.backend.bonus.extract;
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
import java.lang.annotation.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
131
src/dev/peerat/backend/bonus/extract/RouteExtracter.java
Normal file
131
src/dev/peerat/backend/bonus/extract/RouteExtracter.java
Normal file
|
@ -0,0 +1,131 @@
|
||||||
|
package dev.peerat.backend.bonus.extract;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import dev.peerat.backend.model.PeerAtUser;
|
||||||
|
import dev.peerat.framework.RequestType;
|
||||||
|
import dev.peerat.framework.Response;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.RouteMapper;
|
||||||
|
import dev.peerat.framework.Router;
|
||||||
|
import dev.peerat.framework.utils.json.JsonArray;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class RouteExtracter {
|
||||||
|
|
||||||
|
private Router<PeerAtUser> router;
|
||||||
|
|
||||||
|
public RouteExtracter(Router<PeerAtUser> router){
|
||||||
|
this.router = router;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void extract() throws Exception{
|
||||||
|
RouteMapper[] mappers = getField(Router.class, router, "mappers");
|
||||||
|
for(RequestType type : RequestType.values()){
|
||||||
|
RouteMapper mapper = mappers[type.ordinal()];
|
||||||
|
Response[] responses = getField(RouteMapper.class, mapper, "responses");
|
||||||
|
Route[] routes = getField(RouteMapper.class, mapper, "routes");
|
||||||
|
synchronized (responses){
|
||||||
|
for(int i = 0; i < responses.length; i++){
|
||||||
|
Route route = routes[i];
|
||||||
|
System.out.println("["+type+"] ("+route.needLogin()+") "+route.path());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void extractDoc() throws Exception{
|
||||||
|
RouteMapper[] mappers = getField(Router.class, router, "mappers");
|
||||||
|
for(RequestType type : RequestType.values()){
|
||||||
|
RouteMapper mapper = mappers[type.ordinal()];
|
||||||
|
Response[] responses = getField(RouteMapper.class, mapper, "responses");
|
||||||
|
synchronized (responses){
|
||||||
|
for(int i = 0; i < responses.length; i++){
|
||||||
|
Response response = responses[i];
|
||||||
|
Method method = response.getClass().getDeclaredMethod("exec",
|
||||||
|
Response.class.getDeclaredMethods()[0].getParameterTypes());
|
||||||
|
for(RouteDoc doc : method.getDeclaredAnnotationsByType(RouteDoc.class)){
|
||||||
|
System.out.println(doc.path()+((doc.path().isEmpty() ? "":"\n"))+" ["+doc.responseCode()+"] "+doc.responseDescription());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonMap swagger(String host) throws Exception{
|
||||||
|
JsonMap result = new JsonMap();
|
||||||
|
result.set("swagger","2.0");
|
||||||
|
JsonMap info = new JsonMap();
|
||||||
|
info.set("title", "Peer-at-code backend api routes");
|
||||||
|
info.set("description", "Using Peer-at Code Framework");
|
||||||
|
result.set("info", info);
|
||||||
|
result.set("host", host);
|
||||||
|
result.set("basePath","/");
|
||||||
|
|
||||||
|
List<Response> routes = new ArrayList<>();
|
||||||
|
RouteMapper[] mappers = getField(Router.class, router, "mappers");
|
||||||
|
for(RequestType type : RequestType.values()){
|
||||||
|
RouteMapper mapper = mappers[type.ordinal()];
|
||||||
|
Response[] responses = getField(RouteMapper.class, mapper, "responses");
|
||||||
|
synchronized (responses){
|
||||||
|
routes.addAll(Arrays.asList(responses));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<String> packages = new HashSet<>();
|
||||||
|
for(Response response : routes){
|
||||||
|
String name = response.getClass().getPackage().getName();
|
||||||
|
name = name.substring(name.lastIndexOf('.')+1, name.length());
|
||||||
|
packages.add(name);
|
||||||
|
}
|
||||||
|
JsonArray tags = new JsonArray();
|
||||||
|
for(String tag : packages){
|
||||||
|
JsonMap current = new JsonMap();
|
||||||
|
current.set("name", tag);
|
||||||
|
tags.add(current);
|
||||||
|
}
|
||||||
|
result.set("tags", tags);
|
||||||
|
JsonArray schemes = new JsonArray();
|
||||||
|
schemes.add("https");
|
||||||
|
result.set("schemes", schemes);
|
||||||
|
|
||||||
|
JsonMap paths = new JsonMap();
|
||||||
|
|
||||||
|
for(Response response : routes){
|
||||||
|
Method method = response.getClass().getDeclaredMethod("exec",
|
||||||
|
Response.class.getDeclaredMethods()[0].getParameterTypes());
|
||||||
|
Route route = method.getDeclaredAnnotation(Route.class);
|
||||||
|
RouteDoc[] docs = method.getDeclaredAnnotationsByType(RouteDoc.class);
|
||||||
|
if(docs.length < 1) continue;
|
||||||
|
RouteDoc base = docs[0];
|
||||||
|
JsonMap current = new JsonMap();
|
||||||
|
JsonMap data = new JsonMap();
|
||||||
|
JsonArray tag = new JsonArray();
|
||||||
|
String pack = response.getClass().getPackage().getName();
|
||||||
|
pack = pack.substring(pack.lastIndexOf('.')+1, pack.length());
|
||||||
|
tag.add(pack);
|
||||||
|
data.set("tags", tag);
|
||||||
|
|
||||||
|
|
||||||
|
current.set(route.type().toString().toLowerCase(), data);
|
||||||
|
|
||||||
|
paths.set(base.path(), current);
|
||||||
|
}
|
||||||
|
|
||||||
|
result.set("paths", paths);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private <E> E getField(Class<?> clazz, Object instance, String name) throws Exception{
|
||||||
|
Field field = clazz.getDeclaredField(name);
|
||||||
|
field.setAccessible(true);
|
||||||
|
return (E) field.get(instance);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +1,5 @@
|
||||||
package be.jeffcheasey88.peeratcode.model;
|
package dev.peerat.backend.model;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.mapping.Treasure;
|
|
||||||
|
|
||||||
@Treasure
|
|
||||||
public class Badge {
|
public class Badge {
|
||||||
|
|
||||||
private String name;
|
private String name;
|
|
@ -1,6 +1,7 @@
|
||||||
package be.jeffcheasey88.peeratcode.model;
|
package dev.peerat.backend.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,13 @@ public class Chapter {
|
||||||
this.puzzles = puzzles;
|
this.puzzles = puzzles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean hasStarted(){
|
||||||
|
LocalDateTime now = LocalDateTime.now();
|
||||||
|
boolean show = true;
|
||||||
|
if(startDate != null) show &= now.isAfter(startDate.toLocalDateTime());
|
||||||
|
return show;
|
||||||
|
}
|
||||||
|
|
||||||
public Timestamp getStartDate() {
|
public Timestamp getStartDate() {
|
||||||
return startDate;
|
return startDate;
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package be.jeffcheasey88.peeratcode.model;
|
package dev.peerat.backend.model;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ public class Completion{
|
||||||
private int score;
|
private int score;
|
||||||
private String puzzleName;
|
private String puzzleName;
|
||||||
|
|
||||||
public Completion(int playerId, int puzzleId, int tries, String fileName, int score) {
|
public Completion(int playerId, int puzzleId, int tries, String fileName, int score){
|
||||||
this(playerId, puzzleId, tries, score, fileName, null, null, null, null);
|
this(playerId, puzzleId, tries, score, fileName, null, null, null, null);
|
||||||
}
|
}
|
||||||
public Completion(int playerId, int puzzleId, int tries, String fileName, int score, String puzzleName) {
|
public Completion(int playerId, int puzzleId, int tries, String fileName, int score, String puzzleName) {
|
||||||
|
@ -22,13 +22,13 @@ public class Completion{
|
||||||
this(playerId, puzzleId, 0, 0, fileName, file, response, currentPuzzle, null);
|
this(playerId, puzzleId, 0, 0, fileName, file, response, currentPuzzle, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Completion(int initTries, int initScore) {
|
public Completion(int initTries, int initScore){
|
||||||
// For group leaderboard
|
// For group leaderboard
|
||||||
this(-1, -1, initTries, initScore, null, null, null, null, null);
|
this(-1, -1, initTries, initScore, null, null, null, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Completion(int playerId, int puzzleId, int tries, int score, String fileName, byte[] file, byte[] response,
|
public Completion(int playerId, int puzzleId, int tries, int score, String fileName, byte[] file, byte[] response,
|
||||||
Puzzle currentPuzzle, String initPuzzleName) {
|
Puzzle currentPuzzle, String initPuzzleName){
|
||||||
this.playerId = playerId;
|
this.playerId = playerId;
|
||||||
this.puzzleId = puzzleId;
|
this.puzzleId = puzzleId;
|
||||||
this.fileName = fileName;
|
this.fileName = fileName;
|
||||||
|
@ -40,7 +40,7 @@ public class Completion{
|
||||||
this.score = score;
|
this.score = score;
|
||||||
|
|
||||||
if (currentPuzzle != null)
|
if (currentPuzzle != null)
|
||||||
addTry(currentPuzzle, response);
|
addTry(currentPuzzle, response, 0);
|
||||||
|
|
||||||
puzzleName = initPuzzleName;
|
puzzleName = initPuzzleName;
|
||||||
}
|
}
|
||||||
|
@ -62,12 +62,17 @@ public class Completion{
|
||||||
return tries;
|
return tries;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addTry(Puzzle currentPuzzle, byte[] response) {
|
public void addTry(Puzzle currentPuzzle, byte[] response, int chapter){
|
||||||
if (score <= 0){
|
if (score <= 0){
|
||||||
tries++;
|
tries++;
|
||||||
if (response != null && Arrays.equals(currentPuzzle.getSoluce(), response)) {
|
if (response != null && Arrays.equals(currentPuzzle.getSoluce(), response)){
|
||||||
if (tries > 1) { // Loose 5% each try with a minimum of 1 for score
|
if (tries > 1) { // Loose 5% each try with a minimum of 1 for score
|
||||||
score = (int) Math.ceil(currentPuzzle.getScoreMax() * (1 - ((tries - 1) / 20.)));
|
if(chapter < 4){
|
||||||
|
score = currentPuzzle.getScoreMax();
|
||||||
|
}else{
|
||||||
|
score = (int) Math.ceil(currentPuzzle.getScoreMax() * (1 - ((tries - 1) / 20.)));
|
||||||
|
}
|
||||||
|
// score = currentPuzzle.getScoreMax();
|
||||||
if (score < 1)
|
if (score < 1)
|
||||||
score = 1;
|
score = 1;
|
||||||
} else
|
} else
|
|
@ -1,35 +1,49 @@
|
||||||
package be.jeffcheasey88.peeratcode.model;
|
package dev.peerat.backend.model;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
import org.json.simple.JSONArray;
|
import org.jose4j.json.internal.json_simple.JSONArray;
|
||||||
import org.json.simple.JSONObject;
|
import org.jose4j.json.internal.json_simple.JSONObject;
|
||||||
|
|
||||||
|
import be.jeffcheasey88.peeratcode.mapping.SeaBottle;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
public class Group implements Comparable<Group> {
|
public class Group implements Comparable<Group> {
|
||||||
private String name;
|
private String name;
|
||||||
private Integer linkToChapter;
|
private Integer linkToChapter;
|
||||||
// private Integer linkToPuzzle;
|
// private Integer linkToPuzzle;
|
||||||
private List<Player> players;
|
private List<Player> players;
|
||||||
|
private int playerCount;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString(){
|
public String toString(){
|
||||||
return "Group[name="+name+", chapter="+linkToChapter+"]";
|
return "Group[name="+name+", chapter="+linkToChapter+"]";
|
||||||
}
|
}
|
||||||
|
|
||||||
public Group(JSONObject json) {
|
public Group(JsonMap json){
|
||||||
this.name = (String) json.get("name");
|
this.name = (String) json.get("name");
|
||||||
if (json.containsKey("chapter"))
|
if (json.has("chapter"))
|
||||||
this.linkToChapter = ((Number) json.get("chapter")).intValue();
|
this.linkToChapter = ((Number) json.get("chapter")).intValue();
|
||||||
// if (json.containsKey("puzzle"))
|
// if (json.has("puzzle"))
|
||||||
// this.linkToPuzzle = ((Number) json.get("puzzle")).intValue();
|
// this.linkToPuzzle = ((Number) json.get("puzzle")).intValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Group(String name, Integer initChap, Integer initPuzz) {
|
public Group(String name, Integer initChap, Integer initPuzz, int playerCount) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.linkToChapter = initChap;
|
this.linkToChapter = initChap;
|
||||||
// this.linkToPuzzle = initPuzz;
|
// this.linkToPuzzle = initPuzz;
|
||||||
|
this.playerCount = playerCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPlayerCount(){
|
||||||
|
return this.playerCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SeaBottle
|
||||||
|
public static Group getGroup(int player){
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addPlayer(Player newPlayer) {
|
public void addPlayer(Player newPlayer) {
|
||||||
|
@ -105,6 +119,7 @@ public class Group implements Comparable<Group> {
|
||||||
}
|
}
|
||||||
groupJSON.put("players", groupsPlayerJSON);
|
groupJSON.put("players", groupsPlayerJSON);
|
||||||
}
|
}
|
||||||
|
groupJSON.put("playerCount", this.playerCount);
|
||||||
return groupJSON;
|
return groupJSON;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,7 +139,7 @@ public class Group implements Comparable<Group> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode(){
|
||||||
return Objects.hash(name);
|
return Objects.hash(name);
|
||||||
}
|
}
|
||||||
|
|
28
src/dev/peerat/backend/model/PeerAtUser.java
Normal file
28
src/dev/peerat/backend/model/PeerAtUser.java
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
package dev.peerat.backend.model;
|
||||||
|
|
||||||
|
import org.jose4j.jwt.JwtClaims;
|
||||||
|
|
||||||
|
public class PeerAtUser extends dev.peerat.framework.User{
|
||||||
|
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
public PeerAtUser(int id){
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PeerAtUser(JwtClaims claims){
|
||||||
|
this.id = ((Long) claims.getClaimValue("id")).intValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(JwtClaims claims){
|
||||||
|
claims.setClaim("id", this.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId(){
|
||||||
|
return this.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -1,12 +1,12 @@
|
||||||
package be.jeffcheasey88.peeratcode.model;
|
package dev.peerat.backend.model;
|
||||||
|
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.json.simple.JSONArray;
|
import dev.peerat.framework.utils.json.JsonArray;
|
||||||
import org.json.simple.JSONObject;
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
public class Player implements Comparable<Player> {
|
public class Player implements Comparable<Player> {
|
||||||
private String pseudo;
|
private String pseudo;
|
||||||
|
@ -22,12 +22,12 @@ public class Player implements Comparable<Player> {
|
||||||
|
|
||||||
private Set<Badge> badges;
|
private Set<Badge> badges;
|
||||||
|
|
||||||
public Player(String pseudo, String email, String firstname, String lastname, String description) {
|
public Player(String pseudo, String email, String firstname, String lastname) {
|
||||||
this.pseudo = pseudo;
|
this.pseudo = pseudo;
|
||||||
this.email = email;
|
this.email = email;
|
||||||
this.firstname = firstname;
|
this.firstname = firstname;
|
||||||
this.lastname = lastname;
|
this.lastname = lastname;
|
||||||
this.description = description;
|
this.description = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player(String pseudo, int score, int tries) {
|
public Player(String pseudo, int score, int tries) {
|
||||||
|
@ -37,6 +37,15 @@ 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 Player(String email) {
|
||||||
|
// For player find in Map during register process
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPseudo(String pseudo){
|
||||||
|
this.pseudo = pseudo;
|
||||||
|
}
|
||||||
|
|
||||||
public String getPseudo() {
|
public String getPseudo() {
|
||||||
return this.pseudo;
|
return this.pseudo;
|
||||||
}
|
}
|
||||||
|
@ -61,9 +70,9 @@ public class Player implements Comparable<Player> {
|
||||||
return groups;
|
return groups;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JSONArray getJsonGroups() {
|
public JsonArray getJsonGroups() {
|
||||||
if (groups != null) {
|
if (groups != null) {
|
||||||
JSONArray groupsJSON = new JSONArray();
|
JsonArray groupsJSON = new JsonArray();
|
||||||
for (Group group : groups) {
|
for (Group group : groups) {
|
||||||
groupsJSON.add(group.toJson());
|
groupsJSON.add(group.toJson());
|
||||||
}
|
}
|
||||||
|
@ -112,13 +121,13 @@ public class Player implements Comparable<Player> {
|
||||||
return completions.size();
|
return completions.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public JSONArray getJsonCompletions() {
|
public JsonArray getJsonCompletions() {
|
||||||
JSONArray completionsJSON = new JSONArray();
|
JsonArray completionsJSON = new JsonArray();
|
||||||
for (Completion completion : completions) {
|
for (Completion completion : completions) {
|
||||||
JSONObject completionJSON = new JSONObject();
|
JsonMap completionJSON = new JsonMap();
|
||||||
completionJSON.put("puzzleName", completion.getPuzzleName());
|
completionJSON.set("puzzleName", completion.getPuzzleName());
|
||||||
completionJSON.put("tries", completion.getTries());
|
completionJSON.set("tries", completion.getTries());
|
||||||
completionJSON.put("score", completion.getScore());
|
completionJSON.set("score", completion.getScore());
|
||||||
completionsJSON.add(completionJSON);
|
completionsJSON.add(completionJSON);
|
||||||
}
|
}
|
||||||
return completionsJSON;
|
return completionsJSON;
|
||||||
|
@ -136,17 +145,17 @@ public class Player implements Comparable<Player> {
|
||||||
return badges;
|
return badges;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JSONArray getJsonBadges() {
|
public JsonArray getJsonBadges() {
|
||||||
if (badges == null)
|
if (badges == null)
|
||||||
return null;
|
return null;
|
||||||
JSONArray badgesJSON = new JSONArray();
|
JsonArray badgesJSON = new JsonArray();
|
||||||
for (Badge badge : badges) {
|
for (Badge badge : badges) {
|
||||||
JSONObject badgeJSON = new JSONObject();
|
JsonMap badgeJSON = new JsonMap();
|
||||||
badgeJSON.put("name", badge.getName());
|
badgeJSON.set("name", badge.getName());
|
||||||
byte[] logo = badge.getLogo();
|
byte[] logo = badge.getLogo();
|
||||||
if (logo != null)
|
if (logo != null)
|
||||||
badgeJSON.put("logo", Base64.getEncoder().encodeToString(logo));
|
badgeJSON.set("logo", Base64.getEncoder().encodeToString(logo));
|
||||||
badgeJSON.put("level", badge.getLevel());
|
badgeJSON.set("level", badge.getLevel());
|
||||||
badgesJSON.add(badgeJSON);
|
badgesJSON.add(badgeJSON);
|
||||||
}
|
}
|
||||||
return badgesJSON;
|
return badgesJSON;
|
||||||
|
@ -180,7 +189,7 @@ public class Player implements Comparable<Player> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode(){
|
||||||
return Objects.hash(email, pseudo);
|
return Objects.hash(email, pseudo);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
package be.jeffcheasey88.peeratcode.model;
|
package dev.peerat.backend.model;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.json.simple.JSONArray;
|
import dev.peerat.framework.utils.json.JsonArray;
|
||||||
import org.json.simple.JSONObject;
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
public class Puzzle {
|
public class Puzzle {
|
||||||
|
|
||||||
|
@ -17,9 +19,10 @@ public class Puzzle {
|
||||||
private int scoreMax;
|
private int scoreMax;
|
||||||
private Set<String> tags;
|
private Set<String> tags;
|
||||||
private int depend;
|
private int depend;
|
||||||
|
private Timestamp startDate;
|
||||||
|
|
||||||
public Puzzle(int id, String name, String content, byte[] soluce, String verify, int scoreMax, String tags,
|
public Puzzle(int id, String name, String content, byte[] soluce, String verify, int scoreMax, String tags,
|
||||||
int depend) {
|
int depend, Timestamp startDate) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.content = content;
|
this.content = content;
|
||||||
|
@ -28,6 +31,7 @@ public class Puzzle {
|
||||||
this.scoreMax = scoreMax;
|
this.scoreMax = scoreMax;
|
||||||
setTags(tags);
|
setTags(tags);
|
||||||
this.depend = depend;
|
this.depend = depend;
|
||||||
|
this.startDate = startDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getId() {
|
public int getId() {
|
||||||
|
@ -59,13 +63,13 @@ public class Puzzle {
|
||||||
*
|
*
|
||||||
* @return DEATH
|
* @return DEATH
|
||||||
*/
|
*/
|
||||||
public JSONArray getJsonTags() {
|
public JsonArray getJsonTags() {
|
||||||
if (tags == null)
|
if (tags == null)
|
||||||
return null;
|
return null;
|
||||||
JSONArray tagsJSON = new JSONArray();
|
JsonArray tagsJSON = new JsonArray();
|
||||||
for (String tag : tags) {
|
for (String tag : tags) {
|
||||||
JSONObject tagJSON = new JSONObject();
|
JsonMap tagJSON = new JsonMap();
|
||||||
tagJSON.put("name", tag);
|
tagJSON.set("name", tag);
|
||||||
tagsJSON.add(tagJSON);
|
tagsJSON.add(tagJSON);
|
||||||
}
|
}
|
||||||
return tagsJSON;
|
return tagsJSON;
|
||||||
|
@ -82,6 +86,13 @@ public class Puzzle {
|
||||||
return this.depend;
|
return this.depend;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean hasStarted(){
|
||||||
|
LocalDateTime now = LocalDateTime.now();
|
||||||
|
boolean show = true;
|
||||||
|
if(startDate != null) show &= now.isAfter(startDate.toLocalDateTime());
|
||||||
|
return show;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object object) {
|
public boolean equals(Object object) {
|
||||||
if (this == object)
|
if (this == object)
|
30
src/dev/peerat/backend/model/Tag.java
Normal file
30
src/dev/peerat/backend/model/Tag.java
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
package dev.peerat.backend.model;
|
||||||
|
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class Tag{
|
||||||
|
|
||||||
|
private int id;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Tag(int id, String name){
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId(){
|
||||||
|
return this.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName(){
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonMap toJson(){
|
||||||
|
JsonMap result = new JsonMap();
|
||||||
|
result.set("id", id);
|
||||||
|
result.set("name", name);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
114
src/dev/peerat/backend/repository/BaseDatabaseQuery.java
Normal file
114
src/dev/peerat/backend/repository/BaseDatabaseQuery.java
Normal file
|
@ -0,0 +1,114 @@
|
||||||
|
package dev.peerat.backend.repository;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.ResultSetMetaData;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
|
import dev.peerat.backend.model.Badge;
|
||||||
|
import dev.peerat.backend.model.Chapter;
|
||||||
|
import dev.peerat.backend.model.Completion;
|
||||||
|
import dev.peerat.backend.model.Group;
|
||||||
|
import dev.peerat.backend.model.Player;
|
||||||
|
import dev.peerat.backend.model.Puzzle;
|
||||||
|
|
||||||
|
public class BaseDatabaseQuery{
|
||||||
|
|
||||||
|
private ConnectionManager con;
|
||||||
|
private Configuration config;
|
||||||
|
|
||||||
|
public BaseDatabaseQuery(ConnectionManager con){
|
||||||
|
this.con = con;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Connection ensureConnection() throws SQLException{
|
||||||
|
return this.con.ensureConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
public PreparedStatement prepare(String request) throws SQLException{
|
||||||
|
return this.con.ensureConnection().prepareStatement(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Puzzle makePuzzle(ResultSet puzzleResult) throws SQLException {
|
||||||
|
return new Puzzle(puzzleResult.getInt("id_puzzle"), puzzleResult.getString("name"),
|
||||||
|
puzzleResult.getString("content"), puzzleResult.getBytes("soluce"), puzzleResult.getString("verify"),
|
||||||
|
puzzleResult.getInt("score_max"), puzzleResult.getString("tags"),
|
||||||
|
hasColumn(puzzleResult, "origin") ? puzzleResult.getInt("origin") : -1,
|
||||||
|
puzzleResult.getTimestamp("start_date"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Chapter makeChapter(ResultSet chapterResult) throws SQLException {
|
||||||
|
return new Chapter(chapterResult.getInt("id_chapter"), chapterResult.getString("name"),
|
||||||
|
chapterResult.getTimestamp("start_date"), chapterResult.getTimestamp("end_date"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Completion makeCompletion(ResultSet completionResult) throws SQLException {
|
||||||
|
String fileName = null;
|
||||||
|
if (hasColumn(completionResult, "fileName"))
|
||||||
|
fileName = completionResult.getString("fileName");
|
||||||
|
String puzzleName = null;
|
||||||
|
if (hasColumn(completionResult, "name"))
|
||||||
|
puzzleName = completionResult.getString("name");
|
||||||
|
|
||||||
|
return new Completion(completionResult.getInt("fk_player"), completionResult.getInt("fk_puzzle"), completionResult.getInt("tries"),
|
||||||
|
fileName, completionResult.getInt("score"), puzzleName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Player makePlayer(ResultSet playerResult, int id) throws SQLException {
|
||||||
|
Player p = new Player(playerResult.getString("pseudo"), playerResult.getString("email"),
|
||||||
|
playerResult.getString("firstName"), playerResult.getString("lastName"));
|
||||||
|
if (hasColumn(playerResult, "avatar")) {
|
||||||
|
p.setAvatar(playerResult.getBytes("avatar"));
|
||||||
|
}
|
||||||
|
if (hasColumn(playerResult, "score")) {
|
||||||
|
p.addCompletion(new Completion(playerResult.getInt("tries"), playerResult.getInt("score")));
|
||||||
|
for (int ct = 1; ct < playerResult.getInt("completions"); ct++)
|
||||||
|
{ // TODO refactor for V3
|
||||||
|
p.addCompletion(new Completion(0, 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (hasColumn(playerResult, "name")) {
|
||||||
|
// Manage groups
|
||||||
|
String groupName = playerResult.getString("name");
|
||||||
|
if (groupName != null) {
|
||||||
|
p.addGroup(makeGroup(playerResult));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ADD rank
|
||||||
|
PreparedStatement completionsStmt = con.ensureConnection().prepareStatement(DatabasePlayerRepository.GET_PLAYER_RANK());
|
||||||
|
completionsStmt.setInt(1, id);
|
||||||
|
ResultSet result = completionsStmt.executeQuery();
|
||||||
|
while (result.next()) {
|
||||||
|
p.setRank(result.getInt("rank"));
|
||||||
|
}
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Group makeGroup(ResultSet result) throws SQLException {
|
||||||
|
return new Group(result.getString("name"), result.getInt("fk_chapter"), result.getInt("fk_puzzle"), ((hasColumn(result, "countPlayer")) ? result.getInt("countPlayer") : 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Player makeGroupPlayer(ResultSet result) throws SQLException {
|
||||||
|
return new Player(result.getString("pseudo"), result.getInt("score"), result.getInt("tries"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Badge makeBadge(ResultSet rs) throws SQLException {
|
||||||
|
return new Badge(rs.getString("name"), rs.getBytes("logo"), rs.getInt("level"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasColumn(ResultSet rs, String columnName) throws SQLException {
|
||||||
|
// Found on StackOverflow
|
||||||
|
ResultSetMetaData rsmd = rs.getMetaData();
|
||||||
|
int columns = rsmd.getColumnCount();
|
||||||
|
for (int x = 1; x <= columns; x++) {
|
||||||
|
if (columnName.equals(rsmd.getColumnName(x)))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
26
src/dev/peerat/backend/repository/ConnectionManager.java
Normal file
26
src/dev/peerat/backend/repository/ConnectionManager.java
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
package dev.peerat.backend.repository;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
|
|
||||||
|
public class ConnectionManager {
|
||||||
|
|
||||||
|
private Connection con;
|
||||||
|
private Configuration config;
|
||||||
|
|
||||||
|
public ConnectionManager(Configuration config){
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Connection ensureConnection() throws SQLException {
|
||||||
|
if (con == null || (!con.isValid(5))) {
|
||||||
|
this.con = DriverManager.getConnection(
|
||||||
|
"jdbc:mysql://" + config.getDbHost() + ":" + config.getDbPort() + "/" + config.getDbDatabase() + "",
|
||||||
|
config.getDbUser(), config.getDbPassword());
|
||||||
|
}
|
||||||
|
return this.con;
|
||||||
|
}
|
||||||
|
}
|
194
src/dev/peerat/backend/repository/DatabaseAdminRepository.java
Normal file
194
src/dev/peerat/backend/repository/DatabaseAdminRepository.java
Normal file
|
@ -0,0 +1,194 @@
|
||||||
|
package dev.peerat.backend.repository;
|
||||||
|
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
|
import dev.peerat.backend.model.Chapter;
|
||||||
|
import dev.peerat.backend.model.Puzzle;
|
||||||
|
import dev.peerat.backend.model.Tag;
|
||||||
|
|
||||||
|
public class DatabaseAdminRepository extends BaseDatabaseQuery{
|
||||||
|
|
||||||
|
private static enum Query{
|
||||||
|
|
||||||
|
//ADMIN
|
||||||
|
ADD_CHAPTER("INSERT INTO chapters (name, start_date, end_date) VALUES (?,?,?)"),
|
||||||
|
DELETE_CHAPTER("DELETE FROM chapters WHERE id_chapter = ?"),
|
||||||
|
EDIT_CHAPTER("UPDATE chapters SET name = ?, start_date = ?, end_date = ? WHERE id_chapter = ?"),
|
||||||
|
GET_CHAPTER("SELECT * FROM chapters WHERE id_chapter = ?"),
|
||||||
|
|
||||||
|
ADD_PUZZLE("INSERT INTO puzzles (name, content, soluce, verify, score_max, fk_chapter) VALUES (?,?,?,?,?,?)"),
|
||||||
|
DELETE_PUZZLE("DELETE FROM puzzles WHERE id_puzzle = ?"),
|
||||||
|
EDIT_PUZZLE("UPDATE puzzles SET name = ?, content = ?, soluce = ?, verify = ?, score_max = ?, fk_chapter = ? WHERE id_puzzle = ?"),
|
||||||
|
GET_PUZZLE("SELECT p.*, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE p.id_puzzle = ? GROUP BY p.id_puzzle"),
|
||||||
|
GET_PUZZLES("SELECT p.*, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag GROUP BY p.id_puzzle"),
|
||||||
|
|
||||||
|
ADD_TAG("INSERT INTO tags (name) VALUES (?)"),
|
||||||
|
DELETE_TAG("DELETE FROM tags WHERE id_tag = ?"),
|
||||||
|
EDIT_TAG("UPDATE tags SET name = ? WHERE id_tag = ?"),
|
||||||
|
GET_TAGS("SELECT * FROM tags");
|
||||||
|
|
||||||
|
private String request;
|
||||||
|
|
||||||
|
Query(Query parent, String request) {
|
||||||
|
this.request = parent.request + request;
|
||||||
|
}
|
||||||
|
|
||||||
|
Query(String request) {
|
||||||
|
this.request = request;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||||
|
return base.prepare(this.request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.request;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Configuration config;
|
||||||
|
|
||||||
|
public DatabaseAdminRepository(ConnectionManager con, Configuration config){
|
||||||
|
super(con);
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
//ADMIN
|
||||||
|
public Chapter getAdminChapter(int id){
|
||||||
|
try {
|
||||||
|
PreparedStatement chapterStmt = Query.GET_CHAPTER.prepare(this);
|
||||||
|
chapterStmt.setInt(1, id);
|
||||||
|
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||||
|
if (chapterResult.next()) {
|
||||||
|
Chapter chapter = makeChapter(chapterResult);
|
||||||
|
return chapter;
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Puzzle getAdminPuzzle(int id){
|
||||||
|
try {
|
||||||
|
PreparedStatement chapterStmt = Query.GET_PUZZLE.prepare(this);
|
||||||
|
chapterStmt.setInt(1, id);
|
||||||
|
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||||
|
if (chapterResult.next()) {
|
||||||
|
return makePuzzle(chapterResult);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Puzzle> getAdminPuzzles(){
|
||||||
|
try {
|
||||||
|
PreparedStatement chapterStmt = Query.GET_PUZZLES.prepare(this);
|
||||||
|
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||||
|
List<Puzzle> list = new ArrayList<>();
|
||||||
|
while(chapterResult.next()){
|
||||||
|
list.add(makePuzzle(chapterResult));
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Tag> getAdminTags(){
|
||||||
|
try {
|
||||||
|
PreparedStatement chapterStmt = Query.GET_TAGS.prepare(this);
|
||||||
|
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||||
|
List<Tag> list = new ArrayList<>();
|
||||||
|
while(chapterResult.next()){
|
||||||
|
list.add(new Tag(chapterResult.getInt("id_tag"), chapterResult.getString("name")));
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean adminAddChapter(Chapter chapter) throws SQLException {
|
||||||
|
PreparedStatement statement = Query.ADD_CHAPTER.prepare(this);
|
||||||
|
statement.setString(1, chapter.getName());
|
||||||
|
statement.setTimestamp(2, chapter.getStartDate());
|
||||||
|
statement.setTimestamp(3, chapter.getEndDate());
|
||||||
|
return (statement.executeUpdate() >= 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean adminAddPuzzle(Puzzle puzzle, int chapter) throws SQLException {
|
||||||
|
PreparedStatement statement = Query.ADD_PUZZLE.prepare(this);
|
||||||
|
statement.setString(1, puzzle.getName());
|
||||||
|
statement.setString(2, puzzle.getContent());
|
||||||
|
statement.setBytes(3, puzzle.getSoluce());
|
||||||
|
statement.setString(4, "");
|
||||||
|
statement.setInt(5, puzzle.getScoreMax());
|
||||||
|
statement.setInt(6, chapter);
|
||||||
|
return (statement.executeUpdate() >= 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean adminAddTag(String name) throws SQLException {
|
||||||
|
PreparedStatement statement = Query.ADD_TAG.prepare(this);
|
||||||
|
statement.setString(1, name);
|
||||||
|
return (statement.executeUpdate() >= 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean adminUpdateChapter(int id, Chapter chapter) throws SQLException {
|
||||||
|
PreparedStatement statement = Query.EDIT_CHAPTER.prepare(this);
|
||||||
|
statement.setString(1, chapter.getName());
|
||||||
|
statement.setTimestamp(2, chapter.getStartDate());
|
||||||
|
statement.setTimestamp(3, chapter.getEndDate());
|
||||||
|
statement.setInt(4, id);
|
||||||
|
return (statement.executeUpdate() >= 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean adminUpdatePuzzle(int id, Puzzle puzzle, int chapter) throws SQLException {
|
||||||
|
PreparedStatement statement = Query.EDIT_PUZZLE.prepare(this);
|
||||||
|
statement.setString(1, puzzle.getName());
|
||||||
|
statement.setString(2, puzzle.getContent());
|
||||||
|
statement.setBytes(3, puzzle.getSoluce());
|
||||||
|
statement.setString(4, "");
|
||||||
|
statement.setInt(5, puzzle.getScoreMax());
|
||||||
|
statement.setInt(6, chapter);
|
||||||
|
statement.setInt(7, id);
|
||||||
|
return (statement.executeUpdate() >= 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean adminUpdateTag(Tag tag) throws SQLException {
|
||||||
|
PreparedStatement statement = Query.EDIT_TAG.prepare(this);
|
||||||
|
statement.setString(1, tag.getName());
|
||||||
|
statement.setInt(2, tag.getId());
|
||||||
|
return (statement.executeUpdate() >= 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean adminDeleteChapter(int id) throws SQLException {
|
||||||
|
PreparedStatement statement = Query.DELETE_CHAPTER.prepare(this);
|
||||||
|
statement.setInt(1, id);
|
||||||
|
return (statement.executeUpdate() >= 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean adminDeletePuzzle(int id) throws SQLException {
|
||||||
|
PreparedStatement statement = Query.DELETE_PUZZLE.prepare(this);
|
||||||
|
statement.setInt(1, id);
|
||||||
|
return (statement.executeUpdate() >= 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean adminDeleteTag(int id) throws SQLException {
|
||||||
|
PreparedStatement statement = Query.DELETE_TAG.prepare(this);
|
||||||
|
statement.setInt(1, id);
|
||||||
|
return (statement.executeUpdate() >= 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
167
src/dev/peerat/backend/repository/DatabaseAuthRepository.java
Normal file
167
src/dev/peerat/backend/repository/DatabaseAuthRepository.java
Normal file
|
@ -0,0 +1,167 @@
|
||||||
|
package dev.peerat.backend.repository;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
|
||||||
|
import com.password4j.Password;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
|
|
||||||
|
public class DatabaseAuthRepository extends BaseDatabaseQuery{
|
||||||
|
|
||||||
|
private static enum Query{
|
||||||
|
|
||||||
|
// REGISTER
|
||||||
|
CHECK_PSEUDO_AVAILABLE_QUERY("SELECT * FROM players WHERE pseudo = ?"),
|
||||||
|
CHECK_EMAIL_AVAILABLE_QUERY("SELECT * FROM players WHERE email = ?"),
|
||||||
|
REGISTER_QUERY(
|
||||||
|
"INSERT INTO players (pseudo, email, passwd, firstname, lastname, description, avatar) VALUES (?, ?, ?, ?, ?, ?, ?)"),
|
||||||
|
REGISTER_PLAYER_IN_EXISTING_GROUP(
|
||||||
|
"INSERT INTO containsGroups (fk_player, fk_group) VALUES (?, (SELECT id_group FROM groups WHERE name = ?));"),
|
||||||
|
|
||||||
|
// LOGIN
|
||||||
|
CHECK_PASSWORD("SELECT id_player, passwd FROM players WHERE pseudo=?");
|
||||||
|
|
||||||
|
private String request;
|
||||||
|
|
||||||
|
Query(Query parent, String request) {
|
||||||
|
this.request = parent.request + request;
|
||||||
|
}
|
||||||
|
|
||||||
|
Query(String request) {
|
||||||
|
this.request = request;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||||
|
return base.prepare(this.request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.request;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Configuration config;
|
||||||
|
|
||||||
|
public DatabaseAuthRepository(ConnectionManager con, Configuration config){
|
||||||
|
super(con);
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a pseudo is available
|
||||||
|
*
|
||||||
|
* @param pseudo The pseudo to check
|
||||||
|
* @return True if the pseudo is available, false if it's already taken
|
||||||
|
*/
|
||||||
|
public boolean checkPseudoAvailability(String pseudo) {
|
||||||
|
return checkAvailability(pseudo, Query.CHECK_PSEUDO_AVAILABLE_QUERY.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if an email is available
|
||||||
|
*
|
||||||
|
* @param email The email to check
|
||||||
|
* @return True if the email is available, false if it's already taken
|
||||||
|
*/
|
||||||
|
public boolean checkEmailAvailability(String email) {
|
||||||
|
return checkAvailability(email, Query.CHECK_EMAIL_AVAILABLE_QUERY.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean checkAvailability(String queriedString, String correspondingQuery) {
|
||||||
|
try {
|
||||||
|
PreparedStatement statement = prepare(correspondingQuery);
|
||||||
|
statement.setString(1, queriedString);
|
||||||
|
ResultSet result = statement.executeQuery();
|
||||||
|
return !result.next();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a new user
|
||||||
|
*
|
||||||
|
* @param pseudo The pseudo of the user
|
||||||
|
* @param email The email of the user
|
||||||
|
* @param password The password of the user
|
||||||
|
* @param firstname The firstname of the user
|
||||||
|
* @param lastname The lastname of the user
|
||||||
|
* @param description The description of the user
|
||||||
|
* @param sgroup The group of the user
|
||||||
|
* @param avatar The avatar of the user
|
||||||
|
* @return True if the user was registered, false if an error occurred
|
||||||
|
*/
|
||||||
|
public int register(String pseudo, String email, String password, String firstname, String lastname,
|
||||||
|
String description, String sgroup, String avatar) {
|
||||||
|
try {
|
||||||
|
String pass = Password.hash(password).withArgon2().getResult();
|
||||||
|
System.out.println("pass("+pass.length()+") "+pass);
|
||||||
|
Connection con = ensureConnection();
|
||||||
|
con.setAutoCommit(false);
|
||||||
|
try (PreparedStatement playerStatement = con.prepareStatement(Query.REGISTER_QUERY.toString(),
|
||||||
|
Statement.RETURN_GENERATED_KEYS)) {
|
||||||
|
playerStatement.setString(1, pseudo);
|
||||||
|
playerStatement.setString(2, email);
|
||||||
|
playerStatement.setString(3, Password.hash(password).withArgon2().getResult());
|
||||||
|
playerStatement.setString(4, firstname);
|
||||||
|
playerStatement.setString(5, lastname);
|
||||||
|
playerStatement.setString(6, description);
|
||||||
|
playerStatement.setString(7, avatar);
|
||||||
|
if (playerStatement.executeUpdate() == 1) {
|
||||||
|
ResultSet inserted = playerStatement.getGeneratedKeys();
|
||||||
|
if (inserted.next()) {
|
||||||
|
int newPlayerId = inserted.getInt(1);
|
||||||
|
if (!sgroup.isEmpty()) {
|
||||||
|
try (PreparedStatement containsGroupsStatement = con
|
||||||
|
.prepareStatement(Query.REGISTER_PLAYER_IN_EXISTING_GROUP.toString())) {
|
||||||
|
containsGroupsStatement.setInt(1, newPlayerId);
|
||||||
|
containsGroupsStatement.setString(2, sgroup);
|
||||||
|
containsGroupsStatement.executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
con.commit();
|
||||||
|
con.setAutoCommit(true);
|
||||||
|
return newPlayerId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
con.rollback();
|
||||||
|
con.setAutoCommit(true);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Login a user
|
||||||
|
*
|
||||||
|
* @param username The username of the user
|
||||||
|
* @param password The password of the user
|
||||||
|
* @return id the id of the user, -1 if not login successefuly
|
||||||
|
*/
|
||||||
|
public int login(String username, String password) {
|
||||||
|
try {
|
||||||
|
ensureConnection();
|
||||||
|
PreparedStatement statement = prepare(Query.CHECK_PASSWORD.toString());
|
||||||
|
statement.setString(1, username);
|
||||||
|
ResultSet result = statement.executeQuery();
|
||||||
|
if (result.next()) {
|
||||||
|
String hashedPassword = result.getString("passwd");
|
||||||
|
if (Password.check(password, hashedPassword).withArgon2())
|
||||||
|
return result.getInt("id_player");
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,69 @@
|
||||||
|
package dev.peerat.backend.repository;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
|
import dev.peerat.backend.model.Badge;
|
||||||
|
import dev.peerat.backend.model.Puzzle;
|
||||||
|
|
||||||
|
public class DatabaseBadgeRepository extends BaseDatabaseQuery{
|
||||||
|
|
||||||
|
public static String GET_BADGES_OF_PLAYER(){
|
||||||
|
return Query.GET_BADGES_OF_PLAYER.request;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static enum Query{
|
||||||
|
|
||||||
|
// BADGES
|
||||||
|
GET_BADGE("SELECT * FROM badges WHERE id_badge = ?"),
|
||||||
|
GET_BADGES_OF_PLAYER(
|
||||||
|
"SELECT * FROM badges b LEFT JOIN containsBadges cb ON cb.fk_badge = b.id_badge WHERE cb.fk_player = ?");
|
||||||
|
|
||||||
|
|
||||||
|
private String request;
|
||||||
|
|
||||||
|
Query(Query parent, String request) {
|
||||||
|
this.request = parent.request + request;
|
||||||
|
}
|
||||||
|
|
||||||
|
Query(String request) {
|
||||||
|
this.request = request;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||||
|
return base.prepare(this.request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.request;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Configuration config;
|
||||||
|
|
||||||
|
public DatabaseBadgeRepository(ConnectionManager con, Configuration config){
|
||||||
|
super(con);
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Badge getBadge(int badgeId) {
|
||||||
|
try {
|
||||||
|
ensureConnection();
|
||||||
|
PreparedStatement completionsStmt = Query.GET_BADGE.prepare(this);
|
||||||
|
completionsStmt.setInt(1, badgeId);
|
||||||
|
ResultSet result = completionsStmt.executeQuery();
|
||||||
|
if (result.next()) {
|
||||||
|
return makeBadge(result);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
122
src/dev/peerat/backend/repository/DatabaseChapterRepository.java
Normal file
122
src/dev/peerat/backend/repository/DatabaseChapterRepository.java
Normal file
|
@ -0,0 +1,122 @@
|
||||||
|
package dev.peerat.backend.repository;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
|
import dev.peerat.backend.model.Chapter;
|
||||||
|
import dev.peerat.backend.model.Puzzle;
|
||||||
|
|
||||||
|
public class DatabaseChapterRepository extends BaseDatabaseQuery{
|
||||||
|
|
||||||
|
private static enum Query{
|
||||||
|
|
||||||
|
// CHAPTERS
|
||||||
|
SPECIFIC_CHAPTER_QUERY("SELECT * FROM chapters WHERE id_chapter = ?"),
|
||||||
|
CHAPTER_FROM_PUZZLE("SELECT c.*\r\n"
|
||||||
|
+ "FROM chapters c\r\n"
|
||||||
|
+ "JOIN puzzles p ON p.fk_chapter = c.id_chapter\r\n"
|
||||||
|
+ "WHERE p.id_puzzle = ?"),
|
||||||
|
ALL_CHAPTERS_QUERY("SELECT * FROM chapters WHERE id_chapter > 0");
|
||||||
|
|
||||||
|
|
||||||
|
private String request;
|
||||||
|
|
||||||
|
Query(Query parent, String request) {
|
||||||
|
this.request = parent.request + request;
|
||||||
|
}
|
||||||
|
|
||||||
|
Query(String request) {
|
||||||
|
this.request = request;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||||
|
return base.prepare(this.request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.request;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Configuration config;
|
||||||
|
private DatabasePuzzleRepository puzzleRepo;
|
||||||
|
|
||||||
|
public DatabaseChapterRepository(ConnectionManager con, Configuration config, DatabasePuzzleRepository puzzleRepo){
|
||||||
|
super(con);
|
||||||
|
this.config = config;
|
||||||
|
this.puzzleRepo = puzzleRepo;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a specific chapter
|
||||||
|
*
|
||||||
|
* @param id The id of the chapter
|
||||||
|
* @return The chapter or null if an error occurred
|
||||||
|
*/
|
||||||
|
public Chapter getChapter(int id) {
|
||||||
|
try {
|
||||||
|
ensureConnection();
|
||||||
|
PreparedStatement chapterStmt = Query.SPECIFIC_CHAPTER_QUERY.prepare(this);
|
||||||
|
chapterStmt.setInt(1, id);
|
||||||
|
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||||
|
if (chapterResult.next()) {
|
||||||
|
Chapter chapter = makeChapter(chapterResult);
|
||||||
|
List<Puzzle> puzzles = puzzleRepo.getPuzzlesInChapter(id);
|
||||||
|
chapter.setPuzzles(puzzles);
|
||||||
|
return chapter;
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Chapter getChapter(Puzzle puzzle){
|
||||||
|
try {
|
||||||
|
ensureConnection();
|
||||||
|
PreparedStatement chapterStmt = Query.CHAPTER_FROM_PUZZLE.prepare(this);
|
||||||
|
chapterStmt.setInt(1, puzzle.getId());
|
||||||
|
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||||
|
if (chapterResult.next()) {
|
||||||
|
Chapter chapter = makeChapter(chapterResult);
|
||||||
|
List<Puzzle> puzzles = puzzleRepo.getPuzzlesInChapter(chapter.getId());
|
||||||
|
chapter.setPuzzles(puzzles);
|
||||||
|
return chapter;
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all chapters in the database
|
||||||
|
*
|
||||||
|
* @return List of all chapters or null if an error occurred
|
||||||
|
*/
|
||||||
|
public List<Chapter> getAllChapters() {
|
||||||
|
try {
|
||||||
|
List<Chapter> chapterList = new ArrayList<>();
|
||||||
|
ensureConnection();
|
||||||
|
PreparedStatement chapterStmt = Query.ALL_CHAPTERS_QUERY.prepare(this);
|
||||||
|
ResultSet chapterResult = chapterStmt.executeQuery();
|
||||||
|
while (chapterResult.next()) {
|
||||||
|
Chapter chapter = makeChapter(chapterResult);
|
||||||
|
chapter.setPuzzles(puzzleRepo.getPuzzlesInChapter(chapter.getId()));
|
||||||
|
chapterList.add(chapter);
|
||||||
|
}
|
||||||
|
return chapterList;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,162 @@
|
||||||
|
package dev.peerat.backend.repository;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
|
import dev.peerat.backend.model.Completion;
|
||||||
|
import dev.peerat.backend.model.Puzzle;
|
||||||
|
|
||||||
|
public class DatabaseCompletionRepository extends BaseDatabaseQuery{
|
||||||
|
|
||||||
|
private static enum Query{
|
||||||
|
|
||||||
|
// COMPLETIONS
|
||||||
|
GET_COMPLETION(
|
||||||
|
"SELECT * FROM completions WHERE fk_puzzle = ? AND fk_player = ?"),
|
||||||
|
GET_COMPLETION_GROUP("SELECT c.*\r\n"
|
||||||
|
+ "FROM completions c\r\n"
|
||||||
|
+ "WHERE c.fk_puzzle = ? AND c.fk_player IN\r\n"
|
||||||
|
+ "(select f.fk_player FROM containsGroups cgs JOIN containsGroups f ON f.fk_group = cgs.fk_group JOIN groups g ON g.id_group = cgs.fk_group WHERE g.fk_chapter = 12 AND cgs.fk_player = ?)"),
|
||||||
|
INSERT_COMPLETION(
|
||||||
|
"INSERT INTO completions (fk_puzzle, fk_player, tries, code, fileName, score) values (?, ?, ?, ?, ?, ?)"),
|
||||||
|
UPDATE_COMPLETION(
|
||||||
|
"UPDATE completions SET tries = ?, score = ?, fk_player = ? WHERE fk_puzzle = ? AND fk_player = ?"),
|
||||||
|
SCORE("SELECT score FROM completions WHERE fk_player = ? AND fk_puzzle = ?"),
|
||||||
|
SCORE_GROUP("SELECT c.score\r\n"
|
||||||
|
+ "FROM completions c\r\n"
|
||||||
|
+ "WHERE c.fk_puzzle = ? AND c.fk_player IN\r\n"
|
||||||
|
+ "(select f.fk_player FROM containsGroups cgs JOIN containsGroups f ON f.fk_group = cgs.fk_group JOIN groups g ON g.id_group = cgs.fk_group WHERE g.fk_chapter = 12 AND cgs.fk_player = ?)");
|
||||||
|
|
||||||
|
private String request;
|
||||||
|
|
||||||
|
Query(Query parent, String request) {
|
||||||
|
this.request = parent.request + request;
|
||||||
|
}
|
||||||
|
|
||||||
|
Query(String request) {
|
||||||
|
this.request = request;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||||
|
return base.prepare(this.request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.request;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Configuration config;
|
||||||
|
private DatabaseChapterRepository chapterRepo;
|
||||||
|
|
||||||
|
public DatabaseCompletionRepository(ConnectionManager con, Configuration config, DatabaseChapterRepository chapterRepo){
|
||||||
|
super(con);
|
||||||
|
this.config = config;
|
||||||
|
this.chapterRepo = chapterRepo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Completion getCompletionGroup(int user, int puzzle) {
|
||||||
|
try {
|
||||||
|
PreparedStatement stmt = Query.GET_COMPLETION_GROUP.prepare(this);
|
||||||
|
stmt.setInt(1, puzzle);
|
||||||
|
stmt.setInt(2, user);
|
||||||
|
ResultSet result = stmt.executeQuery();
|
||||||
|
if (result.next())
|
||||||
|
return makeCompletion(result);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return getCompletion(user, puzzle);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Completion getCompletion(int playerId, int puzzleId) {
|
||||||
|
try {
|
||||||
|
PreparedStatement completionsStmt = Query.GET_COMPLETION.prepare(this);
|
||||||
|
completionsStmt.setInt(1, puzzleId);
|
||||||
|
completionsStmt.setInt(2, playerId);
|
||||||
|
ResultSet result = completionsStmt.executeQuery();
|
||||||
|
if (result.next()) {
|
||||||
|
return makeCompletion(result);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Completion insertOrUpdatePuzzleResponse(int puzzleId, int userId, String fileName, byte[] code,
|
||||||
|
byte[] response, Puzzle currentPuzzle){
|
||||||
|
try {
|
||||||
|
ensureConnection();
|
||||||
|
Completion completion = getCompletionGroup(userId, puzzleId);
|
||||||
|
if (completion == null){
|
||||||
|
System.out.println("Completion is null");
|
||||||
|
completion = new Completion(userId, puzzleId, fileName, code, response, currentPuzzle);
|
||||||
|
insertCompletion(completion);
|
||||||
|
} else {
|
||||||
|
System.out.println(completion);
|
||||||
|
completion.addTry(currentPuzzle, response, chapterRepo.getChapter(currentPuzzle).getId());
|
||||||
|
int lastUserId = completion.getPlayerId();
|
||||||
|
completion.updatePlayer(userId);
|
||||||
|
updateCompletion(completion, lastUserId);
|
||||||
|
}
|
||||||
|
return completion;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void insertCompletion(Completion newCompletion) throws SQLException {
|
||||||
|
PreparedStatement statement = Query.INSERT_COMPLETION.prepare(this);
|
||||||
|
statement.setInt(1, newCompletion.getPuzzleId());
|
||||||
|
statement.setInt(2, newCompletion.getPlayerId());
|
||||||
|
statement.setInt(3, newCompletion.getTries());
|
||||||
|
statement.setBytes(4, newCompletion.getCode());
|
||||||
|
statement.setString(5, newCompletion.getFileName());
|
||||||
|
statement.setInt(6, newCompletion.getScore());
|
||||||
|
statement.executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void updateCompletion(Completion completionToUpdate, int user) throws SQLException{
|
||||||
|
System.out.println("update "+completionToUpdate);
|
||||||
|
PreparedStatement statement = Query.UPDATE_COMPLETION.prepare(this);
|
||||||
|
statement.setInt(1, completionToUpdate.getTries());
|
||||||
|
statement.setInt(2, completionToUpdate.getScore());
|
||||||
|
statement.setInt(3, completionToUpdate.getPlayerId());
|
||||||
|
statement.setInt(4, completionToUpdate.getPuzzleId());
|
||||||
|
statement.setInt(5, user);
|
||||||
|
statement.executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getScore(int user, int puzzle) {
|
||||||
|
try {
|
||||||
|
ensureConnection();
|
||||||
|
PreparedStatement stmt = Query.SCORE_GROUP.prepare(this);
|
||||||
|
stmt.setInt(1, puzzle);
|
||||||
|
stmt.setInt(2, user);
|
||||||
|
ResultSet result = stmt.executeQuery();
|
||||||
|
if (result.next())
|
||||||
|
return result.getInt("score");
|
||||||
|
|
||||||
|
stmt = Query.SCORE.prepare(this);
|
||||||
|
stmt.setInt(1, user);
|
||||||
|
stmt.setInt(2, puzzle);
|
||||||
|
|
||||||
|
result = stmt.executeQuery();
|
||||||
|
if (result.next())
|
||||||
|
return result.getInt("score");
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
166
src/dev/peerat/backend/repository/DatabaseGroupRepository.java
Normal file
166
src/dev/peerat/backend/repository/DatabaseGroupRepository.java
Normal file
|
@ -0,0 +1,166 @@
|
||||||
|
package dev.peerat.backend.repository;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
|
import dev.peerat.backend.model.Group;
|
||||||
|
import dev.peerat.backend.model.PeerAtUser;
|
||||||
|
|
||||||
|
public class DatabaseGroupRepository extends BaseDatabaseQuery{
|
||||||
|
|
||||||
|
private static enum Query{
|
||||||
|
|
||||||
|
// GROUPS
|
||||||
|
ALL_GROUPS("SELECT * FROM groups"),
|
||||||
|
ALL_GROUPS_BY_CHAPTER("select g.*, count(cg.fk_player) as countPlayer from groups g left join containsGroups cg on cg.fk_group = g.id_group where g.fk_chapter = ? group by g.id_group"),
|
||||||
|
GET_GROUP_FOR_PLAYER("SELECT g.* FROM groups g JOIN containsGroups cg ON cg.fk_group = g.id_group WHERE cg.fk_player = ? AND g.fk_chapter = ?"), // AND g.fk_puzzle = ?
|
||||||
|
GET_GROUP_ID_BY_DATA("SELECT id_group FROM groups WHERE name = ? AND (fk_chapter = ?)"), // OR fk_puzzle = ?
|
||||||
|
GET_GROUP_USERS_COUNT("SELECT count(*) as howmany FROM containsGroups WHERE fk_group = ?"),
|
||||||
|
INSERT_GROUP("INSERT INTO groups (name, fk_chapter) VALUES (?,?)"),
|
||||||
|
INSERT_PLAYER_IN_GROUP("INSERT INTO containsGroups (fk_player, fk_group) VALUES (?,?)"),
|
||||||
|
LEAVE_GROUP("DELETE FROM containsGroups WHERE fk_player = ? AND fk_group = ?");
|
||||||
|
|
||||||
|
private String request;
|
||||||
|
|
||||||
|
Query(Query parent, String request) {
|
||||||
|
this.request = parent.request + request;
|
||||||
|
}
|
||||||
|
|
||||||
|
Query(String request) {
|
||||||
|
this.request = request;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||||
|
return base.prepare(this.request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.request;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Configuration config;
|
||||||
|
|
||||||
|
public DatabaseGroupRepository(ConnectionManager con, Configuration config){
|
||||||
|
super(con);
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Group> getAllGroups() {
|
||||||
|
try {
|
||||||
|
ensureConnection();
|
||||||
|
List<Group> list = new ArrayList<>();
|
||||||
|
PreparedStatement stmt = Query.ALL_GROUPS.prepare(this);
|
||||||
|
ResultSet groupResult = stmt.executeQuery();
|
||||||
|
while (groupResult.next())
|
||||||
|
list.add(makeGroup(groupResult));
|
||||||
|
return list;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Group> getAllGroupsByChapter(int chapter){
|
||||||
|
try {
|
||||||
|
ensureConnection();
|
||||||
|
List<Group> list = new ArrayList<>();
|
||||||
|
PreparedStatement stmt = Query.ALL_GROUPS_BY_CHAPTER.prepare(this);
|
||||||
|
stmt.setInt(1, chapter);
|
||||||
|
ResultSet groupResult = stmt.executeQuery();
|
||||||
|
while (groupResult.next())
|
||||||
|
list.add(makeGroup(groupResult));
|
||||||
|
return list;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean insertGroup(Group group, PeerAtUser creator) throws SQLException {
|
||||||
|
Integer groupId = getGroupId(group);
|
||||||
|
if (groupId == null)
|
||||||
|
ensureConnection();
|
||||||
|
PreparedStatement statement = Query.INSERT_GROUP.prepare(this);
|
||||||
|
statement.setString(1, group.getName());
|
||||||
|
statement.setObject(2, group.getLinkToChapter());
|
||||||
|
// statement.setObject(3, group.getLinkToPuzzle());
|
||||||
|
if (statement.executeUpdate() >= 0)
|
||||||
|
return insertUserInGroup(group, creator);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Group getPlayerGroup(int user, Integer chapter) throws SQLException {
|
||||||
|
ensureConnection();
|
||||||
|
PreparedStatement stmt = Query.GET_GROUP_FOR_PLAYER.prepare(this);
|
||||||
|
stmt.setInt(1, user);
|
||||||
|
stmt.setObject(2, chapter);
|
||||||
|
// stmt.setObject(3, puzzle);
|
||||||
|
|
||||||
|
ResultSet result = stmt.executeQuery();
|
||||||
|
if (result.next())
|
||||||
|
return makeGroup(result);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getGroupId(Group group) throws SQLException {
|
||||||
|
ensureConnection();
|
||||||
|
PreparedStatement stmt = Query.GET_GROUP_ID_BY_DATA.prepare(this);
|
||||||
|
stmt.setString(1, group.getName());
|
||||||
|
stmt.setObject(2, group.getLinkToChapter());
|
||||||
|
// stmt.setObject(3, group.getLinkToPuzzle());
|
||||||
|
|
||||||
|
ResultSet result = stmt.executeQuery();
|
||||||
|
if (result.next())
|
||||||
|
return result.getInt("id_group");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean insertUserInGroup(Group group, PeerAtUser user) throws SQLException{
|
||||||
|
Integer id = getGroupId(group);
|
||||||
|
if(id != null){
|
||||||
|
int howmany = numberInGroup(id);
|
||||||
|
System.out.println("join group, already have "+howmany);
|
||||||
|
if(howmany > config.getGroupMaxPlayers()) return false;
|
||||||
|
}
|
||||||
|
Group alreadyInGroup = getPlayerGroup(user.getId(), group.getLinkToChapter());
|
||||||
|
if (id != null && alreadyInGroup == null) {
|
||||||
|
PreparedStatement stmt = Query.INSERT_PLAYER_IN_GROUP.prepare(this);
|
||||||
|
|
||||||
|
stmt.setInt(1, user.getId());
|
||||||
|
stmt.setInt(2, id);
|
||||||
|
|
||||||
|
return stmt.executeUpdate() >= 0;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int numberInGroup(int group) throws SQLException{
|
||||||
|
PreparedStatement stmt = Query.GET_GROUP_USERS_COUNT.prepare(this);
|
||||||
|
stmt.setInt(1, group);
|
||||||
|
|
||||||
|
ResultSet result = stmt.executeQuery();
|
||||||
|
if(result.next()) return result.getInt("howmany");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean leaveGroup(Group group, PeerAtUser user) throws SQLException {
|
||||||
|
Integer id = getGroupId(group);
|
||||||
|
if (id != null) {
|
||||||
|
PreparedStatement stmt = Query.LEAVE_GROUP.prepare(this);
|
||||||
|
|
||||||
|
stmt.setInt(1, user.getId());
|
||||||
|
stmt.setInt(2, id);
|
||||||
|
|
||||||
|
return stmt.executeUpdate() >= 0;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,102 @@
|
||||||
|
package dev.peerat.backend.repository;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.SortedSet;
|
||||||
|
import java.util.TreeSet;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
|
import dev.peerat.backend.model.Group;
|
||||||
|
import dev.peerat.backend.model.Player;
|
||||||
|
|
||||||
|
public class DatabaseLeaderboardRepository extends BaseDatabaseQuery{
|
||||||
|
|
||||||
|
private static enum Query{
|
||||||
|
|
||||||
|
// LEADERBOARD
|
||||||
|
ALL_PLAYERS_FOR_LEADERBOARD(
|
||||||
|
"select p.*, scores.*, g.* from players p ,(SELECT fk_player, SUM(c.score) AS score, COUNT(c.id_completion) AS completions, SUM(c.tries) AS tries, rank() over(ORDER BY score DESC) AS rank FROM completions c LEFT JOIN puzzles puzz on puzz.id_puzzle = c.fk_puzzle LEFT JOIN chapters chap on chap.id_chapter = puzz.fk_chapter WHERE chap.id_chapter > 1 GROUP BY c.fk_player) AS scores LEFT JOIN containsGroups cg ON scores.fk_player = cg.fk_player LEFT JOIN groups g ON cg.fk_group = g.id_group WHERE p.id_player = scores.fk_player ORDER BY g.fk_chapter"),
|
||||||
|
ALL_GROUP_FOR_CHAPTER_LEADERBOARD(
|
||||||
|
"SELECT g.*, pl.pseudo, co.score, co.tries FROM groups g LEFT JOIN containsGroups cg ON g.id_group = cg.fk_group LEFT JOIN players pl ON cg.fk_player = pl.id_player LEFT JOIN completions co ON pl.id_player = co.fk_player WHERE cg.fk_player IS NOT NULL AND fk_chapter = ? AND (co.fk_puzzle IN (SELECT id_puzzle FROM puzzles puz WHERE puz.fk_chapter = g.fk_chapter) OR co.score IS NULL);");
|
||||||
|
|
||||||
|
private String request;
|
||||||
|
|
||||||
|
Query(Query parent, String request) {
|
||||||
|
this.request = parent.request + request;
|
||||||
|
}
|
||||||
|
|
||||||
|
Query(String request) {
|
||||||
|
this.request = request;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||||
|
return base.prepare(this.request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.request;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Configuration config;
|
||||||
|
|
||||||
|
public DatabaseLeaderboardRepository(ConnectionManager con, Configuration config){
|
||||||
|
super(con);
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SortedSet<Player> getAllPlayerForLeaderboard() {
|
||||||
|
try {
|
||||||
|
ensureConnection();
|
||||||
|
PreparedStatement playersStmt = Query.ALL_PLAYERS_FOR_LEADERBOARD.prepare(this);
|
||||||
|
ResultSet result = playersStmt.executeQuery();
|
||||||
|
ArrayList<Player> players = new ArrayList<Player>();
|
||||||
|
Player tmpPlayer;
|
||||||
|
while (result.next()) {
|
||||||
|
tmpPlayer = makePlayer(result, result.getInt("id_player"));
|
||||||
|
if (!players.contains(tmpPlayer)) {
|
||||||
|
players.add(tmpPlayer);
|
||||||
|
} else {
|
||||||
|
players.get(players.indexOf(tmpPlayer)).addGroup(makeGroup(result));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new TreeSet<Player>(players);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SortedSet<Group> getAllGroupForChapterLeaderboard(int chapterId){
|
||||||
|
try{
|
||||||
|
ensureConnection();
|
||||||
|
PreparedStatement groupsStmt = Query.ALL_GROUP_FOR_CHAPTER_LEADERBOARD.prepare(this);
|
||||||
|
groupsStmt.setInt(1, chapterId);
|
||||||
|
ResultSet result = groupsStmt.executeQuery();
|
||||||
|
List<Group> groups = new ArrayList<Group>();
|
||||||
|
Group tmpGroup;
|
||||||
|
while (result.next()) {
|
||||||
|
tmpGroup = makeGroup(result);
|
||||||
|
if (tmpGroup != null) {
|
||||||
|
int gPosition = groups.indexOf(tmpGroup);
|
||||||
|
if (gPosition < 0) {
|
||||||
|
tmpGroup.addPlayer(makeGroupPlayer(result));
|
||||||
|
groups.add(tmpGroup);
|
||||||
|
} else {
|
||||||
|
groups.get(gPosition).addPlayer(makeGroupPlayer(result));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new TreeSet<Group>(groups);
|
||||||
|
}catch(SQLException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
191
src/dev/peerat/backend/repository/DatabasePlayerRepository.java
Normal file
191
src/dev/peerat/backend/repository/DatabasePlayerRepository.java
Normal file
|
@ -0,0 +1,191 @@
|
||||||
|
package dev.peerat.backend.repository;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.password4j.Password;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
|
import dev.peerat.backend.model.Player;
|
||||||
|
import dev.peerat.backend.model.Puzzle;
|
||||||
|
|
||||||
|
public class DatabasePlayerRepository extends BaseDatabaseQuery{
|
||||||
|
|
||||||
|
public static String GET_PLAYER_RANK(){
|
||||||
|
return Query.GET_PLAYER_RANK.request;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static enum Query{
|
||||||
|
|
||||||
|
// PLAYERS
|
||||||
|
GET_PLAYER_SIMPLE("SELECT pseudo, email, firstname, lastname, description FROM players WHERE id_player = ?"),
|
||||||
|
GET_PLAYER_EMAIL("SELECT id_player FROM players WHERE email = ?"),
|
||||||
|
GET_PLAYER_PSEUDO("SELECT * FROM players WHERE pseudo = ?"),
|
||||||
|
GET_PLAYER_DETAILS("SELECT p.*, g.*\r\n"
|
||||||
|
+ "FROM players p\r\n"
|
||||||
|
+ "LEFT OUTER JOIN containsGroups cg ON p.id_player = cg.fk_player\r\n"
|
||||||
|
+ "LEFT OUTER JOIN groups g ON cg.fk_group = g.id_group\r\n"
|
||||||
|
+ "LEFT OUTER JOIN completions c on p.id_player = c.fk_player\r\n"
|
||||||
|
+ "WHERE "),
|
||||||
|
GET_PLAYER_DETAILS_BY_ID(GET_PLAYER_DETAILS, " p.id_player = ? GROUP BY g.name ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
||||||
|
GET_PLAYER_DETAILS_BY_PSEUDO(GET_PLAYER_DETAILS, "p.pseudo = ? GROUP BY g.name ORDER BY g.fk_chapter, g.fk_puzzle;"),
|
||||||
|
GET_PLAYER_COMPLETIONS("select c.*, p.name from completions c left join puzzles p on c.fk_puzzle = p.id_puzzle where fk_player = ?;"),
|
||||||
|
GET_PLAYER_RANK("SELECT * FROM (SELECT fk_player, RANK() OVER(ORDER BY SUM(score) DESC) rank FROM completions c LEFT JOIN puzzles puzz on puzz.id_puzzle = c.fk_puzzle LEFT JOIN chapters chap on chap.id_chapter = puzz.fk_chapter LEFT JOIN players p ON p.id_player = c.fk_player WHERE chap.id_chapter > 1 GROUP BY fk_player ORDER BY rank) AS ranks WHERE ranks.fk_player = ?;"),
|
||||||
|
UPDATE_PLAYER_INFO("UPDATE players SET pseudo = ?, email = ?, firstname = ?, lastname = ? WHERE id_player = ?"),
|
||||||
|
UPDATE_PLAYER_PASSWORD("UPDATE players SET passwd = ? WHERE id_player = ?");
|
||||||
|
private String request;
|
||||||
|
|
||||||
|
Query(Query parent, String request) {
|
||||||
|
this.request = parent.request + request;
|
||||||
|
}
|
||||||
|
|
||||||
|
Query(String request) {
|
||||||
|
this.request = request;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||||
|
return base.prepare(this.request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.request;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Configuration config;
|
||||||
|
|
||||||
|
public DatabasePlayerRepository(ConnectionManager con, Configuration config){
|
||||||
|
super(con);
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Player getPlayer(int idPlayer) {
|
||||||
|
try {
|
||||||
|
PreparedStatement completionsStmt = Query.GET_PLAYER_SIMPLE.prepare(this);
|
||||||
|
completionsStmt.setInt(1, idPlayer);
|
||||||
|
ResultSet result = completionsStmt.executeQuery();
|
||||||
|
if (result.next()) {
|
||||||
|
return makePlayer(result, idPlayer);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPlayerId(String email){
|
||||||
|
try {
|
||||||
|
ensureConnection();
|
||||||
|
PreparedStatement completionsStmt = Query.GET_PLAYER_EMAIL.prepare(this);
|
||||||
|
completionsStmt.setString(1, email);
|
||||||
|
ResultSet result = completionsStmt.executeQuery();
|
||||||
|
if (result.next()) {
|
||||||
|
return result.getInt("id_player");
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean updatePseudo(int id, Player player, String pseudo){
|
||||||
|
try{
|
||||||
|
PreparedStatement statment = Query.GET_PLAYER_PSEUDO.prepare(this);
|
||||||
|
statment.setString(1, pseudo);
|
||||||
|
ResultSet result = statment.executeQuery();
|
||||||
|
if(result.next()) return false;
|
||||||
|
statment = Query.UPDATE_PLAYER_INFO.prepare(this);
|
||||||
|
statment.setString(1, pseudo);
|
||||||
|
statment.setString(2, player.getEmail());
|
||||||
|
statment.setString(3, player.getFirstname());
|
||||||
|
statment.setString(4, player.getLastname());
|
||||||
|
statment.setInt(5, id);
|
||||||
|
return statment.executeUpdate() > 0;
|
||||||
|
}catch(Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateProfile(int id, Player player, String lastname, String firstname){
|
||||||
|
try{
|
||||||
|
PreparedStatement statment = Query.UPDATE_PLAYER_INFO.prepare(this);
|
||||||
|
statment.setString(1, player.getPseudo());
|
||||||
|
statment.setString(2, player.getEmail());
|
||||||
|
statment.setString(3, firstname);
|
||||||
|
statment.setString(4, lastname);
|
||||||
|
statment.setInt(5, id);
|
||||||
|
statment.executeUpdate();
|
||||||
|
}catch(Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updatePassword(int id, String password){
|
||||||
|
try{
|
||||||
|
PreparedStatement statment = Query.UPDATE_PLAYER_PASSWORD.prepare(this);
|
||||||
|
statment.setString(1, Password.hash(password).withArgon2().getResult());
|
||||||
|
statment.setInt(2, id);
|
||||||
|
statment.executeUpdate();
|
||||||
|
}catch(Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Player getPlayerDetails(int idPlayer) {
|
||||||
|
return getPlayerDetails(idPlayer, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Player getPlayerDetails(String pseudoPlayer) {
|
||||||
|
return getPlayerDetails(-1, pseudoPlayer);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Player getPlayerDetails(int id, String pseudo) {
|
||||||
|
try {
|
||||||
|
ensureConnection();
|
||||||
|
PreparedStatement completionsStmt;
|
||||||
|
if (pseudo != null) {
|
||||||
|
completionsStmt = Query.GET_PLAYER_DETAILS_BY_PSEUDO.prepare(this);
|
||||||
|
completionsStmt.setString(1, pseudo);
|
||||||
|
} else {
|
||||||
|
completionsStmt = Query.GET_PLAYER_DETAILS_BY_ID.prepare(this);
|
||||||
|
completionsStmt.setInt(1, id);
|
||||||
|
}
|
||||||
|
ResultSet result = completionsStmt.executeQuery();
|
||||||
|
Player player = null;
|
||||||
|
while (result.next()) {
|
||||||
|
if (player == null) {
|
||||||
|
id = result.getInt("id_player");
|
||||||
|
player = makePlayer(result, id);
|
||||||
|
completionsStmt = prepare(DatabaseBadgeRepository.GET_BADGES_OF_PLAYER());
|
||||||
|
completionsStmt.setInt(1, id);
|
||||||
|
ResultSet resultBadges = completionsStmt.executeQuery();
|
||||||
|
while (resultBadges.next()) {
|
||||||
|
player.addBadge(makeBadge(resultBadges));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
player.addGroup(makeGroup(result));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ADD completions
|
||||||
|
completionsStmt = Query.GET_PLAYER_COMPLETIONS.prepare(this);
|
||||||
|
completionsStmt.setInt(1, id);
|
||||||
|
result = completionsStmt.executeQuery();
|
||||||
|
while (result.next()) {
|
||||||
|
player.addCompletion(makeCompletion(result));
|
||||||
|
}
|
||||||
|
|
||||||
|
return player;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,78 @@
|
||||||
|
package dev.peerat.backend.repository;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
|
import dev.peerat.backend.model.Puzzle;
|
||||||
|
|
||||||
|
public class DatabasePuzzleRepository extends BaseDatabaseQuery{
|
||||||
|
|
||||||
|
private static enum Query{
|
||||||
|
|
||||||
|
SPECIFIC_PUZZLE_QUERY(
|
||||||
|
"SELECT p.*, np.origin, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN nextPart np ON p.id_puzzle = np.next LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE p.id_puzzle = ? GROUP BY p.id_puzzle"),
|
||||||
|
PUZZLES_IN_CHAPTER_QUERY(
|
||||||
|
"SELECT p.*, GROUP_CONCAT(t.name) AS tags FROM puzzles p LEFT JOIN containsTags ct ON ct.fk_puzzle = p.id_puzzle LEFT JOIN tags t ON t.id_tag = ct.fk_tag WHERE fk_chapter = ? GROUP BY p.id_puzzle");
|
||||||
|
|
||||||
|
private String request;
|
||||||
|
|
||||||
|
Query(Query parent, String request) {
|
||||||
|
this.request = parent.request + request;
|
||||||
|
}
|
||||||
|
|
||||||
|
Query(String request) {
|
||||||
|
this.request = request;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PreparedStatement prepare(BaseDatabaseQuery base) throws SQLException {
|
||||||
|
return base.prepare(this.request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.request;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Configuration config;
|
||||||
|
|
||||||
|
public DatabasePuzzleRepository(ConnectionManager con, Configuration config){
|
||||||
|
super(con);
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Puzzle> getPuzzlesInChapter(int id) throws SQLException {
|
||||||
|
List<Puzzle> puzzles = new ArrayList<>();
|
||||||
|
ensureConnection();
|
||||||
|
PreparedStatement puzzleStmt = Query.PUZZLES_IN_CHAPTER_QUERY.prepare(this);
|
||||||
|
puzzleStmt.setInt(1, id);
|
||||||
|
ResultSet puzzleResult = puzzleStmt.executeQuery();
|
||||||
|
while (puzzleResult.next()) {
|
||||||
|
puzzles.add(makePuzzle(puzzleResult));
|
||||||
|
}
|
||||||
|
return puzzles;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a specific puzzle
|
||||||
|
*
|
||||||
|
* @param id The id of the puzzle
|
||||||
|
* @return The puzzle or null if an error occurred
|
||||||
|
*/
|
||||||
|
public Puzzle getPuzzle(int id) throws SQLException {
|
||||||
|
ensureConnection();
|
||||||
|
PreparedStatement puzzleStmt = Query.SPECIFIC_PUZZLE_QUERY.prepare(this);
|
||||||
|
puzzleStmt.setInt(1, id);
|
||||||
|
ResultSet puzzleResult = puzzleStmt.executeQuery();
|
||||||
|
if (puzzleResult.next()) {
|
||||||
|
return makePuzzle(puzzleResult);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
181
src/dev/peerat/backend/repository/DatabaseQuery.java
Normal file
181
src/dev/peerat/backend/repository/DatabaseQuery.java
Normal file
|
@ -0,0 +1,181 @@
|
||||||
|
package dev.peerat.backend.repository;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public enum DatabaseQuery {
|
||||||
|
|
||||||
|
//TRIGGER
|
||||||
|
FIRST_TRY("CREATE OR REPLACE TRIGGER FirstTry\r\n"
|
||||||
|
+ "AFTER INSERT\r\n"
|
||||||
|
+ " ON completions FOR EACH ROW\r\n"
|
||||||
|
+ "BEGIN\r\n"
|
||||||
|
+ " DECLARE badge INT;\r\n"
|
||||||
|
+ " DECLARE contain INT;\r\n"
|
||||||
|
+ " \r\n"
|
||||||
|
+ " SELECT id_badge\r\n"
|
||||||
|
+ " INTO @badge\r\n"
|
||||||
|
+ " FROM badges\r\n"
|
||||||
|
+ " WHERE name = 'FirstTry';\r\n"
|
||||||
|
+ " \r\n"
|
||||||
|
+ " IF @badge is not null THEN\r\n"
|
||||||
|
+ " \r\n"
|
||||||
|
+ " SELECT count(*)\r\n"
|
||||||
|
+ " INTO @contain\r\n"
|
||||||
|
+ " FROM containsBadges\r\n"
|
||||||
|
+ " WHERE fk_badge = @badge AND fk_player = NEW.fk_player;\r\n"
|
||||||
|
+ " \r\n"
|
||||||
|
+ " IF (@contain = 0) THEN\r\n"
|
||||||
|
+ " INSERT INTO containsBadges(fk_player, fk_badge) VALUES (NEW.fk_player, @badge);\r\n"
|
||||||
|
+ " END IF;\r\n"
|
||||||
|
+ " END IF;\r\n"
|
||||||
|
+ "END;"),
|
||||||
|
EventParticipation("CREATE OR REPLACE TRIGGER EventParticipation\r\n"
|
||||||
|
+ "AFTER INSERT\r\n"
|
||||||
|
+ " ON completions FOR EACH ROW\r\n"
|
||||||
|
+ "BEGIN\r\n"
|
||||||
|
+ " DECLARE badge INT;\r\n"
|
||||||
|
+ " DECLARE endDate datetime;\r\n"
|
||||||
|
+ " DECLARE player INT;\r\n"
|
||||||
|
+ " DECLARE contain INT;\r\n"
|
||||||
|
+ "\r\n"
|
||||||
|
+ " SELECT id_badge\r\n"
|
||||||
|
+ " INTO badge\r\n"
|
||||||
|
+ " FROM badges\r\n"
|
||||||
|
+ " WHERE name = 'EventParticipation';\r\n"
|
||||||
|
+ "\r\n"
|
||||||
|
+ " IF @badge is not null THEN\r\n"
|
||||||
|
+ "\r\n"
|
||||||
|
+ " SELECT c.end_date, i.fk_player\r\n"
|
||||||
|
+ " INTO endDate, player\r\n"
|
||||||
|
+ " FROM inserted i\r\n"
|
||||||
|
+ " JOIN puzzles p ON i.fk_puzzle = p.id_puzzle\r\n"
|
||||||
|
+ " JOIN chapters c on p.fk_chapter = c.id_chapter;\r\n"
|
||||||
|
+ "\r\n"
|
||||||
|
+ " IF @endDate is not null THEN\r\n"
|
||||||
|
+ "\r\n"
|
||||||
|
+ " SELECT count(*)\r\n"
|
||||||
|
+ " INTO contain\r\n"
|
||||||
|
+ " FROM containsBadges\r\n"
|
||||||
|
+ " WHERE fk_badge = badge AND fk_player = player;\r\n"
|
||||||
|
+ "\r\n"
|
||||||
|
+ " IF (@contain = 0) THEN\r\n"
|
||||||
|
+ " INSERT INTO containsBadges(fk_player, fk_badge) VALUES (@player, @badge);\r\n"
|
||||||
|
+ " END IF;\r\n"
|
||||||
|
+ " END IF;\r\n"
|
||||||
|
+ " END IF;\r\n"
|
||||||
|
+ "END;");
|
||||||
|
|
||||||
|
private String request;
|
||||||
|
|
||||||
|
DatabaseQuery(DatabaseQuery parent, String request) {
|
||||||
|
this.request = parent.request + request;
|
||||||
|
}
|
||||||
|
|
||||||
|
DatabaseQuery(String request) {
|
||||||
|
this.request = request;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PreparedStatement prepare(Connection con) throws SQLException {
|
||||||
|
return con.prepareStatement(this.request);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.request;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
|
||||||
|
SELECT p.*, scores.score, scores.completions, scores.tries, scores.rank, g.*
|
||||||
|
FROM players p,
|
||||||
|
(SELECT fk_player, SUM(c.score) AS score,
|
||||||
|
COUNT(c.id_completion) AS completions,
|
||||||
|
SUM(c.tries) AS tries,
|
||||||
|
rank() over(ORDER BY score DESC) AS rank
|
||||||
|
FROM completions c GROUP BY c.fk_player)
|
||||||
|
AS scores
|
||||||
|
LEFT OUTER JOIN containsGroups cg ON scores.fk_player = cg.fk_player
|
||||||
|
LEFT OUTER JOIN groups g ON cg.fk_group = g.id_group
|
||||||
|
WHERE p.id_player = scores.fk_player AND "),
|
||||||
|
|
||||||
|
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* TRIGGER PLACE
|
||||||
|
*
|
||||||
|
|
||||||
|
CREATE OR REPLACE TRIGGER FirstTry
|
||||||
|
AFTER INSERT
|
||||||
|
ON completions FOR EACH ROW
|
||||||
|
BEGIN
|
||||||
|
DECLARE badge INT;
|
||||||
|
DECLARE contain INT;
|
||||||
|
|
||||||
|
SELECT id_badge
|
||||||
|
INTO @badge
|
||||||
|
FROM badges
|
||||||
|
WHERE name = 'FirstTry';
|
||||||
|
|
||||||
|
IF @badge is not null THEN
|
||||||
|
|
||||||
|
SELECT count(*)
|
||||||
|
INTO @contain
|
||||||
|
FROM containsBadges
|
||||||
|
WHERE fk_badge = @badge AND fk_player = NEW.fk_player;
|
||||||
|
|
||||||
|
IF (@contain = 0) THEN
|
||||||
|
|
||||||
|
IF (NEW.score >= 0 AND NEW.tries < 2) THEN
|
||||||
|
INSERT INTO containsBadges(fk_player, fk_badge) VALUES (NEW.fk_player, @badge);
|
||||||
|
END IF;
|
||||||
|
END IF;
|
||||||
|
END IF;
|
||||||
|
END;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
CREATE OR REPLACE TRIGGER EventParticipation
|
||||||
|
AFTER INSERT
|
||||||
|
ON completions FOR EACH ROW
|
||||||
|
BEGIN
|
||||||
|
DECLARE badge INT;
|
||||||
|
DECLARE endDate datetime;
|
||||||
|
DECLARE player INT;
|
||||||
|
DECLARE contain INT;
|
||||||
|
|
||||||
|
SELECT id_badge
|
||||||
|
INTO badge
|
||||||
|
FROM badges
|
||||||
|
WHERE name = 'EventParticipation';
|
||||||
|
|
||||||
|
IF @badge is not null THEN
|
||||||
|
|
||||||
|
SELECT c.end_date, i.fk_player
|
||||||
|
INTO endDate, player
|
||||||
|
FROM inserted i
|
||||||
|
JOIN puzzles p ON i.fk_puzzle = p.id_puzzle
|
||||||
|
JOIN chapters c on p.fk_chapter = c.id_chapter;
|
||||||
|
|
||||||
|
IF @endDate is not null THEN
|
||||||
|
|
||||||
|
SELECT count(*)
|
||||||
|
INTO contain
|
||||||
|
FROM containsBadges
|
||||||
|
WHERE fk_badge = badge AND fk_player = player;
|
||||||
|
|
||||||
|
IF (@contain = 0) THEN
|
||||||
|
INSERT INTO containsBadges(fk_player, fk_badge) VALUES (@player, @badge);
|
||||||
|
END IF;
|
||||||
|
END IF;
|
||||||
|
END IF;
|
||||||
|
END;
|
||||||
|
|
||||||
|
|
||||||
|
*
|
||||||
|
*/
|
239
src/dev/peerat/backend/repository/DatabaseRepository.java
Normal file
239
src/dev/peerat/backend/repository/DatabaseRepository.java
Normal file
|
@ -0,0 +1,239 @@
|
||||||
|
package dev.peerat.backend.repository;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.nio.file.FileSystems;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.StandardWatchEventKinds;
|
||||||
|
import java.nio.file.WatchEvent;
|
||||||
|
import java.nio.file.WatchKey;
|
||||||
|
import java.nio.file.WatchService;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
|
|
||||||
|
public class DatabaseRepository extends BaseDatabaseQuery{
|
||||||
|
|
||||||
|
private Connection con; //refractor chain
|
||||||
|
private Configuration config;
|
||||||
|
|
||||||
|
private DatabasePuzzleRepository puzzleRepo;
|
||||||
|
private DatabaseChapterRepository chapterRepo;
|
||||||
|
private DatabaseAdminRepository adminRepo;
|
||||||
|
private DatabaseAuthRepository authRepo;
|
||||||
|
private DatabaseBadgeRepository badgeRepo;
|
||||||
|
private DatabaseCompletionRepository completionRepo;
|
||||||
|
private DatabaseGroupRepository groupRepo;
|
||||||
|
private DatabaseLeaderboardRepository leaderboardRepo;
|
||||||
|
private DatabasePlayerRepository playerRepo;
|
||||||
|
|
||||||
|
public DatabaseRepository(ConnectionManager con, Configuration config) throws Exception {
|
||||||
|
super(con);
|
||||||
|
this.config = config;
|
||||||
|
|
||||||
|
this.puzzleRepo = new DatabasePuzzleRepository(con, config);
|
||||||
|
this.chapterRepo = new DatabaseChapterRepository(con, config, puzzleRepo);
|
||||||
|
this.adminRepo = new DatabaseAdminRepository(con, config);
|
||||||
|
this.authRepo = new DatabaseAuthRepository(con, config);
|
||||||
|
this.badgeRepo = new DatabaseBadgeRepository(con, config);
|
||||||
|
this.completionRepo = new DatabaseCompletionRepository(con, config, chapterRepo);
|
||||||
|
this.groupRepo = new DatabaseGroupRepository(con, config);
|
||||||
|
this.leaderboardRepo = new DatabaseLeaderboardRepository(con, config);
|
||||||
|
this.playerRepo = new DatabasePlayerRepository(con, config);
|
||||||
|
|
||||||
|
loadConfig(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadConfig(Configuration config) throws Exception{
|
||||||
|
String name = DatabaseRepository.class.getPackage().getName();
|
||||||
|
InputStream stream = ClassLoader.getSystemClassLoader().getResourceAsStream(name.replace(".", "/"));
|
||||||
|
File folder = new File(config.getSqlFolder());
|
||||||
|
try{
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
|
||||||
|
String line;
|
||||||
|
while((line = reader.readLine()) != null){
|
||||||
|
if(line.endsWith(".class")){
|
||||||
|
Class<?> clazz = Class.forName(name+"."+line.substring(0, line.length()-6));
|
||||||
|
if(BaseDatabaseQuery.class.isAssignableFrom(clazz)){
|
||||||
|
for(Class<?> subClazz : clazz.getDeclaredClasses()){
|
||||||
|
if(subClazz.isEnum()){
|
||||||
|
configure(subClazz, folder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
reader.close();
|
||||||
|
}catch(Exception e){
|
||||||
|
System.err.println("Failed to read "+name);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
new Thread(() -> {
|
||||||
|
try{
|
||||||
|
WatchService watchService = FileSystems.getDefault().newWatchService();
|
||||||
|
Path path = folder.toPath();
|
||||||
|
path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
|
||||||
|
while (true) {
|
||||||
|
WatchKey key = watchService.take();
|
||||||
|
for(WatchEvent<?> event : key.pollEvents()){
|
||||||
|
Path edited = (Path) event.context();
|
||||||
|
String targetClazz = edited.toFile().getName().split("\\.")[0];
|
||||||
|
Class<?> clazz = Class.forName(name+"."+targetClazz);
|
||||||
|
if(BaseDatabaseQuery.class.isAssignableFrom(clazz)){
|
||||||
|
for(Class<?> subClazz : clazz.getDeclaredClasses()){
|
||||||
|
if(subClazz.isEnum()){
|
||||||
|
bind(subClazz, new File(folder, edited.toFile().getName()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
key.reset();
|
||||||
|
}
|
||||||
|
}catch(Exception ex){
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void configure(Class<?> clazz, File folder) throws Exception{
|
||||||
|
String name = clazz.getName().split("\\$")[0];
|
||||||
|
String[] split = name.split("\\.");
|
||||||
|
File file = new File(folder, split[split.length-1]+".txt");
|
||||||
|
if(file.exists()){
|
||||||
|
bind(clazz, file);
|
||||||
|
}else{
|
||||||
|
File parent = file.getParentFile();
|
||||||
|
if(!parent.exists()) parent.mkdirs();
|
||||||
|
file.createNewFile();
|
||||||
|
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
|
||||||
|
for(Object obj : clazz.getEnumConstants()){
|
||||||
|
Enum instance = (Enum) obj;
|
||||||
|
writer.write(instance.name()+"="+instance.toString().replace("\n", " ").replace("\r", " ")+"\n");
|
||||||
|
}
|
||||||
|
writer.flush();
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void bind(Class<?> clazz, File file) throws Exception{
|
||||||
|
Map<String, String> map = new HashMap<>();
|
||||||
|
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||||
|
String line;
|
||||||
|
while((line = reader.readLine()) != null){
|
||||||
|
int index = line.indexOf('=');
|
||||||
|
String key = line.substring(0, index);
|
||||||
|
String value = line.substring(index+1);
|
||||||
|
map.put(key, value);
|
||||||
|
}
|
||||||
|
reader.close();
|
||||||
|
|
||||||
|
for(Object obj : clazz.getEnumConstants()){
|
||||||
|
Enum instance = (Enum) obj;
|
||||||
|
String value = map.get(instance.name());
|
||||||
|
Field field = obj.getClass().getDeclaredField("request");
|
||||||
|
field.setAccessible(true);
|
||||||
|
field.set(instance, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public DatabasePuzzleRepository getPuzzleRepository(){
|
||||||
|
return this.puzzleRepo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DatabaseChapterRepository getChapterRepository(){
|
||||||
|
return this.chapterRepo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DatabaseAdminRepository getAdminRepository(){
|
||||||
|
return this.adminRepo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DatabaseAuthRepository getAuthRepository(){
|
||||||
|
return this.authRepo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DatabaseBadgeRepository getBadgeRepository(){
|
||||||
|
return this.badgeRepo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DatabaseCompletionRepository getCompletionRepository(){
|
||||||
|
return this.completionRepo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DatabaseGroupRepository getGroupRepository(){
|
||||||
|
return this.groupRepo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DatabaseLeaderboardRepository getLeaderboardRepository(){
|
||||||
|
return this.leaderboardRepo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DatabasePlayerRepository getPlayerRepository(){
|
||||||
|
return this.playerRepo;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void hotfix() throws Exception{
|
||||||
|
ensureConnection();
|
||||||
|
|
||||||
|
PreparedStatement stmt = con.prepareStatement("SELECT c.*, g.* FROM completions c JOIN puzzles p ON c.fk_puzzle = p.id_puzzle JOIN containsGroups cg ON cg.fk_player = c.fk_player JOIN groups g ON g.id_group = cg.fk_group WHERE g.fk_chapter = 12 AND p.fk_chapter = 12;");
|
||||||
|
ResultSet results = stmt.executeQuery();
|
||||||
|
List<Completionz> list = new ArrayList<>();
|
||||||
|
while(results.next()){
|
||||||
|
Completionz complete = new Completionz(results);
|
||||||
|
list.add(complete);
|
||||||
|
}
|
||||||
|
Map<Integer, Map<Integer, Completionz>> map = new HashMap<>();
|
||||||
|
for(Completionz comp : list){
|
||||||
|
Map<Integer,Completionz> puz = map.get(comp.puzzle);
|
||||||
|
if(puz == null){
|
||||||
|
puz = new HashMap<>();
|
||||||
|
map.put(comp.puzzle, puz);
|
||||||
|
}
|
||||||
|
Completionz c = puz.get(comp.groups);
|
||||||
|
if(c == null){
|
||||||
|
puz.put(comp.groups, comp);
|
||||||
|
}else{
|
||||||
|
if(comp.score >= c.score){
|
||||||
|
puz.put(comp.groups, comp);
|
||||||
|
System.out.println("remove compl "+c.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class Completionz{
|
||||||
|
|
||||||
|
public int id;
|
||||||
|
public int puzzle;
|
||||||
|
public int player;
|
||||||
|
public int tries;
|
||||||
|
public int score;
|
||||||
|
public int groups;
|
||||||
|
|
||||||
|
public Completionz(ResultSet result) throws Exception{
|
||||||
|
id = result.getInt("id_completion");
|
||||||
|
puzzle = result.getInt("fk_puzzle");
|
||||||
|
player = result.getInt("fk_player");
|
||||||
|
tries = result.getInt("tries");
|
||||||
|
score = result.getInt("score");
|
||||||
|
groups = result.getInt("id_group");
|
||||||
|
System.out.println(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
46
src/dev/peerat/backend/routes/BadgeDetails.java
Normal file
46
src/dev/peerat/backend/routes/BadgeDetails.java
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
package dev.peerat.backend.routes;
|
||||||
|
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||||
|
import dev.peerat.backend.model.Badge;
|
||||||
|
import dev.peerat.backend.repository.DatabaseBadgeRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Response;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class BadgeDetails implements Response {
|
||||||
|
|
||||||
|
private final DatabaseBadgeRepository databaseRepo;
|
||||||
|
|
||||||
|
public BadgeDetails(DatabaseRepository databaseRepo) {
|
||||||
|
this.databaseRepo = databaseRepo.getBadgeRepository();
|
||||||
|
}
|
||||||
|
|
||||||
|
@RouteDoc(path = "/badge/<id>", responseCode = 200, responseDescription = "JSON contenant les informations du badge")
|
||||||
|
@RouteDoc(responseCode = 400, responseDescription = "Aucun id donner")
|
||||||
|
|
||||||
|
@Route(path = "^\\/badge\\/([0-9]+)$", needLogin = true)
|
||||||
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
|
if (matcher.groupCount() > 0) {
|
||||||
|
int badgeId = Integer.parseInt(matcher.group(1));
|
||||||
|
Badge badge = databaseRepo.getBadge(badgeId);
|
||||||
|
JsonMap badgeJSON = new JsonMap();
|
||||||
|
if(badge != null){
|
||||||
|
badgeJSON.set("name", badge.getName());
|
||||||
|
if(badge.getLogo() != null) badgeJSON.set("logo", Base64.getEncoder().encodeToString(badge.getLogo()));
|
||||||
|
badgeJSON.set("level", badge.getLevel());
|
||||||
|
}
|
||||||
|
context.response(200);
|
||||||
|
writer.write(badgeJSON.toString().replace("\\", ""));
|
||||||
|
} else {
|
||||||
|
context.response(400);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
68
src/dev/peerat/backend/routes/ChapterElement.java
Normal file
68
src/dev/peerat/backend/routes/ChapterElement.java
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
package dev.peerat.backend.routes;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||||
|
import dev.peerat.backend.model.Chapter;
|
||||||
|
import dev.peerat.backend.model.PeerAtUser;
|
||||||
|
import dev.peerat.backend.model.Puzzle;
|
||||||
|
import dev.peerat.backend.repository.DatabaseChapterRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseCompletionRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseGroupRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Response;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.utils.json.JsonArray;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class ChapterElement implements Response {
|
||||||
|
|
||||||
|
private final DatabaseChapterRepository chapterRepo;
|
||||||
|
private final DatabaseCompletionRepository completionRepo;
|
||||||
|
|
||||||
|
public ChapterElement(DatabaseRepository repo) {
|
||||||
|
this.chapterRepo = repo.getChapterRepository();
|
||||||
|
this.completionRepo = repo.getCompletionRepository();
|
||||||
|
}
|
||||||
|
|
||||||
|
@RouteDoc(path = "/chapter/<id>", responseCode = 200, responseDescription = "JSON contenant les informations du chapitre demander")
|
||||||
|
@RouteDoc(responseCode = 400, responseDescription = "Aucun id donner")
|
||||||
|
|
||||||
|
@Route(path = "^\\/chapter\\/([0-9]+)$", needLogin = true)
|
||||||
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
Chapter chapter = chapterRepo.getChapter(Integer.parseInt(matcher.group(1)));
|
||||||
|
if (chapter != null){
|
||||||
|
JsonMap chapterJSON = new JsonMap();
|
||||||
|
chapterJSON.set("id", chapter.getId());
|
||||||
|
chapterJSON.set("name", chapter.getName());
|
||||||
|
boolean show = chapter.hasStarted();
|
||||||
|
chapterJSON.set("show", show);
|
||||||
|
if(chapter.getStartDate() != null) chapterJSON.set("start", chapter.getStartDate().toString());
|
||||||
|
if(chapter.getEndDate() != null) chapterJSON.set("end", chapter.getEndDate().toString());
|
||||||
|
PeerAtUser user = context.getUser();
|
||||||
|
if(show){
|
||||||
|
JsonArray puzzles = new JsonArray();
|
||||||
|
for (Puzzle puzzle : chapter.getPuzzles()){
|
||||||
|
JsonMap puzzleJSON = new JsonMap();
|
||||||
|
puzzleJSON.set("id", puzzle.getId());
|
||||||
|
puzzleJSON.set("name", puzzle.getName());
|
||||||
|
puzzleJSON.set("scoreMax", puzzle.getScoreMax());
|
||||||
|
if (puzzle.getTags() != null) puzzleJSON.set("tags", puzzle.getJsonTags());
|
||||||
|
int score = this.completionRepo.getScore(user.getId(), puzzle.getId());
|
||||||
|
if(score >= 0) puzzleJSON.set("score", score);
|
||||||
|
puzzleJSON.set("show", puzzle.hasStarted());
|
||||||
|
puzzles.add(puzzleJSON);
|
||||||
|
}
|
||||||
|
chapterJSON.set("puzzles", puzzles);
|
||||||
|
}
|
||||||
|
context.response(200);
|
||||||
|
writer.write(chapterJSON.toString());
|
||||||
|
} else {
|
||||||
|
context.response(400);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
50
src/dev/peerat/backend/routes/ChapterList.java
Normal file
50
src/dev/peerat/backend/routes/ChapterList.java
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
package dev.peerat.backend.routes;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||||
|
import dev.peerat.backend.model.Chapter;
|
||||||
|
import dev.peerat.backend.repository.DatabaseChapterRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Response;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.utils.json.JsonArray;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class ChapterList implements Response {
|
||||||
|
|
||||||
|
private final DatabaseChapterRepository databaseRepo;
|
||||||
|
|
||||||
|
public ChapterList(DatabaseRepository databaseRepo) {
|
||||||
|
this.databaseRepo = databaseRepo.getChapterRepository();
|
||||||
|
}
|
||||||
|
|
||||||
|
@RouteDoc(path = "/chapters", responseCode = 200, responseDescription = "JSON contenant les informations des chapitres")
|
||||||
|
@RouteDoc(responseCode = 400, responseDescription = "Aucun chapitre trouver")
|
||||||
|
|
||||||
|
@Route(path = "^\\/chapters$", needLogin = true)
|
||||||
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
|
List<Chapter> allChapters = databaseRepo.getAllChapters();
|
||||||
|
if (allChapters != null) {
|
||||||
|
JsonArray chaptersJSON = new JsonArray();
|
||||||
|
for (Chapter chapter : allChapters) {
|
||||||
|
JsonMap chapterJSON = new JsonMap();
|
||||||
|
chapterJSON.set("id", chapter.getId());
|
||||||
|
chapterJSON.set("name", chapter.getName());
|
||||||
|
chapterJSON.set("show", chapter.hasStarted());
|
||||||
|
if(chapter.getStartDate() != null) chapterJSON.set("start", chapter.getStartDate().toString());
|
||||||
|
if(chapter.getEndDate() != null) chapterJSON.set("end", chapter.getEndDate().toString());
|
||||||
|
chaptersJSON.add(chapterJSON);
|
||||||
|
}
|
||||||
|
context.response(200);
|
||||||
|
writer.write(chaptersJSON.toString());
|
||||||
|
} else {
|
||||||
|
context.response(400);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,24 +1,25 @@
|
||||||
package be.jeffcheasey88.peeratcode.routes;
|
package dev.peerat.backend.routes;
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
import be.jeffcheasey88.peeratcode.bonus.extract.RouteDoc;
|
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||||
import be.jeffcheasey88.peeratcode.framework.HttpReader;
|
import dev.peerat.backend.model.Completion;
|
||||||
import be.jeffcheasey88.peeratcode.framework.HttpWriter;
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
import be.jeffcheasey88.peeratcode.framework.Locker;
|
import dev.peerat.framework.Context;
|
||||||
import be.jeffcheasey88.peeratcode.framework.Locker.Key;
|
import dev.peerat.framework.HttpReader;
|
||||||
import be.jeffcheasey88.peeratcode.framework.Route;
|
import dev.peerat.framework.HttpWriter;
|
||||||
import be.jeffcheasey88.peeratcode.framework.User;
|
import dev.peerat.framework.Injection;
|
||||||
import be.jeffcheasey88.peeratcode.model.Completion;
|
import dev.peerat.framework.Locker;
|
||||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
import dev.peerat.framework.Locker.Key;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
|
||||||
public class DynamicLeaderboard extends Leaderboard{
|
public class DynamicLeaderboard extends Leaderboard{
|
||||||
|
|
||||||
private Locker<Completion> locker;
|
private Locker<Completion> locker;
|
||||||
|
|
||||||
public DynamicLeaderboard(DatabaseRepository databaseRepo){
|
public DynamicLeaderboard(DatabaseRepository databaseRepo, @Injection("leaderboard") Locker<Completion> locker){
|
||||||
super(databaseRepo);
|
super(databaseRepo);
|
||||||
this.locker = new Locker<>();
|
this.locker = locker;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Locker<Completion> getLocker(){
|
public Locker<Completion> getLocker(){
|
||||||
|
@ -28,7 +29,7 @@ public class DynamicLeaderboard extends Leaderboard{
|
||||||
@RouteDoc(path = "/rleaderboard/{id}", responseCode = 101, responseDescription = "WebSocket")
|
@RouteDoc(path = "/rleaderboard/{id}", responseCode = 101, responseDescription = "WebSocket")
|
||||||
|
|
||||||
@Route(path = "^\\/rleaderboard\\/?(\\d+)?$", websocket = true)
|
@Route(path = "^\\/rleaderboard\\/?(\\d+)?$", websocket = true)
|
||||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
Key key = new Key();
|
Key key = new Key();
|
||||||
|
|
||||||
locker.init(key);
|
locker.init(key);
|
65
src/dev/peerat/backend/routes/EventSSE.java
Normal file
65
src/dev/peerat/backend/routes/EventSSE.java
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
package dev.peerat.backend.routes;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Injection;
|
||||||
|
import dev.peerat.framework.Locker;
|
||||||
|
import dev.peerat.framework.Locker.Key;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class EventSSE{
|
||||||
|
|
||||||
|
private Locker<GroupMessage> locker;
|
||||||
|
|
||||||
|
public EventSSE(@Injection("groupMessages") Locker<GroupMessage> locker){
|
||||||
|
this.locker = locker;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/group/event/$", websocket = true)
|
||||||
|
public void connect(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
|
String group = reader.<JsonMap>readJson().get("group");
|
||||||
|
|
||||||
|
Key key = new Key();
|
||||||
|
|
||||||
|
locker.init(key);
|
||||||
|
try {
|
||||||
|
while(true){
|
||||||
|
locker.lock(key);
|
||||||
|
GroupMessage message = locker.getValue(key);
|
||||||
|
if(message.getGroup() == null || message.getGroup().equals(group)){
|
||||||
|
JsonMap send = new JsonMap();
|
||||||
|
send.set("message", message.getMessage());
|
||||||
|
writer.write(send.toString()+"\n");
|
||||||
|
writer.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}catch(Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
locker.remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class GroupMessage{
|
||||||
|
|
||||||
|
private String group;
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
public GroupMessage(String group, String message){
|
||||||
|
this.group = group;
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGroup(){
|
||||||
|
return this.group;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMessage(){
|
||||||
|
return this.message;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
91
src/dev/peerat/backend/routes/Leaderboard.java
Normal file
91
src/dev/peerat/backend/routes/Leaderboard.java
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
package dev.peerat.backend.routes;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.SortedSet;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||||
|
import dev.peerat.backend.model.Chapter;
|
||||||
|
import dev.peerat.backend.model.Group;
|
||||||
|
import dev.peerat.backend.model.Player;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Response;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.utils.json.JsonArray;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class Leaderboard implements Response {
|
||||||
|
|
||||||
|
private final DatabaseRepository databaseRepo;
|
||||||
|
|
||||||
|
public Leaderboard(DatabaseRepository databaseRepo) {
|
||||||
|
this.databaseRepo = databaseRepo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RouteDoc(path = "/leaderboard/{id}", responseCode = 200, responseDescription = "JSON contenant le leaderboard")
|
||||||
|
|
||||||
|
@Route(path = "^\\/leaderboard\\/?(\\d+)?$")
|
||||||
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
|
context.response(200);
|
||||||
|
if (matcher.group(1) != null) {
|
||||||
|
groupsLeaderboard(Integer.parseInt(matcher.group(1)), writer);
|
||||||
|
} else {
|
||||||
|
playersLeaderboard(writer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void groupsLeaderboard(int chapterId, HttpWriter writer) throws IOException {
|
||||||
|
Chapter chInfo = databaseRepo.getChapterRepository().getChapter(chapterId);
|
||||||
|
|
||||||
|
SortedSet<Group> allGroupsForChapter = databaseRepo.getLeaderboardRepository().getAllGroupForChapterLeaderboard(chapterId);
|
||||||
|
JsonMap leaderboardJSON = new JsonMap();
|
||||||
|
if (chInfo.getStartDate() != null)
|
||||||
|
leaderboardJSON.set("start_date", chInfo.getStartDate().toString());
|
||||||
|
if (chInfo.getEndDate() != null)
|
||||||
|
leaderboardJSON.set("end_date", chInfo.getEndDate().toString());
|
||||||
|
JsonArray groupsJSON = new JsonArray();
|
||||||
|
if (allGroupsForChapter != null) {
|
||||||
|
int rank = 1;
|
||||||
|
int sameRankCount = 1;
|
||||||
|
Group previousGroup = null;
|
||||||
|
for (Group g : allGroupsForChapter) {
|
||||||
|
if (previousGroup != null) {
|
||||||
|
if (g.compareTo(previousGroup) == 0) {
|
||||||
|
sameRankCount++;
|
||||||
|
} else {
|
||||||
|
rank = rank + sameRankCount;
|
||||||
|
sameRankCount = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
groupsJSON.add(g.toJson(rank));
|
||||||
|
previousGroup = g;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
leaderboardJSON.set("groups", groupsJSON);
|
||||||
|
writer.write(leaderboardJSON.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void playersLeaderboard(HttpWriter writer) throws IOException {
|
||||||
|
SortedSet<Player> allPlayers = databaseRepo.getLeaderboardRepository().getAllPlayerForLeaderboard();
|
||||||
|
JsonArray playersJSON = new JsonArray();
|
||||||
|
if (allPlayers != null) {
|
||||||
|
for (Player player : allPlayers) {
|
||||||
|
JsonMap playerJSON = new JsonMap();
|
||||||
|
playerJSON.set("pseudo", player.getPseudo());
|
||||||
|
if (player.getGroups() != null)
|
||||||
|
playerJSON.set("groups", player.getJsonGroups());
|
||||||
|
// if (player.getAvatar() != null)
|
||||||
|
// playerJSON.put("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
|
||||||
|
playerJSON.set("rank", player.getRank());
|
||||||
|
playerJSON.set("score", player.getTotalScore());
|
||||||
|
playerJSON.set("completions", player.getTotalCompletion());
|
||||||
|
playerJSON.set("tries", player.getTotalTries());
|
||||||
|
playersJSON.add(playerJSON);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writer.write(playersJSON.toString());
|
||||||
|
}
|
||||||
|
}
|
62
src/dev/peerat/backend/routes/PlayerDetails.java
Normal file
62
src/dev/peerat/backend/routes/PlayerDetails.java
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
package dev.peerat.backend.routes;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||||
|
import dev.peerat.backend.model.PeerAtUser;
|
||||||
|
import dev.peerat.backend.model.Player;
|
||||||
|
import dev.peerat.backend.repository.DatabasePlayerRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Response;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class PlayerDetails implements Response {
|
||||||
|
|
||||||
|
private final DatabasePlayerRepository databaseRepo;
|
||||||
|
|
||||||
|
public PlayerDetails(DatabaseRepository databaseRepo) {
|
||||||
|
this.databaseRepo = databaseRepo.getPlayerRepository();
|
||||||
|
}
|
||||||
|
|
||||||
|
@RouteDoc(path = "/player/{id}", responseCode = 200, responseDescription = "JSON contenant les informations de l'utilisateur")
|
||||||
|
@RouteDoc(responseCode = 400, responseDescription = "Utilisateur introuvable dans la base de donnée")
|
||||||
|
|
||||||
|
@Route(path = "^\\/player\\/?(.+)?$", needLogin = true)
|
||||||
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
|
Player player;
|
||||||
|
if (matcher.group(1) != null) {
|
||||||
|
player = databaseRepo.getPlayerDetails(matcher.group(1));
|
||||||
|
} else {
|
||||||
|
PeerAtUser user = context.getUser();
|
||||||
|
player = databaseRepo.getPlayerDetails(user.getId());
|
||||||
|
}
|
||||||
|
JsonMap playerJSON = new JsonMap();
|
||||||
|
if (player != null) {
|
||||||
|
playerJSON.set("pseudo", player.getPseudo());
|
||||||
|
playerJSON.set("email", player.getEmail());
|
||||||
|
playerJSON.set("firstname", player.getFirstname());
|
||||||
|
playerJSON.set("lastname", player.getLastname());
|
||||||
|
playerJSON.set("description", player.getDescription());
|
||||||
|
if (player.getGroups() != null)
|
||||||
|
playerJSON.set("groups", player.getJsonGroups());
|
||||||
|
playerJSON.set("rank", player.getRank());
|
||||||
|
playerJSON.set("score", player.getTotalScore());
|
||||||
|
playerJSON.set("completions", player.getTotalCompletion());
|
||||||
|
playerJSON.set("completionsList", player.getJsonCompletions());
|
||||||
|
playerJSON.set("tries", player.getTotalTries());
|
||||||
|
if (player.getBadges() != null)
|
||||||
|
playerJSON.set("badges", player.getJsonBadges());
|
||||||
|
// if (player.getAvatar() != null)
|
||||||
|
// playerJSON.set("avatar", Base64.getEncoder().encodeToString(player.getAvatar()));
|
||||||
|
context.response(200);
|
||||||
|
writer.write(playerJSON.toString());
|
||||||
|
} else {
|
||||||
|
context.response(400);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
77
src/dev/peerat/backend/routes/PuzzleElement.java
Normal file
77
src/dev/peerat/backend/routes/PuzzleElement.java
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
package dev.peerat.backend.routes;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||||
|
import dev.peerat.backend.model.Chapter;
|
||||||
|
import dev.peerat.backend.model.Completion;
|
||||||
|
import dev.peerat.backend.model.PeerAtUser;
|
||||||
|
import dev.peerat.backend.model.Puzzle;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Response;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class PuzzleElement implements Response {
|
||||||
|
|
||||||
|
private final DatabaseRepository databaseRepo;
|
||||||
|
private Predicate<PeerAtUser> isAdmin;
|
||||||
|
|
||||||
|
public PuzzleElement(DatabaseRepository databaseRepo, Predicate<PeerAtUser> isAdmin){
|
||||||
|
this.databaseRepo = databaseRepo;
|
||||||
|
this.isAdmin = isAdmin;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RouteDoc(path = "/puzzle/<id>", responseCode = 200, responseDescription = "JSON contenant les informations du puzzle")
|
||||||
|
@RouteDoc(responseCode = 400, responseDescription = "puzzle introuvable dans la base de donnée")
|
||||||
|
@RouteDoc(responseCode = 423, responseDescription = "L'utilisateur essaye de voir le puzzle en dehors de l'event")
|
||||||
|
|
||||||
|
@Route(path = "^\\/puzzle\\/([0-9]+)$", needLogin = true)
|
||||||
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
|
Puzzle puzzle = databaseRepo.getPuzzleRepository().getPuzzle(extractId(matcher));
|
||||||
|
if (puzzle != null){
|
||||||
|
Chapter chapter = this.databaseRepo.getChapterRepository().getChapter(puzzle);
|
||||||
|
if(chapter.getStartDate() != null && !isAdmin.test(context.getUser())){
|
||||||
|
if(LocalDateTime.now().isBefore(chapter.getStartDate().toLocalDateTime())){
|
||||||
|
context.response(423);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// if(chapter.getEndDate() != null){
|
||||||
|
// if(LocalDateTime.now().isAfter(chapter.getEndDate().toLocalDateTime())){
|
||||||
|
// writer.response(423, "Access-Control-Allow-Origin: *");
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
PeerAtUser user = context.getUser();
|
||||||
|
|
||||||
|
JsonMap puzzleJSON = new JsonMap();
|
||||||
|
puzzleJSON.set("id", puzzle.getId());
|
||||||
|
puzzleJSON.set("name", puzzle.getName());
|
||||||
|
puzzleJSON.set("content", puzzle.getContent());
|
||||||
|
puzzleJSON.set("scoreMax", puzzle.getScoreMax());
|
||||||
|
if(puzzle.getTags() != null) puzzleJSON.set("tags", puzzle.getJsonTags());
|
||||||
|
Completion completion = this.databaseRepo.getCompletionRepository().getCompletionGroup(user.getId(), puzzle.getId());
|
||||||
|
if(completion != null && completion.getScore() >= 0){
|
||||||
|
puzzleJSON.set("score", completion.getScore());
|
||||||
|
puzzleJSON.set("tries", completion.getTries());
|
||||||
|
}
|
||||||
|
if(puzzle.getDepend() > 0) puzzleJSON.set("depend", puzzle.getDepend());
|
||||||
|
context.response(200, "Content-Type: application/json");
|
||||||
|
writer.write(puzzleJSON.toString());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
context.response(400);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int extractId(Matcher matcher) {
|
||||||
|
return Integer.parseInt(matcher.group(1));
|
||||||
|
}
|
||||||
|
}
|
172
src/dev/peerat/backend/routes/PuzzleResponse.java
Normal file
172
src/dev/peerat/backend/routes/PuzzleResponse.java
Normal file
|
@ -0,0 +1,172 @@
|
||||||
|
package dev.peerat.backend.routes;
|
||||||
|
|
||||||
|
import static dev.peerat.framework.RequestType.POST;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
|
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||||
|
import dev.peerat.backend.model.Chapter;
|
||||||
|
import dev.peerat.backend.model.Completion;
|
||||||
|
import dev.peerat.backend.model.Group;
|
||||||
|
import dev.peerat.backend.model.PeerAtUser;
|
||||||
|
import dev.peerat.backend.model.Player;
|
||||||
|
import dev.peerat.backend.model.Puzzle;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Injection;
|
||||||
|
import dev.peerat.framework.Locker;
|
||||||
|
import dev.peerat.framework.Response;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class PuzzleResponse implements Response {
|
||||||
|
private final DatabaseRepository databaseRepo;
|
||||||
|
private final String usersFilesPath;
|
||||||
|
|
||||||
|
private final Locker<Completion> leaderboard;
|
||||||
|
private Predicate<PeerAtUser> isAdmin;
|
||||||
|
|
||||||
|
public PuzzleResponse(DatabaseRepository databaseRepo, Configuration config, @Injection("leaderboard") Locker<Completion> locker, Predicate<PeerAtUser> isAdmin){
|
||||||
|
this.databaseRepo = databaseRepo;
|
||||||
|
usersFilesPath = config.getUsersFiles();
|
||||||
|
this.leaderboard = locker;
|
||||||
|
this.isAdmin = isAdmin;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RouteDoc(path = "/puzzleResponse/<id>", responseCode = 200, responseDescription = "Bonne réponse, json contenant les points + tries")
|
||||||
|
@RouteDoc(responseCode = 400, responseDescription = "Pas de réponse")
|
||||||
|
@RouteDoc(responseCode = 403, responseDescription = "Déjà répondu")
|
||||||
|
@RouteDoc(responseCode = 406, responseDescription = "Mauvaise réponse")
|
||||||
|
@RouteDoc(responseCode = 423, responseDescription = "L'utilisateur essaye de répondre en dehors de l'event")
|
||||||
|
|
||||||
|
@Route(path = "^\\/puzzleResponse\\/([0-9]+)$", type = POST, needLogin = true)
|
||||||
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
ReceivedResponse received = new ReceivedResponse(matcher, reader);
|
||||||
|
if (received.getResponse() == null){
|
||||||
|
context.response(400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
PeerAtUser user = context.getUser();
|
||||||
|
|
||||||
|
//saveSourceCode(received, databaseRepo.getPlayer(user.getId()));
|
||||||
|
JsonMap responseJSON = new JsonMap();
|
||||||
|
if(this.databaseRepo.getCompletionRepository().getScore(user.getId(), received.getPuzzleId()) > 0){
|
||||||
|
context.response(403);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Puzzle currentPuzzle = databaseRepo.getPuzzleRepository().getPuzzle(received.getPuzzleId());
|
||||||
|
if(!currentPuzzle.hasStarted()){
|
||||||
|
context.response(423);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Chapter chapter = this.databaseRepo.getChapterRepository().getChapter(currentPuzzle);
|
||||||
|
if(!chapter.hasStarted()){
|
||||||
|
context.response(423);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(chapter.getEndDate() != null && !isAdmin.test(context.getUser())){
|
||||||
|
if(LocalDateTime.now().isAfter(chapter.getEndDate().toLocalDateTime())){
|
||||||
|
if(Arrays.equals(currentPuzzle.getSoluce(), received.getResponse())){
|
||||||
|
context.response(200, "Content-Type: application/json");
|
||||||
|
JsonMap theoSaisPasJusteRecevoir200 = new JsonMap();
|
||||||
|
theoSaisPasJusteRecevoir200.set("success", true);
|
||||||
|
writer.write(theoSaisPasJusteRecevoir200.toString());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
context.response(423);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Group group = this.databaseRepo.getGroupRepository().getPlayerGroup(user.getId(), chapter.getId());
|
||||||
|
if(group == null){
|
||||||
|
context.response(423);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Completion completion = databaseRepo.getCompletionRepository().insertOrUpdatePuzzleResponse(received.getPuzzleId(), user.getId(),
|
||||||
|
received.getFileName(), received.getSourceCode(), received.getResponse(), currentPuzzle);
|
||||||
|
if(completion == null){
|
||||||
|
context.response(400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(completion.getScore() > 0){
|
||||||
|
context.response(200, "Content-Type: application/json");
|
||||||
|
responseJSON.set("score", completion.getScore());
|
||||||
|
responseJSON.set("tries", completion.getTries());
|
||||||
|
|
||||||
|
}else{
|
||||||
|
context.response(406, "Content-Type: application/json");
|
||||||
|
responseJSON.set("tries", completion.getTries());
|
||||||
|
}
|
||||||
|
writer.write(responseJSON.toString());
|
||||||
|
writer.flush();
|
||||||
|
|
||||||
|
leaderboard.setValue(completion);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void saveSourceCode(ReceivedResponse received, Player player){
|
||||||
|
try{
|
||||||
|
Path path = Paths.get(String.format("%s/%s/puz%04d-%s", usersFilesPath, player.getPseudo(),
|
||||||
|
received.getPuzzleId(), received.getFileName()));
|
||||||
|
Files.write(path, received.getSourceCode());
|
||||||
|
}catch (IOException e){
|
||||||
|
System.err.println("ERREUR D'ENREGISTREMENT DU CODE POUR PUZZLE_RESPONSE : " + e.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class ReceivedResponse {
|
||||||
|
|
||||||
|
private int puzzleId;
|
||||||
|
private byte[] response;
|
||||||
|
private String fileName;
|
||||||
|
private byte[] sourceCode;
|
||||||
|
|
||||||
|
public ReceivedResponse(Matcher matcher, HttpReader reader) throws Exception {
|
||||||
|
puzzleId = Integer.parseInt(matcher.group(1));
|
||||||
|
|
||||||
|
List<String> multiPartData = reader.readMultiPartData();
|
||||||
|
if (multiPartData != null && multiPartData.size() > 0){
|
||||||
|
this.response = multiPartData.get(0).getBytes();
|
||||||
|
System.out.println("Puzzle response:");
|
||||||
|
System.out.println(Arrays.toString(response));
|
||||||
|
System.out.println(new String(response));
|
||||||
|
if (multiPartData.size() == 3) {
|
||||||
|
this.fileName = multiPartData.get(1);
|
||||||
|
this.sourceCode = multiPartData.get(2).getBytes();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPuzzleId() {
|
||||||
|
return puzzleId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getResponse() {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFileName() {
|
||||||
|
return fileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getSourceCode() {
|
||||||
|
return sourceCode;
|
||||||
|
}
|
||||||
|
}
|
41
src/dev/peerat/backend/routes/Result.java
Normal file
41
src/dev/peerat/backend/routes/Result.java
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
package dev.peerat.backend.routes;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||||
|
import dev.peerat.backend.model.PeerAtUser;
|
||||||
|
import dev.peerat.backend.repository.DatabaseCompletionRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Response;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
|
||||||
|
public class Result implements Response {
|
||||||
|
|
||||||
|
private DatabaseCompletionRepository repo;
|
||||||
|
|
||||||
|
public Result(DatabaseRepository repo) {
|
||||||
|
this.repo = repo.getCompletionRepository();
|
||||||
|
}
|
||||||
|
|
||||||
|
@RouteDoc(path = "/result/<id>", responseCode = 200, responseDescription = "Le score")
|
||||||
|
@RouteDoc(responseCode = 425, responseDescription = "Puzzle pas compléter")
|
||||||
|
|
||||||
|
@Route(path = "^\\/result\\/(\\d+)$", needLogin = true)
|
||||||
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
|
int puzzle = Integer.parseInt(matcher.group(1));
|
||||||
|
|
||||||
|
PeerAtUser user = context.getUser();
|
||||||
|
|
||||||
|
int score = this.repo.getScore(user.getId(), puzzle);
|
||||||
|
if (score < 0) {
|
||||||
|
context.response(425);
|
||||||
|
} else {
|
||||||
|
context.response(200);
|
||||||
|
writer.write(score + "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
35
src/dev/peerat/backend/routes/Swagger.java
Normal file
35
src/dev/peerat/backend/routes/Swagger.java
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
package dev.peerat.backend.routes;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
|
import dev.peerat.backend.bonus.extract.RouteExtracter;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Injection;
|
||||||
|
import dev.peerat.framework.Response;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
|
||||||
|
public class Swagger implements Response{
|
||||||
|
|
||||||
|
private String json;
|
||||||
|
|
||||||
|
public Swagger(RouteExtracter extracter, Configuration config){
|
||||||
|
try{
|
||||||
|
// this.json = extracter.swagger(config.getTokenIssuer()).toString();
|
||||||
|
}catch(Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
json = "{}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/swagger$")
|
||||||
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
|
context.response(200);
|
||||||
|
writer.write(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
70
src/dev/peerat/backend/routes/admins/ChapterController.java
Normal file
70
src/dev/peerat/backend/routes/admins/ChapterController.java
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
package dev.peerat.backend.routes.admins;
|
||||||
|
|
||||||
|
import static dev.peerat.framework.RequestType.DELETE;
|
||||||
|
import static dev.peerat.framework.RequestType.POST;
|
||||||
|
import static dev.peerat.framework.RequestType.PUT;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.model.Chapter;
|
||||||
|
import dev.peerat.backend.repository.DatabaseAdminRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class ChapterController{
|
||||||
|
|
||||||
|
private DatabaseAdminRepository repo;
|
||||||
|
|
||||||
|
public ChapterController(DatabaseRepository repo){
|
||||||
|
this.repo = repo.getAdminRepository();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/chapter/$", type = POST, needLogin = true)
|
||||||
|
public void add(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
JsonMap json = reader.readJson();
|
||||||
|
Chapter chapter = new Chapter(-1, json.get("name"), Timestamp.valueOf(json.<String>get("start")), Timestamp.valueOf(json.<String>get("end")));
|
||||||
|
if(repo.adminAddChapter(chapter)){
|
||||||
|
context.response(200);
|
||||||
|
}else{
|
||||||
|
context.response(501);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/chapter/(\\d+)$", type = DELETE, needLogin = true)
|
||||||
|
public void delte(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
if(this.repo.adminDeleteChapter(Integer.parseInt(matcher.group(1)))){
|
||||||
|
context.response(200);
|
||||||
|
}else{
|
||||||
|
context.response(501);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/chapter/(\\d+)$", type = PUT, needLogin = true)
|
||||||
|
public void edit(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
JsonMap json = reader.readJson();
|
||||||
|
Chapter chapter = new Chapter(-1, json.get("name"), Timestamp.valueOf(json.<String>get("start")), Timestamp.valueOf(json.<String>get("end")));
|
||||||
|
if(repo.adminUpdateChapter(Integer.parseInt(matcher.group(1)), chapter)){
|
||||||
|
context.response(200);
|
||||||
|
}else{
|
||||||
|
context.response(501);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/chapter/(\\d+)$", needLogin = true)
|
||||||
|
public void get(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
Chapter chapter = this.repo.getAdminChapter(Integer.parseInt(matcher.group(1)));
|
||||||
|
JsonMap json = new JsonMap();
|
||||||
|
json.set("name", chapter.getName());
|
||||||
|
json.set("start", chapter.getStartDate());
|
||||||
|
json.set("end", chapter.getEndDate());
|
||||||
|
|
||||||
|
context.response(200);
|
||||||
|
writer.write(json.toString()+"\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
32
src/dev/peerat/backend/routes/admins/GroupController.java
Normal file
32
src/dev/peerat/backend/routes/admins/GroupController.java
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
package dev.peerat.backend.routes.admins;
|
||||||
|
|
||||||
|
import dev.peerat.backend.routes.EventSSE.GroupMessage;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.Injection;
|
||||||
|
import dev.peerat.framework.Locker;
|
||||||
|
import dev.peerat.framework.RequestType;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class GroupController {
|
||||||
|
|
||||||
|
private Locker<GroupMessage> locker;
|
||||||
|
|
||||||
|
public GroupController(@Injection("groupMessages") Locker<GroupMessage> locker){
|
||||||
|
this.locker = locker;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/event/$", type = RequestType.POST ,needLogin = true)
|
||||||
|
public void send(Context context, HttpReader reader) throws Exception{
|
||||||
|
JsonMap json = reader.readJson();
|
||||||
|
if(!json.has("message")){
|
||||||
|
context.response(400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
locker.setValue(new GroupMessage(json.get("group"), json.get("message")));
|
||||||
|
context.response(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
78
src/dev/peerat/backend/routes/admins/LogController.java
Normal file
78
src/dev/peerat/backend/routes/admins/LogController.java
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
package dev.peerat.backend.routes.admins;
|
||||||
|
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||||
|
import dev.peerat.backend.model.PeerAtUser;
|
||||||
|
import dev.peerat.backend.repository.DatabaseAdminRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabasePlayerRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Locker;
|
||||||
|
import dev.peerat.framework.Locker.Key;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.Router;
|
||||||
|
import dev.peerat.framework.utils.json.JsonArray;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class LogController {
|
||||||
|
|
||||||
|
private Locker<Context> contextLocker;
|
||||||
|
private Locker<Throwable> exceptionLocker;
|
||||||
|
private DatabaseAdminRepository repo;
|
||||||
|
private DatabasePlayerRepository playerRepo;
|
||||||
|
|
||||||
|
public LogController(Router<PeerAtUser> router, DatabaseRepository repo){
|
||||||
|
this.contextLocker = router.getLogger();
|
||||||
|
this.exceptionLocker = router.getExceptionLogger();
|
||||||
|
this.repo = repo.getAdminRepository();
|
||||||
|
this.playerRepo = repo.getPlayerRepository();
|
||||||
|
}
|
||||||
|
|
||||||
|
@RouteDoc(path = "/admin/logs", responseCode = 200, responseDescription = "L'utilisateur peux voir les logs en directe")
|
||||||
|
@RouteDoc(responseCode = 401, responseDescription = "L'utilisateur n'a pas accès à cette ressource")
|
||||||
|
|
||||||
|
@Route(path = "^/admin/logs$", needLogin = true, websocket = true)
|
||||||
|
public void logs(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
log(reader, writer, this.contextLocker, (json, instance) -> {
|
||||||
|
json.set("logged", instance.isLogged());
|
||||||
|
if(instance.isLogged()) json.set("pseudo", playerRepo.getPlayer(instance.<PeerAtUser>getUser().getId()).getPseudo());
|
||||||
|
json.set("path", instance.getPath());
|
||||||
|
json.set("type", instance.getType().toString());
|
||||||
|
json.set("code", instance.getResponseCode());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/exceptions$", needLogin = true, websocket = true)
|
||||||
|
public void exceptions(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
log(reader, writer, this.exceptionLocker, (json, exception) -> {
|
||||||
|
json.set("type", exception.getClass().getSimpleName());
|
||||||
|
json.set("message", exception.getMessage());
|
||||||
|
JsonArray trace = new JsonArray();
|
||||||
|
for(StackTraceElement element : exception.getStackTrace()) trace.add(element.toString());
|
||||||
|
json.set("trace", trace);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> void log(HttpReader reader, HttpWriter writer, Locker<T> locker, BiConsumer<JsonMap, T> consumer){
|
||||||
|
Key key = new Key();
|
||||||
|
|
||||||
|
locker.init(key);
|
||||||
|
try {
|
||||||
|
while(!reader.isClosed()){
|
||||||
|
locker.lock(key);
|
||||||
|
T instance = locker.getValue(key);
|
||||||
|
JsonMap json = new JsonMap();
|
||||||
|
consumer.accept(json, instance);
|
||||||
|
|
||||||
|
writer.write(json.toString());
|
||||||
|
writer.flush();
|
||||||
|
}
|
||||||
|
}catch(Exception e){}
|
||||||
|
locker.remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
89
src/dev/peerat/backend/routes/admins/PuzzleController.java
Normal file
89
src/dev/peerat/backend/routes/admins/PuzzleController.java
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
package dev.peerat.backend.routes.admins;
|
||||||
|
|
||||||
|
import static dev.peerat.framework.RequestType.DELETE;
|
||||||
|
import static dev.peerat.framework.RequestType.POST;
|
||||||
|
import static dev.peerat.framework.RequestType.PUT;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.model.Puzzle;
|
||||||
|
import dev.peerat.backend.repository.DatabaseAdminRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.utils.json.JsonArray;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class PuzzleController {
|
||||||
|
|
||||||
|
private DatabaseAdminRepository repo;
|
||||||
|
|
||||||
|
public PuzzleController(DatabaseRepository repo){
|
||||||
|
this.repo = repo.getAdminRepository();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/puzzle/$", type = POST, needLogin = true)
|
||||||
|
public void add(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
JsonMap json = reader.readJson();
|
||||||
|
Puzzle puzzle = new Puzzle(-1, json.get("name"), json.get("content"), json.<String>get("soluce").getBytes(), null, json.<Long>get("scoreMax").intValue(), null, -1, null);
|
||||||
|
if(repo.adminAddPuzzle(puzzle, json.<Long>get("chapter").intValue())){
|
||||||
|
context.response(200);
|
||||||
|
}else{
|
||||||
|
context.response(501);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/puzzle/(\\d+)$", type = DELETE, needLogin = true)
|
||||||
|
public void delete(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
if(this.repo.adminDeletePuzzle(Integer.parseInt(matcher.group(1)))){
|
||||||
|
context.response(200);
|
||||||
|
}else{
|
||||||
|
context.response(501);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Route(path = "^/admin/puzzle/(\\d+)$", type = PUT, needLogin = true)
|
||||||
|
public void edit(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
JsonMap json = reader.readJson();
|
||||||
|
Puzzle puzzle = new Puzzle(-1, json.get("name"), json.get("content"), json.<String>get("soluce").getBytes(), null, json.<Long>get("scoreMax").intValue(), null, -1, null);
|
||||||
|
if(repo.adminUpdatePuzzle(Integer.parseInt(matcher.group(1)), puzzle, json.<Long>get("chapter").intValue())){
|
||||||
|
context.response(200);
|
||||||
|
}else{
|
||||||
|
context.response(501);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/puzzle/(\\d+)$", needLogin = true)
|
||||||
|
public void get(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
Puzzle puzzle = this.repo.getAdminPuzzle(Integer.parseInt(matcher.group(1)));
|
||||||
|
JsonMap json = new JsonMap();
|
||||||
|
json.set("name", puzzle.getName());
|
||||||
|
json.set("content", puzzle.getContent());
|
||||||
|
json.set("soluce", new String(puzzle.getSoluce()));
|
||||||
|
json.set("score_max", puzzle.getScoreMax());
|
||||||
|
|
||||||
|
context.response(200);
|
||||||
|
writer.write(json.toString()+"\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/puzzles/$", needLogin = true)
|
||||||
|
public void getAll(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
List<Puzzle> puzzles = repo.getAdminPuzzles();
|
||||||
|
JsonArray array = new JsonArray();
|
||||||
|
for (Puzzle puzzle : puzzles){
|
||||||
|
JsonMap puzzleJSON = new JsonMap();
|
||||||
|
puzzleJSON.set("id", puzzle.getId());
|
||||||
|
puzzleJSON.set("name", puzzle.getName());
|
||||||
|
puzzleJSON.set("scoreMax", puzzle.getScoreMax());
|
||||||
|
if(puzzle.getTags() != null) puzzleJSON.set("tags", puzzle.getJsonTags());
|
||||||
|
puzzleJSON.set("show", puzzle.hasStarted());
|
||||||
|
array.add(puzzleJSON);
|
||||||
|
}
|
||||||
|
context.response(200);
|
||||||
|
writer.write(array.toString());
|
||||||
|
}
|
||||||
|
}
|
68
src/dev/peerat/backend/routes/admins/TagController.java
Normal file
68
src/dev/peerat/backend/routes/admins/TagController.java
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
package dev.peerat.backend.routes.admins;
|
||||||
|
|
||||||
|
import static dev.peerat.framework.RequestType.DELETE;
|
||||||
|
import static dev.peerat.framework.RequestType.POST;
|
||||||
|
import static dev.peerat.framework.RequestType.PUT;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.model.Tag;
|
||||||
|
import dev.peerat.backend.repository.DatabaseAdminRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.utils.json.JsonArray;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class TagController {
|
||||||
|
|
||||||
|
private DatabaseAdminRepository repo;
|
||||||
|
|
||||||
|
public TagController(DatabaseRepository repo){
|
||||||
|
this.repo = repo.getAdminRepository();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/tag/$", type = POST, needLogin = true)
|
||||||
|
public void add(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
JsonMap json = reader.readJson();
|
||||||
|
if(repo.adminAddTag(json.get("name"))){
|
||||||
|
context.response(200);
|
||||||
|
}else{
|
||||||
|
context.response(501);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/tag/(\\d+)$", type = DELETE, needLogin = true)
|
||||||
|
public void delete(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
if(this.repo.adminDeleteTag(Integer.parseInt(matcher.group(1)))){
|
||||||
|
context.response(200);
|
||||||
|
}else{
|
||||||
|
context.response(501);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/tag/(\\d+)$", type = PUT, needLogin = true)
|
||||||
|
public void edit(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
JsonMap json = reader.readJson();
|
||||||
|
if(repo.adminUpdateTag(new Tag(Integer.parseInt(matcher.group(1)), json.get("name")))){
|
||||||
|
context.response(200);
|
||||||
|
}else{
|
||||||
|
context.response(501);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/tag/$", needLogin = true)
|
||||||
|
public void get(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
List<Tag> list = repo.getAdminTags();
|
||||||
|
JsonArray json = new JsonArray();
|
||||||
|
for(Tag tag : list) json.add(tag.toJson());
|
||||||
|
context.response(200);
|
||||||
|
writer.write(json.toString());
|
||||||
|
writer.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
28
src/dev/peerat/backend/routes/admins/WebHookLeaderboard.java
Normal file
28
src/dev/peerat/backend/routes/admins/WebHookLeaderboard.java
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
package dev.peerat.backend.routes.admins;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.model.Completion;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Injection;
|
||||||
|
import dev.peerat.framework.Locker;
|
||||||
|
import dev.peerat.framework.Response;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
|
||||||
|
public class WebHookLeaderboard implements Response{
|
||||||
|
|
||||||
|
private Locker<Completion> locker;
|
||||||
|
|
||||||
|
public WebHookLeaderboard(@Injection("leaderboard") Locker<Completion> locker){
|
||||||
|
this.locker = locker;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/admin/webhook/leaderboard/$", needLogin = true)
|
||||||
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
locker.setValue(new Completion(0, 0));
|
||||||
|
context.response(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
83
src/dev/peerat/backend/routes/groups/GroupCreate.java
Normal file
83
src/dev/peerat/backend/routes/groups/GroupCreate.java
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
package dev.peerat.backend.routes.groups;
|
||||||
|
|
||||||
|
import static dev.peerat.framework.RequestType.POST;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
|
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||||
|
import dev.peerat.backend.model.Chapter;
|
||||||
|
import dev.peerat.backend.model.Group;
|
||||||
|
import dev.peerat.backend.model.PeerAtUser;
|
||||||
|
import dev.peerat.backend.repository.DatabaseChapterRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseGroupRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.backend.utils.FormResponse;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Injection;
|
||||||
|
import dev.peerat.framework.Locker;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class GroupCreate extends FormResponse {
|
||||||
|
|
||||||
|
private Locker<Group> locker;
|
||||||
|
private DatabaseGroupRepository repo;
|
||||||
|
private DatabaseChapterRepository chapterRepo;
|
||||||
|
private int groupDelay;
|
||||||
|
|
||||||
|
public GroupCreate(DatabaseRepository repo, @Injection("groups") Locker<Group> locker, Configuration config){
|
||||||
|
this.repo = repo.getGroupRepository();
|
||||||
|
this.chapterRepo = repo.getChapterRepository();
|
||||||
|
this.locker = locker;
|
||||||
|
this.groupDelay = config.getGroupJoinMinutes();
|
||||||
|
|
||||||
|
validator("name", "[a-zA-Z0-9&|!?{}\\[\\]%/*\\-+=:;,_#@ ]{3,100}");
|
||||||
|
}
|
||||||
|
|
||||||
|
@RouteDoc(path = "/groupCreate", responseCode = 200, responseDescription = "Le groupe a été créé")
|
||||||
|
@RouteDoc(responseCode = 403, responseDescription = "L'utilisateur est déjà dans le groupe / ne peux pas le rejoindre")
|
||||||
|
@RouteDoc(responseCode = 423, responseDescription = "L'utilisateur essaye de crée un groupe après le délai maximum de création de l'event")
|
||||||
|
|
||||||
|
@Route(path = "^\\/groupCreate$", type = POST, needLogin = true)
|
||||||
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
JsonMap json = json(reader);
|
||||||
|
if(!areValids("name")){
|
||||||
|
context.response(403);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Group newGroup = new Group(json);
|
||||||
|
PeerAtUser user = context.getUser();
|
||||||
|
|
||||||
|
if (this.repo.getPlayerGroup(user.getId(), newGroup.getLinkToChapter()) == null) {
|
||||||
|
try {
|
||||||
|
if(this.repo.getGroupId(newGroup) == null) throw new NullPointerException();
|
||||||
|
context.response(403);
|
||||||
|
return;
|
||||||
|
}catch(NullPointerException e){
|
||||||
|
if(newGroup.getLinkToChapter() != null){
|
||||||
|
Chapter chapter = this.chapterRepo.getChapter(newGroup.getLinkToChapter());
|
||||||
|
if(chapter.getStartDate() != null){
|
||||||
|
LocalDateTime start = chapter.getStartDate().toLocalDateTime().plusMinutes(this.groupDelay);
|
||||||
|
if(LocalDateTime.now().isAfter(start)){
|
||||||
|
context.response(423);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (this.repo.insertGroup(newGroup, user)) {
|
||||||
|
context.response(200);
|
||||||
|
|
||||||
|
locker.setValue(newGroup);
|
||||||
|
} else {
|
||||||
|
context.response(403);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
context.response(403);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
84
src/dev/peerat/backend/routes/groups/GroupJoin.java
Normal file
84
src/dev/peerat/backend/routes/groups/GroupJoin.java
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
package dev.peerat.backend.routes.groups;
|
||||||
|
|
||||||
|
import static dev.peerat.framework.RequestType.POST;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
|
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||||
|
import dev.peerat.backend.model.Chapter;
|
||||||
|
import dev.peerat.backend.model.Completion;
|
||||||
|
import dev.peerat.backend.model.Group;
|
||||||
|
import dev.peerat.backend.model.PeerAtUser;
|
||||||
|
import dev.peerat.backend.repository.DatabaseChapterRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseGroupRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Injection;
|
||||||
|
import dev.peerat.framework.Locker;
|
||||||
|
import dev.peerat.framework.Response;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
|
||||||
|
public class GroupJoin implements Response{
|
||||||
|
|
||||||
|
private DatabaseChapterRepository chapterRepo;
|
||||||
|
private DatabaseGroupRepository groupRepo;
|
||||||
|
private int groupDelay;
|
||||||
|
private String waitTime;
|
||||||
|
|
||||||
|
private final Locker<Completion> leaderboard;
|
||||||
|
|
||||||
|
public GroupJoin(DatabaseRepository repo, Configuration config, @Injection("leaderboard") Locker<Completion> locker){
|
||||||
|
this.chapterRepo = repo.getChapterRepository();
|
||||||
|
this.groupRepo = repo.getGroupRepository();
|
||||||
|
this.groupDelay = config.getGroupJoinMinutes();
|
||||||
|
this.waitTime = config.getGroupQuitMinutes();
|
||||||
|
this.leaderboard = locker;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RouteDoc(path = "/groupJoin", responseCode = 200, responseDescription = "L'utilisateur a rejoint le groupe")
|
||||||
|
@RouteDoc(responseCode = 403, responseDescription = "L'utilisateur est déjà dedans / ne peux pas le rejoindre")
|
||||||
|
@RouteDoc(responseCode = 423, responseDescription = "L'utilisateur essaye de rejoindre un groupe après le délai maximum de création de l'event")
|
||||||
|
@RouteDoc(responseCode = 409, responseDescription = "L'utilisateur est un peu débile... ou pas ?")
|
||||||
|
|
||||||
|
@Route(path = "^\\/groupJoin$", type = POST, needLogin = true)
|
||||||
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
Group group = new Group(reader.readJson());
|
||||||
|
PeerAtUser user = context.getUser();
|
||||||
|
|
||||||
|
Group userGroup = this.groupRepo.getPlayerGroup(user.getId(), group.getLinkToChapter());
|
||||||
|
if(group.equals(userGroup)){
|
||||||
|
context.response(403);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(group.getLinkToChapter() == null){
|
||||||
|
context.response(409);
|
||||||
|
writer.write(waitTime);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(group.getLinkToChapter() != null){
|
||||||
|
Chapter chapter = this.chapterRepo.getChapter(group.getLinkToChapter());
|
||||||
|
if(chapter.getStartDate() != null){
|
||||||
|
LocalDateTime start = chapter.getStartDate().toLocalDateTime().plusMinutes(this.groupDelay);
|
||||||
|
if(LocalDateTime.now().isAfter(start)){
|
||||||
|
context.response(423);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.groupRepo.insertUserInGroup(group, user)) {
|
||||||
|
context.response(200);
|
||||||
|
|
||||||
|
leaderboard.setValue(new Completion(0, 0, 0, null, 0));
|
||||||
|
} else {
|
||||||
|
context.response(403);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
43
src/dev/peerat/backend/routes/groups/GroupList.java
Normal file
43
src/dev/peerat/backend/routes/groups/GroupList.java
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
package dev.peerat.backend.routes.groups;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||||
|
import dev.peerat.backend.model.Group;
|
||||||
|
import dev.peerat.backend.repository.DatabaseGroupRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Response;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.utils.json.JsonArray;
|
||||||
|
|
||||||
|
public class GroupList implements Response {
|
||||||
|
|
||||||
|
private DatabaseGroupRepository repo;
|
||||||
|
|
||||||
|
public GroupList(DatabaseRepository repo){
|
||||||
|
this.repo = repo.getGroupRepository();
|
||||||
|
}
|
||||||
|
|
||||||
|
@RouteDoc(path = "/groups", responseCode = 200, responseDescription = "JSON avec la liste des groups")
|
||||||
|
|
||||||
|
@Route(path = "^\\/groups\\/?(\\d+)?$", needLogin = true)
|
||||||
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
String param = matcher.group(1);
|
||||||
|
if(param == null){
|
||||||
|
JsonArray result = new JsonArray();
|
||||||
|
for(Group group : this.repo.getAllGroups()) result.add(group.toJson());
|
||||||
|
context.response(200);
|
||||||
|
writer.write(result.toString());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int chapter = Integer.parseInt(param);
|
||||||
|
JsonArray result = new JsonArray();
|
||||||
|
for(Group group : this.repo.getAllGroupsByChapter(chapter)) result.add(group.toJson());
|
||||||
|
context.response(200);
|
||||||
|
writer.write(result.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
76
src/dev/peerat/backend/routes/groups/GroupQuit.java
Normal file
76
src/dev/peerat/backend/routes/groups/GroupQuit.java
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
package dev.peerat.backend.routes.groups;
|
||||||
|
|
||||||
|
import static dev.peerat.framework.RequestType.POST;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
|
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||||
|
import dev.peerat.backend.model.Chapter;
|
||||||
|
import dev.peerat.backend.model.Completion;
|
||||||
|
import dev.peerat.backend.model.Group;
|
||||||
|
import dev.peerat.backend.model.PeerAtUser;
|
||||||
|
import dev.peerat.backend.repository.DatabaseChapterRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseGroupRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Injection;
|
||||||
|
import dev.peerat.framework.Locker;
|
||||||
|
import dev.peerat.framework.Response;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
|
||||||
|
public class GroupQuit implements Response{
|
||||||
|
|
||||||
|
private DatabaseChapterRepository chapterRepo;
|
||||||
|
private DatabaseGroupRepository groupRepo;
|
||||||
|
private int groupDelay;
|
||||||
|
|
||||||
|
private final Locker<Completion> leaderboard;
|
||||||
|
|
||||||
|
public GroupQuit(DatabaseRepository repo, Configuration config, @Injection("leaderboard") Locker<Completion> locker){
|
||||||
|
this.chapterRepo = repo.getChapterRepository();
|
||||||
|
this.groupRepo = repo.getGroupRepository();
|
||||||
|
this.groupDelay = config.getGroupJoinMinutes();
|
||||||
|
|
||||||
|
this.leaderboard = locker;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RouteDoc(path = "/groupQuit", responseCode = 200, responseDescription = "L'utilisateur à quitter le groupe")
|
||||||
|
@RouteDoc(responseCode = 403, responseDescription = "L'utilisateur n'est pas dans le groupe / n'a pas pu le quittez")
|
||||||
|
@RouteDoc(responseCode = 423, responseDescription = "L'utilisateur essaye de quitter un groupe après le délai maximum de création de l'event")
|
||||||
|
|
||||||
|
@Route(path = "^\\/groupQuit$", type = POST, needLogin = true)
|
||||||
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
Group group = new Group(reader.readJson());
|
||||||
|
PeerAtUser user = context.getUser();
|
||||||
|
|
||||||
|
Group userGroup = this.groupRepo.getPlayerGroup(user.getId(), group.getLinkToChapter());
|
||||||
|
if(!group.equals(userGroup)){
|
||||||
|
context.response(403);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(group.getLinkToChapter() != null){
|
||||||
|
Chapter chapter = this.chapterRepo.getChapter(group.getLinkToChapter());
|
||||||
|
if(chapter.getStartDate() != null){
|
||||||
|
LocalDateTime start = chapter.getStartDate().toLocalDateTime().plusMinutes(this.groupDelay);
|
||||||
|
if(LocalDateTime.now().isAfter(start)){
|
||||||
|
context.response(423);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.groupRepo.leaveGroup(group, user)) {
|
||||||
|
context.response(200);
|
||||||
|
|
||||||
|
leaderboard.setValue(new Completion(0, 0, 0, null, 0));
|
||||||
|
} else {
|
||||||
|
context.response(403);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
36
src/dev/peerat/backend/routes/users/ChangePassword.java
Normal file
36
src/dev/peerat/backend/routes/users/ChangePassword.java
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
package dev.peerat.backend.routes.users;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||||
|
import dev.peerat.backend.model.PeerAtUser;
|
||||||
|
import dev.peerat.backend.repository.DatabasePlayerRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.RequestType;
|
||||||
|
import dev.peerat.framework.Response;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class ChangePassword implements Response{
|
||||||
|
|
||||||
|
private DatabasePlayerRepository repo;
|
||||||
|
|
||||||
|
public ChangePassword(DatabaseRepository repo){
|
||||||
|
this.repo = repo.getPlayerRepository();
|
||||||
|
}
|
||||||
|
|
||||||
|
@RouteDoc(path = "/user/cpw", responseCode = 200, responseDescription = "L'utilisateur a mis à jours sont mots de passe")
|
||||||
|
@RouteDoc(responseCode = 400, responseDescription = "L'utilisateur a envoyer un mots de passe invalide")
|
||||||
|
|
||||||
|
@Route(path = "^/user/cpw$", type = RequestType.POST, needLogin = true)
|
||||||
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
|
String password = reader.<JsonMap>readJson().get("password");
|
||||||
|
|
||||||
|
repo.updatePassword(context.<PeerAtUser>getUser().getId(), password);
|
||||||
|
context.response(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
124
src/dev/peerat/backend/routes/users/ForgotPassword.java
Normal file
124
src/dev/peerat/backend/routes/users/ForgotPassword.java
Normal file
|
@ -0,0 +1,124 @@
|
||||||
|
package dev.peerat.backend.routes.users;
|
||||||
|
|
||||||
|
import static dev.peerat.framework.RequestType.POST;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.model.PeerAtUser;
|
||||||
|
import dev.peerat.backend.repository.DatabasePlayerRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.backend.utils.FormResponse;
|
||||||
|
import dev.peerat.backend.utils.Mail;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.Router;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class ForgotPassword extends FormResponse{
|
||||||
|
|
||||||
|
private Router<PeerAtUser> router;
|
||||||
|
private DatabasePlayerRepository repo;
|
||||||
|
private Mail mail;
|
||||||
|
private Map<String, String> codes;
|
||||||
|
private List<Random> randoms;
|
||||||
|
|
||||||
|
public ForgotPassword(Router<PeerAtUser> router, DatabaseRepository repo, Mail mail){
|
||||||
|
this.router = router;
|
||||||
|
this.repo = repo.getPlayerRepository();
|
||||||
|
this.mail = mail;
|
||||||
|
this.codes = new HashMap<>();
|
||||||
|
|
||||||
|
this.randoms = new ArrayList<>();
|
||||||
|
|
||||||
|
Random random = new Random();
|
||||||
|
int randoms = random.nextInt(10)+3;
|
||||||
|
for(int i = 0; i < randoms; i++) this.randoms.add(new SecureRandom());
|
||||||
|
|
||||||
|
try {
|
||||||
|
Constructor<?> constructor = UUID.class.getDeclaredConstructor(byte[].class);
|
||||||
|
constructor.setAccessible(true);
|
||||||
|
this.uuidBuilder = constructor;
|
||||||
|
} catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route(path = "^/user/fpw$", type = POST)
|
||||||
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
|
if(context.isLogged()){
|
||||||
|
context.response(403);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonMap json = json(reader);
|
||||||
|
if(!areValids("email")){
|
||||||
|
context.response(400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String email = json.get("email");
|
||||||
|
|
||||||
|
int player = repo.getPlayerId(email);
|
||||||
|
if(player < 0){
|
||||||
|
context.response(400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(hasFields("code") && areValids("password")){
|
||||||
|
String checkCode = codes.get(email);
|
||||||
|
if(checkCode == null){
|
||||||
|
context.response(400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String code = json.<String>get("code");
|
||||||
|
String password = json.get("password");
|
||||||
|
|
||||||
|
if(checkCode.equals(code)){
|
||||||
|
codes.remove(email);
|
||||||
|
|
||||||
|
repo.updatePassword(player, password);
|
||||||
|
context.response(200,
|
||||||
|
"Access-Control-Expose-Headers: Authorization",
|
||||||
|
"Authorization: Bearer " + this.router.createAuthUser(new PeerAtUser(player)));
|
||||||
|
}else{
|
||||||
|
context.response(400);
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
String code = codeGenerator();
|
||||||
|
codes.put(email, code);
|
||||||
|
mail.send(email, "Forgot your Peer @ Code password ?", "Your check code is "+code+" !");
|
||||||
|
context.response(200);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Constructor<?> uuidBuilder;
|
||||||
|
private int[] start = {4, 9, 14, 19};
|
||||||
|
|
||||||
|
private String codeGenerator() throws Exception{
|
||||||
|
Random random = new Random();
|
||||||
|
Random target = this.randoms.get(random.nextInt(this.randoms.size()));
|
||||||
|
byte[] arrayOfByte = new byte[16];
|
||||||
|
target.nextBytes(arrayOfByte);
|
||||||
|
arrayOfByte[6] = (byte)(arrayOfByte[6] & 0xF);
|
||||||
|
arrayOfByte[6] = (byte)(arrayOfByte[6] | 0x40);
|
||||||
|
arrayOfByte[8] = (byte)(arrayOfByte[8] & 0x3F);
|
||||||
|
arrayOfByte[8] = (byte)(arrayOfByte[8] | 0x80);
|
||||||
|
String uuid = uuidBuilder.newInstance(arrayOfByte).toString();
|
||||||
|
target = this.randoms.get(random.nextInt(this.randoms.size()));
|
||||||
|
|
||||||
|
int start = this.start[target.nextInt(this.start.length)];
|
||||||
|
return uuid.substring(start, start+9);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
54
src/dev/peerat/backend/routes/users/Login.java
Normal file
54
src/dev/peerat/backend/routes/users/Login.java
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
package dev.peerat.backend.routes.users;
|
||||||
|
|
||||||
|
import static dev.peerat.framework.RequestType.POST;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||||
|
import dev.peerat.backend.model.PeerAtUser;
|
||||||
|
import dev.peerat.backend.repository.DatabaseAuthRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.backend.utils.FormResponse;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.Router;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class Login extends FormResponse{
|
||||||
|
|
||||||
|
private DatabaseAuthRepository repo;
|
||||||
|
private Router<PeerAtUser> router;
|
||||||
|
|
||||||
|
public Login(DatabaseRepository databaseRepo, Router<PeerAtUser> router){
|
||||||
|
this.repo = databaseRepo.getAuthRepository();
|
||||||
|
this.router = router;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RouteDoc(path = "/login", 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 = "^\\/login$", type = POST)
|
||||||
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
|
if(context.isLogged()){
|
||||||
|
context.response(403);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
JsonMap json = json(reader);
|
||||||
|
if(!areValids("pseudo", "passwd")){
|
||||||
|
context.response(400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int id;
|
||||||
|
if((id = repo.login(json.get("pseudo"), json.get("passwd"))) >= 0){
|
||||||
|
context.response(200,
|
||||||
|
"Access-Control-Expose-Headers: Authorization",
|
||||||
|
"Authorization: Bearer " + this.router.createAuthUser(new PeerAtUser(id)));
|
||||||
|
}else{
|
||||||
|
context.response(400);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
230
src/dev/peerat/backend/routes/users/MailConfirmation.java
Normal file
230
src/dev/peerat/backend/routes/users/MailConfirmation.java
Normal file
|
@ -0,0 +1,230 @@
|
||||||
|
package dev.peerat.backend.routes.users;
|
||||||
|
|
||||||
|
import static dev.peerat.framework.RequestType.POST;
|
||||||
|
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.security.KeyPair;
|
||||||
|
import java.security.KeyPairGenerator;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.security.interfaces.RSAPublicKey;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.Base64.Encoder;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import javax.net.ssl.HttpsURLConnection;
|
||||||
|
|
||||||
|
import org.jose4j.json.internal.json_simple.JSONAware;
|
||||||
|
import org.jose4j.json.internal.json_simple.JSONObject;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
|
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||||
|
import dev.peerat.backend.model.PeerAtUser;
|
||||||
|
import dev.peerat.backend.repository.DatabaseAuthRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.backend.utils.FormResponse;
|
||||||
|
import dev.peerat.backend.utils.Mail;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Injection;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.Router;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class MailConfirmation extends FormResponse{
|
||||||
|
|
||||||
|
private DatabaseAuthRepository databaseRepo;
|
||||||
|
private Router<PeerAtUser> router;
|
||||||
|
private String usersFilesPath;
|
||||||
|
private KeyPairGenerator generator;
|
||||||
|
private Encoder encoder;
|
||||||
|
private String gitToken;
|
||||||
|
private Map<String, String> playersWaiting;
|
||||||
|
private Mail mail;
|
||||||
|
private List<Random> randoms;
|
||||||
|
|
||||||
|
public MailConfirmation(
|
||||||
|
DatabaseRepository databaseRepo,
|
||||||
|
Router<PeerAtUser> router,
|
||||||
|
Configuration config,
|
||||||
|
@Injection("waitting") Map<String, String> playersWaiting,
|
||||||
|
Mail mail) throws NoSuchAlgorithmException{
|
||||||
|
|
||||||
|
this.databaseRepo = databaseRepo.getAuthRepository();
|
||||||
|
this.router = router;
|
||||||
|
this.usersFilesPath = config.getUsersFiles();
|
||||||
|
this.gitToken = config.getGitToken();
|
||||||
|
this.playersWaiting = playersWaiting;
|
||||||
|
this.mail = mail;
|
||||||
|
|
||||||
|
generator = KeyPairGenerator.getInstance("RSA");
|
||||||
|
generator.initialize(4096);
|
||||||
|
encoder = Base64.getEncoder();
|
||||||
|
|
||||||
|
this.randoms = new ArrayList<>();
|
||||||
|
|
||||||
|
Random random = new Random();
|
||||||
|
int randoms = random.nextInt(10)+3;
|
||||||
|
for(int i = 0; i < randoms; i++) this.randoms.add(new SecureRandom());
|
||||||
|
|
||||||
|
try {
|
||||||
|
Constructor<?> constructor = UUID.class.getDeclaredConstructor(byte[].class);
|
||||||
|
constructor.setAccessible(true);
|
||||||
|
this.uuidBuilder = constructor;
|
||||||
|
} catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
validator("pseudo", "[a-zA-Z0-9&|!?{}\\[\\]%/*\\-+=:;,_#@ ]{3,100}");
|
||||||
|
validator("firstname", "^(?>[A-Za-zÀ-ÖØ-öø-ÿ]+ ?)+$");
|
||||||
|
validator("lastname", "^(?>[A-Za-zÀ-ÖØ-öø-ÿ]+ ?)+$");
|
||||||
|
}
|
||||||
|
|
||||||
|
@RouteDoc(path = "/confirmation", responseCode = 200, responseDescription = "L'utilisateur est inscrit")
|
||||||
|
@RouteDoc(responseCode = 403, responseDescription = "L'utilisateur est connecté")
|
||||||
|
@RouteDoc(responseCode = 400, responseDescription = "Aucune données fournie / données invalide")
|
||||||
|
|
||||||
|
@Route(path = "^\\/confirmation$", type = POST)
|
||||||
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
|
if(context.isLogged()){
|
||||||
|
context.response(403);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
JsonMap json = json(reader);
|
||||||
|
if((!areValids("email","pseudo","firstname","lastname","passwd")) || (!hasFields("code"))){
|
||||||
|
context.response(400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String email = json.get("email");
|
||||||
|
String code = json.<String>get("code");
|
||||||
|
String pseudo = json.get("pseudo");
|
||||||
|
String firstname = json.get("firstname");
|
||||||
|
String lastname = json.get("lastname");
|
||||||
|
String password = json.get("passwd");
|
||||||
|
|
||||||
|
String checkCode = playersWaiting.get(email);
|
||||||
|
if(checkCode == null){
|
||||||
|
context.response(400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean pseudoAvailable = databaseRepo.checkPseudoAvailability(pseudo);
|
||||||
|
boolean emailAvailable = databaseRepo.checkEmailAvailability(email);
|
||||||
|
if(pseudoAvailable && emailAvailable){
|
||||||
|
if(code.equals(checkCode)){
|
||||||
|
playersWaiting.remove(email);
|
||||||
|
int id = databaseRepo.register(pseudo, email, password, firstname, lastname, "", "", "");
|
||||||
|
if(id >= 0){
|
||||||
|
// createFolderToSaveSourceCode(pseudo);
|
||||||
|
// generateGitKey(email, pseudo, password);
|
||||||
|
context.response(200,
|
||||||
|
"Access-Control-Expose-Headers: Authorization",
|
||||||
|
"Authorization: Bearer " + this.router.createAuthUser(new PeerAtUser(id)));
|
||||||
|
}else{
|
||||||
|
context.response(400);
|
||||||
|
JsonMap error = new JsonMap();
|
||||||
|
error.set("username_valid", pseudo);
|
||||||
|
error.set("email_valid", email);
|
||||||
|
writer.write(error.toString());
|
||||||
|
String ncode = codeGenerator();
|
||||||
|
playersWaiting.put(email, ncode);
|
||||||
|
mail.send(email, "Welcome @ Peer @ Code", "Your check code is "+ncode+" !");
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
context.response(400);
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
context.response(400);
|
||||||
|
JsonMap error = new JsonMap();
|
||||||
|
error.set("username_valid", pseudo);
|
||||||
|
error.set("email_valid", email);
|
||||||
|
writer.write(error.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Constructor<?> uuidBuilder;
|
||||||
|
private int[] start = {4, 9, 14, 19};
|
||||||
|
|
||||||
|
private String codeGenerator() throws Exception{
|
||||||
|
Random random = new Random();
|
||||||
|
Random target = this.randoms.get(random.nextInt(this.randoms.size()));
|
||||||
|
byte[] arrayOfByte = new byte[16];
|
||||||
|
target.nextBytes(arrayOfByte);
|
||||||
|
arrayOfByte[6] = (byte)(arrayOfByte[6] & 0xF);
|
||||||
|
arrayOfByte[6] = (byte)(arrayOfByte[6] | 0x40);
|
||||||
|
arrayOfByte[8] = (byte)(arrayOfByte[8] & 0x3F);
|
||||||
|
arrayOfByte[8] = (byte)(arrayOfByte[8] | 0x80);
|
||||||
|
String uuid = uuidBuilder.newInstance(arrayOfByte).toString();
|
||||||
|
target = this.randoms.get(random.nextInt(this.randoms.size()));
|
||||||
|
|
||||||
|
int start = this.start[target.nextInt(this.start.length)];
|
||||||
|
return uuid.substring(start, start+9);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createFolderToSaveSourceCode(String pseudo) throws IOException {
|
||||||
|
|
||||||
|
Files.createDirectories(Paths.get(String.format("%s/%s", usersFilesPath, pseudo)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static byte[] PREFIX = new byte[] {0,0,0,7,(byte)'s',(byte)'s',(byte)'h',(byte)'-',(byte)'r',(byte)'s',(byte)'a'};
|
||||||
|
|
||||||
|
private String generateGitKey(String email, String pseudo, String password) throws Exception{
|
||||||
|
KeyPair pair = generator.generateKeyPair(); //doit être unique ???
|
||||||
|
|
||||||
|
JSONObject createUser = new JSONObject();
|
||||||
|
createUser.put("email", email);
|
||||||
|
createUser.put("username", pseudo);
|
||||||
|
createUser.put("password", password);
|
||||||
|
post("https://git-users.peerat.dev/api/v1/admin/users/", createUser);
|
||||||
|
|
||||||
|
JSONObject sendKey = new JSONObject();
|
||||||
|
RSAPublicKey pub = (RSAPublicKey) pair.getPublic();
|
||||||
|
byte[] exponent = pub.getPublicExponent().toByteArray();
|
||||||
|
byte[] modulus = pub.getModulus().toByteArray();
|
||||||
|
byte[] key = new byte[19+exponent.length+modulus.length];
|
||||||
|
System.arraycopy(PREFIX, 0, key, 0, 11);
|
||||||
|
byte[] exLength = ByteBuffer.allocate(4).putInt(exponent.length).array();
|
||||||
|
byte[] modLength = ByteBuffer.allocate(4).putInt(modulus.length).array();
|
||||||
|
System.arraycopy(exLength, 0, key, 11, 4);
|
||||||
|
System.arraycopy(exponent, 0, key, 15, exponent.length);
|
||||||
|
System.arraycopy(modLength, 0, key, 15+exponent.length, 4);
|
||||||
|
System.arraycopy(modulus, 0, key, 19+exponent.length, modulus.length);
|
||||||
|
|
||||||
|
sendKey.put("key", "ssh-rsa "+new String(encoder.encode(key)));
|
||||||
|
sendKey.put("read_only", false);
|
||||||
|
sendKey.put("title", "peer_at_code_auto_push_key_"+pseudo);
|
||||||
|
post("https://git-users.peerat.dev/api/v1/admin/users/"+pseudo+"/keys", sendKey);
|
||||||
|
|
||||||
|
return new String(encoder.encode(pair.getPrivate().getEncoded()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void post(String url, JSONAware json) throws Exception{
|
||||||
|
HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
|
||||||
|
connection.setRequestMethod("POST");
|
||||||
|
connection.setRequestProperty("Content-Type","application/json");
|
||||||
|
connection.setRequestProperty("Authorization","Bearer "+this.gitToken);
|
||||||
|
connection.setDoInput(true);
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
|
||||||
|
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
|
||||||
|
writer.write(json.toJSONString());
|
||||||
|
writer.flush();
|
||||||
|
writer.close();
|
||||||
|
|
||||||
|
int response = connection.getResponseCode();
|
||||||
|
if(response != 201) throw new Exception("Call to "+url+" failed with response code "+response);
|
||||||
|
}
|
||||||
|
}
|
64
src/dev/peerat/backend/routes/users/ProfileSettings.java
Normal file
64
src/dev/peerat/backend/routes/users/ProfileSettings.java
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
package dev.peerat.backend.routes.users;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||||
|
import dev.peerat.backend.model.PeerAtUser;
|
||||||
|
import dev.peerat.backend.model.Player;
|
||||||
|
import dev.peerat.backend.repository.DatabasePlayerRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.backend.utils.FormResponse;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.RequestType;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class ProfileSettings extends FormResponse{
|
||||||
|
|
||||||
|
private DatabasePlayerRepository repo;
|
||||||
|
|
||||||
|
public ProfileSettings(DatabaseRepository repo){
|
||||||
|
this.repo = repo.getPlayerRepository();
|
||||||
|
|
||||||
|
validator("pseudo", "[a-zA-Z0-9&|!?{}\\[\\]%/*\\-+=:;,_#@ ]{3,100}");
|
||||||
|
}
|
||||||
|
|
||||||
|
@RouteDoc(path = "/user/settings", responseCode = 200, responseDescription = "L'utilisateur a mis à jours sont profile")
|
||||||
|
@RouteDoc(responseCode = 400, responseDescription = "L'utilisateur a envoyer une donnée unique, déjà utilisée")
|
||||||
|
|
||||||
|
@Route(path = "^/user/settings$", type = RequestType.POST, needLogin = true)
|
||||||
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception {
|
||||||
|
JsonMap json = json(reader);
|
||||||
|
|
||||||
|
String pseudo = json.get("pseudo");
|
||||||
|
String email = json.get("email");
|
||||||
|
String firstname = json.get("firstname");
|
||||||
|
String lastname = json.get("lastname");
|
||||||
|
Player player = repo.getPlayer(context.<PeerAtUser>getUser().getId());
|
||||||
|
|
||||||
|
if(!areValids("pseudo", "firstname", "lastname")){
|
||||||
|
context.response(400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!player.getPseudo().equals(pseudo)){
|
||||||
|
if(!repo.updatePseudo(context.<PeerAtUser>getUser().getId(), player, pseudo)){
|
||||||
|
context.response(400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
player.setPseudo(pseudo);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!player.getEmail().equals(email)){
|
||||||
|
}
|
||||||
|
|
||||||
|
if((!player.getFirstname().equals(firstname)) || (!player.getLastname().equals(lastname))){
|
||||||
|
repo.updateProfile(context.<PeerAtUser>getUser().getId(), player, lastname, firstname);
|
||||||
|
}
|
||||||
|
|
||||||
|
context.response(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
106
src/dev/peerat/backend/routes/users/Register.java
Normal file
106
src/dev/peerat/backend/routes/users/Register.java
Normal file
|
@ -0,0 +1,106 @@
|
||||||
|
package dev.peerat.backend.routes.users;
|
||||||
|
|
||||||
|
import static dev.peerat.framework.RequestType.POST;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
|
import dev.peerat.backend.Configuration;
|
||||||
|
import dev.peerat.backend.bonus.extract.RouteDoc;
|
||||||
|
import dev.peerat.backend.repository.DatabaseAuthRepository;
|
||||||
|
import dev.peerat.backend.repository.DatabaseRepository;
|
||||||
|
import dev.peerat.backend.utils.FormResponse;
|
||||||
|
import dev.peerat.backend.utils.Mail;
|
||||||
|
import dev.peerat.framework.Context;
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.HttpWriter;
|
||||||
|
import dev.peerat.framework.Injection;
|
||||||
|
import dev.peerat.framework.Route;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public class Register extends FormResponse{
|
||||||
|
|
||||||
|
private DatabaseAuthRepository databaseRepo;
|
||||||
|
private Map<String, String> playersWaiting;
|
||||||
|
private Mail mail;
|
||||||
|
private String host;
|
||||||
|
private List<Random> randoms;
|
||||||
|
|
||||||
|
public Register(DatabaseRepository databaseRepo, @Injection("waitting") Map<String, String> playersWaiting, Mail mail, Configuration config){
|
||||||
|
this.databaseRepo = databaseRepo.getAuthRepository();
|
||||||
|
this.playersWaiting = playersWaiting;
|
||||||
|
this.mail = mail;
|
||||||
|
this.host = config.getTokenIssuer();
|
||||||
|
this.randoms = new ArrayList<>();
|
||||||
|
|
||||||
|
Random random = new Random();
|
||||||
|
int randoms = random.nextInt(10)+3;
|
||||||
|
for(int i = 0; i < randoms; i++) this.randoms.add(new SecureRandom());
|
||||||
|
|
||||||
|
try {
|
||||||
|
Constructor<?> constructor = UUID.class.getDeclaredConstructor(byte[].class);
|
||||||
|
constructor.setAccessible(true);
|
||||||
|
this.uuidBuilder = constructor;
|
||||||
|
} catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@RouteDoc(path = "/register", responseCode = 200, responseDescription = "L'utilisateur est inscrit")
|
||||||
|
@RouteDoc(responseCode = 403, responseDescription = "L'utilisateur est connecté")
|
||||||
|
@RouteDoc(responseCode = 400, responseDescription = "Aucune données fournie / données invalide")
|
||||||
|
|
||||||
|
@Route(path = "^\\/register$", type = POST)
|
||||||
|
public void exec(Matcher matcher, Context context, HttpReader reader, HttpWriter writer) throws Exception{
|
||||||
|
if (context.isLogged()){
|
||||||
|
context.response(403);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
JsonMap json = json(reader);
|
||||||
|
if(!areValids("email")){
|
||||||
|
context.response(400);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String email = json.get("email");
|
||||||
|
|
||||||
|
boolean emailAvailable = databaseRepo.checkEmailAvailability(email);
|
||||||
|
if(emailAvailable){
|
||||||
|
String code = codeGenerator();
|
||||||
|
playersWaiting.put(email, code);
|
||||||
|
mail.send(email, "Welcome @ Peer @ Code", "Your check code is "+code+" !");
|
||||||
|
context.response(200);
|
||||||
|
}else{
|
||||||
|
context.response(400);
|
||||||
|
JsonMap error = new JsonMap();
|
||||||
|
error.set("email_valid", emailAvailable);
|
||||||
|
writer.write(error.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Constructor<?> uuidBuilder;
|
||||||
|
private int[] start = {4, 9, 14, 19};
|
||||||
|
|
||||||
|
private String codeGenerator() throws Exception{
|
||||||
|
Random random = new Random();
|
||||||
|
Random target = this.randoms.get(random.nextInt(this.randoms.size()));
|
||||||
|
byte[] arrayOfByte = new byte[16];
|
||||||
|
target.nextBytes(arrayOfByte);
|
||||||
|
arrayOfByte[6] = (byte)(arrayOfByte[6] & 0xF);
|
||||||
|
arrayOfByte[6] = (byte)(arrayOfByte[6] | 0x40);
|
||||||
|
arrayOfByte[8] = (byte)(arrayOfByte[8] & 0x3F);
|
||||||
|
arrayOfByte[8] = (byte)(arrayOfByte[8] | 0x80);
|
||||||
|
String uuid = uuidBuilder.newInstance(arrayOfByte).toString();
|
||||||
|
target = this.randoms.get(random.nextInt(this.randoms.size()));
|
||||||
|
|
||||||
|
int start = this.start[target.nextInt(this.start.length)];
|
||||||
|
return uuid.substring(start, start+9);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
49
src/dev/peerat/backend/utils/FormResponse.java
Normal file
49
src/dev/peerat/backend/utils/FormResponse.java
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
package dev.peerat.backend.utils;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import dev.peerat.framework.HttpReader;
|
||||||
|
import dev.peerat.framework.Response;
|
||||||
|
import dev.peerat.framework.utils.json.Json;
|
||||||
|
import dev.peerat.framework.utils.json.JsonMap;
|
||||||
|
|
||||||
|
public abstract class FormResponse implements Response{
|
||||||
|
|
||||||
|
private Json json;
|
||||||
|
private Map<String, Pattern> checker;
|
||||||
|
|
||||||
|
public FormResponse(){
|
||||||
|
this.checker = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validator(String key, String regex){
|
||||||
|
this.checker.put(key, Pattern.compile(regex, Pattern.CASE_INSENSITIVE));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T extends Json> T json(HttpReader reader) throws Exception{
|
||||||
|
return (T) (this.json = reader.readJson());
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasFields(String... fields){
|
||||||
|
JsonMap map = (JsonMap)json;
|
||||||
|
for(String field : fields){
|
||||||
|
if(!map.has(field)) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean areValids(String... fields){
|
||||||
|
JsonMap map = (JsonMap)json;
|
||||||
|
for(String field : fields){
|
||||||
|
String value = (String) map.get(field);
|
||||||
|
if(value == null) return false;
|
||||||
|
if(value.isEmpty()) return false;
|
||||||
|
Pattern pattern = checker.get(field);
|
||||||
|
if(pattern == null) continue;
|
||||||
|
if(!pattern.matcher(value).matches()) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
57
src/dev/peerat/backend/utils/Mail.java
Normal file
57
src/dev/peerat/backend/utils/Mail.java
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
package dev.peerat.backend.utils;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import jakarta.mail.Authenticator;
|
||||||
|
import jakarta.mail.Message;
|
||||||
|
import jakarta.mail.PasswordAuthentication;
|
||||||
|
import jakarta.mail.Session;
|
||||||
|
import jakarta.mail.Transport;
|
||||||
|
import jakarta.mail.internet.InternetAddress;
|
||||||
|
import jakarta.mail.internet.MimeMessage;
|
||||||
|
|
||||||
|
public class Mail {
|
||||||
|
private Session session;
|
||||||
|
private String fromAddress;
|
||||||
|
|
||||||
|
public Mail(String initUsername, String initPassword, String initSmtpHost, int initSmtpPort, String initFromAddress) {
|
||||||
|
System.out.println("login on "+initUsername+" into "+initSmtpHost+":"+initSmtpPort);
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.put("mail.smtp.host", initSmtpHost);
|
||||||
|
props.put("mail.smtp.port", initSmtpPort);
|
||||||
|
props.put("mail.smtp.auth", "true");
|
||||||
|
props.put("mail.smtp.starttls.enable", "true");
|
||||||
|
|
||||||
|
Authenticator auth = new Authenticator() {
|
||||||
|
protected PasswordAuthentication getPasswordAuthentication() {
|
||||||
|
return new PasswordAuthentication(initUsername, initPassword);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
session = Session.getInstance(props, auth);
|
||||||
|
fromAddress = initFromAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void send(String toAddress, String subject, String text){
|
||||||
|
try
|
||||||
|
{
|
||||||
|
MimeMessage msg = new MimeMessage(session);
|
||||||
|
//set message headers
|
||||||
|
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
|
||||||
|
msg.addHeader("format", "flowed");
|
||||||
|
msg.addHeader("Content-Transfer-Encoding", "8bit");
|
||||||
|
|
||||||
|
msg.setFrom(new InternetAddress(fromAddress, "Peer-at Code"));
|
||||||
|
msg.setReplyTo(InternetAddress.parse(fromAddress, false));
|
||||||
|
msg.setSubject(subject, "UTF-8");
|
||||||
|
msg.setText(text, "UTF-8");
|
||||||
|
msg.setSentDate(new Date());
|
||||||
|
|
||||||
|
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress, false));
|
||||||
|
Transport.send(msg);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Reference in a new issue