From 0f4431957351745a71dfe67eee97095e7d40dbba Mon Sep 17 00:00:00 2001
From: Hugo <58655174+Rhodless@users.noreply.github.com>
Date: Wed, 11 May 2022 22:50:29 +0200
Subject: [PATCH] commit
---
pom.xml | 37 ++++++
.../java/fr/arashi/common/RedisProvider.java | 106 ++++++++++++++++++
.../java/fr/arashi/common/api/CommonAPI.java | 36 ++++++
.../java/fr/arashi/common/api/ProfileAPI.java | 22 ++++
.../fr/arashi/common/api/PunishmentAPI.java | 9 ++
.../java/fr/arashi/common/api/RankAPI.java | 37 ++++++
.../common/cache/BucketServerCache.java | 18 +++
.../java/fr/arashi/common/cache/Division.java | 51 +++++++++
.../fr/arashi/common/cache/ProfileData.java | 46 ++++++++
.../arashi/common/cache/PunishmentData.java | 14 +++
.../java/fr/arashi/common/cache/Rank.java | 53 +++++++++
11 files changed, 429 insertions(+)
create mode 100644 pom.xml
create mode 100644 src/main/java/fr/arashi/common/RedisProvider.java
create mode 100644 src/main/java/fr/arashi/common/api/CommonAPI.java
create mode 100644 src/main/java/fr/arashi/common/api/ProfileAPI.java
create mode 100644 src/main/java/fr/arashi/common/api/PunishmentAPI.java
create mode 100644 src/main/java/fr/arashi/common/api/RankAPI.java
create mode 100644 src/main/java/fr/arashi/common/cache/BucketServerCache.java
create mode 100644 src/main/java/fr/arashi/common/cache/Division.java
create mode 100644 src/main/java/fr/arashi/common/cache/ProfileData.java
create mode 100644 src/main/java/fr/arashi/common/cache/PunishmentData.java
create mode 100644 src/main/java/fr/arashi/common/cache/Rank.java
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..ad403fe
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,37 @@
+
+
+ 4.0.0
+
+ fr.kohei
+ Common
+ 1.0.0
+
+
+ 8
+ 8
+
+
+
+
+ org.redisson
+ redisson
+ 3.17.1
+ compile
+
+
+ com.google.code.gson
+ gson
+ 2.9.0
+ compile
+
+
+ org.projectlombok
+ lombok
+ 1.18.22
+ provided
+
+
+
+
\ No newline at end of file
diff --git a/src/main/java/fr/arashi/common/RedisProvider.java b/src/main/java/fr/arashi/common/RedisProvider.java
new file mode 100644
index 0000000..ccbfbc4
--- /dev/null
+++ b/src/main/java/fr/arashi/common/RedisProvider.java
@@ -0,0 +1,106 @@
+package fr.arashi.common;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import fr.arashi.common.api.CommonAPI;
+import fr.arashi.common.cache.BucketServerCache;
+import fr.arashi.common.cache.ProfileData;
+import fr.arashi.common.cache.Rank;
+import lombok.Getter;
+import org.redisson.Redisson;
+import org.redisson.api.RMap;
+import org.redisson.api.RSet;
+import org.redisson.api.RedissonClient;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+@Getter
+public class RedisProvider implements CommonAPI {
+
+ public static RedisProvider redisProvider;
+
+ public final Gson GSON = new GsonBuilder().create();
+
+ public final RMap playersGson;
+ private final Set ranks;
+ private final RSet ranksGson;
+ private final RedissonClient client;
+
+ private final ProfileData defaultProfile;
+
+ public RedisProvider() {
+
+ redisProvider = this;
+ this.client = Redisson.create();
+
+ this.playersGson = client.getMap("players");
+ this.ranksGson = client.getSet("ranks");
+ this.ranks = new HashSet<>();
+
+ ranksGson.readAll().forEach(s -> ranks.add(GSON.fromJson(s, Rank.class)));
+
+ Optional defaultRank = ranks.stream().filter(tk -> tk.token().equalsIgnoreCase("default")).findFirst();
+
+ if (!defaultRank.isPresent()) {
+ System.out.println("Couldn't find the default rank creating it...");
+ Rank rank = new Rank("default", 0, "&7", "&7");
+ addRank(rank);
+ this.defaultProfile = new ProfileData(rank, 0, 0, "fr-FR");
+ } else {
+ this.defaultProfile = new ProfileData(defaultRank.get(), 0, 0, "fr-FR");
+ }
+
+ }
+
+ @Override
+ public BucketServerCache createBucketServerCache(String token) {
+ return new BucketServerCache<>(client.getBucket(token));
+ }
+
+ @Override
+ public void deleteAllDataFromAPlayer(UUID uuid) {
+ playersGson.remove(uuid);
+ }
+
+ @Override
+ public Set getRanks() {
+ return ranks;
+ }
+
+ @Override
+ public Optional getRank(String token){
+ return ranks.stream().filter(rk -> rk.token().equalsIgnoreCase(token)).findFirst();
+ }
+
+ @Override
+ public Set getRankUp(int minimum){
+ return ranks.stream().filter(rk -> rk.permissionPower() >= minimum).collect(Collectors.toSet());
+ }
+
+ @Override
+ public void addRank(Rank rank) {
+ ranks.add(rank);
+ ranksGson.add(GSON.toJson(rank));
+ }
+
+ @Override
+ public void removeRank(String token) {
+ ranks.removeIf(rk -> (rk.token().equalsIgnoreCase(token)));
+ ranksGson.removeIf(rk -> (GSON.fromJson(rk, Rank.class).token().equalsIgnoreCase(token)));
+ }
+
+ @Override
+ public ProfileData getProfile(UUID uuid) {
+ if (!playersGson.containsKeyAsync(uuid).getNow()) {
+ playersGson.putAsync(uuid, GSON.toJson(defaultProfile));
+ }
+
+ return GSON.fromJson(playersGson.getAsync(uuid).getNow(), ProfileData.class);
+ }
+
+ @Override
+ public void saveProfile(UUID uuid, ProfileData data){
+ playersGson.putAsync(uuid, GSON.toJson(data));
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/fr/arashi/common/api/CommonAPI.java b/src/main/java/fr/arashi/common/api/CommonAPI.java
new file mode 100644
index 0000000..2e15563
--- /dev/null
+++ b/src/main/java/fr/arashi/common/api/CommonAPI.java
@@ -0,0 +1,36 @@
+package fr.arashi.common.api;
+
+import fr.arashi.common.RedisProvider;
+import fr.arashi.common.cache.BucketServerCache;
+
+import java.util.UUID;
+
+public interface CommonAPI extends ProfileAPI, RankAPI, PunishmentAPI {
+ /**
+ * Create a CacheMap with player uuid as key,
+ * Use it for your custom game or data
+ *
+ * Employ it as a singleton function
+ *
+ * @param token is a unique key that will be used to recognize your cache. The bucket's data will be deleted from the map if the whole player is deleted.
+ * @param is the data type of the map, use your custom object.
+ * @return the cache map object.
+ */
+ BucketServerCache createBucketServerCache(String token);
+
+ /**
+ * Remove every data from a player in the common map (Profiles) and in the custom map (see function below)
+ *
+ * @param uuid is the player uuid
+ */
+ void deleteAllDataFromAPlayer(UUID uuid);
+
+ /**
+ * Use as singleton to make the link to the database
+ *
+ * @return the instance of the api
+ */
+ static CommonAPI create() {
+ return new RedisProvider();
+ }
+}
diff --git a/src/main/java/fr/arashi/common/api/ProfileAPI.java b/src/main/java/fr/arashi/common/api/ProfileAPI.java
new file mode 100644
index 0000000..3bf4c46
--- /dev/null
+++ b/src/main/java/fr/arashi/common/api/ProfileAPI.java
@@ -0,0 +1,22 @@
+package fr.arashi.common.api;
+
+import fr.arashi.common.cache.ProfileData;
+
+import java.util.UUID;
+
+public interface ProfileAPI {
+
+ /**
+ * @param uuid of the player
+ * @return the players data
+ */
+ ProfileData getProfile(UUID uuid);
+
+ /**
+ * Save the data on the redis's database
+ *
+ * @param uuid of the player
+ * @param data of the player
+ */
+ void saveProfile(UUID uuid, ProfileData data);
+}
diff --git a/src/main/java/fr/arashi/common/api/PunishmentAPI.java b/src/main/java/fr/arashi/common/api/PunishmentAPI.java
new file mode 100644
index 0000000..f006eb1
--- /dev/null
+++ b/src/main/java/fr/arashi/common/api/PunishmentAPI.java
@@ -0,0 +1,9 @@
+package fr.arashi.common.api;
+
+import java.util.List;
+
+public interface PunishmentAPI {
+
+
+
+}
diff --git a/src/main/java/fr/arashi/common/api/RankAPI.java b/src/main/java/fr/arashi/common/api/RankAPI.java
new file mode 100644
index 0000000..a5e6f37
--- /dev/null
+++ b/src/main/java/fr/arashi/common/api/RankAPI.java
@@ -0,0 +1,37 @@
+package fr.arashi.common.api;
+
+import fr.arashi.common.cache.Rank;
+
+import java.util.Optional;
+import java.util.Set;
+
+public interface RankAPI {
+
+ /**
+ * @return all the ranks who are stored on the database
+ */
+ Set getRanks();
+
+ /**
+ * @param token is the token rank
+ * @return the first rank who have the token specified below. Empty optional if the rank doesn't exist
+ */
+ Optional getRank(String token);
+
+ /**
+ * @param power is the permission power required
+ * @return all the rank who have as minimum this permission power
+ */
+ Set getRankUp(int power);
+
+ /**
+ * @param rank the rank who'll go on the database
+ */
+ void addRank(Rank rank);
+
+ /**
+ * @param token is the token that
+ */
+ void removeRank(String token);
+
+}
diff --git a/src/main/java/fr/arashi/common/cache/BucketServerCache.java b/src/main/java/fr/arashi/common/cache/BucketServerCache.java
new file mode 100644
index 0000000..cd410c6
--- /dev/null
+++ b/src/main/java/fr/arashi/common/cache/BucketServerCache.java
@@ -0,0 +1,18 @@
+package fr.arashi.common.cache;
+
+import lombok.RequiredArgsConstructor;
+import org.redisson.api.RBucket;
+
+@RequiredArgsConstructor
+public class BucketServerCache {
+
+ private final RBucket bucket;
+
+ public T get(){
+ return bucket.get();
+ }
+
+ public void save(T t){
+ bucket.set(t);
+ }
+}
diff --git a/src/main/java/fr/arashi/common/cache/Division.java b/src/main/java/fr/arashi/common/cache/Division.java
new file mode 100644
index 0000000..dc3047b
--- /dev/null
+++ b/src/main/java/fr/arashi/common/cache/Division.java
@@ -0,0 +1,51 @@
+package fr.arashi.common.cache;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+
+import java.util.function.Consumer;
+
+@Getter
+@RequiredArgsConstructor
+public enum Division {
+
+ AUCUNE("&cAucune", 150, profile -> {
+ profile.setHosts(profile.getHosts() + 1);
+ profile.setCoins(profile.getCoins() + 300);
+ }, "&fVous avez obtenu &e300 coins &fet &c1 host&f."),
+
+ BRONZE("&6Bronze", 250, profile -> {
+ profile.setHosts(profile.getHosts() + 2);
+ profile.setCoins(profile.getCoins() + 500);
+ }, "&fVous avez obtenu &e500 coins &fet &c2 host&f."),
+
+ ARGENT("&7Argent", 400, profile -> {
+ profile.setHosts(profile.getHosts() + 2);
+ profile.setCoins(profile.getCoins() + 700);
+ }, "&fVous avez obtenu &e700 coins &fet &c2 host&f."),
+
+ OR("&eOr", 750, profile -> {
+ profile.setHosts(profile.getHosts() + 3);
+ profile.setCoins(profile.getCoins() + 1000);
+ }, "&fVous avez obtenu &e1000 coins &fet &c3 host&f."),
+
+ DIAMANT("&bDiamant", 900, profile -> {
+ // TODO RANK FOR 5 DAYS
+ }, "&fVous avez obtenu le grade &c &fpour &c5 jours&f."),
+
+ EMERAUDE("&aEmeraude", 1200, profile -> {
+ // TODO RANK FOR 14 DAYS
+ }, "&fVous avez obtenu le grade &c &fpour &c14 jours&f."),
+
+ PLATINE("&8Platine", 1500, profile -> {
+ // TODO RANK FOR 30 DAYS
+ }, "&fVous avez obtenu le grade &c &fpour &c30 jours&f."),
+
+ MAITRE("&dMaitre", 10000000, profile -> {}, "");
+
+ private final String display;
+ private final int experience;
+ private final Consumer consumer;
+ private final String message;
+
+}
diff --git a/src/main/java/fr/arashi/common/cache/ProfileData.java b/src/main/java/fr/arashi/common/cache/ProfileData.java
new file mode 100644
index 0000000..fddea9d
--- /dev/null
+++ b/src/main/java/fr/arashi/common/cache/ProfileData.java
@@ -0,0 +1,46 @@
+package fr.arashi.common.cache;
+
+import fr.arashi.common.RedisProvider;
+import fr.arashi.common.api.CommonAPI;
+import lombok.Getter;
+import lombok.Setter;
+import org.redisson.spring.support.RedissonGenericObjectDefinitionParser;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+@Getter
+@Setter
+public class ProfileData implements Serializable {
+
+ private String displayName;
+ private String rankName;
+ private int coins, hosts;
+ private String language;
+ private List silentPlayer;
+ private List friends;
+ private boolean friendRequests;
+ private boolean privateMessages;
+ private boolean notifications;
+
+ private Division division;
+ private int experience;
+
+ public ProfileData(Rank rank, int coins, int hosts, String language) {
+ this.rankName = rank.token();
+ this.coins = coins;
+ this.hosts = hosts;
+ this.language = language;
+ this.silentPlayer = new ArrayList<>();
+ this.friends = new ArrayList<>();
+ this.friendRequests = true;
+ this.privateMessages = true;
+ this.notifications = true;
+ this.division = Division.AUCUNE;
+ }
+
+ public Rank getRank() {
+ return RedisProvider.redisProvider.getRanks().stream().filter(rank -> rank.token().equalsIgnoreCase(rankName)).findFirst().orElse(RedisProvider.redisProvider.getRank("default").get());
+ }
+}
diff --git a/src/main/java/fr/arashi/common/cache/PunishmentData.java b/src/main/java/fr/arashi/common/cache/PunishmentData.java
new file mode 100644
index 0000000..ce5a163
--- /dev/null
+++ b/src/main/java/fr/arashi/common/cache/PunishmentData.java
@@ -0,0 +1,14 @@
+package fr.arashi.common.cache;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import java.util.UUID;
+
+@Getter
+@Setter
+public class PunishmentData {
+
+ private UUID punished;
+
+}
diff --git a/src/main/java/fr/arashi/common/cache/Rank.java b/src/main/java/fr/arashi/common/cache/Rank.java
new file mode 100644
index 0000000..0b05bf6
--- /dev/null
+++ b/src/main/java/fr/arashi/common/cache/Rank.java
@@ -0,0 +1,53 @@
+
+package fr.arashi.common.cache;
+
+import java.io.Serializable;
+
+public class Rank implements Serializable {
+
+ private String token;
+ private int permissionPower;
+ private String chatPrefix;
+ private String tabPrefix;
+
+
+ public Rank(String token, int permissionPower, String chatPrefix, String tabPrefix) {
+ this.token = token;
+ this.permissionPower = permissionPower;
+ this.chatPrefix = chatPrefix;
+ this.tabPrefix = tabPrefix;
+ }
+
+ public String token() {
+ return token;
+ }
+
+ public void setToken(String token) {
+ this.token = token;
+ }
+
+ public int permissionPower() {
+ return permissionPower;
+ }
+
+ public void setPermissionPower(int permissionPower) {
+ this.permissionPower = permissionPower;
+ }
+
+ public String chatPrefix() {
+ return chatPrefix;
+ }
+
+ public void setChatPrefix(String chatPrefix) {
+ this.chatPrefix = chatPrefix;
+ }
+
+ public String tabPrefix() {
+ return tabPrefix;
+ }
+
+ public void setTabPrefix(String tabPrefix) {
+ this.tabPrefix = tabPrefix;
+ }
+
+}