Skip to content

Commit

Permalink
[AB-xxx] making mines command work with user installable commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheldan committed Jun 16, 2024
1 parent 47a7200 commit abe383b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
import dev.sheldan.abstracto.core.command.config.HelpInfo;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.config.UserCommandConfig;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
Expand All @@ -15,6 +16,7 @@
import dev.sheldan.abstracto.core.service.FeatureFlagService;
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.templating.service.TemplateService;
import dev.sheldan.abstracto.core.utils.ContextUtils;
import dev.sheldan.abstracto.core.utils.FutureUtils;
import dev.sheldan.abstracto.entertainment.config.EntertainmentFeatureDefinition;
import dev.sheldan.abstracto.entertainment.config.EntertainmentModuleDefinition;
Expand Down Expand Up @@ -81,33 +83,39 @@ public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEven
mines = slashCommandParameterService.getCommandOption(MINES_PARAMETER, event, Integer.class);
}
Integer credit = null;
long serverId = event.getGuild().getIdLong();
boolean economyEnabled = featureFlagService.getFeatureFlagValue(EntertainmentFeatureDefinition.ECONOMY, serverId);
if(economyEnabled){
credit = 50;
if(slashCommandParameterService.hasCommandOption(CREDITS_PARAMETER, event)) {
credit = slashCommandParameterService.getCommandOption(CREDITS_PARAMETER, event, Integer.class);
}

Optional<EconomyUser> userOptional = economyUserManagementService.getUser(ServerUser.fromMember(event.getMember()));
if(!userOptional.isPresent()) {
throw new NotEnoughCreditsException();
}
EconomyUser user = userOptional.get();
if(user.getCredits() < credit) {
throw new NotEnoughCreditsException();
Long serverId;
boolean economyEnabled = false;
if(ContextUtils.isGuildAware(event)) {
serverId = event.getGuild().getIdLong();
economyEnabled = featureFlagService.getFeatureFlagValue(EntertainmentFeatureDefinition.ECONOMY, serverId);
if(economyEnabled){
credit = 50;
if(slashCommandParameterService.hasCommandOption(CREDITS_PARAMETER, event)) {
credit = slashCommandParameterService.getCommandOption(CREDITS_PARAMETER, event, Integer.class);
}

Optional<EconomyUser> userOptional = economyUserManagementService.getUser(ServerUser.fromMember(event.getMember()));
if(!userOptional.isPresent()) {
throw new NotEnoughCreditsException();
}
EconomyUser user = userOptional.get();
if(user.getCredits() < credit) {
throw new NotEnoughCreditsException();
}
}
} else {
serverId = null;
}
MineBoard board = gameService.createBoard(width, height, mines, serverId);
board.setCreditsEnabled(economyEnabled);
board.setUserId(event.getMember().getIdLong());
board.setUserId(event.getUser().getIdLong());
board.setServerId(serverId);
board.setCredits(credit);
MessageToSend messageToSend = templateService.renderEmbedTemplate(MINE_BOARD_TEMPLATE_KEY, board);
return interactionService.replyMessageToSend(messageToSend, event)
.thenCompose(interactionHook -> interactionHook.retrieveOriginal().submit())
.thenApply(message -> {
gameService.persistMineBoardMessage(board, message);
gameService.persistMineBoardMessage(board, message, serverId);
return CommandResult.fromSuccess();
});
}
Expand All @@ -129,7 +137,7 @@ public CompletableFuture<CommandResult> executeAsync(CommandContext commandConte
mines = (Integer) parameters.get(2);
}
Integer credit = null;
long serverId = commandContext.getGuild().getIdLong();
Long serverId = commandContext.getGuild().getIdLong();
boolean economyEnabled = featureFlagService.getFeatureFlagValue(EntertainmentFeatureDefinition.ECONOMY, serverId);
if(economyEnabled){
credit = 50;
Expand All @@ -154,7 +162,7 @@ public CompletableFuture<CommandResult> executeAsync(CommandContext commandConte
MessageToSend messageToSend = templateService.renderEmbedTemplate(MINE_BOARD_TEMPLATE_KEY, board);
List<CompletableFuture<Message>> futures = channelService.sendMessageToSendToChannel(messageToSend, commandContext.getChannel());
return FutureUtils.toSingleFutureGeneric(futures)
.thenAccept(unused -> gameService.persistMineBoardMessage(board, futures.get(0).join()))
.thenAccept(unused -> gameService.persistMineBoardMessage(board, futures.get(0).join(), serverId))
.thenApply(unused -> CommandResult.fromSuccess());

}
Expand Down Expand Up @@ -207,6 +215,8 @@ public CommandConfiguration getConfiguration() {
SlashCommandConfig slashCommandConfig = SlashCommandConfig
.builder()
.enabled(true)
.userInstallable(true)
.userCommandConfig(UserCommandConfig.all())
.rootCommandName(EntertainmentSlashCommandNames.GAME)
.commandName(MINES_COMMAND_NAME)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import dev.sheldan.abstracto.core.service.FeatureFlagService;
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.templating.service.TemplateService;
import dev.sheldan.abstracto.core.utils.ContextUtils;
import dev.sheldan.abstracto.entertainment.command.games.Mines;
import dev.sheldan.abstracto.entertainment.config.EntertainmentFeatureDefinition;
import dev.sheldan.abstracto.entertainment.model.command.games.MineBoard;
Expand Down Expand Up @@ -49,8 +50,10 @@ public ButtonClickedListenerResult execute(ButtonClickedListenerModel model) {
GameService.MineResult mineResult = gameService.uncoverField(mineBoard, payload.getX(), payload.getY());
mineBoard.setState(mineResult);
if(mineBoard.getState() != GameService.MineResult.CONTINUE) {
if(featureFlagService.getFeatureFlagValue(EntertainmentFeatureDefinition.ECONOMY, model.getServerId())){
gameService.evaluateCreditChanges(mineBoard);
if(ContextUtils.isGuildAware(model.getEvent())) {
if(featureFlagService.getFeatureFlagValue(EntertainmentFeatureDefinition.ECONOMY, model.getServerId())){
gameService.evaluateCreditChanges(mineBoard);
}
}
gameService.uncoverBoard(mineBoard);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,16 @@ public MineBoard createBoard(Integer width, Integer height, Integer mines, Long

@Override
@Transactional
public void persistMineBoardMessage(MineBoard mineBoard, Message message) {
public void persistMineBoardMessage(MineBoard mineBoard, Message message, Long serverId) {
mineBoard.setMessageId(message.getIdLong());
mineBoard.setChannelId(message.getChannel().getIdLong());
AServer server = serverManagementService.loadServer(message.getGuild());

AServer server;
if(serverId != null) {
server = serverManagementService.loadServer(serverId);
} else {
server = null;
}
mineBoard.getFields().forEach(mineBoardField -> {
MineBoardPayload payload = MineBoardPayload
.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

public interface GameService {
MineBoard createBoard(Integer width, Integer height, Integer mines, Long serverId);
void persistMineBoardMessage(MineBoard mineBoard, Message message);
void persistMineBoardMessage(MineBoard mineBoard, Message message, Long serverId);
void updateMineBoard(MineBoard mineBoard);
void uncoverBoard(MineBoard mineBoard);
void evaluateCreditChanges(MineBoard mineBoard);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import dev.sheldan.abstracto.core.service.FeatureModeService;
import dev.sheldan.abstracto.core.interaction.ComponentPayloadManagementService;
import dev.sheldan.abstracto.core.utils.BeanUtils;
import dev.sheldan.abstracto.core.utils.ContextUtils;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
Expand Down Expand Up @@ -124,6 +125,9 @@ private List<ButtonClickedListener> filterFeatureAwareListener(List<ButtonClicke
if(!model.getEvent().isFromGuild()) {
return true;
}
if(ContextUtils.isNotGuildAware(model.getEvent())) {
return true;
}
if (!featureFlagService.isFeatureEnabled(feature, model.getServerId())) {
return false;
}
Expand Down

0 comments on commit abe383b

Please sign in to comment.