Compare commits

..

3 commits

Author SHA1 Message Date
27a15a7ff0 Admin route for loggin 2023-09-07 20:17:42 +02:00
03e64abf9c Remove verif hour from front 2023-09-07 18:54:17 +02:00
4a499e3540 to switch branch 2023-09-06 22:56:14 +02:00
7 changed files with 89 additions and 36 deletions

View file

@ -30,6 +30,7 @@ import be.jeffcheasey88.peeratcode.routes.PlayerDetails;
import be.jeffcheasey88.peeratcode.routes.PuzzleElement; import be.jeffcheasey88.peeratcode.routes.PuzzleElement;
import be.jeffcheasey88.peeratcode.routes.PuzzleResponse; import be.jeffcheasey88.peeratcode.routes.PuzzleResponse;
import be.jeffcheasey88.peeratcode.routes.Result; import be.jeffcheasey88.peeratcode.routes.Result;
import be.jeffcheasey88.peeratcode.routes.admins.DynamicLogs;
import be.jeffcheasey88.peeratcode.routes.groups.GroupCreate; import be.jeffcheasey88.peeratcode.routes.groups.GroupCreate;
import be.jeffcheasey88.peeratcode.routes.groups.GroupJoin; import be.jeffcheasey88.peeratcode.routes.groups.GroupJoin;
import be.jeffcheasey88.peeratcode.routes.groups.GroupList; import be.jeffcheasey88.peeratcode.routes.groups.GroupList;
@ -53,8 +54,6 @@ public class Main{
router.setDefault((matcher, user, reader, writer) -> { router.setDefault((matcher, user, reader, writer) -> {
writer.response(404, "Access-Control-Allow-Origin: *"); writer.response(404, "Access-Control-Allow-Origin: *");
writer.write("404 not Found.\n"); writer.write("404 not Found.\n");
writer.flush();
writer.close();
}); });
router.register(new Response(){ router.register(new Response(){
@ -82,6 +81,8 @@ public class Main{
router.register(new ChangePassword(repo)); router.register(new ChangePassword(repo));
router.register(new ForgotPassword()); router.register(new ForgotPassword());
router.register(new DynamicLogs(repo, new Locker<>())); //to change
router.register(new ChapterElement(repo)); router.register(new ChapterElement(repo));
router.register(new ChapterList(repo)); router.register(new ChapterList(repo));
router.register(new PuzzleElement(repo)); router.register(new PuzzleElement(repo));
@ -91,14 +92,12 @@ public class Main{
router.register(new BadgeDetails(repo)); router.register(new BadgeDetails(repo));
Locker<Group> groupLock = new Locker<>(); Locker<Group> groupLock = new Locker<>();
router.register(new GroupCreate(repo, groupLock, config.getGroupJoinMinutes())); Locker<Completion> leaderboard = new Locker<>();
DynamicLeaderboard dlb = new DynamicLeaderboard(repo);
router.register(dlb);
Locker<Completion> leaderboard = dlb.getLocker();
router.register(new DynamicLeaderboard(repo, leaderboard));
router.register(new PuzzleResponse(repo, config.getUsersFiles(), leaderboard)); router.register(new PuzzleResponse(repo, config.getUsersFiles(), leaderboard));
router.register(new GroupCreate(repo, groupLock, config.getGroupJoinMinutes()));
router.register(new GroupList(repo)); router.register(new GroupList(repo));
router.register(new GroupJoin(repo, config.getGroupJoinMinutes(), config.getGroupQuitMinutes(), leaderboard)); router.register(new GroupJoin(repo, config.getGroupJoinMinutes(), config.getGroupQuitMinutes(), leaderboard));
router.register(new GroupQuit(repo, config.getGroupJoinMinutes(), leaderboard)); router.register(new GroupQuit(repo, config.getGroupJoinMinutes(), leaderboard));

View file

@ -1,6 +1,7 @@
package be.jeffcheasey88.peeratcode.model; package be.jeffcheasey88.peeratcode.model;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List; import java.util.List;
public class Chapter { public class Chapter {
@ -34,6 +35,14 @@ public class Chapter {
this.puzzles = puzzles; this.puzzles = puzzles;
} }
public boolean isInCurrentTime(){
LocalDateTime now = LocalDateTime.now();
boolean show = true;
if(startDate != null) show &= now.isAfter(startDate.toLocalDateTime());
if(endDate != null) show &= now.isBefore(endDate.toLocalDateTime());
return show;
}
public Timestamp getStartDate() { public Timestamp getStartDate() {
return startDate; return startDate;
} }

View file

@ -33,10 +33,10 @@ public class ChapterElement implements Response {
JSONObject chapterJSON = new JSONObject(); JSONObject chapterJSON = new JSONObject();
chapterJSON.put("id", chapter.getId()); chapterJSON.put("id", chapter.getId());
chapterJSON.put("name", chapter.getName()); chapterJSON.put("name", chapter.getName());
if (chapter.getStartDate() != null) boolean show = chapter.isInCurrentTime();
chapterJSON.put("startDate", chapter.getStartDate().toString()); chapterJSON.put("show", show);
if (chapter.getEndDate() != null)
chapterJSON.put("endDate", chapter.getEndDate().toString()); if(show){
JSONArray puzzles = new JSONArray(); JSONArray puzzles = new JSONArray();
for (Puzzle puzzle : chapter.getPuzzles()){ for (Puzzle puzzle : chapter.getPuzzles()){
JSONObject puzzleJSON = new JSONObject(); JSONObject puzzleJSON = new JSONObject();
@ -49,6 +49,8 @@ public class ChapterElement implements Response {
puzzles.add(puzzleJSON); puzzles.add(puzzleJSON);
} }
chapterJSON.put("puzzles", puzzles); chapterJSON.put("puzzles", puzzles);
}
writer.response(200, "Access-Control-Allow-Origin: *"); writer.response(200, "Access-Control-Allow-Origin: *");
writer.write(chapterJSON.toJSONString()); writer.write(chapterJSON.toJSONString());
} else { } else {

View file

@ -35,10 +35,7 @@ public class ChapterList implements Response {
JSONObject chapterJSON = new JSONObject(); JSONObject chapterJSON = new JSONObject();
chapterJSON.put("id", chapter.getId()); chapterJSON.put("id", chapter.getId());
chapterJSON.put("name", chapter.getName()); chapterJSON.put("name", chapter.getName());
if (chapter.getStartDate() != null) chapterJSON.put("show", chapter.isInCurrentTime());
chapterJSON.put("startDate", chapter.getStartDate().toString());
if (chapter.getEndDate() != null)
chapterJSON.put("endDate", chapter.getEndDate().toString());
chaptersJSON.add(chapterJSON); chaptersJSON.add(chapterJSON);
} }
writer.response(200, "Access-Control-Allow-Origin: *"); writer.response(200, "Access-Control-Allow-Origin: *");

View file

@ -16,13 +16,9 @@ public class DynamicLeaderboard extends Leaderboard{
private Locker<Completion> locker; private Locker<Completion> locker;
public DynamicLeaderboard(DatabaseRepository databaseRepo){ public DynamicLeaderboard(DatabaseRepository databaseRepo, Locker<Completion> locker){
super(databaseRepo); super(databaseRepo);
this.locker = new Locker<>(); this.locker = locker;
}
public Locker<Completion> getLocker(){
return this.locker;
} }
@RouteDoc(path = "/rleaderboard/{id}", responseCode = 101, responseDescription = "WebSocket") @RouteDoc(path = "/rleaderboard/{id}", responseCode = 101, responseDescription = "WebSocket")

View file

@ -0,0 +1,49 @@
package be.jeffcheasey88.peeratcode.routes.admins;
import java.util.regex.Matcher;
import org.json.simple.JSONObject;
import be.jeffcheasey88.peeratcode.framework.HttpReader;
import be.jeffcheasey88.peeratcode.framework.HttpWriter;
import be.jeffcheasey88.peeratcode.framework.Locker;
import be.jeffcheasey88.peeratcode.framework.Response;
import be.jeffcheasey88.peeratcode.framework.Route;
import be.jeffcheasey88.peeratcode.framework.User;
import be.jeffcheasey88.peeratcode.framework.Locker.Key;
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
public class DynamicLogs implements Response{
private Locker<Object> locker; //Context
private DatabaseRepository repo;
public DynamicLogs(DatabaseRepository repo, Locker<Object> locker){
this.repo = repo;
this.locker = locker;
}
@Route(path = "^/admin/logs$", needLogin = true, websocket = true)
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
//check if admin
Key key = new Key();
locker.init(key);
try {
while(!reader.isClosed()){
Object instance = locker.getValue(key);
JSONObject json = new JSONObject();
writer.write(json.toJSONString());
writer.flush();
locker.lock(key);
}
}catch(Exception e){
e.printStackTrace();
}
locker.remove(key);
}
}

View file

@ -17,6 +17,7 @@ public class ForgotPassword implements Response{
return; return;
} }
} }