Little edit

This commit is contained in:
jeffcheasey88 2023-04-09 23:03:08 +02:00
parent fa6f0774a1
commit eac490f040
3 changed files with 21 additions and 27 deletions

View file

@ -34,8 +34,8 @@ import be.jeffcheasey88.peeratcode.routes.groups.GroupJoin;
import be.jeffcheasey88.peeratcode.routes.groups.GroupList; import be.jeffcheasey88.peeratcode.routes.groups.GroupList;
import be.jeffcheasey88.peeratcode.routes.groups.GroupQuit; import be.jeffcheasey88.peeratcode.routes.groups.GroupQuit;
public class Main { public class Main{
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception{
Configuration config = new Configuration("config.txt"); Configuration config = new Configuration("config.txt");
config.load(); config.load();

View file

@ -18,7 +18,7 @@ import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
public class Router{ public class Router{
private Map<Response, Route> responses; private Map<RequestType, Map<Response, Route>> responses;
private Map<Response, Pattern> patterns; private Map<Response, Pattern> patterns;
private Response noFileFound; private Response noFileFound;
private RsaJsonWebKey rsaJsonWebKey; private RsaJsonWebKey rsaJsonWebKey;
@ -31,6 +31,7 @@ public class Router{
this.token_issuer = token_issuer; this.token_issuer = token_issuer;
this.token_expiration = token_expiration; this.token_expiration = token_expiration;
this.responses = new HashMap<>(); this.responses = new HashMap<>();
for(RequestType type : RequestType.values()) this.responses.put(type, new HashMap<>());
this.patterns = new HashMap<>(); this.patterns = new HashMap<>();
this.rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048); this.rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048);
} }
@ -45,7 +46,7 @@ public class Router{
Response.class.getDeclaredMethods()[0].getParameterTypes()); Response.class.getDeclaredMethods()[0].getParameterTypes());
Route route = method.getAnnotation(Route.class); Route route = method.getAnnotation(Route.class);
this.responses.put(response, route); this.responses.get(route.type()).put(response, route);
this.patterns.put(response, Pattern.compile(route.path())); this.patterns.put(response, Pattern.compile(route.path()));
}catch(Exception e){ }catch(Exception e){
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
@ -58,8 +59,7 @@ public class Router{
public void exec(RequestType type, String path, User user, HttpReader reader, HttpWriter writer) throws Exception{ public void exec(RequestType type, String path, User user, HttpReader reader, HttpWriter writer) throws Exception{
if(type == null) return; if(type == null) return;
for(Entry<Response, Route> routes : this.responses.entrySet()){ for(Entry<Response, Route> routes : this.responses.get(type).entrySet()){
if(routes.getValue().type().equals(type)){
Matcher matcher = this.patterns.get(routes.getKey()).matcher(path); Matcher matcher = this.patterns.get(routes.getKey()).matcher(path);
if(matcher.matches()){ if(matcher.matches()){
if(user == null && routes.getValue().needLogin()) return; if(user == null && routes.getValue().needLogin()) return;
@ -67,7 +67,6 @@ public class Router{
return; return;
} }
} }
}
if(noFileFound != null) noFileFound.exec(null, user, reader, writer); if(noFileFound != null) noFileFound.exec(null, user, reader, writer);
} }

View file

@ -6,7 +6,6 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -33,12 +32,7 @@ public class PuzzleResponse implements Response {
} }
@Route(path = "^\\/puzzleResponse\\/([0-9]+)$", type = POST, needLogin = true) @Route(path = "^\\/puzzleResponse\\/([0-9]+)$", 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{
if (user == null) {
HttpUtil.responseHeaders(writer, 401, "Access-Control-Allow-Origin: *");
return;
}
ReceivedResponse received = new ReceivedResponse(matcher, reader); ReceivedResponse received = new ReceivedResponse(matcher, reader);
saveSourceCode(received, databaseRepo.getPlayer(user.getId())); saveSourceCode(received, databaseRepo.getPlayer(user.getId()));
@ -46,27 +40,28 @@ public class PuzzleResponse implements Response {
Puzzle currentPuzzle = databaseRepo.getPuzzle(received.getPuzzleId()); Puzzle currentPuzzle = databaseRepo.getPuzzle(received.getPuzzleId());
Completion completion = databaseRepo.insertOrUpdatePuzzleResponse(received.getPuzzleId(), user.getId(), Completion completion = databaseRepo.insertOrUpdatePuzzleResponse(received.getPuzzleId(), user.getId(),
received.getFileName(), received.getSourceCode(), received.getResponse(), currentPuzzle); received.getFileName(), received.getSourceCode(), received.getResponse(), currentPuzzle);
if (completion.getScore() > 0) { if(completion == null){
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
return;
}
if(completion.getScore() > 0){
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *", "Content-Type: application/json"); HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *", "Content-Type: application/json");
responseJSON.put("score", completion.getScore()); responseJSON.put("score", completion.getScore());
responseJSON.put("tries", completion.getTries()); responseJSON.put("tries", completion.getTries());
} else if (completion != null) { }else{
HttpUtil.responseHeaders(writer, 406, "Access-Control-Allow-Origin: *", "Content-Type: application/json"); HttpUtil.responseHeaders(writer, 406, "Access-Control-Allow-Origin: *", "Content-Type: application/json");
responseJSON.put("tries", completion.getTries()); responseJSON.put("tries", completion.getTries());
} else {
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
return;
} }
writer.write(responseJSON.toJSONString()); writer.write(responseJSON.toJSONString());
} }
private void saveSourceCode(ReceivedResponse received, Player player) { private void saveSourceCode(ReceivedResponse received, Player player){
try { try{
Path path = Paths.get(String.format("%s/%s/puz%04d-%s", usersFilesPath, player.getPseudo(), Path path = Paths.get(String.format("%s/%s/puz%04d-%s", usersFilesPath, player.getPseudo(),
received.getPuzzleId(), received.getFileName())); received.getPuzzleId(), received.getFileName()));
Files.write(path, received.getSourceCode()); Files.write(path, received.getSourceCode());
} catch (IOException e) { }catch (IOException e){
System.err.println("ERREUR D'ENREGISTREMENT DU CODE POUR PUZZLE_RESPONSE : " + e.toString()); System.err.println("ERREUR D'ENREGISTREMENT DU CODE POUR PUZZLE_RESPONSE : " + e.toString());
} }