Bazel structure and bin to convert HallOfBeorn dump to CSV for ALEP pipeline
This commit is contained in:
26
cmd/beornextract/BUILD
Normal file
26
cmd/beornextract/BUILD
Normal file
@ -0,0 +1,26 @@
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
|
||||
|
||||
package(
|
||||
default_visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "beornextract_lib",
|
||||
srcs = ["main.go"],
|
||||
data = [
|
||||
"//cmd/beornextract/data:Bot.Cards.json",
|
||||
"//cmd/beornextract/data:Export.Cards.json",
|
||||
],
|
||||
importpath = "github.com/squk/lotr/cmd/beornextract",
|
||||
deps = [
|
||||
"//cmd/beornextract/types",
|
||||
"@com_github_grokify_html_strip_tags_go//:html-strip-tags-go",
|
||||
"@com_github_jessevdk_go_flags//:go_default_library",
|
||||
"@io_bazel_rules_go//go/tools/bazel:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_binary(
|
||||
name = "beornextract",
|
||||
embed = [":beornextract_lib"],
|
||||
)
|
2
cmd/beornextract/data/BUILD
Normal file
2
cmd/beornextract/data/BUILD
Normal file
@ -0,0 +1,2 @@
|
||||
exports_files(glob(["*.json"]))
|
||||
# exports_files(["Export.Cards.json"])
|
1
cmd/beornextract/data/Bot.Cards.json
Normal file
1
cmd/beornextract/data/Bot.Cards.json
Normal file
File diff suppressed because one or more lines are too long
1
cmd/beornextract/data/Export.Cards.json
Normal file
1
cmd/beornextract/data/Export.Cards.json
Normal file
File diff suppressed because one or more lines are too long
1
cmd/beornextract/data/ringsdb.json
Normal file
1
cmd/beornextract/data/ringsdb.json
Normal file
File diff suppressed because one or more lines are too long
136
cmd/beornextract/main.go
Normal file
136
cmd/beornextract/main.go
Normal file
@ -0,0 +1,136 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/bazelbuild/rules_go/go/tools/bazel"
|
||||
strip "github.com/grokify/html-strip-tags-go"
|
||||
"github.com/jessevdk/go-flags"
|
||||
"github.com/squk/lotr/cmd/beornextract/types"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
RawConversion bool `short:"r" long:"raw" description:"Enable to keep the original text from HallOfBeorn dump. Enable to prep for ALEP pipeline."`
|
||||
}
|
||||
|
||||
var opts = Options{
|
||||
RawConversion: false,
|
||||
}
|
||||
|
||||
func main() {
|
||||
_, err := flags.ParseArgs(&opts, os.Args)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("LOTR CARD PARSE")
|
||||
f, err := bazel.Runfile(".")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = filepath.Walk(f,
|
||||
func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(path, info.Size())
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
// Open our jsonFile
|
||||
// jsonFile, err := os.Open("cmd/beornextract/data/Bot.Cards.json")
|
||||
jsonFile, err := os.Open("cmd/beornextract/data/Export.Cards.json")
|
||||
// if we os.Open returns an error then handle it
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
// defer the closing of our jsonFile so that we can parse it later on
|
||||
defer jsonFile.Close()
|
||||
|
||||
byteValue, _ := ioutil.ReadAll(jsonFile)
|
||||
cards := []types.HallOfBeornCard{}
|
||||
json.Unmarshal(byteValue, &cards)
|
||||
|
||||
// Open a file for writing
|
||||
csvFile, err := os.Create("/Users/christian/Downloads/lotr-lcg-set-generator.csv")
|
||||
defer csvFile.Close()
|
||||
if err != nil {
|
||||
// Handle error
|
||||
}
|
||||
|
||||
// Create a writer
|
||||
w := csv.NewWriter(csvFile)
|
||||
|
||||
// Write some rows
|
||||
for _, card := range cards {
|
||||
if card.EncounterSet != "" {
|
||||
continue // skip non=player cards
|
||||
}
|
||||
|
||||
w.Write(
|
||||
[]string{
|
||||
card.Octgnid,
|
||||
"", // hidden
|
||||
"", // hidden
|
||||
card.EncounterSet,
|
||||
strconv.Itoa(card.Position),
|
||||
strconv.Itoa(card.Quantity),
|
||||
card.Name,
|
||||
fmt.Sprintf(
|
||||
"%t",
|
||||
card.IsUnique,
|
||||
),
|
||||
card.TypeName,
|
||||
card.SphereName,
|
||||
card.Traits,
|
||||
findKeywords(card.Text),
|
||||
card.Cost,
|
||||
card.EngagementCost,
|
||||
strconv.Itoa(card.Threat),
|
||||
strconv.Itoa(card.Willpower),
|
||||
strconv.Itoa(card.Attack),
|
||||
strconv.Itoa(card.Defense),
|
||||
strconv.Itoa(card.Health),
|
||||
card.QuestPoints,
|
||||
strconv.Itoa(card.VictoryPoints),
|
||||
"", // Special Icon
|
||||
transformText(card.Name, card.Text),
|
||||
card.Flavor,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// Close the writer
|
||||
w.Flush()
|
||||
// spew.Dump(c)
|
||||
}
|
||||
|
||||
func transformText(name, text string) string {
|
||||
if opts.RawConversion {
|
||||
return text
|
||||
}
|
||||
|
||||
out := strings.ReplaceAll(text, name, "[name]") // insert name tag
|
||||
out = strip.StripTags(out)
|
||||
out = keywordPattern.ReplaceAllLiteralString(out, "")
|
||||
return strings.TrimSpace(out)
|
||||
}
|
||||
|
||||
var keywordPattern = regexp.MustCompile(`((?:(?:[A-Z][a-z]+(\.|\s[0-9]+\.)\s*)+))`)
|
||||
|
||||
func findKeywords(text string) string {
|
||||
return strings.TrimSpace(keywordPattern.FindString(text))
|
||||
}
|
8
cmd/beornextract/types/BUILD
Normal file
8
cmd/beornextract/types/BUILD
Normal file
@ -0,0 +1,8 @@
|
||||
load("@io_bazel_rules_go//go:def.bzl", "go_library")
|
||||
|
||||
go_library(
|
||||
name = "types",
|
||||
srcs = ["card.go"],
|
||||
importpath = "github.com/squk/lotr/cmd/beornextract/types",
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
27
cmd/beornextract/types/card.go
Normal file
27
cmd/beornextract/types/card.go
Normal file
@ -0,0 +1,27 @@
|
||||
package types
|
||||
|
||||
type HallOfBeornCard struct {
|
||||
PackCode string `json:"pack_code,omitempty"`
|
||||
PackName string `json:"pack_name,omitempty"`
|
||||
IsOfficial bool `json:"is_official,omitempty"`
|
||||
TypeCode string `json:"type_code,omitempty"`
|
||||
TypeName string `json:"type_name,omitempty"`
|
||||
SphereCode string `json:"sphere_code,omitempty"`
|
||||
SphereName string `json:"sphere_name,omitempty"`
|
||||
Position int `json:"position,omitempty"`
|
||||
Threat int `json:"threat,omitempty"`
|
||||
Willpower int `json:"willpower,omitempty"`
|
||||
Attack int `json:"attack,omitempty"`
|
||||
Defense int `json:"defense,omitempty"`
|
||||
Health int `json:"health,omitempty"`
|
||||
Octgnid string `json:"octgnid,omitempty"`
|
||||
HasErrata bool `json:"has_errata,omitempty"`
|
||||
URL string `json:"url,omitempty"`
|
||||
QuestPoints string `json:"quest,omitempty"`
|
||||
VictoryPoints int `json:"victory,omitempty"`
|
||||
Imagesrc string `json:"imagesrc,omitempty"`
|
||||
|
||||
EncounterSet string `json:"encounter_set,omitempty"`
|
||||
EngagementCost string `json:"engagement_cost,omitempty"`
|
||||
ThreatStrength int `json:"threat_strength,omitempty"`
|
||||
}
|
Reference in New Issue
Block a user