[AB-66] adding min mines ratio and other input restrictions for mines game which should avoid some too easy setups

This commit is contained in:
Sheldan
2022-12-02 21:16:32 +01:00
parent 4e1db26df7
commit 9ddd386c6f
8 changed files with 48 additions and 12 deletions

View File

@@ -81,7 +81,8 @@ public class Mines extends AbstractConditionableCommand {
mines = slashCommandParameterService.getCommandOption(MINES_PARAMETER, event, Integer.class);
}
Integer credit = null;
boolean economyEnabled = featureFlagService.getFeatureFlagValue(EntertainmentFeatureDefinition.ECONOMY, event.getGuild().getIdLong());
long serverId = event.getGuild().getIdLong();
boolean economyEnabled = featureFlagService.getFeatureFlagValue(EntertainmentFeatureDefinition.ECONOMY, serverId);
if(economyEnabled){
credit = 50;
if(slashCommandParameterService.hasCommandOption(CREDITS_PARAMETER, event)) {
@@ -97,10 +98,10 @@ public class Mines extends AbstractConditionableCommand {
throw new NotEnoughCreditsException();
}
}
MineBoard board = gameService.createBoard(width, height, mines);
MineBoard board = gameService.createBoard(width, height, mines, serverId);
board.setCreditsEnabled(economyEnabled);
board.setUserId(event.getMember().getIdLong());
board.setServerId(event.getGuild().getIdLong());
board.setServerId(serverId);
board.setCredits(credit);
MessageToSend messageToSend = templateService.renderEmbedTemplate(MINE_BOARD_TEMPLATE_KEY, board);
return interactionService.replyMessageToSend(messageToSend, event)
@@ -128,7 +129,8 @@ public class Mines extends AbstractConditionableCommand {
mines = (Integer) parameters.get(2);
}
Integer credit = null;
boolean economyEnabled = featureFlagService.getFeatureFlagValue(EntertainmentFeatureDefinition.ECONOMY, commandContext.getGuild().getIdLong());
long serverId = commandContext.getGuild().getIdLong();
boolean economyEnabled = featureFlagService.getFeatureFlagValue(EntertainmentFeatureDefinition.ECONOMY, serverId);
if(economyEnabled){
credit = 50;
if(parameters.size() == 4) {
@@ -144,10 +146,10 @@ public class Mines extends AbstractConditionableCommand {
throw new NotEnoughCreditsException();
}
}
MineBoard board = gameService.createBoard(width, height, mines);
MineBoard board = gameService.createBoard(width, height, mines, serverId);
board.setCreditsEnabled(economyEnabled);
board.setUserId(commandContext.getAuthor().getIdLong());
board.setServerId(commandContext.getGuild().getIdLong());
board.setServerId(serverId);
board.setCredits(credit);
MessageToSend messageToSend = templateService.renderEmbedTemplate(MINE_BOARD_TEMPLATE_KEY, board);
List<CompletableFuture<Message>> futures = channelService.sendMessageToSendToChannel(messageToSend, commandContext.getChannel());

View File

@@ -39,6 +39,9 @@ public class MinesButtonClickedListener implements ButtonClickedListener {
@Override
public ButtonClickedListenerResult execute(ButtonClickedListenerModel model) {
MineBoardPayload payload = (MineBoardPayload) model.getDeserializedPayload();
if(model.getEvent().getUser().getIdLong() != payload.getMineBoard().getUserId()) {
return ButtonClickedListenerResult.IGNORED;
}
MineBoard mineBoard = payload.getMineBoard();
if(!mineBoard.getState().equals(GameService.MineResult.CONTINUE)) {
return ButtonClickedListenerResult.IGNORED;

View File

@@ -23,6 +23,7 @@ import java.security.SecureRandom;
import java.util.*;
import java.util.stream.Collectors;
import static dev.sheldan.abstracto.entertainment.config.EconomyFeatureConfig.MINES_MINIMUM_MINES_RATIO;
import static dev.sheldan.abstracto.entertainment.config.GamesFeatureConfig.MINES_CREDITS_FACTOR;
@Component
@@ -51,9 +52,13 @@ public class GameServiceBean implements GameService {
private ConfigService configService;
@Override
public MineBoard createBoard(Integer width, Integer height, Integer mines) {
if(mines >= width * height || width > 5 || height > 5 || mines <= 0 || height == 0 || width == 0) {
throw new InvalidGameBoardException();
public MineBoard createBoard(Integer width, Integer height, Integer mines, Long serverId) {
double minMinesRatio = configService.getDoubleValueOrConfigDefault(MINES_MINIMUM_MINES_RATIO, serverId);
if(mines >= width * height || width > 5 || height > 5 || mines <= 1 || height <= 1 || width <= 1) {
throw new InvalidGameBoardException(minMinesRatio);
}
if((double) mines / (width * height) < minMinesRatio) {
throw new InvalidGameBoardException(minMinesRatio);
}
MineBoard mineBoard = generateEmptyBoard(width, height);
mineBoard.setMineCount(mines);

View File

@@ -26,6 +26,9 @@ abstracto.featureFlags.games.enabled=false
abstracto.systemConfigs.minesCreditsFactor.name=minesCreditsFactor
abstracto.systemConfigs.minesCreditsFactor.doubleValue=5
abstracto.systemConfigs.minesMinMineRatio.name=minesMinMineRatio
abstracto.systemConfigs.minesMinMineRatio.doubleValue=0.2
# for now this is fine
abstracto.systemConfigs.creditGambleJackpot.name=creditGambleJackpot
abstracto.systemConfigs.creditGambleJackpot.longValue=1000

View File

@@ -13,6 +13,7 @@ public class EconomyFeatureConfig implements FeatureConfig {
public static final String PAYDAY_CREDITS_CONFIG_KEY = "paydayCredits";
public static final String PAYDAY_COOLDOWN_CONFIG_KEY = "paydayCooldown";
public static final String SLOTS_COOLDOWN_CONFIG_KEY = "slotsCooldown";
public static final String MINES_MINIMUM_MINES_RATIO = "minesMinMineRatio";
@Override
public FeatureDefinition getFeature() {
@@ -21,6 +22,6 @@ public class EconomyFeatureConfig implements FeatureConfig {
@Override
public List<String> getRequiredSystemConfigKeys() {
return Arrays.asList(PAYDAY_CREDITS_CONFIG_KEY, PAYDAY_COOLDOWN_CONFIG_KEY, SLOTS_COOLDOWN_CONFIG_KEY);
return Arrays.asList(PAYDAY_CREDITS_CONFIG_KEY, PAYDAY_COOLDOWN_CONFIG_KEY, SLOTS_COOLDOWN_CONFIG_KEY, MINES_MINIMUM_MINES_RATIO);
}
}

View File

@@ -1,8 +1,20 @@
package dev.sheldan.abstracto.entertainment.exception;
import dev.sheldan.abstracto.core.exception.AbstractoTemplatableException;
import dev.sheldan.abstracto.entertainment.model.exception.InvalidGameBoardExceptionModel;
public class InvalidGameBoardException extends AbstractoTemplatableException {
private final InvalidGameBoardExceptionModel model;
public InvalidGameBoardException(Double minRatio) {
super();
this.model = InvalidGameBoardExceptionModel
.builder()
.minMinesRatio(minRatio)
.build();
}
@Override
public String getTemplateName() {
return "invalid_mine_board_config_exception";
@@ -10,6 +22,6 @@ public class InvalidGameBoardException extends AbstractoTemplatableException {
@Override
public Object getTemplateModel() {
return new Object();
return model;
}
}

View File

@@ -0,0 +1,10 @@
package dev.sheldan.abstracto.entertainment.model.exception;
import lombok.Builder;
import lombok.Getter;
@Getter
@Builder
public class InvalidGameBoardExceptionModel {
private Double minMinesRatio;
}

View File

@@ -4,7 +4,7 @@ import dev.sheldan.abstracto.entertainment.model.command.games.MineBoard;
import net.dv8tion.jda.api.entities.Message;
public interface GameService {
MineBoard createBoard(Integer width, Integer height, Integer mines);
MineBoard createBoard(Integer width, Integer height, Integer mines, Long serverId);
void persistMineBoardMessage(MineBoard mineBoard, Message message);
void updateMineBoard(MineBoard mineBoard);
void uncoverBoard(MineBoard mineBoard);