Skip to content

Commit 93485a4

Browse files
committed
feat: common game player data repo util
1 parent 95a6b5c commit 93485a4

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

build.gradle.kts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ repositories {
1717
}
1818

1919
dependencies {
20-
api("dev.emortal.minestom:core:844abe4")
20+
api("dev.emortal.minestom:core:80be174") {
21+
exclude(group = "dev.emortal.api", module = "common-proto-sdk")
22+
}
23+
api("dev.emortal.api:common-proto-sdk:b05808d")
2124
}
2225

2326
java {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package dev.emortal.minestom.gamesdk.util;
2+
3+
import com.google.protobuf.Message;
4+
import dev.emortal.api.model.gamedata.GameDataGameMode;
5+
import dev.emortal.api.service.gameplayerdata.GamePlayerDataService;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
8+
9+
import java.util.Map;
10+
import java.util.Set;
11+
import java.util.UUID;
12+
import java.util.stream.Collectors;
13+
14+
@SuppressWarnings("unused")
15+
public class GamePlayerDataRepository<T extends Message> {
16+
private final @Nullable GamePlayerDataService gamePlayerDataService;
17+
18+
private final @NotNull T defaultData;
19+
private final @NotNull Class<T> playerDataClass;
20+
private final @NotNull GameDataGameMode gameMode;
21+
22+
public GamePlayerDataRepository(@Nullable GamePlayerDataService gamePlayerDataService, @NotNull T defaultData, @NotNull Class<T> playerDataClass, @NotNull GameDataGameMode gameMode) {
23+
this.gamePlayerDataService = gamePlayerDataService;
24+
this.defaultData = defaultData;
25+
this.playerDataClass = playerDataClass;
26+
this.gameMode = gameMode;
27+
}
28+
29+
public @NotNull T getPlayerData(@NotNull UUID playerId) {
30+
if (this.gamePlayerDataService == null) return this.defaultData;
31+
32+
T playerData = this.gamePlayerDataService.getGameData(this.gameMode, this.playerDataClass, playerId);
33+
return playerData != null ? playerData : this.defaultData;
34+
}
35+
36+
public @NotNull Map<UUID, T> getPlayerData(@NotNull Set<UUID> playerIds) {
37+
if (this.gamePlayerDataService == null)
38+
return playerIds.stream().collect(Collectors.toMap(id -> id, id -> this.defaultData));
39+
40+
Map<UUID, T> responseData = this.gamePlayerDataService.getGameData(this.gameMode, this.playerDataClass, playerIds);
41+
42+
for (Map.Entry<UUID, T> entry : responseData.entrySet()) {
43+
if (entry.getValue() == null) {
44+
responseData.put(entry.getKey(), this.defaultData);
45+
}
46+
}
47+
48+
return responseData;
49+
}
50+
}

0 commit comments

Comments
 (0)