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.PuzzleResponse;
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.GroupJoin;
import be.jeffcheasey88.peeratcode.routes.groups.GroupList;
@ -53,8 +54,6 @@ public class Main{
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(){
@ -82,6 +81,8 @@ public class Main{
router.register(new ChangePassword(repo));
router.register(new ForgotPassword());
router.register(new DynamicLogs(repo, new Locker<>())); //to change
router.register(new ChapterElement(repo));
router.register(new ChapterList(repo));
router.register(new PuzzleElement(repo));
@ -91,14 +92,12 @@ public class Main{
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();
Locker<Completion> leaderboard = new Locker<>();
router.register(new DynamicLeaderboard(repo, 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 GroupJoin(repo, config.getGroupJoinMinutes(), config.getGroupQuitMinutes(), leaderboard));
router.register(new GroupQuit(repo, config.getGroupJoinMinutes(), leaderboard));

View file

@ -1,6 +1,7 @@
package be.jeffcheasey88.peeratcode.model;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
public class Chapter {
@ -34,6 +35,14 @@ public class Chapter {
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() {
return startDate;
}

View file

@ -33,22 +33,24 @@ public class ChapterElement implements Response {
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);
boolean show = chapter.isInCurrentTime();
chapterJSON.put("show", show);
if(show){
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);
}
chapterJSON.put("puzzles", puzzles);
writer.response(200, "Access-Control-Allow-Origin: *");
writer.write(chapterJSON.toJSONString());
} else {

View file

@ -27,23 +27,20 @@ public class ChapterList implements Response {
@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 {
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{
List<Chapter> allChapters = databaseRepo.getAllChapters();
if (allChapters != null) {
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());
chapterJSON.put("show", chapter.isInCurrentTime());
chaptersJSON.add(chapterJSON);
}
writer.response(200, "Access-Control-Allow-Origin: *");
writer.write(chaptersJSON.toJSONString());
} else {
}else{
writer.response(400, "Access-Control-Allow-Origin: *");
}
}

View file

@ -16,13 +16,9 @@ public class DynamicLeaderboard extends Leaderboard{
private Locker<Completion> locker;
public DynamicLeaderboard(DatabaseRepository databaseRepo){
public DynamicLeaderboard(DatabaseRepository databaseRepo, Locker<Completion> locker){
super(databaseRepo);
this.locker = new Locker<>();
}
public Locker<Completion> getLocker(){
return this.locker;
this.locker = locker;
}
@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;
}
}