Skip to content

Commit

Permalink
index bootlegs
Browse files Browse the repository at this point in the history
  • Loading branch information
pwinckles committed Jul 16, 2023
1 parent efbc215 commit ed20a57
Show file tree
Hide file tree
Showing 7 changed files with 2,997 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.pwinckles.cassette.common.model;

import io.avaje.jsonb.Json;
import io.soabase.recordbuilder.core.RecordBuilderFull;
import java.util.List;
import java.util.Map;

@Json
@RecordBuilderFull
public record BootlegMoves(List<String> common, Map<SpeciesType, List<String>> typeSpecific) {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

@Json
@RecordBuilderFull
public record SpeciesMoves(List<String> initial, List<String> learned, List<String> compatible) {}
public record SpeciesMoves(List<String> initial, List<String> learned, List<String> compatible, BootlegMoves bootleg) {}
62 changes: 54 additions & 8 deletions index/src/main/java/com/pwinckles/cassette/index/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import com.pwinckles.cassette.common.model.MoveAccuracy;
import com.pwinckles.cassette.common.model.MoveCategory;
import com.pwinckles.cassette.common.model.Species;
import com.pwinckles.cassette.common.model.SpeciesType;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -83,8 +86,17 @@ public void index(Data data) throws IOException {
var writerConfig = new IndexWriterConfig(analyzer);
writerConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
try (var writer = new IndexWriter(dir, writerConfig)) {
data.species().stream().map(this::toDocument).forEach(doc -> writeDoc(writer, doc));
data.moves().stream().map(this::toDocument).forEach(doc -> writeDoc(writer, doc));
var bootlegMoveMap = new HashMap<String, List<String>>();
data.species().forEach(species -> {
writeDoc(writer, toDocument(species));
species.moves().bootleg().typeSpecific().keySet().forEach(type -> {
addBootlegMoves(type, species, bootlegMoveMap);
writeDoc(writer, toBootlegDocument(type, species));
});
});
data.moves().stream()
.map(move -> toDocument(move, bootlegMoveMap.getOrDefault(move.name(), Collections.emptyList())))
.forEach(doc -> writeDoc(writer, doc));
}
}

Expand All @@ -94,10 +106,36 @@ public Searcher createSearcher() throws IOException {

private Document toDocument(Species species) {
var doc = new Document();
doc.add(new TextField(SpeciesIndexNames.TYPE, species.type().name(), Field.Store.YES));
doc.add(new TextField(SpeciesIndexNames.BOOTLEG, "false", Field.Store.NO));
populateCommonSpeciesFields(doc, species);
species.moves()
.compatible()
.forEach(move -> doc.add(new TextField(SpeciesIndexNames.MOVE, move, Field.Store.NO)));
return doc;
}

private Document toBootlegDocument(SpeciesType type, Species species) {
var doc = new Document();
doc.add(new TextField(SpeciesIndexNames.TYPE, type.name(), Field.Store.YES));
doc.add(new TextField(SpeciesIndexNames.BOOTLEG, "true", Field.Store.NO));
populateCommonSpeciesFields(doc, species);
species.moves()
.bootleg()
.common()
.forEach(move -> doc.add(new TextField(SpeciesIndexNames.MOVE, move, Field.Store.NO)));
species.moves()
.bootleg()
.typeSpecific()
.get(type)
.forEach(move -> doc.add(new TextField(SpeciesIndexNames.MOVE, move, Field.Store.NO)));
return doc;
}

private void populateCommonSpeciesFields(Document doc, Species species) {
doc.add(new TextField(DOC_TYPE_INDEX, DOC_TYPE_SPECIES, Field.Store.YES));
doc.add(new IntField(SpeciesIndexNames.NUMBER, species.number(), Field.Store.NO));
doc.add(new TextField(SpeciesIndexNames.NAME, species.name(), Field.Store.YES));
doc.add(new TextField(SpeciesIndexNames.TYPE, species.type().name(), Field.Store.NO));
doc.add(new TextField(
SpeciesIndexNames.REMASTER_FROM,
Objects.requireNonNullElse(species.remasterFrom(), NONE),
Expand All @@ -115,13 +153,9 @@ private Document toDocument(Species species) {
doc.add(new IntField(SpeciesIndexNames.ATTR_SUM, species.stats().attributeSum(), Field.Store.NO));
doc.add(new IntField(SpeciesIndexNames.AP, species.stats().ap(), Field.Store.NO));
doc.add(new IntField(SpeciesIndexNames.SLOTS, species.stats().moveSlots(), Field.Store.NO));
species.moves()
.compatible()
.forEach(move -> doc.add(new TextField(SpeciesIndexNames.MOVE, move, Field.Store.NO)));
return doc;
}

private Document toDocument(Move move) {
private Document toDocument(Move move, List<String> bootlegSpecies) {
var doc = new Document();
doc.add(new TextField(DOC_TYPE_INDEX, DOC_TYPE_MOVE, Field.Store.YES));
doc.add(new TextField(MoveIndexNames.NAME, move.name(), Field.Store.YES));
Expand Down Expand Up @@ -154,9 +188,21 @@ private Document toDocument(Move move) {
});
move.compatibleSpecies()
.forEach(species -> doc.add(new TextField(MoveIndexNames.SPECIES, species.name(), Field.Store.NO)));
bootlegSpecies.forEach(
species -> doc.add(new TextField(MoveIndexNames.BOOTLEG_SPECIES, species, Field.Store.NO)));
return doc;
}

private void addBootlegMoves(SpeciesType type, Species species, Map<String, List<String>> bootlegMoveMap) {
var name = species.name() + " " + type;
species.moves().bootleg().common().stream()
.map(move -> bootlegMoveMap.computeIfAbsent(move, k -> new ArrayList<>()))
.forEach(list -> list.add(name));
species.moves().bootleg().typeSpecific().get(type).stream()
.map(move -> bootlegMoveMap.computeIfAbsent(move, k -> new ArrayList<>()))
.forEach(list -> list.add(name));
}

private void writeDoc(IndexWriter writer, Document doc) {
try {
writer.addDocument(doc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ final class MoveIndexNames {
static final String STATUS_EFFECT = "status_effect";
static final String STATUS_EFFECT_KIND = "status_effect_kind";
static final String SPECIES = "compatible_species";
static final String BOOTLEG_SPECIES = "compatible_bootleg";

private MoveIndexNames() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ final class SpeciesIndexNames {
static final String NUMBER = "species_num";
static final String NAME = "species_name";
static final String TYPE = "species_type";
static final String BOOTLEG = "bootleg";
static final String HP = "hp";
static final String M_ATTACK = "matk";
static final String M_DEFENSE = "mdef";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.pwinckles.cassette.scraper;

import com.pwinckles.cassette.common.model.BootlegMovesBuilder;
import com.pwinckles.cassette.common.model.Species;
import com.pwinckles.cassette.common.model.SpeciesBuilder;
import com.pwinckles.cassette.common.model.SpeciesMovesBuilder;
import com.pwinckles.cassette.common.model.SpeciesStatsBuilder;
import com.pwinckles.cassette.common.model.SpeciesType;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.function.BiConsumer;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
Expand Down Expand Up @@ -108,6 +112,28 @@ private Species scrape(Document doc) {
moves.addCompatible(cells.get(1).text());
});

var bootlegBuilder = BootlegMovesBuilder.builder();

var commonBootlegTable = doc.selectFirst("h3:has(span#Bootlegs) + table");
var commonBootlegRows = commonBootlegTable.select("tr");

commonBootlegRows.stream().skip(2).forEach(row -> {
var cells = row.select("td");
bootlegBuilder.addCommon(cells.get(1).text());
});

var bootlegTypeMap = new HashMap<SpeciesType, List<String>>();

var bootlegTypedRows = commonBootlegTable.nextElementSibling().select("tr");
bootlegTypedRows.stream().skip(2).forEach(row -> {
var cells = row.select("td");
var type = SpeciesType.fromString(cells.get(0).text().split(" ", 2)[0]);
var list = bootlegTypeMap.computeIfAbsent(type, k -> new ArrayList<>());
list.add(cells.get(1).text());
});

bootlegBuilder.typeSpecific(bootlegTypeMap);
moves.bootleg(bootlegBuilder.build());
species.moves(moves.build());

return species.build();
Expand Down
Loading

0 comments on commit ed20a57

Please sign in to comment.