Skip to content

Commit

Permalink
Glad to see Macaulay Culkin is still alive and kickin.
Browse files Browse the repository at this point in the history
  • Loading branch information
cheezballs committed Dec 12, 2023
1 parent 65ce328 commit fae83c6
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 76 deletions.
43 changes: 22 additions & 21 deletions src/main/java/dev/indoors/ringrats/RingRats.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,28 @@
@Slf4j
public class RingRats implements CommandLineRunner {

@Autowired
ConfigurationService configService;

@Autowired
SimulationService simulationService;

public static void main(String[] args) {
SpringApplication.run(RingRats.class, args);
}

@Override
public void run(String... args) {
configService.readCommandLineArguments(args);
try {
MatchConfiguration matchConfig = configService.buildMatchConfiguration(args);
simulationService.simulateMatch(matchConfig);
} catch (ArgumentException e) {
log.error(e.getMessage(), e);
}

}
@Autowired
ConfigurationService configService;

@Autowired
SimulationService simulationService;

public static void main(String[] args) {
SpringApplication.run(RingRats.class, args);
}

@Override
public void run(String... args) {
if (args.length > 0) {
configService.readCommandLineArguments(args);
try {
MatchConfiguration matchConfig = configService.buildMatchConfiguration(args);
simulationService.simulateMatch(matchConfig);
} catch (ArgumentException e) {
log.error(e.getMessage(), e);
}
}
}

}

