feat: code

This commit is contained in:
2026-02-11 14:09:38 -08:00
parent 1f008968d1
commit ce7b7f50ab
10 changed files with 574 additions and 6 deletions

View File

@@ -1,5 +1,14 @@
package online.mineroo.common.request;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import online.mineroo.common.NetworkServiceInterface;
import org.slf4j.Logger;
@@ -7,8 +16,207 @@ public class CurrencyRequest {
private final Logger logger;
private final NetworkServiceInterface networkService;
private final ConcurrentHashMap<UUID, PlayerWallet> currencyCache;
public CurrencyRequest(Logger logger, NetworkServiceInterface networkService) {
this.logger = logger;
this.networkService = networkService;
this.currencyCache = new ConcurrentHashMap<>();
}
public static class PlayerWallet {
private final ConcurrentHashMap<String, Long> currencies;
public PlayerWallet() {
this.currencies = new ConcurrentHashMap<>();
}
public long getBalance(String currency) {
return currencies.getOrDefault(currency, 0L);
}
public void setBalance(String currency, long amount) {
currencies.put(currency, amount);
}
public void add(String currency, long delta) {
currencies.merge(currency, delta, Long::sum);
}
public void updateAll(JsonObject json) {
for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
try {
this.currencies.put(entry.getKey(), entry.getValue().getAsLong());
} catch (Exception ignored) {
}
}
}
}
public static class LoadPlayerCurrencyResponse {
@SerializedName("amount") private long amount;
@SerializedName("slug") private String slug;
public LoadPlayerCurrencyResponse(long amount, String slug) {
this.amount = amount;
this.slug = slug;
}
public long getAmount() {
return amount;
}
public String getSlug() {
return slug;
}
}
public static class ModifyPlayerCurrencyResponse {
@SerializedName("message") private String message;
@SerializedName("new_balance") private long newBalance;
@SerializedName("success") private boolean success;
public ModifyPlayerCurrencyResponse(String message, long newBalance, boolean success) {
this.message = message;
this.newBalance = newBalance;
this.success = success;
}
public String getMessage() {
return message;
}
public long getNewBalance() {
return newBalance;
}
public boolean isSuccess() {
return success;
}
}
private LoadPlayerCurrencyResponse createLoadPlayerCurrencyErrorResponse() {
return new LoadPlayerCurrencyResponse(0, "");
}
private ModifyPlayerCurrencyResponse createModifyPlayerCurrencyErrorResponse() {
return new ModifyPlayerCurrencyResponse("", 0, false);
}
// INFO: the amount need `/ 100` and convert to double
public CompletableFuture<LoadPlayerCurrencyResponse> LoadPlayerCurrency(
UUID playerUuid, String slug
) {
String path = "/server/currency/user";
Map<String, String> params = new HashMap<>();
if (slug != null) {
params.put("currency_slug", slug);
}
if (playerUuid != null) {
String simpleUuid = playerUuid.toString().replace("-", "");
params.put("mc_uuid", simpleUuid);
}
return networkService.getData(path, params)
.thenApply(response -> {
Gson gson = new Gson();
String responseBody = response.getBody();
if (response.getStatusCode() != 200) {
try {
LoadPlayerCurrencyResponse res =
gson.fromJson(responseBody, LoadPlayerCurrencyResponse.class);
PlayerWallet wallet =
currencyCache.computeIfAbsent(playerUuid, k -> new PlayerWallet());
wallet.setBalance(res.getSlug(), res.getAmount());
return res;
} catch (Exception ignored) {
logger.error("API Returned: " + responseBody);
return createLoadPlayerCurrencyErrorResponse();
}
}
try {
LoadPlayerCurrencyResponse res =
gson.fromJson(responseBody, LoadPlayerCurrencyResponse.class);
PlayerWallet wallet =
currencyCache.computeIfAbsent(playerUuid, k -> new PlayerWallet());
wallet.setBalance(res.getSlug(), res.getAmount());
return res;
} catch (Exception e) {
logger.error("API Returned: " + responseBody);
return createLoadPlayerCurrencyErrorResponse();
}
})
.exceptionally(e -> { return createLoadPlayerCurrencyErrorResponse(); });
}
public CompletableFuture<ModifyPlayerCurrencyResponse> ModifyPlayerCurrency(
long amount, UUID playerUuid, String requestId, String currencySlug, String reason
) {
String path = "/server/currency/user";
JsonObject json = new JsonObject();
json.addProperty("amount", amount);
if (playerUuid != null) {
String simpleUuid = playerUuid.toString().replace("-", "");
json.addProperty("mc_uuid", simpleUuid);
}
if (requestId != null) {
json.addProperty("request_id", requestId);
}
if (currencySlug != null) {
json.addProperty("currency", currencySlug);
}
if (reason != null) {
json.addProperty("reason", reason);
}
return networkService.postData(path, json)
.thenApply(response -> {
Gson gson = new Gson();
String responseBody = response.getBody();
if (response.getStatusCode() != 200) {
try {
return gson.fromJson(responseBody, ModifyPlayerCurrencyResponse.class);
} catch (Exception ignored) {
return createModifyPlayerCurrencyErrorResponse();
}
}
try {
return gson.fromJson(responseBody, ModifyPlayerCurrencyResponse.class);
} catch (Exception e) {
return createModifyPlayerCurrencyErrorResponse();
}
})
.exceptionally(e -> { return createModifyPlayerCurrencyErrorResponse(); });
}
// sync functions
public double getCachedBalance(UUID uuid, String currencySlug) {
PlayerWallet wallet = currencyCache.get(uuid);
if (wallet == null)
return 0.0;
return wallet.getBalance(currencySlug) / 100;
}
public void modifyCachedBalance(UUID uuid, String currencySlug, double amount) {
long delta = (long) (amount * 100);
currencyCache.computeIfAbsent(uuid, k -> new PlayerWallet()).add(currencySlug, delta);
}
public void unloadPlayer(UUID uuid) {
currencyCache.remove(uuid);
}
}

View File

@@ -6,10 +6,12 @@ import org.slf4j.Logger;
public class RequestClient {
private final ManagementRequest managementRequest;
private final UserRequest userRequest;
private final CurrencyRequest currencyRequest;
public RequestClient(Logger logger, NetworkServiceInterface networkService) {
this.managementRequest = new ManagementRequest(logger, networkService);
this.userRequest = new UserRequest(logger, networkService);
this.currencyRequest = new CurrencyRequest(logger, networkService);
}
public ManagementRequest management() {
@@ -19,4 +21,8 @@ public class RequestClient {
public UserRequest user() {
return this.userRequest;
}
public CurrencyRequest currency() {
return this.currencyRequest;
}
}