Skip to content

Commit

Permalink
Auto reload gacha banners on change
Browse files Browse the repository at this point in the history
  • Loading branch information
Melledy committed Dec 31, 2023
1 parent ed1f292 commit 62fcdf6
Showing 1 changed file with 50 additions and 4 deletions.
54 changes: 50 additions & 4 deletions src/main/java/emu/lunarcore/game/gacha/GachaService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package emu.lunarcore.game.gacha;

import java.io.FileReader;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
Expand Down Expand Up @@ -33,7 +34,8 @@
@Getter
public class GachaService extends BaseGameService {
private final Int2ObjectMap<GachaBanner> gachaBanners;
private GetGachaInfoScRsp cachedProto;
private WatchService watchService;
private Thread watchThread;

private int[] yellowAvatars = new int[] {1003, 1004, 1101, 1107, 1104, 1209, 1211};
private int[] yellowWeapons = new int[] {23000, 23002, 23003, 23004, 23005, 23012, 23013};
Expand All @@ -48,7 +50,12 @@ public class GachaService extends BaseGameService {
public GachaService(GameServer server) {
super(server);
this.gachaBanners = new Int2ObjectOpenHashMap<>();
this.load();

try {
this.watch();
} catch (Exception e) {
LunarCore.getLogger().error("Watch service error: ", e);
}
}

public int randomRange(int min, int max) {
Expand All @@ -58,9 +65,48 @@ public int randomRange(int min, int max) {
public int getRandom(int[] array) {
return array[randomRange(0, array.length - 1)];
}

private String getBannerFileName() {
return LunarCore.getConfig().getDataDir() + "/Banners.json";
}

public void watch() throws Exception {
// Load banners first
this.loadBanners();

// Create watch service
this.watchService = FileSystems.getDefault().newWatchService();
Path watchPath = Paths.get(LunarCore.getConfig().getDataDir());
watchPath.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);

// Start watch thread
this.watchThread = new Thread(() -> {
WatchKey key = null;
try {
while ((key = watchService.take()) != null) {
for (var event : key.pollEvents()) {
if (event.context() == null) {
continue;
}

if (event.context() instanceof Path path && path.toString().equals("Banners.json")) {
loadBanners();
}
}

key.reset();
}
} catch (Exception e) {
LunarCore.getLogger().error("Watch service thread error: ", e);
}
});
this.watchThread.start();
}

public synchronized void load() {
try (FileReader fileReader = new FileReader(LunarCore.getConfig().getDataDir() + "/Banners.json")) {
public synchronized void loadBanners() {
this.getGachaBanners().clear();

try (FileReader fileReader = new FileReader(getBannerFileName())) {
List<GachaBanner> banners = JsonUtils.loadToList(fileReader, GachaBanner.class);
for (GachaBanner banner : banners) {
getGachaBanners().put(banner.getId(), banner);
Expand Down

0 comments on commit 62fcdf6

Please sign in to comment.