Little edit
This commit is contained in:
parent
fa6f0774a1
commit
eac490f040
3 changed files with 21 additions and 27 deletions
|
@ -34,8 +34,8 @@ import be.jeffcheasey88.peeratcode.routes.groups.GroupJoin;
|
|||
import be.jeffcheasey88.peeratcode.routes.groups.GroupList;
|
||||
import be.jeffcheasey88.peeratcode.routes.groups.GroupQuit;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws Exception {
|
||||
public class Main{
|
||||
public static void main(String[] args) throws Exception{
|
||||
Configuration config = new Configuration("config.txt");
|
||||
config.load();
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
|||
|
||||
public class Router{
|
||||
|
||||
private Map<Response, Route> responses;
|
||||
private Map<RequestType, Map<Response, Route>> responses;
|
||||
private Map<Response, Pattern> patterns;
|
||||
private Response noFileFound;
|
||||
private RsaJsonWebKey rsaJsonWebKey;
|
||||
|
@ -31,6 +31,7 @@ public class Router{
|
|||
this.token_issuer = token_issuer;
|
||||
this.token_expiration = token_expiration;
|
||||
this.responses = new HashMap<>();
|
||||
for(RequestType type : RequestType.values()) this.responses.put(type, new HashMap<>());
|
||||
this.patterns = new HashMap<>();
|
||||
this.rsaJsonWebKey = RsaJwkGenerator.generateJwk(2048);
|
||||
}
|
||||
|
@ -45,7 +46,7 @@ public class Router{
|
|||
Response.class.getDeclaredMethods()[0].getParameterTypes());
|
||||
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()));
|
||||
}catch(Exception e){
|
||||
throw new IllegalArgumentException(e);
|
||||
|
@ -58,14 +59,12 @@ public class Router{
|
|||
|
||||
public void exec(RequestType type, String path, User user, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
if(type == null) return;
|
||||
for(Entry<Response, Route> routes : this.responses.entrySet()){
|
||||
if(routes.getValue().type().equals(type)){
|
||||
Matcher matcher = this.patterns.get(routes.getKey()).matcher(path);
|
||||
if(matcher.matches()){
|
||||
if(user == null && routes.getValue().needLogin()) return;
|
||||
routes.getKey().exec(matcher, user, reader, writer);
|
||||
return;
|
||||
}
|
||||
for(Entry<Response, Route> routes : this.responses.get(type).entrySet()){
|
||||
Matcher matcher = this.patterns.get(routes.getKey()).matcher(path);
|
||||
if(matcher.matches()){
|
||||
if(user == null && routes.getValue().needLogin()) return;
|
||||
routes.getKey().exec(matcher, user, reader, writer);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if(noFileFound != null) noFileFound.exec(null, user, reader, writer);
|
||||
|
|
|
@ -6,7 +6,6 @@ import java.io.IOException;
|
|||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
|
@ -33,12 +32,7 @@ public class PuzzleResponse implements Response {
|
|||
}
|
||||
|
||||
@Route(path = "^\\/puzzleResponse\\/([0-9]+)$", type = POST, needLogin = true)
|
||||
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;
|
||||
}
|
||||
|
||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
ReceivedResponse received = new ReceivedResponse(matcher, reader);
|
||||
saveSourceCode(received, databaseRepo.getPlayer(user.getId()));
|
||||
|
||||
|
@ -46,27 +40,28 @@ public class PuzzleResponse implements Response {
|
|||
Puzzle currentPuzzle = databaseRepo.getPuzzle(received.getPuzzleId());
|
||||
Completion completion = databaseRepo.insertOrUpdatePuzzleResponse(received.getPuzzleId(), user.getId(),
|
||||
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");
|
||||
responseJSON.put("score", completion.getScore());
|
||||
responseJSON.put("tries", completion.getTries());
|
||||
|
||||
} else if (completion != null) {
|
||||
}else{
|
||||
HttpUtil.responseHeaders(writer, 406, "Access-Control-Allow-Origin: *", "Content-Type: application/json");
|
||||
responseJSON.put("tries", completion.getTries());
|
||||
} else {
|
||||
HttpUtil.responseHeaders(writer, 400, "Access-Control-Allow-Origin: *");
|
||||
return;
|
||||
}
|
||||
writer.write(responseJSON.toJSONString());
|
||||
}
|
||||
|
||||
private void saveSourceCode(ReceivedResponse received, Player player) {
|
||||
try {
|
||||
private void saveSourceCode(ReceivedResponse received, Player player){
|
||||
try{
|
||||
Path path = Paths.get(String.format("%s/%s/puz%04d-%s", usersFilesPath, player.getPseudo(),
|
||||
received.getPuzzleId(), received.getFileName()));
|
||||
Files.write(path, received.getSourceCode());
|
||||
} catch (IOException e) {
|
||||
}catch (IOException e){
|
||||
System.err.println("ERREUR D'ENREGISTREMENT DU CODE POUR PUZZLE_RESPONSE : " + e.toString());
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue