Skip to content

Commit

Permalink
rename model schema
Browse files Browse the repository at this point in the history
  • Loading branch information
fhnaumann committed Feb 2, 2024
1 parent 2b887d6 commit bb47857
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 26 deletions.
6 changes: 6 additions & 0 deletions builder_website/challenges_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,12 @@
},
"rules": {
"$ref": "#/definitions/RulesConfig"
},
"timer": {
"default": 0,
"description": "The time (in seconds) that has passed since the challenge was started.",
"minimum": 0,
"type": "integer"
}
},
"type": "object"
Expand Down
11 changes: 10 additions & 1 deletion builder_website/src/components/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import type { RulesConfig } from "./rules"

export interface Model {
rules?: RulesConfig,
goals?: GoalsConfig
goals?: GoalsConfig,

/**
* The time (in seconds) that has passed since the challenge was started.
*
* @minimum 0
* @default 0
* @TJS-type integer
*/
timer?: number
}

2 changes: 1 addition & 1 deletion plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<sourceDirectory>${basedir}/src/main/resources/test-output-schema.json</sourceDirectory>
<sourceDirectory>${basedir}/src/main/resources/challenges_schema.json</sourceDirectory>
<targetPackage>wand555.github.io.challenges.generated</targetPackage>
<includeJsr303Annotations>true</includeJsr303Annotations>
<includeJsr305Annotations>true</includeJsr305Annotations>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public void setGoals(@NotNull List<Goal> goals) {
this.goals = goals;
}

public long getTime() {
return timerRunnable.getTimer();
}

