-
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 #41 from Ender-Cube/spleef-database
Add a spleef database
- Loading branch information
Showing
6 changed files
with
145 additions
and
14 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
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
46 changes: 46 additions & 0 deletions
46
Spleef/src/main/java/net/endercube/spleef/minigame/SpleefDatabase.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,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); | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
Spleef/src/main/java/net/endercube/spleef/minigame/commands/StatsCommand.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,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); | ||
} | ||
} |
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