Skip to content

Commit 9e7fee0

Browse files
author
Julien Poulton
committed
Merge branch '4595-sdk-tool-for-statement-preview' into 'master'
feature(statement preview): add preview for statement See merge request codingame/game-engine!250
2 parents 876c280 + 3068bb6 commit 9e7fee0

17 files changed

+605
-9
lines changed

playground/misc/misc-3-release-notes.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ The CodinGame SDK is regularly updated and improved. This document lets you know
44

55
## NEXT RELEASE
66

7+
### 🎁 Features
8+
9+
- Added game statement editor with preview
10+
711
### 🐞 Bug fix
812

9-
- Fixed crashes when invalid JSON files are in assets folder
13+
- Report malformed JSON error instead of crashing when there are invalid JSON files in assets folder
1014

1115
## 3.9.0
1216

runner/src/main/java/com/codingame/gameengine/runner/Renderer.java

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Enumeration;
2525
import java.util.HashSet;
2626
import java.util.List;
27+
import java.util.Optional;
2728
import java.util.Properties;
2829
import java.util.Set;
2930
import java.util.regex.Matcher;
@@ -50,6 +51,7 @@
5051
import com.google.gson.JsonArray;
5152
import com.google.gson.JsonElement;
5253
import com.google.gson.JsonIOException;
54+
import com.google.gson.JsonNull;
5355
import com.google.gson.JsonObject;
5456
import com.google.gson.JsonParser;
5557
import com.google.gson.JsonPrimitive;
@@ -682,8 +684,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
682684
}
683685

684686
exchange.setStatusCode(StatusCodes.OK);
685-
}
686-
if (exchange.getRelativePath().equals("/stub")) {
687+
} else if (exchange.getRelativePath().equals("/stub")) {
687688
File stubFile = sourceFolderPath.resolve("config/stub.txt").toFile();
688689
if (exchange.getRequestMethod().equalToString("GET")) {
689690
String stub = FileUtils.readFileToString(stubFile, StandardCharsets.UTF_8);
@@ -700,6 +701,46 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
700701
} else {
701702
exchange.setStatusCode(StatusCodes.NOT_FOUND);
702703
}
704+
} else if (exchange.getRelativePath().equals("/statement")) {
705+
File statementFileEN = sourceFolderPath.resolve("config/statement_en.html").toFile();
706+
File statementFileFR = sourceFolderPath.resolve("config/statement_fr.html").toFile();
707+
if (exchange.getRequestMethod().equalToString("GET")) {
708+
JsonObject statements = new JsonObject();
709+
String statementEN = FileUtils.readFileToString(statementFileEN, StandardCharsets.UTF_8);
710+
String statementFR;
711+
if (!statementFileFR.exists()) {
712+
statementFR = null;
713+
} else {
714+
statementFR = FileUtils.readFileToString(statementFileFR, StandardCharsets.UTF_8);
715+
}
716+
JsonElement statementElement = toJsonElement(statementFR);
717+
statements.add("EN", new JsonPrimitive(statementEN));
718+
statements.add("FR", statementElement);
719+
exchange.getResponseSender().send(statements.toString());
720+
} else if (exchange.getRequestMethod().equalToString("PUT")) {
721+
JsonParser parser = new JsonParser();
722+
exchange.getRequestReceiver().receiveFullString((e, data) -> {
723+
try {
724+
JsonObject result = parser.parse(data).getAsJsonObject();
725+
String language = result.get("language").getAsString();
726+
String statement = result.get("statement").getAsString();
727+
if (language.equals("FR")) {
728+
if (!createFileIfNotExists(statementFileFR, e)) {
729+
// terminate if error in createFileIfNotExists
730+
return;
731+
}
732+
FileUtils.write(statementFileFR, statement, StandardCharsets.UTF_8);
733+
} else if (language.equals("EN")) {
734+
FileUtils.write(statementFileEN, statement, StandardCharsets.UTF_8);
735+
}
736+
exchange.setStatusCode(StatusCodes.CREATED);
737+
} catch (IOException ex) {
738+
sendException(e, ex, StatusCodes.BAD_REQUEST);
739+
}
740+
}, StandardCharsets.UTF_8);
741+
} else {
742+
exchange.setStatusCode(StatusCodes.NOT_FOUND);
743+
}
703744
}
704745
} catch (MissingConfigException e) {
705746
sendException(exchange, e, StatusCodes.UNPROCESSABLE_ENTITY);
@@ -710,6 +751,27 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
710751
}
711752
}
712753

754+
private JsonElement toJsonElement(String statementFR) {
755+
return Optional.ofNullable(statementFR)
756+
.map(s -> (JsonElement) new JsonPrimitive(s))
757+
.orElse(JsonNull.INSTANCE);
758+
}
759+
760+
private boolean createFileIfNotExists(File statementFileFR, HttpServerExchange e) {
761+
if (!statementFileFR.exists()) {
762+
try {
763+
statementFileFR.createNewFile();
764+
return true;
765+
} catch (IOException ex) {
766+
sendException(e, ex, StatusCodes.INTERNAL_SERVER_ERROR);
767+
// only return false in case of error
768+
return false;
769+
}
770+
} else {
771+
return true;
772+
}
773+
}
774+
713775
private JsonObject extractDemoFromGameJson(File gameFile) {
714776
JsonObject result = new JsonObject();
715777
JsonParser parser = new JsonParser();
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)