Skip to content

Commit

Permalink
Merge pull request #41 from Ender-Cube/spleef-database
Browse files Browse the repository at this point in the history
Add a spleef database
  • Loading branch information
zax71 authored Mar 2, 2024
2 parents 9061a18 + 09e9330 commit 3be033d
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.jetbrains.annotations.NotNull;

Expand All @@ -22,6 +24,23 @@ private ComponentUtils() {

private final static int CENTER_PX = 154;

public static Component getTitle(Component text) {
final int LINE_SIZE = 10;
return centerComponent(Component.empty()
.append(getLine(LINE_SIZE).color(NamedTextColor.BLUE))
.append(Component.space())
.append(text)
.append(Component.space())
.append(getLine(LINE_SIZE).color(NamedTextColor.BLUE))
.append(Component.newline())
);
}

public static Component getLine(int size) {
String spaces = " ".repeat(size);
return Component.text(spaces).decorate(TextDecoration.STRIKETHROUGH);
}

public static Component convertToSmallCaps(Component input) {
return input.replaceText(
TextReplacementConfig.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.jetbrains.annotations.NotNull;

import static net.endercube.Common.EndercubeMinigame.logger;
import static net.endercube.spleef.minigame.SpleefMinigame.database;
import static net.endercube.spleef.minigame.SpleefMinigame.spleefMinigame;

public class RemoveEntityFromInstance implements EventListener<RemoveEntityFromInstanceEvent> {
Expand All @@ -40,7 +41,7 @@ public RemoveEntityFromInstance(Instance gameInstance) {
player.setGameMode(GameMode.ADVENTURE);
player.getInventory().setItemStack(0, ItemStack.AIR);


// We need the next tick because this event is called before the player is removed from the instance
gameInstance.scheduleNextTick((instance -> {
if (gameInstance.getPlayers().size() == 1) {
Expand All @@ -57,8 +58,16 @@ public RemoveEntityFromInstance(Instance gameInstance) {
return Result.SUCCESS;
}


int placement = gameInstance.getPlayers().size();

// Increment database
if (placement == 1) {
database.addWonGame(player);
} else {
database.addLostGame(player);
}

// Tell the player their placement
player.sendMessage(spleefMinigame.getChatPrefix()
.append(Component.text("You got "))
.append(ComponentUtils.addOrdinals(placement).decorate(TextDecoration.BOLD))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package net.endercube.spleef.minigame;

import net.endercube.Common.database.AbstractDatabase;
import net.minestom.server.entity.Player;
import org.jetbrains.annotations.Nullable;
import redis.clients.jedis.JedisPooled;

public class SpleefDatabase extends AbstractDatabase {
/**
* An Endercube database
*
* @param jedis A {@code JedisPooled} to get jedis instances from
* @param nameSpace The prefix for all keys, does not need a colon on the end
*/
public SpleefDatabase(JedisPooled jedis, String nameSpace) {
super(jedis, nameSpace);
}

public void addWonGame(Player player) {
jedis.incr(nameSpace + "wonGames:" + player.getUuid());
}

public void addLostGame(Player player) {
jedis.incr(nameSpace + "lostGames:" + player.getUuid());
}

public int getWonGames(Player player) {
@Nullable String wonGames = jedis.get(nameSpace + "wonGames:" + player.getUuid());
if (wonGames == null) {
return 0;
}
return Integer.parseInt(wonGames);
}

public int getLostGames(Player player) {
@Nullable String lostGames = jedis.get(nameSpace + "lostGames:" + player.getUuid());
if (lostGames == null) {
return 0;
}
return Integer.parseInt(lostGames);
}

public int getAllGames(Player player) {
return getWonGames(player) + getLostGames(player);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.endercube.Common.EndercubeMinigame;
import net.endercube.Common.EndercubeServer;
import net.endercube.Common.dimensions.FullbrightDimension;
import net.endercube.spleef.minigame.commands.StatsCommand;
import net.endercube.spleef.minigame.listeners.MinigamePlayerJoin;
import net.endercube.spleef.minigame.listeners.MinigamePlayerLeave;
import net.endercube.spleef.minigame.listeners.PlayerMove;
Expand All @@ -25,6 +26,7 @@ public class SpleefMinigame extends EndercubeMinigame {

public static SpleefMinigame spleefMinigame;
public Instance spleefHub;
public static SpleefDatabase database;
private final int minimumPlayersConfigValue = config.node("minimumPlayers").getInt();
private final int maximumPlayersConfigValue = config.node("maximumPlayers").getInt();

Expand All @@ -37,6 +39,13 @@ public SpleefMinigame(EndercubeServer endercubeServer) {
.addListener(new MinigamePlayerLeave())
.addListener(new MinigamePlayerJoin());

try {
database = this.createDatabase(SpleefDatabase.class);
} catch (Exception e) {
logger.error("Failed to create a spleef database");
throw new RuntimeException(e);
}

this.registerCommands();
}

Expand Down Expand Up @@ -118,6 +127,7 @@ protected ArrayList<InstanceContainer> initInstances() throws IOException {
protected Command initCommands(Command rootCommand) {
// TODO: implement map voting
// rootCommand.addSubcommand(new VoteCommand());
rootCommand.addSubcommand(new StatsCommand());
return rootCommand;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package net.endercube.spleef.minigame.commands;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.command.builder.Command;
import net.minestom.server.entity.Player;
import net.minestom.server.utils.MathUtils;

import static net.endercube.Common.utils.ComponentUtils.getTitle;
import static net.endercube.spleef.minigame.SpleefMinigame.database;

public class StatsCommand extends Command {
public StatsCommand() {
super("stats");

setDefaultExecutor(((commandSender, commandContext) -> {
Player player = (Player) commandSender;
commandSender.sendMessage(getTitle(Component.text("Spleef Stats"))
.append(
Component.text("Wins: ")
)
.append(
Component.text(database.getWonGames(player)).color(NamedTextColor.GRAY)
)
.append(Component.newline())
.append(
Component.text("Losses: ")
)
.append(
Component.text(database.getLostGames(player)).color(NamedTextColor.GRAY)
)
.append(Component.newline())
.append(
Component.text("Games: ")
)
.append(
Component.text(database.getAllGames(player)).color(NamedTextColor.GRAY)
)
.append(Component.newline())
.append(
Component.text("Win%: ")
)
.append(
Component.text(getWinPercent(player) + "%").color(NamedTextColor.GRAY)
)
);
}));
}

private float getWinPercent(Player player) {
// Stop divide by 0 errors
if (database.getAllGames(player) == 0) {
return 0;
}
return MathUtils.round(((float) database.getWonGames(player) / (float) database.getAllGames(player)) * 100, 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.minestom.server.command.builder.Command;
import net.minestom.server.utils.MathUtils;

import static net.endercube.Common.utils.ComponentUtils.getTitle;
import static net.endercube.Endercube.listeners.ServerTickMonitor.RAW_MSPT;

public class PerformanceCommand extends Command {
Expand All @@ -16,17 +16,7 @@ public PerformanceCommand() {
double tps = Math.min(20, 1000 / mspt);

setDefaultExecutor(((commandSender, commandContext) -> {
commandSender.sendMessage(Component.empty()
.append(
Component.text(" ").decorate(TextDecoration.STRIKETHROUGH).color(NamedTextColor.BLUE)
)
.append(
Component.text(" Server Performance ").color(NamedTextColor.BLUE)
)
.append(
Component.text(" ").decorate(TextDecoration.STRIKETHROUGH).color(NamedTextColor.BLUE)
)
.append(Component.newline())
commandSender.sendMessage(getTitle(Component.text("Server Stats"))
.append(
Component.text("MSPT: ").color(NamedTextColor.WHITE)
)
Expand Down

0 comments on commit 3be033d

Please sign in to comment.