added more sided dialog, added api and added PlayerHandler
This commit is contained in:
1 parent
9f63680d79
commit
4e6255483b
11 files changed
+635
-3
No files matched your search
@@ -0,0 +1,98 @@
|
||||
package eu.mhsl.craftattack.loginServer;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URLEncoder;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class Api {
|
||||
private static final HttpClient client = HttpClient.newHttpClient();
|
||||
|
||||
private Api() {}
|
||||
|
||||
public static void setProfile(UUID uuid,String username, String name, String secondName, String birthday, String phoneNumber) {
|
||||
JsonObject body = new JsonObject();
|
||||
body.addProperty("uuid", uuid.toString());
|
||||
body.addProperty("username", username);
|
||||
body.addProperty("name", name);
|
||||
body.addProperty("secondName", secondName);
|
||||
body.addProperty("birthday", birthday);
|
||||
body.addProperty("phoneNumber", phoneNumber);
|
||||
|
||||
send("POST", "players", "", body);
|
||||
}
|
||||
|
||||
public static boolean isRegistered(UUID uuid) {
|
||||
return find("players", "uuid", uuid.toString()).isPresent();
|
||||
}
|
||||
|
||||
public static boolean isSecretValid(String username, String secret) {
|
||||
return find("secrets", "player", username, "secret", secret).isPresent();
|
||||
}
|
||||
|
||||
private static Optional<JsonObject> find(String collection, String... fieldValuePairs) {
|
||||
StringBuilder filter = new StringBuilder();
|
||||
for (int i = 0; i < fieldValuePairs.length; i += 2) {
|
||||
if (i > 0) filter.append(" && ");
|
||||
String escaped = fieldValuePairs[i + 1].replace("\\", "\\\\").replace("'", "\\'");
|
||||
filter.append(fieldValuePairs[i]).append("='").append(escaped).append("'");
|
||||
}
|
||||
|
||||
String query = "?perPage=1&filter=" + URLEncoder.encode(filter.toString(), StandardCharsets.UTF_8);
|
||||
JsonArray items = send("GET", collection, query, null).getAsJsonArray("items");
|
||||
return items.size() == 0 ? Optional.empty() : Optional.of(items.get(0).getAsJsonObject());
|
||||
}
|
||||
|
||||
private static JsonObject send(String method, String collection, String path, JsonObject body) {
|
||||
JsonObject login = new JsonObject();
|
||||
login.addProperty("identity", config("email"));
|
||||
login.addProperty("password", config("password"));
|
||||
|
||||
String token = call("POST", "/api/collections/_superusers/auth-with-password", login, null)
|
||||
.get("token").getAsString();
|
||||
return call(method, "/api/collections/" + collection + "/records" + path, body, token);
|
||||
}
|
||||
|
||||
private static JsonObject call(String method, String path, JsonObject body, String token) {
|
||||
try {
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder(URI.create(config("url") + path))
|
||||
.timeout(Duration.ofSeconds(5))
|
||||
.header("Content-Type", "application/json")
|
||||
.method(method, body == null
|
||||
? HttpRequest.BodyPublishers.noBody()
|
||||
: HttpRequest.BodyPublishers.ofString(body.toString()));
|
||||
if (token != null) builder.header("Authorization", token);
|
||||
|
||||
HttpResponse<String> response = client.send(builder.build(), HttpResponse.BodyHandlers.ofString());
|
||||
if (response.statusCode() >= 300) {
|
||||
throw new IllegalStateException("API-Fehler " + response.statusCode() + ": " + response.body());
|
||||
}
|
||||
if (response.body().isBlank()) return new JsonObject();
|
||||
return JsonParser.parseString(response.body()).getAsJsonObject();
|
||||
} catch (IOException | InterruptedException e) {
|
||||
throw new IllegalStateException("API nicht erreichbar", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String config(String key) {
|
||||
return Main.globalConfig.node("api", key).getString("");
|
||||
}
|
||||
|
||||
public static boolean removeSecret(String username, String secret) {
|
||||
Optional<JsonObject> entry = find("secrets", "player", username, "secret", secret);
|
||||
if (entry.isEmpty()) return false;
|
||||
|
||||
send("DELETE", "secrets", "/" + entry.get().get("id").getAsString(), null);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user