-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from efrei-craft/stable
Dev
- Loading branch information
Showing
9 changed files
with
551 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/main/java/fr/efreicraft/ecatup/players/statistics/PlayerStatistic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/main/java/fr/efreicraft/ecatup/players/statistics/PlayerStatisticsManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/fr/efreicraft/ecatup/players/statistics/Statistic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/fr/efreicraft/ecatup/players/statistics/interfaces/IPassiveStatisticValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]>} | ||
* @project ECATUP | ||
*/ | ||
public interface IPassiveStatisticValue { | ||
int value(ECPlayer player); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.