Skip to content

Commit

Permalink
feat: stats system
Browse files Browse the repository at this point in the history
  • Loading branch information
JiveOff committed Sep 13, 2024
1 parent 4163feb commit e4ffd45
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/main/java/fr/efreicraft/ecatup/players/ECPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import fr.efreicraft.ecatup.ECATUP;
import fr.efreicraft.ecatup.players.menus.PlayerMenus;
import fr.efreicraft.ecatup.players.scoreboards.PlayerScoreboard;
import fr.efreicraft.ecatup.players.statistics.PlayerStatisticsManager;
import fr.efreicraft.ecatup.utils.MessageUtils;
import fr.efreicraft.ecatup.utils.SoundUtils;
import fr.efreicraft.ecatup.utils.TitleUtils;
Expand Down Expand Up @@ -48,6 +49,11 @@ public class ECPlayer {
*/
private final PlayerMenus playerMenus;

/**
* Instance du gestionnaire de statistiques du joueur.
*/
private final PlayerStatisticsManager statisticsManager;

/**
* Instance de gestionnaire de permissions du joueur.
*/
Expand All @@ -59,10 +65,12 @@ public class ECPlayer {
*/
public ECPlayer(org.bukkit.entity.Player playerEntity, fr.efreicraft.animus.models.Player animusPlayer) throws ApiException {
this.playerEntity = playerEntity;
this.playerMenus = new PlayerMenus();
this.animusPlayer = animusPlayer;
this.attachment = playerEntity.addAttachment(ECATUP.getInstance());

this.statisticsManager = new PlayerStatisticsManager(this);
this.playerMenus = new PlayerMenus();
this.scoreboard = new PlayerScoreboard(this);
this.animusPlayer = animusPlayer;
this.setPrefix(this.animusPlayer.getPermGroups().get(0).getPrefix());

ECATUP.getInstance().getGroupManager().addPlayerToTeam(this);
Expand All @@ -75,6 +83,7 @@ public ECPlayer(org.bukkit.entity.Player playerEntity, fr.efreicraft.animus.mode
*/
public void unload() {
this.scoreboard.unload();
this.statisticsManager.unload();
}

/**
Expand Down Expand Up @@ -281,4 +290,8 @@ public List<String> getPermissions() {
.map(Map.Entry::getKey) // Ne prendre que les clés
.toList();
}

public PlayerStatisticsManager getStatisticsManager() {
return statisticsManager;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package fr.efreicraft.ecatup.players.statistics;

import fr.efreicraft.animus.endpoints.PlayerService;
import fr.efreicraft.animus.invoker.ApiException;
import fr.efreicraft.animus.models.PlayerStatisticRecord;
import fr.efreicraft.ecatup.players.ECPlayer;
import fr.efreicraft.ecatup.utils.MessageUtils;

import java.math.BigDecimal;

public class PlayerStatistic {

private Statistic statistic;

private ECPlayer player;

private BigDecimal value;

public PlayerStatistic(ECPlayer player, PlayerStatisticRecord record) {
this.player = player;
this.statistic = Statistic.fromRecord(record);
this.value = record.getValue();
}

public PlayerStatistic(ECPlayer player, Statistic statistic) {
this.player = player;
this.statistic = statistic;
this.value = BigDecimal.ZERO;
}

public BigDecimal getValue() {
return value;
}

public void set(int value, String reason, Boolean sendMessage) {
try {
PlayerService.manipulatePlayerStatistic(
this.player.getAnimusPlayer().getUuid(),
this.statistic.getKey(),
BigDecimal.valueOf(value),
reason,
true,
this.statistic.getType(),
this.statistic.getDisplayName(),
this.statistic.getColor()
);

if(sendMessage) {
this.player.sendMessage(MessageUtils.ChatPrefix.STATS, "&7Ton montant de " + this.statistic.getColor() + this.statistic.getDisplayName() + " &7a été mis à jour à " + this.statistic.getColor() + value + "&7.");
}

refreshStatistic();
} catch (ApiException e) {
e.printStackTrace();
}
}

public void add(int value, String reason, Boolean sendMessage) {
try {
PlayerService.manipulatePlayerStatistic(
this.player.getAnimusPlayer().getUuid(),
this.statistic.getKey(),
BigDecimal.valueOf(value),
reason,
false,
this.statistic.getType(),
this.statistic.getDisplayName(),
this.statistic.getColor()
);

if(sendMessage) {
StringBuilder sb = new StringBuilder(" ");

if (value > 0)
sb.append("&a+");
else
sb.append("&c-");

sb.append(this.statistic.getColor()).append(Math.abs(value)).append(" ").append(this.statistic.getDisplayName())
.append(" &7(").append(reason).append(")");

this.player.sendMessage(MessageUtils.ChatPrefix.EMPTY, sb.toString());
}

refreshStatistic();
} catch (ApiException e) {
e.printStackTrace();
}
}

public void remove(int value, String reason, Boolean sendMessage) {
this.add(-value, reason, sendMessage);
}

private void refreshStatistic() {
try {
PlayerStatisticRecord record = PlayerService.getPlayerStatistic(this.player.getAnimusPlayer().getUuid(), this.statistic.getKey());
if(record != null) {
this.statistic = Statistic.fromRecord(record);
this.value = record.getValue();
}
} catch (ApiException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package fr.efreicraft.ecatup.players.statistics;

import fr.efreicraft.animus.endpoints.PlayerService;
import fr.efreicraft.animus.invoker.ApiException;
import fr.efreicraft.ecatup.ECATUP;
import fr.efreicraft.ecatup.players.ECPlayer;
import fr.efreicraft.ecatup.players.statistics.interfaces.IPassiveStatisticValue;
import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitTask;

import java.util.HashMap;
import java.util.Map;

public class PlayerStatisticsManager {

private static final Integer PASSIVE_REFRESH_INTERVAL = 10;

private ECPlayer player;

private Map<String, PlayerStatistic> records = new HashMap<>();

private Map<String, IPassiveStatisticValue> passiveValues = new HashMap<>();

private BukkitTask passiveRefreshTask;

public PlayerStatisticsManager(ECPlayer player) {
this.player = player;

this.initializeStatistics();
this.startPassiveRefreshTask();
}

private void initializeStatistics() {
try {
this.records = PlayerService.getPlayerStatistics(this.player.getAnimusPlayer().getUuid())
.stream()
.collect(
HashMap::new,
(map, record) -> map.put(record.getKey(), new PlayerStatistic(this.player, record)),
HashMap::putAll
);
} catch (ApiException e) {
e.printStackTrace();
}
}

private void startPassiveRefreshTask() {
this.passiveRefreshTask = Bukkit.getScheduler().runTaskTimerAsynchronously(
ECATUP.getInstance(),
() -> {
this.passiveValues.forEach((key, value) -> {
this.get(key).set(value.value(this.player), "Passive refresh", false);
});
},
0,
20L * PASSIVE_REFRESH_INTERVAL
);
}

public void registerPassiveValue(Statistic statistic, IPassiveStatisticValue value, Boolean now) {
this.passiveValues.put(statistic.getKey(), value);

if (now) {
this.get(statistic).set(value.value(this.player), "Passive registration", false);
}
}

public void triggerPassiveRefresh() {
this.passiveValues.forEach((key, value) -> {
this.get(key).set(value.value(this.player), "Forced passive refresh", false);
});
}

public void unload() {
this.passiveRefreshTask.cancel();
}

public PlayerStatistic get(Statistic statistic) {
if (!this.records.containsKey(statistic.getKey())) {
PlayerStatistic stat = new PlayerStatistic(this.player, statistic);
this.records.put(statistic.getKey(), stat);
return stat;
} else {
return this.records.get(statistic.getKey());
}
}

private PlayerStatistic get(String key) {
return this.get(new Statistic(key));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package fr.efreicraft.ecatup.players.statistics;

import fr.efreicraft.animus.models.PlayerStatisticRecord;

public class Statistic {
private String key;

private String displayName;

/**
* Minecraft Color Code
*/
private String color;

private PlayerStatisticRecord.TypeEnum type;

public Statistic(String key) {
this.key = key;
}

public Statistic(String key, String displayName, String color, PlayerStatisticRecord.TypeEnum type) {
this(key);
this.displayName = displayName;
this.color = color;
this.type = type;
}

public String getKey() {
return key;
}

public String getDisplayName() {
return displayName;
}

public String getColor() {
return color;
}

public PlayerStatisticRecord.TypeEnum getType() {
return type;
}

public static Statistic fromRecord(PlayerStatisticRecord record) {
return new Statistic(record.getKey(), record.getDisplayName(), record.getColor(), record.getType());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fr.efreicraft.ecatup.players.statistics.interfaces;

import fr.efreicraft.ecatup.players.ECPlayer;

/**
* Interface fonctionnelle pour les lambda de récupération de valeur de statistiques passives.
*
* @author Antoine B. {@literal <antoine@jiveoff.fr>}
* @project ECATUP
*/
public interface IPassiveStatisticValue {
int value(ECPlayer player);
}
5 changes: 5 additions & 0 deletions src/main/java/fr/efreicraft/ecatup/utils/MessageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public enum ChatPrefix {
*/
TEAM("&dÉquipe"),

/**
* Préfixe pour les messages concernant les statistiques.
*/
STATS("&eStatistiques"),

/**
* Préfixe vide.
*/
Expand Down

0 comments on commit e4ffd45

Please sign in to comment.