feature: conditions calling base

This commit is contained in:
Ilia Kuznetsov 2022-09-17 19:45:31 +02:00
parent 2fbae5399d
commit d2c644ee07
13 changed files with 374 additions and 18 deletions

View File

@ -1,3 +1,5 @@
import emu.grasscutter.gen.GenerateActivityConditions
/*
* This file was generated by the Gradle 'init' task.
*
@ -277,3 +279,5 @@ public final class BuildConfig {
processResources {
dependsOn "generateProto"
}
task generateActivityConditions(type: GenerateActivityConditions)

View File

@ -0,0 +1,82 @@
package emu.grasscutter.gen;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.options.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static java.lang.System.lineSeparator;
import static java.nio.file.Files.readAllLines;
import static java.nio.file.Files.writeString;
/**
* Task that can be used for generating/updating activity conditions enum. These
* activities come from Resources/ExcelBinOutput/NewActivityCondExcelConfigData.json
* resource file. Format file with formatter after this job is executed
* <br />
* Usage example: <i>./gradlew generateActivityConditions --conf-file=/Users/xxx/IdeaProjects/Grasscutter_Resources/Resources/ExcelBinOutput/NewActivityCondExcelConfigData.json</i>
*/
public class GenerateActivityConditions extends DefaultTask {
private static final Logger log = LoggerFactory.getLogger(GenerateActivityConditions.class);
private static final String ACTIVITY_CONDITIONS_SRC = "/src/main/java/emu/grasscutter/game/activity/condition/ActivityConditions.java";
private static final String activityClassStart = """
package emu.grasscutter.game.activity;
public enum ActivityConditions {
""";
@Option(option = "conf-file", description = "Path to NewActivityCondExcelConfigData.json")
String confFile;
@SuppressWarnings("unused") //Used by Gradle
public void setConfFile(String confFile) {
this.confFile = confFile;
}
@TaskAction
void run() {
List<String> configFileContent = getFileContent(confFile);
Set<String> configEnums = configFileContent.stream()
.filter(s -> s.contains("\"type\":"))
.map(s -> s.split("\"")[3])
.map(s -> " " + s)
.collect(Collectors.toSet());
String finalActivityClass =
activityClassStart +
String.join("," + lineSeparator(), configEnums) + lineSeparator() + "}";
writeFile(finalActivityClass, Path.of(getProject().getProjectDir() + ACTIVITY_CONDITIONS_SRC));
log.info("Successfully added {} enums to {}", configEnums.size(), ACTIVITY_CONDITIONS_SRC);
}
private List<String> getFileContent(String path) {
try {
return readAllLines(Path.of(confFile));
} catch (IOException e) {
log.error("Cannot read file: {}", path);
throw new RuntimeException(e);
}
}
private void writeFile(String content, Path path) {
try {
writeString(path, content, StandardCharsets.UTF_8);
} catch (IOException e) {
log.error("Cannot read file: {}", path);
throw new RuntimeException(e);
}
}
}

View File

