[AB-63] adding fun and entertainment related commands commands: roll, roulette, lovecalc, choose, 8ball

This commit is contained in:
Sheldan
2020-12-13 20:42:41 +01:00
parent 325264a325
commit 69aa82e26e
30 changed files with 1070 additions and 6 deletions

View File

@@ -0,0 +1,65 @@
package dev.sheldan.abstracto.utility.commands.entertainment;
import dev.sheldan.abstracto.core.command.UtilityModuleInterface;
import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand;
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.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.command.execution.ContextConverter;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.utils.FutureUtils;
import dev.sheldan.abstracto.utility.config.features.UtilityFeature;
import dev.sheldan.abstracto.utility.models.template.commands.ChooseResponseModel;
import dev.sheldan.abstracto.utility.service.EntertainmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@Component
public class Choose extends AbstractConditionableCommand {
public static final String CHOOSE_RESPONSE_TEMPLATE_KEY = "choose_response";
@Autowired
private EntertainmentService entertainmentService;
@Autowired
private ChannelService channelService;
@Override
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
List<String> choices = (List) commandContext.getParameters().getParameters().get(0);
String choice = entertainmentService.takeChoice(choices, commandContext.getAuthor());
ChooseResponseModel responseModel = (ChooseResponseModel) ContextConverter.slimFromCommandContext(commandContext, ChooseResponseModel.class);
responseModel.setChosenValue(choice);
return FutureUtils.toSingleFutureGeneric(channelService.sendEmbedTemplateInChannel(CHOOSE_RESPONSE_TEMPLATE_KEY, responseModel, commandContext.getChannel()))
.thenApply(unused -> CommandResult.fromIgnored());
}
@Override
public CommandConfiguration getConfiguration() {
List<Parameter> parameters = new ArrayList<>();
parameters.add(Parameter.builder().name("text").type(String.class).templated(true).remainder(true).isListParam(true).build());
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
return CommandConfiguration.builder()
.name("choose")
.async(true)
.module(UtilityModuleInterface.UTILITY)
.templated(true)
.supportsEmbedException(true)
.causesReaction(true)
.parameters(parameters)
.help(helpInfo)
.build();
}
@Override
public FeatureEnum getFeature() {
return UtilityFeature.UTILITY;
}
}

View File

@@ -0,0 +1,69 @@
package dev.sheldan.abstracto.utility.commands.entertainment;
import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand;
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.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.command.execution.ContextConverter;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.utils.FutureUtils;
import dev.sheldan.abstracto.templating.service.TemplateService;
import dev.sheldan.abstracto.utility.config.EntertainmentModuleInterface;
import dev.sheldan.abstracto.utility.config.features.UtilityFeature;
import dev.sheldan.abstracto.utility.models.template.commands.EightBallResponseModel;
import dev.sheldan.abstracto.utility.service.EntertainmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@Component
public class EightBall extends AbstractConditionableCommand {
public static final String EIGHT_BALL_RESPONSE_TEMPLATE_KEY = "eight_ball_response";
@Autowired
private EntertainmentService entertainmentService;
@Autowired
private TemplateService templateService;
@Autowired
private ChannelService channelService;
@Override
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
String text = (String) commandContext.getParameters().getParameters().get(0);
String chosenKey = entertainmentService.getEightBallValue(text);
EightBallResponseModel responseModel = (EightBallResponseModel) ContextConverter.slimFromCommandContext(commandContext, EightBallResponseModel.class);
responseModel.setChosenKey(chosenKey);
return FutureUtils.toSingleFutureGeneric(channelService.sendEmbedTemplateInChannel(EIGHT_BALL_RESPONSE_TEMPLATE_KEY, responseModel, commandContext.getChannel()))
.thenApply(unused -> CommandResult.fromIgnored());
}
@Override
public CommandConfiguration getConfiguration() {
List<Parameter> parameters = new ArrayList<>();
parameters.add(Parameter.builder().name("text").type(String.class).templated(true).remainder(true).build());
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
return CommandConfiguration.builder()
.name("8Ball")
.async(true)
.module(EntertainmentModuleInterface.ENTERTAINMENT)
.templated(true)
.supportsEmbedException(true)
.causesReaction(true)
.parameters(parameters)
.help(helpInfo)
.build();
}
@Override
public FeatureEnum getFeature() {
return UtilityFeature.ENTERTAINMENT;
}
}

View File