@Override
public Component getCurrentStatus() {
Component empty = Component.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import wand555.github.io.challenges.generated.TestOutputSchema;
import wand555.github.io.challenges.generated.ChallengesSchema;
import wand555.github.io.challenges.goals.Collect;
import wand555.github.io.challenges.goals.MobGoal;
import wand555.github.io.challenges.mapping.ModelMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ public static void writeToFile(ChallengeManager challengeManager, Writer writer)
GoalsConfig goalsConfig = new GoalsConfig();
challengeManager.getGoals().forEach(goal -> goal.addToGeneratedConfig(goalsConfig));


TestOutputSchema testOutputSchema = new TestOutputSchema(goalsConfig, rulesConfig);
// casting time from long to int could be problematic...
// on the other hand ~24000 days fit into an int, no one will reach that (hopefully)
ChallengesSchema ChallengesSchema = new ChallengesSchema(goalsConfig, rulesConfig, (int) challengeManager.getTime());
ObjectMapper objectMapper = new ObjectMapper();
try {
objectMapper.writerWithDefaultPrettyPrinter().writeValue(writer, testOutputSchema);
objectMapper.writerWithDefaultPrettyPrinter().writeValue(writer, ChallengesSchema);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -57,9 +58,9 @@ public static ChallengeManager readFromFile(File file, Challenges plugin) throws
ValidationResult validationResult = validator.validate(json);
if(validationResult.isValid()) {
ObjectMapper objectMapper = new ObjectMapper();
TestOutputSchema testOutputSchema = null;
ChallengesSchema ChallengesSchema = null;
try {
testOutputSchema = objectMapper.readValue(json, TestOutputSchema.class);
ChallengesSchema = objectMapper.readValue(json, ChallengesSchema.class);

Context context = new Context.Builder()
.withPlugin(plugin)
Expand All @@ -71,7 +72,7 @@ public static ChallengeManager readFromFile(File file, Challenges plugin) throws
.withChallengeManager(new ChallengeManager())
.build();
context.challengeManager().setContext(context); // immediately set context so it is available in the manager
ModelMapper.map2ModelClasses(context, testOutputSchema);
ModelMapper.map2ModelClasses(context, ChallengesSchema);


return context.challengeManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.json.XML;
import wand555.github.io.challenges.generated.DeathPunishmentConfig;
import wand555.github.io.challenges.generated.HealthPunishmentConfig;
import wand555.github.io.challenges.generated.TestOutputSchema;
import wand555.github.io.challenges.generated.ChallengesSchema;
import wand555.github.io.challenges.utils.ResourcePackHelper;

import javax.xml.transform.Source;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class TimerRunnable implements Consumer<BukkitTask> {

public TimerRunnable(Context context) {
this.context = context;
timer = context.schemaRoot().at("/definitions/Model/properties/timer").asLong(0L);
}

public void start() {
Expand All @@ -41,6 +42,7 @@ public void accept(BukkitTask bukkitTask) {
}




public long getTimer() {
return timer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ModelMapper(Context context) {
this.context = context;
}

public static void map2ModelClasses(Context context, TestOutputSchema json) {
public static void map2ModelClasses(Context context, ChallengesSchema json) {

List<Rule> rules = mapToRules(context, json.getRules().getEnabledRules());
List<Punishment> globalPunishments = mapToPunishments(context, json.getRules().getEnabledGlobalPunishments());
Expand Down
6 changes: 6 additions & 0 deletions plugin/src/main/resources/challenges_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,12 @@
},
"rules": {
"$ref": "#/definitions/RulesConfig"
},
"timer": {
"default": 0,
"description": "The time (in seconds) that has passed since the challenge was started.",
"minimum": 0,
"type": "integer"
}
},
"type": "object"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import wand555.github.io.challenges.*;
import wand555.github.io.challenges.generated.TestOutputSchema;
import wand555.github.io.challenges.generated.ChallengesSchema;
import wand555.github.io.challenges.mapping.ModelMapper;
import wand555.github.io.challenges.rules.NoBlockBreakRule;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import wand555.github.io.challenges.ResourceBundleContext;
import wand555.github.io.challenges.generated.NoBlockBreakRuleConfig;
import wand555.github.io.challenges.generated.PunishmentsConfig;
import wand555.github.io.challenges.generated.TestOutputSchema;
import wand555.github.io.challenges.generated.ChallengesSchema;
import wand555.github.io.challenges.mapping.ModelMapper;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -105,13 +105,13 @@ public void testEmptyNoBlockBreakRuleJSON2Model() throws JsonProcessingException
}
}
""";
TestOutputSchema testOutputSchema = new ObjectMapper().readValue(emptyNoBlockBreakJSON, TestOutputSchema.class);
NoBlockBreakRuleConfig emptyNoBlockBreakRuleConfig = testOutputSchema.getRules().getEnabledRules().getNoBlockBreak();
ChallengesSchema ChallengesSchema = new ObjectMapper().readValue(emptyNoBlockBreakJSON, ChallengesSchema.class);
NoBlockBreakRuleConfig emptyNoBlockBreakRuleConfig = ChallengesSchema.getRules().getEnabledRules().getNoBlockBreak();

assertTrue(emptyNoBlockBreakRuleConfig.getExemptions().isEmpty());

NoBlockBreakRule emptyNoBlockBreakRule = new NoBlockBreakRule(context, emptyNoBlockBreakRuleConfig);
ModelMapper.map2ModelClasses(context, testOutputSchema);
ModelMapper.map2ModelClasses(context, ChallengesSchema);

assertEquals(emptyNoBlockBreakRule, context.challengeManager().getRules().get(0));
}
Expand All @@ -130,13 +130,13 @@ public void testComplexNoBlockBreakRuleJSON2Model() throws JsonProcessingExcepti
}
}
""";
TestOutputSchema testOutputSchema = new ObjectMapper().readValue(complexNoBlockBreakJSON, TestOutputSchema.class);
NoBlockBreakRuleConfig complexNoBlockBreakRuleConfig = testOutputSchema.getRules().getEnabledRules().getNoBlockBreak();
ChallengesSchema ChallengesSchema = new ObjectMapper().readValue(complexNoBlockBreakJSON, ChallengesSchema.class);
NoBlockBreakRuleConfig complexNoBlockBreakRuleConfig = ChallengesSchema.getRules().getEnabledRules().getNoBlockBreak();

assertEquals(List.of("STONE", "DIRT"), complexNoBlockBreakRuleConfig.getExemptions());

NoBlockBreakRule compelxNoBlockBreakRule = new NoBlockBreakRule(context, complexNoBlockBreakRuleConfig);
ModelMapper.map2ModelClasses(context, testOutputSchema);
ModelMapper.map2ModelClasses(context, ChallengesSchema);

assertEquals(compelxNoBlockBreakRule, context.challengeManager().getRules().get(0));
}
Expand All @@ -157,7 +157,7 @@ public void testInvalidExemptionsJSON2Model() {
}
}
""";
assertThrows(JsonMappingException.class, () -> objectMapper.readValue(additionalPropertyJSON, TestOutputSchema.class));
assertThrows(JsonMappingException.class, () -> objectMapper.readValue(additionalPropertyJSON, ChallengesSchema.class));

String invalidStringInExemptionListJSON =
"""
Expand All @@ -172,8 +172,8 @@ public void testInvalidExemptionsJSON2Model() {
}
""";
assertThrows(RuntimeException.class, () -> {
TestOutputSchema testOutputSchema = objectMapper.readValue(invalidStringInExemptionListJSON, TestOutputSchema.class);
ModelMapper.map2ModelClasses(context, testOutputSchema);
ChallengesSchema ChallengesSchema = objectMapper.readValue(invalidStringInExemptionListJSON, ChallengesSchema.class);
ModelMapper.map2ModelClasses(context, ChallengesSchema);
});

String invalidMaterialInExemptionListJSON =
Expand All @@ -189,8 +189,8 @@ public void testInvalidExemptionsJSON2Model() {
}
""";
assertThrows(RuntimeException.class, () -> {
TestOutputSchema testOutputSchema = objectMapper.readValue(invalidMaterialInExemptionListJSON, TestOutputSchema.class);
ModelMapper.map2ModelClasses(context, testOutputSchema);
ChallengesSchema ChallengesSchema = objectMapper.readValue(invalidMaterialInExemptionListJSON, ChallengesSchema.class);
ModelMapper.map2ModelClasses(context, ChallengesSchema);
});
}

Expand Down

0 comments on commit bb47857

Please sign in to comment.