20 changes: 10 additions & 10 deletions src/main/java/dev/indoors/ringrats/serializer/JacksonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import dev.indoors.ringrats.simulation.stipulation.Stipulation;
import dev.indoors.ringrats.simulation.match.MatchConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class JacksonConfig {

@Bean
@Primary
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(Stipulation.class, new StipulationDeserializer());
mapper.registerModule(module);
return mapper;
}
@Bean
@Primary
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(MatchConfiguration.class, new MatchConfigDeserializer());
mapper.registerModule(module);
return mapper;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package dev.indoors.ringrats.serializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.indoors.ringrats.simulation.match.MatchConfiguration;
import dev.indoors.ringrats.simulation.stipulation.Stipulation;
import dev.indoors.ringrats.simulation.stipulation.StipulationFactory;
import dev.indoors.ringrats.simulation.wrestler.Wrestler;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class MatchConfigDeserializer extends JsonDeserializer<MatchConfiguration> {

@Override
public MatchConfiguration deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException {
JsonNode root = jsonParser.getCodec().readTree(jsonParser);
ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec();

Set<Wrestler> wrestlers = mapper.treeToValue(root.get("wrestlers"), mapper.getTypeFactory().constructCollectionType(HashSet.class, Wrestler.class));

Set<Stipulation> stipulations = new HashSet<>();
for (JsonNode stipNode : root.get("stipulations")) {
Stipulation stipulation = StipulationFactory.getStipulation(stipNode.get("name").asText());
stipulations.add(stipulation);
}

return new MatchConfiguration(stipulations, wrestlers);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package dev.indoors.ringrats.simulation.attribute;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Attribute {

private Integer beginningValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class InitiativeComparator implements Comparator<Wrestler> {

@Override
public int compare(Wrestler w1, Wrestler w2) {
return Integer.compare(w1.calcInitiative(), w2.calcInitiative());
return -Integer.compare(w1.calcInitiative(), w2.calcInitiative());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public void start() {
wrestler.initializeForSimulation();

// make the wrestler focus on one of the other wrestlers
// TODO: make this better and smarter, dont hit friendly wrestlers, etc
Performer performer = findRandomOpponent(wrestler);
wrestler.addTask(new FocusAttentionTask(performer));
}
Expand All @@ -70,7 +69,9 @@ public MatchResults end() {
}

private Performer findRandomOpponent(Wrestler wrestler) {
List<Wrestler> filteredWrestlers = wrestlers.stream().filter(w -> !w.getName().equals(wrestler.getName())).toList();
List<Wrestler> filteredWrestlers = wrestlers.stream().filter(w ->
!w.getName().equals(wrestler.getName()) && !w.getTeam().equalsIgnoreCase(wrestler.getTeam())
).toList();
Random random = new Random();
return filteredWrestlers.get(random.nextInt(filteredWrestlers.size()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import dev.indoors.ringrats.simulation.stipulation.Stipulation;
import dev.indoors.ringrats.simulation.wrestler.Wrestler;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Set;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class MatchConfiguration {

@JsonProperty()
Set<Stipulation> stipulations;
@JsonProperty()
Set<Stipulation> stipulations;

@JsonProperty()
Set<Wrestler> wrestlers;
@JsonProperty()
Set<Wrestler> wrestlers;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.indoors.ringrats.simulation.condition.Condition;
import dev.indoors.ringrats.simulation.condition.StandingCondition;
import dev.indoors.ringrats.simulation.wrestler.Wrestler;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand All @@ -15,6 +16,7 @@
public class OneFallStipulation extends Stipulation {

private String name = "One Fall";
private Set<Wrestler> activeWrestlers;

@Override
public Set<Condition> getStartingConditions() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package dev.indoors.ringrats.simulation.wrestler;

import dev.indoors.ringrats.simulation.attribute.Attribute;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Health {

Attribute head;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Wrestler extends Performer {
Attribute energy;
Attribute stamina;
Attribute quickness;
String team;

public int calcInitiative() {
int currQuickness = quickness.getCurrentValue();
Expand Down
6 changes: 4 additions & 2 deletions src/main/resources/examples/1v1-basic.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
},
"quickness": {
"beginningValue": 60
}
},
"team": "1"
},
{
"name": "Rufus",
Expand All @@ -16,7 +17,8 @@
},
"quickness": {
"beginningValue": 75
}
},
"team": "2"
}
],
"stipulations": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
package dev.indoors.ringrats.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import dev.indoors.ringrats.service.impl.SimulationServiceImpl;
import dev.indoors.ringrats.simulation.match.MatchConfiguration;
import dev.indoors.ringrats.simulation.match.MatchResults;
import dev.indoors.ringrats.util.MockMatchConfigurations;
import dev.indoors.ringrats.util.MockWrestlers;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(args = {"-f", "examples/1v1-basic.json"})
@SpringBootTest
public class SimulationServiceTests {

@InjectMocks
SimulationServiceImpl simService;

@Autowired
ObjectMapper mapper;

@Test
void firstTest() {
MatchResults results = simService.simulateMatch(MockMatchConfigurations.basicOneFall(MockWrestlers.create("Wang Chung", "Dangerous Davey")));
void firstTest() throws IOException {
ClassPathResource resource = new ClassPathResource("1v1-basic.json");
String json = new String(Files.readAllBytes(Paths.get(resource.getURI())));
MatchConfiguration config = mapper.readValue(json, MatchConfiguration.class);
MatchResults results = simService.simulateMatch(config);
assertThat(results).isNotNull();
}

}
44 changes: 40 additions & 4 deletions src/test/resources/1v1-basic.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,53 @@
"beginningValue": 100
},
"quickness": {
"beginningValue": 100
}
"beginningValue": 60
},
"health": {
"head": {
"beginningValue": 60
},
"arm": {
"beginningValue": 60
},
"leg": {
"beginningValue": 60
},
"back": {
"beginningValue": 60
},
"core": {
"beginningValue": 60
}
},
"team": "1"
},
{
"name": "Rufus",
"energy": {
"beginningValue": 100
},
"quickness": {
"beginningValue": 100
}
"beginningValue": 75
},
"health": {
"head": {
"beginningValue": 70
},
"arm": {
"beginningValue": 40
},
"leg": {
"beginningValue": 70
},
"back": {
"beginningValue": 60
},
"core": {
"beginningValue": 59
}
},
"team": "2"
}
],
"stipulations": [
Expand Down

0 comments on commit fae83c6

Please sign in to comment.