@@ -0,0 +1,71 @@
package dev.sheldan.abstracto.utility.commands.entertainment;
import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand;
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.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.command.execution.ContextConverter;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.utils.FutureUtils;
import dev.sheldan.abstracto.utility.config.EntertainmentModuleInterface;
import dev.sheldan.abstracto.utility.config.features.UtilityFeature;
import dev.sheldan.abstracto.utility.models.template.commands.LoveCalcResponseModel;
import dev.sheldan.abstracto.utility.service.EntertainmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@Component
public class LoveCalc extends AbstractConditionableCommand {
public static final String LOVE_CALC_RESPONSE_TEMPLATE_KEY = "loveCalc_response";
@Autowired
private ChannelService channelService;
@Autowired
private EntertainmentService entertainmentService;
@Override
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
List<Object> parameters = commandContext.getParameters().getParameters();
String firstPart = (String) parameters.get(0);
String secondPart = (String) parameters.get(1);
Integer rolled = entertainmentService.getLoveCalcValue(firstPart, secondPart);
LoveCalcResponseModel model = (LoveCalcResponseModel) ContextConverter.slimFromCommandContext(commandContext, LoveCalcResponseModel.class);
model.setRolled(rolled);
model.setFirstPart(firstPart);
model.setSecondPart(secondPart);
return FutureUtils.toSingleFutureGeneric(channelService.sendEmbedTemplateInChannel(LOVE_CALC_RESPONSE_TEMPLATE_KEY, model, commandContext.getChannel()))
.thenApply(unused -> CommandResult.fromIgnored());
}
@Override
public CommandConfiguration getConfiguration() {
List<Parameter> parameters = new ArrayList<>();
parameters.add(Parameter.builder().name("firstSubject").type(String.class).templated(true).optional(true).build());
parameters.add(Parameter.builder().name("secondSubject").type(String.class).templated(true).optional(true).build());
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
return CommandConfiguration.builder()
.name("loveCalc")
.async(true)
.module(EntertainmentModuleInterface.ENTERTAINMENT)
.templated(true)
.supportsEmbedException(true)
.causesReaction(true)
.parameters(parameters)
.help(helpInfo)
.build();
}
@Override
public FeatureEnum getFeature() {
return UtilityFeature.ENTERTAINMENT;
}
}

View File

@@ -0,0 +1,82 @@
package dev.sheldan.abstracto.utility.commands.entertainment;
import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand;
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.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.command.execution.ContextConverter;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.ConfigService;
import dev.sheldan.abstracto.core.utils.FutureUtils;
import dev.sheldan.abstracto.utility.config.EntertainmentModuleInterface;
import dev.sheldan.abstracto.utility.config.features.UtilityFeature;
import dev.sheldan.abstracto.utility.models.template.commands.RollResponseModel;
import dev.sheldan.abstracto.utility.service.EntertainmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import static dev.sheldan.abstracto.utility.config.features.EntertainmentFeature.ROLL_DEFAULT_HIGH_KEY;
@Component
public class Roll extends AbstractConditionableCommand {
public static final String ROLL_RESPONSE_TEMPLATE_KEY = "roll_response";
@Autowired
private ChannelService channelService;
@Autowired
private EntertainmentService entertainmentService;
@Autowired
private ConfigService configService;
@Override
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
List<Object> parameters = commandContext.getParameters().getParameters();
Integer high = configService.getLongValue(ROLL_DEFAULT_HIGH_KEY, commandContext.getGuild().getIdLong()).intValue();
Integer low = 1;
if(parameters.size() > 1) {
low = (Integer) parameters.get(1);
}
if(!parameters.isEmpty()) {
high = (Integer) parameters.get(0);
}
Integer rolled = entertainmentService.calculateRollResult(low, high);
RollResponseModel model = (RollResponseModel) ContextConverter.slimFromCommandContext(commandContext, RollResponseModel.class);
model.setRolled(rolled);
return FutureUtils.toSingleFutureGeneric(channelService.sendEmbedTemplateInChannel(ROLL_RESPONSE_TEMPLATE_KEY, model, commandContext.getChannel()))
.thenApply(unused -> CommandResult.fromIgnored());
}
@Override
public CommandConfiguration getConfiguration() {
List<Parameter> parameters = new ArrayList<>();
parameters.add(Parameter.builder().name("high").type(Integer.class).templated(true).optional(true).build());
parameters.add(Parameter.builder().name("low").type(Integer.class).templated(true).optional(true).build());
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
return CommandConfiguration.builder()
.name("roll")
.async(true)
.module(EntertainmentModuleInterface.ENTERTAINMENT)
.templated(true)
.supportsEmbedException(true)
.causesReaction(true)
.parameters(parameters)
.help(helpInfo)
.build();
}
@Override
public FeatureEnum getFeature() {
return UtilityFeature.ENTERTAINMENT;
}
}

