Fix group dates & Answer Puzzle after event finished

This commit is contained in:
jeffcheasey88 2023-04-21 23:02:50 +02:00
parent ec9021b8c9
commit 209aa5b6ce
7 changed files with 50 additions and 5 deletions

View file

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

View file

@ -13,6 +13,10 @@ public enum DatabaseQuery {
// 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

View file

@ -396,6 +396,24 @@ public class DatabaseRepository {
}
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

View file

@ -6,6 +6,7 @@ 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.List;
import java.util.regex.Matcher;
@ -19,7 +20,9 @@ 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;
@ -40,6 +43,7 @@ public class PuzzleResponse implements Response {
@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 après la fin 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{
@ -54,7 +58,16 @@ public class PuzzleResponse implements Response {
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
return;
}
Puzzle currentPuzzle = databaseRepo.getPuzzle(received.getPuzzleId());
Chapter chapter = this.databaseRepo.getChapter(currentPuzzle);
if(chapter.getEndDate() != null){
if(LocalDateTime.now().isAfter(chapter.getEndDate().toLocalDateTime())){
HttpUtil.responseHeaders(writer, 423, "Access-Control-Allow-Origin: *");
return;
}
}
Completion completion = databaseRepo.insertOrUpdatePuzzleResponse(received.getPuzzleId(), user.getId(),
received.getFileName(), received.getSourceCode(), received.getResponse(), currentPuzzle);
if(completion == null){

View file

@ -49,7 +49,7 @@ public class GroupCreate implements Response {
Chapter chapter = this.repo.getChapter(newGroup.getLinkToChapter());
if(chapter.getStartDate() != null){
LocalDateTime start = chapter.getStartDate().toLocalDateTime().plusMinutes(this.groupDelay);
if(start.isBefore(LocalDateTime.now())){
if(start.isAfter(LocalDateTime.now())){
HttpUtil.responseHeaders(writer, 423, "Access-Control-Allow-Origin: *");
return;
}

View file

@ -22,15 +22,18 @@ public class GroupJoin implements Response{
private DatabaseRepository repo;
private int groupDelay;
private String waitTime;
public GroupJoin(DatabaseRepository repo, int groupDelay){
public GroupJoin(DatabaseRepository repo, int groupDelay, String waitTime){
this.repo = repo;
this.groupDelay = groupDelay;
this.waitTime = waitTime;
}
@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{
@ -42,16 +45,23 @@ public class GroupJoin implements Response{
return;
}
if(group.getLinkToChapter() == null && group.getLinkToPuzzle() == null){
HttpUtil.responseHeaders(writer, 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(start.isBefore(LocalDateTime.now())){
if(start.isAfter(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 {

View file

@ -46,7 +46,7 @@ public class GroupQuit implements Response{
Chapter chapter = this.repo.getChapter(group.getLinkToChapter());
if(chapter.getStartDate() != null){
LocalDateTime start = chapter.getStartDate().toLocalDateTime().plusMinutes(this.groupDelay);
if(start.isBefore(LocalDateTime.now())){
if(start.isAfter(LocalDateTime.now())){
HttpUtil.responseHeaders(writer, 423, "Access-Control-Allow-Origin: *");
return;
}