@ -55,6 +55,8 @@ public class GameData {
private static final Int2ObjectMap<WeaponCurveData> weaponCurveDataMap = new Int2ObjectOpenHashMap<>();
private static final Int2ObjectMap<EquipAffixData> equipAffixDataMap = new Int2ObjectOpenHashMap<>();
private static final Int2ObjectMap<ActivityCondExcelConfigData> activityCondExcelConfigDataMap = new Int2ObjectOpenHashMap<>();
private static final Int2ObjectMap<EnvAnimalGatherConfigData> envAnimalGatherConfigDataMap = new Int2ObjectOpenHashMap<>();
private static final Int2ObjectMap<MonsterData> monsterDataMap = new Int2ObjectOpenHashMap<>();
private static final Int2ObjectMap<NpcData> npcDataMap = new Int2ObjectOpenHashMap<>();
@ -333,6 +335,10 @@ public class GameData {
return avatarCostumeDataMap;
}
public static Int2ObjectMap<ActivityCondExcelConfigData> getActivityCondExcelConfigDataMap() {
return activityCondExcelConfigDataMap;
}
public static Int2ObjectMap<AvatarCostumeData> getAvatarCostumeDataItemIdMap() {
return avatarCostumeDataItemIdMap;
}
@ -473,4 +479,6 @@ public class GameData {
public static Int2ObjectMap<BuffData> getBuffDataMap() {
return buffDataMap;
}
}

View File

@ -0,0 +1,41 @@
package emu.grasscutter.data.excels;
import emu.grasscutter.data.GameResource;
import emu.grasscutter.data.ResourceType;
import emu.grasscutter.game.activity.condition.ActivityConditions;
import emu.grasscutter.game.quest.enums.LogicType;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.experimental.FieldDefaults;
import java.util.List;
@ResourceType(name = "NewActivityCondExcelConfigData.json")
@Getter
@FieldDefaults(level = AccessLevel.PRIVATE)
public class ActivityCondExcelConfigData extends GameResource {
int condId;
LogicType condComb;
List<ActivityConfigCondition> cond;
public static class ActivityConfigCondition {
@Getter
private ActivityConditions type;
@Getter
private List<Integer> param;
public int[] paramArray() {
return param.stream().mapToInt(Integer::intValue).toArray();
}
}
@Override
public int getId() {
return condId;
}
@Override
public void onLoad() {
cond.removeIf(c -> c.type == null);
}
}

View File

@ -4,23 +4,40 @@ import com.esotericsoftware.reflectasm.ConstructorAccess;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.DataLoader;
import emu.grasscutter.data.GameData;
import emu.grasscutter.data.excels.ActivityCondExcelConfigData;
import emu.grasscutter.data.excels.QuestData;
import emu.grasscutter.game.activity.condition.ActivityConditions;
import emu.grasscutter.game.activity.condition.AllActivityConditionBuilder;
import emu.grasscutter.game.activity.condition.ActivityConditionBaseHandler;
import emu.grasscutter.game.activity.condition.all.UnknownActivityConditionHandler;
import emu.grasscutter.game.player.BasePlayerManager;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.game.props.ActivityType;
import emu.grasscutter.game.props.WatcherTriggerType;
import emu.grasscutter.game.quest.GameQuest;
import emu.grasscutter.game.quest.enums.LogicType;
import emu.grasscutter.net.proto.ActivityInfoOuterClass;
import emu.grasscutter.server.packet.send.PacketActivityScheduleInfoNotify;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import lombok.Getter;
import org.reflections.Reflections;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BooleanSupplier;
import java.util.stream.Collectors;
import static emu.grasscutter.Grasscutter.getLogger;
@Getter
public class ActivityManager extends BasePlayerManager {
private static final Map<Integer, ActivityConfigItem> activityConfigItemMap;
@Getter private static final Map<Integer, ActivityConfigItem> scheduleActivityConfigMap;
@Getter
private static final Map<Integer, ActivityConfigItem> scheduleActivityConfigMap;
private final Map<Integer, PlayerActivityData> playerActivityDataMap;
private final Int2ObjectMap<ActivityCondExcelConfigData> activityConditions;
private final Map<ActivityConditions, ActivityConditionBaseHandler> activityConditionsHandlers;
private static final UnknownActivityConditionHandler UNKNOWN_CONDITION_HANDLER = new UnknownActivityConditionHandler();
static {
activityConfigItemMap = new HashMap<>();
@ -47,7 +64,7 @@ public class ActivityManager extends BasePlayerManager {
DataLoader.loadList("ActivityConfig.json", ActivityConfigItem.class).forEach(item -> {
var activityData = GameData.getActivityDataMap().get(item.getActivityId());
if (activityData == null) {
Grasscutter.getLogger().warn("activity {} not exist.", item.getActivityId());
getLogger().warn("activity {} not exist.", item.getActivityId());
return;
}
var activityHandlerType = activityHandlerTypeMap.get(ActivityType.getTypeByName(activityData.getActivityType()));
@ -55,7 +72,7 @@ public class ActivityManager extends BasePlayerManager {
if (activityHandlerType != null) {
activityHandler = (ActivityHandler) activityHandlerType.newInstance();
}else {
} else {
activityHandler = new DefaultActivityHandler();
}
activityHandler.setActivityConfigItem(item);
@ -68,7 +85,7 @@ public class ActivityManager extends BasePlayerManager {
Grasscutter.getLogger().info("Enable {} activities.", activityConfigItemMap.size());
} catch (Exception e) {
Grasscutter.getLogger().error("Unable to load activities config.", e);
getLogger().error("Unable to load activities config.", e);
}
}
@ -90,6 +107,9 @@ public class ActivityManager extends BasePlayerManager {
});
player.sendPacket(new PacketActivityScheduleInfoNotify(activityConfigItemMap.values()));
activityConditions = GameData.getActivityCondExcelConfigDataMap();
activityConditionsHandlers = AllActivityConditionBuilder.buildActivityConditions();
}
/**
@ -127,15 +147,23 @@ public class ActivityManager extends BasePlayerManager {
return new Date().after(activityConfig.getEndTime());
}
public boolean meetsCondition(int conditionId) {
var activityId = conditionId / 1000; // todo some only have 2 numbers for the condition
var activityConfig = activityConfigItemMap.get(activityId);
if(activityConfig==null){
public boolean meetsCondition(GameQuest quest, int activityCondId) {
//TODO is it really params[0]?
ActivityCondExcelConfigData condData = activityConditions.get(activityCondId);
if (condData == null) {
getLogger().error("Could not find condition for activity with id = {}", activityCondId);
return false;
}
// TODO add condition handling based on ExcelBinOutput/NewActivityCondExcelConfigData.json
return activityConfig.getMeetCondList().contains(conditionId);
List<BooleanSupplier> predicates = condData.getCond()
.stream()
.map(c -> (BooleanSupplier) () ->
activityConditionsHandlers
.getOrDefault(c.getType(), UNKNOWN_CONDITION_HANDLER).execute(quest, c.paramArray()))
.collect(Collectors.toList());
return LogicType.calculate(condData.getCondComb(), predicates);
}
public ActivityInfoOuterClass.ActivityInfo getInfoProtoByActivityId(int activityId) {

View File

@ -0,0 +1,15 @@
package emu.grasscutter.game.activity.condition;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* This annotation marks condition types for NewActivityCondExcelConfigData.json. To use it you should mark
* class that extends ActivityConditionBaseHandler, and it will be automatically picked during activity manager initiation.
*
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface ActivityCondition {
ActivityConditions value();
}

View File

@ -0,0 +1,10 @@
package emu.grasscutter.game.activity.condition;
import emu.grasscutter.game.quest.GameQuest;
/**
* Base handler for all activity conditions that are listed in NewActivityCondExcelConfigData.json
*/
public abstract class ActivityConditionBaseHandler {
public abstract boolean execute(GameQuest quest, int... params);
}

View File

@ -0,0 +1,51 @@
package emu.grasscutter.game.activity.condition;
public enum ActivityConditions {
NEW_ACTIVITY_COND_PLAYER_LEVEL_GREAT_EQUAL,
NEW_ACTIVITY_COND_NOT_FINISH_TALK,
NEW_ACTIVITY_COND_SALESMAN_CAN_DELIVER,
NEW_ACTIVITY_COND_FINISH_PHOTO_POS_ID,
NEW_ACTIVITY_COND_HACHI_FINISH_STEALTH_STAGE_EQUAL,
NEW_ACTIVITY_COND_UNLOCKED_ALL_LISTED_SCENE_POINTS,
NEW_ACTIVITY_COND_DAYS_GREAT_EQUAL,
NEW_ACTIVITY_COND_FINISH_BARTENDER_LEVEL,
NEW_ACTIVITY_COND_FINISH_HACHI_STAGE,
NEW_ACTIVITY_COND_FINISH_ANY_INSTABLE_SPRAY_CHALLENGE_STAGE,
NEW_ACTIVITY_COND_HACHI_FINISH_BATTLE_STAGE_EQUAL,
NEW_ACTIVITY_COND_FINISH_CHANNELLER_SLAB_APPOINTED_STAGE_ALL_CAMP,
NEW_ACTIVITY_COND_FINISH_WATCHER,
NEW_ACTIVITY_COND_FINISH_REGION_SEARCH,
NEW_ACTIVITY_COND_FINISH_WATER_SPIRIT_PHASE,
NEW_ACTIVITY_COND_SEA_LAMP_POPULARIT,
NEW_ACTIVITY_COND_FINISH_DIG_ACTIVITY,
NEW_ACTIVITY_COND_FINISH_FIND_HILICHURL_LEVEL_EQUAL,
NEW_ACTIVITY_COND_GACHA_CAN_CREATE_ROBOT,
NEW_ACTIVITY_COND_FINISH_SALVAGE_STAGE,
NEW_ACTIVITY_COND_FINISH_MUSIC_GAME_ALL_LEVEL,
NEW_ACTIVITY_COND_DAYS_LESS,
NEW_ACTIVITY_COND_QUEST_FINISH,
NEW_ACTIVITY_COND_QUEST_GLOBAL_VAR_EQUAL,
NEW_ACTIVITY_COND_GROUP_BUNDLE_FINISHED,
NEW_ACTIVITY_COND_SEA_LAMP_PHASE,
NEW_ACTIVITY_COND_FINISH_CHANNELLER_SLAB_ANY_STAGE_ALL_CAMP,
NEW_ACTIVITY_COND_LUMINANCE_STONE_CHALLENGE_FINAL_GALLERY_COMPLETE,
NEW_ACTIVITY_COND_PLANT_FLOWER_CAN_DELIVER,
NEW_ACTIVITY_COND_LUMINANCE_STONE_CHALLENGE_STAGE_GREAT_EQUAL,
NEW_ACTIVITY_COND_FINISH_ANY_ARENA_CHALLENGE_LEVEL,
NEW_ACTIVITY_COND_FINISH_CUSTOM_DUNGEON_OFFICIAL,
NEW_ACTIVITY_COND_SCENE_MP_PLAY_ACTIVATED,
NEW_ACTIVITY_COND_FINISH_FIND_HILICHURL_LEVEL_LESS,
NEW_ACTIVITY_COND_TIME_GREATER,
NEW_ACTIVITY_COND_CREATE_NPC,
NEW_ACTIVITY_COND_TREASURE_SEELIE_FINISH_REGION,
NEW_ACTIVITY_COND_LUNA_RITE_ATMOSPHERE,
NEW_ACTIVITY_COND_OFFERING_LEVEL_GREAT_EQUAL,
NEW_ACTIVITY_COND_FINISH_CHANNELLER_SLAB_ANY_ONEOFF_DUNGEON,
NEW_ACTIVITY_COND_QUEST_FINISH_ALLOW_QUICK_OPEN,
NEW_ACTIVITY_COND_FINISH_POTION_ANY_LEVEL,
NEW_ACTIVITY_COND_MECHANICUS_OPEN,
NEW_ACTIVITY_COND_PLAYER_LEVEL_GREATER,
NEW_ACTIVITY_COND_SALESMAN_CAN_GET_REWARD,
NEW_ACTIVITY_COND_FINISH_REGION_SEARCH_LOGIC,
NEW_ACTIVITY_COND_FINISH_CHANNELLER_SLAB_ONEOFF_DUNGEON_IN_STAGE
}

View File

@ -0,0 +1,59 @@
package emu.grasscutter.game.activity.condition;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.quest.handlers.QuestBaseHandler;
import org.reflections.Reflections;
import java.util.AbstractMap;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Class that used for scanning classpath, picking up all activity conditions (for NewActivityCondExcelConfigData.json)
* and saving them to map. Check for more info {@link ActivityCondition}
*/
public class AllActivityConditionBuilder {
/**
* Build activity conditions handlers
*
* @return map containing all condition handlers for NewActivityCondExcelConfigData.json
*/
static public Map<ActivityConditions, ActivityConditionBaseHandler> buildActivityConditions() {
return new AllActivityConditionBuilder().initActivityConditions();
}
private Map<ActivityConditions, ActivityConditionBaseHandler> initActivityConditions() {
Reflections reflector = Grasscutter.reflector;
return reflector.getTypesAnnotatedWith(ActivityCondition.class).stream()
.map(this::newInstance)
.map(h -> new AbstractMap.SimpleEntry<>(extractActionType(h), h))
.collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));
}
private ActivityConditions extractActionType(ActivityConditionBaseHandler e) {
ActivityCondition condition = e.getClass().getAnnotation(ActivityCondition.class);
if (condition == null) {
Grasscutter.getLogger().error("Failed to read command type for class {}", e.getClass().getName());
return null;
}
return condition.value();
}
private ActivityConditionBaseHandler newInstance(Class<?> clazz) {
try {
Object result = clazz.getDeclaredConstructor().newInstance();
if (result instanceof ActivityConditionBaseHandler) {
return (ActivityConditionBaseHandler) result;
}
Grasscutter.getLogger().error("Failed to initiate activity condition: {}, object have wrong type", clazz.getName());
} catch (Exception e) {
String message = String.format("Failed to initiate activity condition: %s, %s", clazz.getName(), e.getMessage());
Grasscutter.getLogger().error(message, e);
}
return null;
}
}

View File

@ -0,0 +1,17 @@
package emu.grasscutter.game.activity.condition.all;
import emu.grasscutter.game.activity.condition.ActivityCondition;
import emu.grasscutter.game.activity.condition.ActivityConditionBaseHandler;
import emu.grasscutter.game.quest.GameQuest;
import static emu.grasscutter.game.activity.condition.ActivityConditions.NEW_ACTIVITY_COND_PLAYER_LEVEL_GREAT_EQUAL;
@ActivityCondition(NEW_ACTIVITY_COND_PLAYER_LEVEL_GREAT_EQUAL)
public class PlayerLevelGreatEqualActivityActivityCondition extends ActivityConditionBaseHandler {
@Override
public boolean execute(GameQuest quest, int... params) {
return quest.getOwner().getLevel() >= params[0];
}
}

View File

@ -0,0 +1,17 @@
package emu.grasscutter.game.activity.condition.all;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.activity.condition.ActivityConditionBaseHandler;
import emu.grasscutter.game.quest.GameQuest;
/**
* This class is used when condition was not found
*/
public class UnknownActivityConditionHandler extends ActivityConditionBaseHandler {
@Override
public boolean execute(GameQuest quest, int... params) {
Grasscutter.getLogger().error("Called unknown condition handler");
return false;
}
}

View File

@ -1,8 +1,6 @@
package emu.grasscutter.game.quest.conditions;
import emu.grasscutter.data.excels.QuestData;
import emu.grasscutter.game.activity.ActivityManager;
import emu.grasscutter.game.props.ActivityType;
import emu.grasscutter.game.quest.GameQuest;
import emu.grasscutter.game.quest.QuestValue;
import emu.grasscutter.game.quest.enums.QuestTrigger;
@ -13,7 +11,6 @@ public class ConditionActivityCond extends QuestBaseHandler {
@Override
public boolean execute(GameQuest quest, QuestData.QuestCondition condition, String paramStr, int... params) {
return quest.getOwner().getActivityManager().meetsCondition(params[0]);
return quest.getOwner().getActivityManager().meetsCondition(quest, params[0]);
}
}

View File

@ -1,6 +1,11 @@
package emu.grasscutter.game.quest.enums;
import emu.grasscutter.Grasscutter;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.function.BooleanSupplier;
public enum LogicType {
LOGIC_NONE (0),
@ -40,4 +45,26 @@ public enum LogicType {
}
}
}
/**
* Apply logic type to all predicates
*
* @param logicType type of logic that should be applied to predicates
* @param predicates list of predicates for which logicType will be applied
* @return result of applying logicType to predicates
*/
public static boolean calculate(@NotNull LogicType logicType, List<BooleanSupplier> predicates) {
switch (logicType) {
case LOGIC_AND -> {
return predicates.stream().allMatch(BooleanSupplier::getAsBoolean);
}
case LOGIC_OR -> {
return predicates.stream().anyMatch(BooleanSupplier::getAsBoolean);
}
default -> {
Grasscutter.getLogger().error("Unimplemented logic operation was called");
return false;
}
}
}
}