View File

@@ -0,0 +1,63 @@
package dev.sheldan.abstracto.utility.commands.entertainment;
import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand;
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.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.command.execution.ContextConverter;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.utils.FutureUtils;
import dev.sheldan.abstracto.utility.config.EntertainmentModuleInterface;
import dev.sheldan.abstracto.utility.config.features.UtilityFeature;
import dev.sheldan.abstracto.utility.models.template.commands.RouletteResponseModel;
import dev.sheldan.abstracto.utility.service.EntertainmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@Component
public class Roulette extends AbstractConditionableCommand {
public static final String ROULETTE_RESPONSE_TEMPLATE_KEY = "roulette_response";
@Autowired
private ChannelService channelService;
@Autowired
private EntertainmentService entertainmentService;
@Override
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
boolean rouletteResult = entertainmentService.executeRoulette(commandContext.getAuthor());
RouletteResponseModel responseModel = (RouletteResponseModel) ContextConverter.slimFromCommandContext(commandContext, RouletteResponseModel.class);
responseModel.setResult(rouletteResult);
return FutureUtils.toSingleFutureGeneric(channelService.sendEmbedTemplateInChannel(ROULETTE_RESPONSE_TEMPLATE_KEY, responseModel, commandContext.getChannel()))
.thenApply(unused -> CommandResult.fromIgnored());
}
@Override
public CommandConfiguration getConfiguration() {
List<Parameter> parameters = new ArrayList<>();
HelpInfo helpInfo = HelpInfo.builder().templated(true).build();
return CommandConfiguration.builder()
.name("roulette")
.async(true)
.module(EntertainmentModuleInterface.ENTERTAINMENT)
.templated(true)
.supportsEmbedException(true)
.causesReaction(true)
.parameters(parameters)
.help(helpInfo)
.build();
}
@Override
public FeatureEnum getFeature() {
return UtilityFeature.ENTERTAINMENT;
}
}

View File

@@ -0,0 +1,58 @@
package dev.sheldan.abstracto.utility.service;
import dev.sheldan.abstracto.core.service.ConfigService;
import net.dv8tion.jda.api.entities.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.List;
import static dev.sheldan.abstracto.utility.config.features.EntertainmentFeature.ROULETTE_BULLETS_CONFIG_KEY;
@Component
public class EntertainmentServiceBean implements EntertainmentService {
public static final List<String> EIGHT_BALL_ANSWER_KEYS = Arrays.asList(
"IS_CERTAIN", "IS_DECIDEDLY", "WITHOUT_DOUBT", "DEFINITELY_SO", "MAY_RELY", // certain
"SEE_IT", "MOST_LIKELY", "OUTLOOK", "YES", "POINT_YES", // certain
"HAZY", "ASK_AGAIN", "NOT_TELL", "CANNOT_PREDICT", "CONCENTRATE", // uncertain
"DONT_COUNT", "REPLY_NO", "SOURCES_NO", "OUTLOOK_NOT_GOOD", "DOUBTFUL" // negative
);
@Autowired
private SecureRandom secureRandom;
@Autowired
private ConfigService configService;
@Override
public String getEightBallValue(String text) {
return EIGHT_BALL_ANSWER_KEYS.get(secureRandom.nextInt(EIGHT_BALL_ANSWER_KEYS.size()));
}
@Override
public Integer getLoveCalcValue(String firstPart, String secondPart) {
return secureRandom.nextInt(100);
}
@Override
public Integer calculateRollResult(Integer low, Integer high) {
int actualLow = Math.min(low, high);
int actualHigh = Math.max(low, high);
return actualLow + secureRandom.nextInt(actualHigh - actualLow);
}
@Override
public boolean executeRoulette(Member memberExecuting) {
Long possibilities = configService.getLongValue(ROULETTE_BULLETS_CONFIG_KEY, memberExecuting.getGuild().getIdLong());
// 1/possibilities of chance, we don't have a state, each time its reset
return secureRandom.nextInt(possibilities.intValue()) == 0;
}
@Override
public String takeChoice(List<String> choices, Member memberExecuting) {
return choices.get(secureRandom.nextInt(choices.size()));
}
}

View File

