Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
User config operation now only happen locally and are synced via command
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrptonaught committed Jun 24, 2023
1 parent 76b7fe1 commit 8c7f330
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 101 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.kyrptonaught</groupId>
<artifactId>LEMBackend</artifactId>
<version>0.0.12</version>
<version>0.0.13</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,69 +12,24 @@

public class UserConfigModule extends Module {

public final ConcurrentHashMap<String, PlayerConfigs> playerCache = new ConcurrentHashMap<>();

public UserConfigModule() {
super("data/userConfigs");
}

public PlayerConfigs getPlayerConfig(String player) {
if (!playerCache.containsKey(player))
load(player);

return playerCache.get(player);
}

public void unloadPlayer(String player) {
if (playerCache.containsKey(player)) {
save(player);
playerCache.remove(player);
}
}

public void setValue(String player, String key, String value) {
getPlayerConfig(player).setValue(key, value);
save(player);
}


public ConcurrentHashMap<String, String> getValues(String player) {
return getPlayerConfig(player).getValues();
}

public void removeValue(String player, String key) {
getPlayerConfig(player).removeValue(key);
save(player);
}

public void saveToPreset(String player, String presetID, JsonObject keys) {
getPlayerConfig(player).saveToPreset(presetID, keys);
save(player);
}

public void loadFromPreset(String player, String presetID, JsonArray keys) {
getPlayerConfig(player).loadFromPreset(presetID, keys);
save(player);
}
public JsonObject loadPlayer(String player) {
JsonObject obj = readFileJson(LEMBackend.gson, player + ".json", JsonObject.class);
if(obj == null) obj = new JsonObject();

public void load(String player) {
JsonObject userConfigObj = readFileJson(LEMBackend.gson, player + ".json", JsonObject.class);
playerCache.put(player, PlayerConfigs.load(userConfigObj));
return obj;
}

public void save(String player) {
writeFile(player + ".json", LEMBackend.gson.toJson(playerCache.get(player)));
public void syncPlayer(String player, String json) {
writeFile(player + ".json", json);
}

@Override
public void load(Gson gson) {
createDirectories();
}

@Override
public void save(Gson gson) {
createDirectories();

playerCache.keySet().forEach(this::save);
}
}
Original file line number Diff line number Diff line change
@@ -1,72 +1,26 @@
package net.kyrptonaught.LEMBackend.userConfig;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import io.javalin.http.Context;
import net.kyrptonaught.LEMBackend.ModuleRouter;

import java.util.concurrent.ConcurrentHashMap;

public class UserConfigRouter extends ModuleRouter<UserConfigModule> {

@Override
public void addRoutes() {
route(HTTP.GET, "/v0/{secret}/getUserConfig/{uuid}", this::getUserConfig);
route(HTTP.POST, "/v0/{secret}/syncUserConfig/{uuid}/{key}/{value}", this::syncUserConfig);
route(HTTP.POST, "/v0/{secret}/removeUserConfig/{uuid}/{key}", this::removeUserConfig);
route(HTTP.POST, "/v0/{secret}/userConfigSaveToPreset/{uuid}/{preset}", this::saveToPreset);
route(HTTP.POST, "/v0/{secret}/userConfigLoadFromPreset/{uuid}/{preset}", this::loadFromPreset);
route(HTTP.POST, "/v0/{secret}/unloadUserConfig/{uuid}", this::unloadPlayer);
route(HTTP.POST, "/v0/{secret}/syncUserConfig/{uuid}", this::syncUserConfig);
}

public void getUserConfig(Context ctx) {
String uuid = ctx.pathParam("uuid");

ConcurrentHashMap<String, String> dataStorage = module.getValues(uuid);
ctx.json(dataStorage);
ctx.json(module.loadPlayer(uuid));
}

public void syncUserConfig(Context ctx) {
String key = ctx.pathParam("key");
String uuid = ctx.pathParam("uuid");
String value = ctx.pathParam("value");

module.setValue(uuid, key, value);
ctx.result("success");
}

public void removeUserConfig(Context ctx) {
String key = ctx.pathParam("key");
String uuid = ctx.pathParam("uuid");

module.removeValue(uuid, key);
ctx.result("success");
}

public void saveToPreset(Context ctx) {
String uuid = ctx.pathParam("uuid");
String presetID = ctx.pathParam("preset");

JsonObject keys = ctx.bodyAsClass(JsonObject.class);
module.saveToPreset(uuid, presetID, keys);
ctx.result("success");
}

public void loadFromPreset(Context ctx) {
String uuid = ctx.pathParam("uuid");
String presetID = ctx.pathParam("preset");

JsonArray keys = ctx.bodyAsClass(JsonArray.class);
module.loadFromPreset(uuid, presetID, keys);

ConcurrentHashMap<String, String> dataStorage = module.getValues(uuid);
ctx.json(dataStorage);
}

public void unloadPlayer(Context ctx) {
String uuid = ctx.pathParam("uuid");

module.unloadPlayer(uuid);
module.syncPlayer(uuid, ctx.body());
ctx.result("success");
}
}

0 comments on commit 8c7f330

Please sign in to comment.