Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhodless committed May 11, 2022
0 parents commit 0f44319
Show file tree
Hide file tree
Showing 11 changed files with 429 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>fr.kohei</groupId>
<artifactId>Common</artifactId>
<version>1.0.0</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.17.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
106 changes: 106 additions & 0 deletions src/main/java/fr/arashi/common/RedisProvider.java
Original file line number Diff line number Diff line change
@@ -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<UUID, String> playersGson;
private final Set<Rank> ranks;
private final RSet<String> 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<Rank> 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 <T> BucketServerCache<T> createBucketServerCache(String token) {
return new BucketServerCache<>(client.getBucket(token));
}

@Override
public void deleteAllDataFromAPlayer(UUID uuid) {
playersGson.remove(uuid);
}

@Override
public Set<Rank> getRanks() {
return ranks;
}

@Override
public Optional<Rank> getRank(String token){
return ranks.stream().filter(rk -> rk.token().equalsIgnoreCase(token)).findFirst();
}

@Override
public Set<Rank> 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));
}
}
36 changes: 36 additions & 0 deletions src/main/java/fr/arashi/common/api/CommonAPI.java
Original file line number Diff line number Diff line change
@@ -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 <T> is the data type of the map, use your custom object.
* @return the cache map object.
*/
<T> BucketServerCache<T> 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();
}
}
22 changes: 22 additions & 0 deletions src/main/java/fr/arashi/common/api/ProfileAPI.java
Original file line number Diff line number Diff line change
@@ -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);
}
9 changes: 9 additions & 0 deletions src/main/java/fr/arashi/common/api/PunishmentAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package fr.arashi.common.api;

import java.util.List;

public interface PunishmentAPI {



}
37 changes: 37 additions & 0 deletions src/main/java/fr/arashi/common/api/RankAPI.java
Original file line number Diff line number Diff line change
@@ -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<Rank> 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<Rank> getRank(String token);

/**
* @param power is the permission power required
* @return all the rank who have as minimum this permission power
*/
Set<Rank> 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);

}
18 changes: 18 additions & 0 deletions src/main/java/fr/arashi/common/cache/BucketServerCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package fr.arashi.common.cache;

import lombok.RequiredArgsConstructor;
import org.redisson.api.RBucket;

@RequiredArgsConstructor
public class BucketServerCache<T> {

private final RBucket<T> bucket;

public T get(){
return bucket.get();
}

public void save(T t){
bucket.set(t);
}
}
51 changes: 51 additions & 0 deletions src/main/java/fr/arashi/common/cache/Division.java
Original file line number Diff line number Diff line change
@@ -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<grade> &fpour &c5 jours&f."),

EMERAUDE("&aEmeraude", 1200, profile -> {
// TODO RANK FOR 14 DAYS
}, "&fVous avez obtenu le grade &c<grade> &fpour &c14 jours&f."),

PLATINE("&8Platine", 1500, profile -> {
// TODO RANK FOR 30 DAYS
}, "&fVous avez obtenu le grade &c<grade> &fpour &c30 jours&f."),

MAITRE("&dMaitre", 10000000, profile -> {}, "");

private final String display;
private final int experience;
private final Consumer<ProfileData> consumer;
private final String message;

}
46 changes: 46 additions & 0 deletions src/main/java/fr/arashi/common/cache/ProfileData.java
Original file line number Diff line number Diff line change
@@ -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<String> silentPlayer;
private List<String> 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());
}
}
14 changes: 14 additions & 0 deletions src/main/java/fr/arashi/common/cache/PunishmentData.java
Original file line number Diff line number Diff line change
@@ -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;

}
Loading

0 comments on commit 0f44319

Please sign in to comment.