Skip to content

Commit

Permalink
Counter server and vassal creator
Browse files Browse the repository at this point in the history
  • Loading branch information
sayden committed Nov 9, 2024
1 parent 812f2d4 commit 168ddea
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 303 deletions.
23 changes: 10 additions & 13 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"os"
"time"

"github.com/alecthomas/kong"
"github.com/charmbracelet/log"
Expand All @@ -11,21 +12,19 @@ import (
var logger = log.New(os.Stderr)

var Cli struct {
// Asset option is used to generate assets directly. This is usually counters images or Card sheets.
Assets AssetsOutput `cmd:"" help:"Generate images of some short, using either counters or cards, from a JSON file"`

// JSON uses a JSON input to generate another JSON output.
Json JsonOutput `cmd:"" help:"Generate a JSON of some short, by transforming another JSON as input"`

// Vassal is used to generate a Vassal module for testing purposes.
Vassal vassalCli `cmd:"" help:"Create a vassal module for testing. It searches for the 'template.xml' in the same folder"` //FIXME

Assets AssetsOutput `cmd:"" help:"Generate images of some short, using either counters or cards, from a JSON file"`
Json JsonOutput `cmd:"" help:"Generate a JSON of some short, by transforming another JSON as input"`
Vassal vassalCli `cmd:"" help:"Create a vassal module for testing"`
GenerateTemplate GenerateTemplate `cmd:"" help:"Generates a new counter template file with default values"`

CheckTemplate CheckTemplate `cmd:"" help:"Check if a JSON file is a valid counter template"`
CheckTemplate CheckTemplate `cmd:"" help:"Check if a JSON file is a valid counter template"`
}

func main() {
now := time.Now()
defer func(now time.Time) {
logger.Infof("Execution time: %v", time.Since(now))
}(now)

flag.Parse()

logger.SetReportTimestamp(false)
Expand All @@ -36,6 +35,4 @@ func main() {

err := ctx.Run()
ctx.FatalIfErrorf(err)

logger.Info("Done")
}
13 changes: 2 additions & 11 deletions cmd/vassal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,9 @@ import (

type vassalCli struct {
Templates []string `help:"List of templates to embed in the Vassal module" short:"t"`
OutputPath string `help:"Path to the temp folder" short:"o"`
output.VassalConfig
OutputPath string `help:"Path for the temp folder" short:"o"`
}

func (i *vassalCli) Run(ctx *kong.Context) error {
if i.Templates != nil {
return i.TemplatesToVassal()
}

return output.CSVToVassalFile(i.VassalConfig)
}

func (v *vassalCli) TemplatesToVassal() error {
func (v *vassalCli) Run(ctx *kong.Context) error {
return output.VassalModule(v.OutputPath, v.Templates)
}
12 changes: 8 additions & 4 deletions counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ type Counter struct {
// Generate the following counter with 'back' suffix in its filename
Back *Counter `json:"back,omitempty"`

Filename string `json:"filename,omitempty"`
VassalPiece *PieceSlot `json:"vassal,omitempty"`
Filename string `json:"filename,omitempty"`
PrototypeName string `json:"-"`
VassalPiece *PieceSlot `json:"vassal,omitempty"`
}

type Counters []Counter
Expand Down Expand Up @@ -82,7 +83,7 @@ func (c *Counter) GetTextInPosition(i int) string {
// filenumber: CounterTemplate.PositionNumberForFilename. So it will always be fixed number
// position: The position of the text in the counter (0-16)
// suffix: A suffix on the file. Constant
func (c *Counter) GetCounterFilename(sideName string, position int, filenamesInUse *sync.Map) {
func (c *Counter) GenerateCounterFilename(sideName string, position int, filenamesInUse *sync.Map) {
if c.Filename != "" {
return
}
Expand Down Expand Up @@ -118,8 +119,11 @@ func (c *Counter) GetCounterFilename(sideName string, position int, filenamesInU
b.WriteString(name + " ")
}

if c.PrototypeName != "" {
b.WriteString(c.PrototypeName + " ")
}

res := b.String()
res = strings.TrimSpace(res)

res = strings.TrimSpace(res)

Expand Down
5 changes: 3 additions & 2 deletions counter_prototype.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type TextPrototype struct {
StringList []string `json:"string_list"`
}

func (p *CounterPrototype) ToCounters(filenamesInUse *sync.Map, sideName string, positionNumberForFilename int) ([]Counter, error) {
func (p *CounterPrototype) ToCounters(filenamesInUse *sync.Map, sideName, prototypeName string, positionNumberForFilename int) ([]Counter, error) {
cts := make([]Counter, 0)

// You can prototype texts and images, so one of the two must be present, get their length
Expand All @@ -47,12 +47,13 @@ func (p *CounterPrototype) ToCounters(filenamesInUse *sync.Map, sideName string,
if err := deepcopy.FromTo(p.Counter, &newCounter); err != nil {
return nil, err
}
newCounter.PrototypeName = prototypeName

if err = p.applyPrototypes(&newCounter, i); err != nil {
return nil, err
}

newCounter.GetCounterFilename(sideName, positionNumberForFilename, filenamesInUse)
newCounter.GenerateCounterFilename(sideName, positionNumberForFilename, filenamesInUse)

if p.Back != nil {
var tempBackCounter Counter
Expand Down
6 changes: 3 additions & 3 deletions counter_prototype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestCounterPrototypeToCounter(t *testing.T) {
}

filenamesInUse := &sync.Map{}
counter, err := proto.ToCounters(filenamesInUse, "side", 0)
counter, err := proto.ToCounters(filenamesInUse, "side", "testing", 0)
if assert.NoError(t, err) {
assert.Equal(t, 2, len(counter))
assert.Equal(t, "text", counter[0].Texts[0].String)
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestJSONPrototypes(t *testing.T) {
// check the marshalling of the template to an expected byte slice
actualBytes, err := json.MarshalIndent(newTempl, "", " ")
assert.NoError(t, err)
if !assert.Equal(t, 18538, len(actualBytes)) {
if !assert.Equal(t, 18356, len(actualBytes)) {
t.FailNow()
}

Expand All @@ -74,7 +74,7 @@ func TestJSONPrototypes(t *testing.T) {
assert.NoError(t, err)

// // ensure we are using the expected file and that it has not been altered by mistake
if !assert.Equal(t, 18538, len(expectedBytes), "expected file has been altered, aborting test") {
if !assert.Equal(t, 18356, len(expectedBytes), "expected file has been altered, aborting test") {
t.FailNow()
}

Expand Down
4 changes: 2 additions & 2 deletions counter_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (t *CounterTemplate) ExpandPrototypeCounterTemplate(filenamesInUse *sync.Ma
for _, prototypeName := range names {
prototype := t.Prototypes[prototypeName]

cts, err := prototype.ToCounters(filenamesInUse, t.Vassal.SideName, t.PositionNumberForFilename)
cts, err := prototype.ToCounters(filenamesInUse, t.Vassal.SideName, prototypeName, t.PositionNumberForFilename)
if err != nil {
return nil, err
}
Expand All @@ -174,7 +174,7 @@ func (t *CounterTemplate) ExpandPrototypeCounterTemplate(filenamesInUse *sync.Ma
if t.Counters != nil {
for i, counter := range t.Counters {
if counter.Filename == "" {
counter.GetCounterFilename(t.Vassal.SideName, t.PositionNumberForFilename, filenamesInUse)
counter.GenerateCounterFilename(t.Vassal.SideName, t.PositionNumberForFilename, filenamesInUse)
t.Counters[i].Filename = counter.Filename
}
}
Expand Down
2 changes: 1 addition & 1 deletion counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestGetCounterFilename(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.counter.GetCounterFilename("side", tt.position, filenamesInUse)
tt.counter.GenerateCounterFilename("side", tt.position, filenamesInUse)
if tt.counter.Filename != tt.expected {
t.Errorf("GetCounterFilename() = '%v', want '%v'", tt.counter.Filename, tt.expected)
}
Expand Down
5 changes: 5 additions & 0 deletions output/counters_to_vassal.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

func VassalModule(outputPath string, templatesFiles []string) error {
// TODO: Side effects
os.MkdirAll(outputPath, 0755)
tempDir, err := os.MkdirTemp(outputPath, "vassal")
if err != nil {
Expand Down Expand Up @@ -67,6 +68,7 @@ func VassalModule(outputPath string, templatesFiles []string) error {
hexGrid.Visible = "false"
}

// TODO: Side effects
CountersToPNG(newTemplate)

// get an array of the vassal pieces
Expand All @@ -89,6 +91,7 @@ func VassalModule(outputPath string, templatesFiles []string) error {
listOfListWidgets = append(listOfListWidgets, list)
}

// TODO: Side effects
// Copy map file to the images folder
if err = fsops.CopyFile(mapFilename, path.Join(tempDir, "images", path.Base(mapFilename))); err != nil {
return errors.Wrap(err, "error copying map file")
Expand All @@ -111,6 +114,7 @@ func writeXMLFiles(tempDir, moduleName, mapFilename, outputPath string,
}
buildFile.PieceWindow.TabWidget.ListWidget = listOfWidgets

// TODO: Side effects
f, err := os.Create(path.Join(tempDir, "buildFile.xml"))
if err != nil {
return fmt.Errorf("error creating buildFile.xml: %w", err)
Expand All @@ -125,6 +129,7 @@ func writeXMLFiles(tempDir, moduleName, mapFilename, outputPath string,
// moduledata
moduleData := vassal.GetModuleData()
moduleData.Name = moduleName
// TODO: Side effects
f2, err := os.Create(path.Join(tempDir, "moduledata"))
if err != nil {
return fmt.Errorf("error creating buildFile.xml: %w", err)
Expand Down
Loading

0 comments on commit 168ddea

Please sign in to comment.