@@ -8,11 +8,13 @@
http://www.liquibase.org/xml/ns/pro ../../dbchangelog-3.8.xsd" >
<property name="utilityModule" value="(SELECT id FROM module WHERE name = 'utility')"/>
<property name="repostDetectionModule" value="(SELECT id FROM module WHERE name = 'repostDetection')"/>
<property name="entertainmentModule" value="(SELECT id FROM module WHERE name = 'entertainment')"/>
<property name="remindFeature" value="(SELECT id FROM feature WHERE key = 'remind')"/>
<property name="starboardFeature" value="(SELECT id FROM feature WHERE key = 'starboard')"/>
<property name="suggestionFeature" value="(SELECT id FROM feature WHERE key = 'suggestion')"/>
<property name="utilityFeature" value="(SELECT id FROM feature WHERE key = 'utility')"/>
<property name="repostDetectionFeature" value="(SELECT id FROM feature WHERE key = 'repostDetection')"/>
<property name="entertainmentFeature" value="(SELECT id FROM feature WHERE key = 'entertainment')"/>
<property name="today" value="(SELECT NOW())"/>
<changeSet author="Sheldan" id="utility_remind-commands">
@@ -128,4 +130,31 @@
</insert>
</changeSet>
<changeSet author="Sheldan" id="utility_entertainment-commands">
<insert tableName="command">
<column name="name" value="8Ball"/>
<column name="module_id" valueComputed="${entertainmentModule}"/>
<column name="feature_id" valueComputed="${entertainmentFeature}"/>
<column name="created" valueComputed="${today}"/>
</insert>
<insert tableName="command">
<column name="name" value="loveCalc"/>
<column name="module_id" valueComputed="${entertainmentModule}"/>
<column name="feature_id" valueComputed="${entertainmentFeature}"/>
<column name="created" valueComputed="${today}"/>
</insert>
<insert tableName="command">
<column name="name" value="roll"/>
<column name="module_id" valueComputed="${entertainmentModule}"/>
<column name="feature_id" valueComputed="${entertainmentFeature}"/>
<column name="created" valueComputed="${today}"/>
</insert>
<insert tableName="command">
<column name="name" value="roulette"/>
<column name="module_id" valueComputed="${entertainmentModule}"/>
<column name="feature_id" valueComputed="${entertainmentFeature}"/>
<column name="created" valueComputed="${today}"/>
</insert>
</changeSet>
</databaseChangeLog>

View File

@@ -34,5 +34,17 @@
<column name="created" valueComputed="${today}"/>
</insert>
</changeSet>
<changeSet author="Sheldan" id="entertainment_default_config-insert">
<insert tableName="default_config">
<column name="name" value="rouletteBullets"/>
<column name="long_value" value="6"/>
<column name="created" valueComputed="${today}"/>
</insert>
<insert tableName="default_config">
<column name="name" value="rollDefaultHigh"/>
<column name="long_value" value="6"/>
<column name="created" valueComputed="${today}"/>
</insert>
</changeSet>
</databaseChangeLog>

View File

@@ -12,6 +12,7 @@
<property name="utilityFeature" value="(SELECT id FROM feature WHERE key = 'utility')"/>
<property name="linkEmbedFeature" value="(SELECT id FROM feature WHERE key = 'link_embeds')"/>
<property name="repostDetectionFeature" value="(SELECT id FROM feature WHERE key = 'repostDetection')"/>
<property name="entertainmentFeature" value="(SELECT id FROM feature WHERE key = 'entertainment')"/>
<property name="today" value="(SELECT NOW())"/>
<changeSet author="Sheldan" id="utility_default_feature_flag-insertion">
<insert tableName="default_feature_flag">
@@ -45,4 +46,11 @@
<column name="created" valueComputed="${today}"/>
</insert>
</changeSet>
<changeSet author="Sheldan" id="utility_entertainment_default_feature_flag-insertion">
<insert tableName="default_feature_flag">
<column name="enabled" value="false"/>
<column name="feature_id" valueComputed="${entertainmentFeature}" />
<column name="created" valueComputed="${today}"/>
</insert>
</changeSet>
</databaseChangeLog>

View File

@@ -32,5 +32,9 @@
<column name="key" value="repostDetection"/>
<column name="created" valueComputed="${today}"/>
</insert>
<insert tableName="feature">
<column name="key" value="entertainment"/>
<column name="created" valueComputed="${today}"/>
</insert>
</changeSet>
</databaseChangeLog>

View File

@@ -13,4 +13,10 @@
<column name="created" valueComputed="${today}"/>
</insert>
</changeSet>
<changeSet author="Sheldan" id="entertainment-module-insertion">
<insert tableName="module">
<column name="name" value="entertainment"/>
<column name="created" valueComputed="${today}"/>
</insert>
</changeSet>
</databaseChangeLog>