Fix Groups operation + add time

This commit is contained in:
jeffcheasey88 2023-04-21 10:58:34 +02:00
parent 0150fc3a23
commit ec9021b8c9
7 changed files with 74 additions and 17 deletions

View file

@ -27,6 +27,9 @@ public class Configuration {
private String token_discord; private String token_discord;
private int groupJoinMinutes;
private String groupQuitMinutes;
private File _file; private File _file;
public Configuration(String path) { public Configuration(String path) {
@ -162,4 +165,12 @@ public class Configuration {
public String getDiscordToken(){ public String getDiscordToken(){
return this.token_discord; return this.token_discord;
} }
public int getGroupJoinMinutes(){
return this.groupJoinMinutes;
}
public String getGroupQuitMinutes(){
return this.groupQuitMinutes;
}
} }

View file

@ -84,11 +84,11 @@ public class Main{
router.register(new BadgeDetails(router.getDataBase())); router.register(new BadgeDetails(router.getDataBase()));
router.register(new GroupList(router.getDataBase())); router.register(new GroupList(router.getDataBase()));
router.register(new GroupJoin(router.getDataBase())); router.register(new GroupJoin(router.getDataBase(), config.getGroupJoinMinutes()));
router.register(new GroupQuit(router.getDataBase())); router.register(new GroupQuit(router.getDataBase(), config.getGroupJoinMinutes()));
Locker<Group> groupLock = new Locker<>(); Locker<Group> groupLock = new Locker<>();
router.register(new GroupCreate(router.getDataBase(), groupLock)); router.register(new GroupCreate(router.getDataBase(), groupLock, config.getGroupJoinMinutes()));
DynamicLeaderboard dlb = new DynamicLeaderboard(router.getDataBase()); DynamicLeaderboard dlb = new DynamicLeaderboard(router.getDataBase());
router.register(dlb); router.register(dlb);

View file

@ -17,7 +17,7 @@ public enum DatabaseQuery {
// GROUPS // GROUPS
ALL_GROUPS("SELECT * FROM groups"), ALL_GROUPS("SELECT * FROM groups"),
GET_GROUP_FOR_PLAYER("SELECT g.* FROM groups g JOIN containsGroups cg ON cg.fk_group = c.id_group WHERE cg.fk_player = ? AND g.fk_chapter = ? AND g.fk_puzzle = ?"), 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_ID_BY_DATA("SELECT id_group FROM groups WHERE name = ? AND (fk_chapter = ? OR fk_puzzle = ?)"),
INSERT_GROUP("INSERT INTO groups (name, fk_chapter, fk_puzzle) VALUES (?,?,?)"), INSERT_GROUP("INSERT INTO groups (name, fk_chapter, fk_puzzle) VALUES (?,?,?)"),
INSERT_PLAYER_IN_GROUP("INSERT INTO containsGroups (fk_player, fk_group) VALUES (?,?)"), INSERT_PLAYER_IN_GROUP("INSERT INTO containsGroups (fk_player, fk_group) VALUES (?,?)"),

View file

@ -2,6 +2,7 @@ package be.jeffcheasey88.peeratcode.routes.groups;
import static be.jeffcheasey88.peeratcode.framework.RequestType.POST; import static be.jeffcheasey88.peeratcode.framework.RequestType.POST;
import java.time.LocalDateTime;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
@ -14,7 +15,7 @@ import be.jeffcheasey88.peeratcode.framework.Locker;
import be.jeffcheasey88.peeratcode.framework.Response; import be.jeffcheasey88.peeratcode.framework.Response;
import be.jeffcheasey88.peeratcode.framework.Route; import be.jeffcheasey88.peeratcode.framework.Route;
import be.jeffcheasey88.peeratcode.framework.User; import be.jeffcheasey88.peeratcode.framework.User;
import be.jeffcheasey88.peeratcode.model.Completion; import be.jeffcheasey88.peeratcode.model.Chapter;
import be.jeffcheasey88.peeratcode.model.Group; import be.jeffcheasey88.peeratcode.model.Group;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository; import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
@ -22,14 +23,17 @@ public class GroupCreate implements Response {
private Locker<Group> locker; private Locker<Group> locker;
private DatabaseRepository repo; private DatabaseRepository repo;
private int groupDelay;
public GroupCreate(DatabaseRepository repo, Locker<Group> locker){ public GroupCreate(DatabaseRepository repo, Locker<Group> locker, int groupDelay){
this.repo = repo; this.repo = repo;
this.locker = locker; this.locker = locker;
this.groupDelay = groupDelay;
} }
@RouteDoc(path = "/groupCreate", responseCode = 200, responseDescription = "Le groupe a été créé") @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 = 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) @Route(path = "^\\/groupCreate$", type = POST, needLogin = true)
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{ public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{
@ -37,11 +41,20 @@ public class GroupCreate implements Response {
if (this.repo.getPlayerGroup(user.getId(), newGroup.getLinkToChapter(), newGroup.getLinkToPuzzle()) == null) { if (this.repo.getPlayerGroup(user.getId(), newGroup.getLinkToChapter(), newGroup.getLinkToPuzzle()) == null) {
try { try {
this.repo.getGroupId(newGroup); if(this.repo.getGroupId(newGroup) == null) throw new NullPointerException();
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
return; return;
} catch (NullPointerException e) { }catch(NullPointerException e){
// if group not exist create it if(newGroup.getLinkToChapter() != null){
Chapter chapter = this.repo.getChapter(newGroup.getLinkToChapter());
if(chapter.getStartDate() != null){
LocalDateTime start = chapter.getStartDate().toLocalDateTime().plusMinutes(this.groupDelay);
if(start.isBefore(LocalDateTime.now())){
HttpUtil.responseHeaders(writer, 423, "Access-Control-Allow-Origin: *");
return;
}
}
}
if (this.repo.insertGroup(newGroup, user)) { if (this.repo.insertGroup(newGroup, user)) {
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
@ -50,8 +63,7 @@ public class GroupCreate implements Response {
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
} }
} }
} }else {
else {
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
} }
} }

View file

@ -2,6 +2,7 @@ package be.jeffcheasey88.peeratcode.routes.groups;
import static be.jeffcheasey88.peeratcode.framework.RequestType.POST; import static be.jeffcheasey88.peeratcode.framework.RequestType.POST;
import java.time.LocalDateTime;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
@ -13,19 +14,23 @@ import be.jeffcheasey88.peeratcode.framework.HttpWriter;
import be.jeffcheasey88.peeratcode.framework.Response; import be.jeffcheasey88.peeratcode.framework.Response;
import be.jeffcheasey88.peeratcode.framework.Route; import be.jeffcheasey88.peeratcode.framework.Route;
import be.jeffcheasey88.peeratcode.framework.User; import be.jeffcheasey88.peeratcode.framework.User;
import be.jeffcheasey88.peeratcode.model.Chapter;
import be.jeffcheasey88.peeratcode.model.Group; import be.jeffcheasey88.peeratcode.model.Group;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository; import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
public class GroupJoin implements Response{ public class GroupJoin implements Response{
private DatabaseRepository repo; private DatabaseRepository repo;
private int groupDelay;
public GroupJoin(DatabaseRepository repo){ public GroupJoin(DatabaseRepository repo, int groupDelay){
this.repo = repo; this.repo = repo;
this.groupDelay = groupDelay;
} }
@RouteDoc(path = "/groupJoin", responseCode = 200, responseDescription = "L'utilisateur a rejoint le groupe") @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 = 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")
@Route(path = "^\\/groupJoin$", type = POST, needLogin = true) @Route(path = "^\\/groupJoin$", type = POST, needLogin = true)
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{ public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{
@ -37,6 +42,16 @@ public class GroupJoin implements Response{
return; 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(start.isBefore(LocalDateTime.now())){
HttpUtil.responseHeaders(writer, 423, "Access-Control-Allow-Origin: *");
return;
}
}
}
if (this.repo.insertUserInGroup(group, user)) { if (this.repo.insertUserInGroup(group, user)) {
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
} else { } else {

View file

@ -2,6 +2,7 @@ package be.jeffcheasey88.peeratcode.routes.groups;
import static be.jeffcheasey88.peeratcode.framework.RequestType.POST; import static be.jeffcheasey88.peeratcode.framework.RequestType.POST;
import java.time.LocalDateTime;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
@ -13,19 +14,23 @@ import be.jeffcheasey88.peeratcode.framework.HttpWriter;
import be.jeffcheasey88.peeratcode.framework.Response; import be.jeffcheasey88.peeratcode.framework.Response;
import be.jeffcheasey88.peeratcode.framework.Route; import be.jeffcheasey88.peeratcode.framework.Route;
import be.jeffcheasey88.peeratcode.framework.User; import be.jeffcheasey88.peeratcode.framework.User;
import be.jeffcheasey88.peeratcode.model.Chapter;
import be.jeffcheasey88.peeratcode.model.Group; import be.jeffcheasey88.peeratcode.model.Group;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository; import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
public class GroupQuit implements Response{ public class GroupQuit implements Response{
private DatabaseRepository repo; private DatabaseRepository repo;
private int groupDelay;
public GroupQuit(DatabaseRepository repo){ public GroupQuit(DatabaseRepository repo, int groupDelay){
this.repo = repo; this.repo = repo;
this.groupDelay = groupDelay;
} }
@RouteDoc(path = "/groupQuit", responseCode = 200, responseDescription = "L'utilisateur à quitter le groupe") @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 = 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) @Route(path = "^\\/groupQuit$", type = POST, needLogin = true)
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{ public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{
@ -37,6 +42,17 @@ public class GroupQuit implements Response{
return; 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(start.isBefore(LocalDateTime.now())){
HttpUtil.responseHeaders(writer, 423, "Access-Control-Allow-Origin: *");
return;
}
}
}
if (this.repo.leaveGroup(group, user)) { if (this.repo.leaveGroup(group, user)) {
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
} else { } else {

View file

@ -2,6 +2,7 @@ package be.jeffcheasey88.peeratcode.routes;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
import org.json.simple.JSONObject;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -41,10 +42,12 @@ public class TmpRoutesTests {
@Test @Test
void testOnDeployed(){ void testOnDeployed(){
try { try {
JSONObject group = new JSONObject();
group.put("name", "MyTest");
group.put("chapter", 1);
client.auth("JeffCheasey88", "TheoPueDesPieds"); client.auth("JeffCheasey88", "TheoPueDesPieds");
client.route("/puzzleResponse/10", "POST", WebClient.buildMultiPartData("1")); client.route("/groupCreate", "POST", group.toJSONString());
client.route("/puzzleResponse/10", "POST", WebClient.buildMultiPartData("1"));
client.route("/puzzleResponse/10", "POST", WebClient.buildMultiPartData("one"));
client.assertResponseCode(200); client.assertResponseCode(200);
}catch(Exception e){ }catch(Exception e){