From ec9021b8c9f7d32f4b80becd0fffaef8eed67505 Mon Sep 17 00:00:00 2001 From: jeffcheasey88 <66554203+jeffcheasey88@users.noreply.github.com> Date: Fri, 21 Apr 2023 10:58:34 +0200 Subject: [PATCH] Fix Groups operation + add time --- .../peeratcode/Configuration.java | 11 ++++++++ src/be/jeffcheasey88/peeratcode/Main.java | 6 ++--- .../peeratcode/repository/DatabaseQuery.java | 2 +- .../peeratcode/routes/groups/GroupCreate.java | 26 ++++++++++++++----- .../peeratcode/routes/groups/GroupJoin.java | 17 +++++++++++- .../peeratcode/routes/groups/GroupQuit.java | 20 ++++++++++++-- .../peeratcode/routes/TmpRoutesTests.java | 9 ++++--- 7 files changed, 74 insertions(+), 17 deletions(-) diff --git a/src/be/jeffcheasey88/peeratcode/Configuration.java b/src/be/jeffcheasey88/peeratcode/Configuration.java index 67e4816..d29ea5d 100644 --- a/src/be/jeffcheasey88/peeratcode/Configuration.java +++ b/src/be/jeffcheasey88/peeratcode/Configuration.java @@ -26,6 +26,9 @@ public class Configuration { private int token_expiration; private String token_discord; + + private int groupJoinMinutes; + private String groupQuitMinutes; private File _file; @@ -162,4 +165,12 @@ public class Configuration { public String getDiscordToken(){ return this.token_discord; } + + public int getGroupJoinMinutes(){ + return this.groupJoinMinutes; + } + + public String getGroupQuitMinutes(){ + return this.groupQuitMinutes; + } } \ No newline at end of file diff --git a/src/be/jeffcheasey88/peeratcode/Main.java b/src/be/jeffcheasey88/peeratcode/Main.java index 3a51dac..944722a 100644 --- a/src/be/jeffcheasey88/peeratcode/Main.java +++ b/src/be/jeffcheasey88/peeratcode/Main.java @@ -84,11 +84,11 @@ public class Main{ router.register(new BadgeDetails(router.getDataBase())); router.register(new GroupList(router.getDataBase())); - router.register(new GroupJoin(router.getDataBase())); - router.register(new GroupQuit(router.getDataBase())); + router.register(new GroupJoin(router.getDataBase(), config.getGroupJoinMinutes())); + router.register(new GroupQuit(router.getDataBase(), config.getGroupJoinMinutes())); Locker 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()); router.register(dlb); diff --git a/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java b/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java index 1297245..ac4f2c3 100644 --- a/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java +++ b/src/be/jeffcheasey88/peeratcode/repository/DatabaseQuery.java @@ -17,7 +17,7 @@ public enum DatabaseQuery { // 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 = ?)"), INSERT_GROUP("INSERT INTO groups (name, fk_chapter, fk_puzzle) VALUES (?,?,?)"), INSERT_PLAYER_IN_GROUP("INSERT INTO containsGroups (fk_player, fk_group) VALUES (?,?)"), diff --git a/src/be/jeffcheasey88/peeratcode/routes/groups/GroupCreate.java b/src/be/jeffcheasey88/peeratcode/routes/groups/GroupCreate.java index 8fc3e4b..4f5b2b3 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/groups/GroupCreate.java +++ b/src/be/jeffcheasey88/peeratcode/routes/groups/GroupCreate.java @@ -2,6 +2,7 @@ package be.jeffcheasey88.peeratcode.routes.groups; import static be.jeffcheasey88.peeratcode.framework.RequestType.POST; +import java.time.LocalDateTime; import java.util.regex.Matcher; 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.Route; 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.repository.DatabaseRepository; @@ -22,14 +23,17 @@ public class GroupCreate implements Response { private Locker locker; private DatabaseRepository repo; + private int groupDelay; - public GroupCreate(DatabaseRepository repo, Locker locker){ + public GroupCreate(DatabaseRepository repo, Locker 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{ @@ -37,11 +41,20 @@ public class GroupCreate implements Response { if (this.repo.getPlayerGroup(user.getId(), newGroup.getLinkToChapter(), newGroup.getLinkToPuzzle()) == null) { try { - this.repo.getGroupId(newGroup); + if(this.repo.getGroupId(newGroup) == null) throw new NullPointerException(); HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); return; - } catch (NullPointerException e) { - // if group not exist create it + }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(start.isBefore(LocalDateTime.now())){ + HttpUtil.responseHeaders(writer, 423, "Access-Control-Allow-Origin: *"); + return; + } + } + } if (this.repo.insertGroup(newGroup, user)) { 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: *"); } } - } - else { + }else { HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); } } diff --git a/src/be/jeffcheasey88/peeratcode/routes/groups/GroupJoin.java b/src/be/jeffcheasey88/peeratcode/routes/groups/GroupJoin.java index 02ea2a5..ef27997 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/groups/GroupJoin.java +++ b/src/be/jeffcheasey88/peeratcode/routes/groups/GroupJoin.java @@ -2,6 +2,7 @@ package be.jeffcheasey88.peeratcode.routes.groups; import static be.jeffcheasey88.peeratcode.framework.RequestType.POST; +import java.time.LocalDateTime; import java.util.regex.Matcher; 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.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 GroupJoin implements Response{ private DatabaseRepository repo; + private int groupDelay; - public GroupJoin(DatabaseRepository repo){ + public GroupJoin(DatabaseRepository repo, int groupDelay){ this.repo = repo; + this.groupDelay = groupDelay; } @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") @Route(path = "^\\/groupJoin$", type = POST, needLogin = true) public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{ @@ -37,6 +42,16 @@ public class GroupJoin implements Response{ 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)) { HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); } else { diff --git a/src/be/jeffcheasey88/peeratcode/routes/groups/GroupQuit.java b/src/be/jeffcheasey88/peeratcode/routes/groups/GroupQuit.java index b0a0664..51a7bbc 100644 --- a/src/be/jeffcheasey88/peeratcode/routes/groups/GroupQuit.java +++ b/src/be/jeffcheasey88/peeratcode/routes/groups/GroupQuit.java @@ -2,6 +2,7 @@ package be.jeffcheasey88.peeratcode.routes.groups; import static be.jeffcheasey88.peeratcode.framework.RequestType.POST; +import java.time.LocalDateTime; import java.util.regex.Matcher; import org.json.simple.JSONObject; @@ -13,20 +14,24 @@ 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.repository.DatabaseRepository; public class GroupQuit implements Response{ private DatabaseRepository repo; + private int groupDelay; - public GroupQuit(DatabaseRepository repo){ + public GroupQuit(DatabaseRepository repo, int groupDelay){ this.repo = repo; + this.groupDelay = groupDelay; } @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((JSONObject) HttpUtil.readJson(reader)); @@ -37,6 +42,17 @@ public class GroupQuit implements Response{ 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)) { HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *"); } else { diff --git a/test/be/jeffcheasey88/peeratcode/routes/TmpRoutesTests.java b/test/be/jeffcheasey88/peeratcode/routes/TmpRoutesTests.java index 7275f10..5aec156 100644 --- a/test/be/jeffcheasey88/peeratcode/routes/TmpRoutesTests.java +++ b/test/be/jeffcheasey88/peeratcode/routes/TmpRoutesTests.java @@ -2,6 +2,7 @@ package be.jeffcheasey88.peeratcode.routes; import static org.junit.jupiter.api.Assertions.fail; +import org.json.simple.JSONObject; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -41,10 +42,12 @@ public class TmpRoutesTests { @Test void testOnDeployed(){ try { + JSONObject group = new JSONObject(); + group.put("name", "MyTest"); + group.put("chapter", 1); + client.auth("JeffCheasey88", "TheoPueDesPieds"); - client.route("/puzzleResponse/10", "POST", WebClient.buildMultiPartData("1")); - client.route("/puzzleResponse/10", "POST", WebClient.buildMultiPartData("1")); - client.route("/puzzleResponse/10", "POST", WebClient.buildMultiPartData("one")); + client.route("/groupCreate", "POST", group.toJSONString()); client.assertResponseCode(200); }catch(Exception e){