Skip to content

Commit

Permalink
refactor to have consistent front and back counter file naming
Browse files Browse the repository at this point in the history
  • Loading branch information
sayden committed Nov 3, 2024
1 parent 4772fe2 commit bc529f2
Show file tree
Hide file tree
Showing 21 changed files with 446 additions and 281 deletions.
7 changes: 5 additions & 2 deletions cmd/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"os"
"sync"

"github.com/alecthomas/kong"
"github.com/pkg/errors"
Expand Down Expand Up @@ -46,6 +47,8 @@ func (i *JsonOutput) Run(ctx *kong.Context) error {
return err
}

filenamesInUse := &sync.Map{}

switch inputContent {
case counters.FileContent_CSV:
switch Cli.Json.OutputType {
Expand Down Expand Up @@ -79,15 +82,15 @@ func (i *JsonOutput) Run(ctx *kong.Context) error {
switch Cli.Json.OutputType {
case "counters":
// JSON counters to Counters
newTempl, err := counterTemplate.ExpandPrototypeCounterTemplate()
newTempl, err := counterTemplate.ExpandPrototypeCounterTemplate(filenamesInUse)
if err != nil {
return errors.Wrap(err, "error trying to convert a counter template into another counter template")
}
return output.ToJSONFile(newTempl, Cli.Json.OutputPath)

case "back-counters":
// JSON counters to Counters
newTempl, err := counterTemplate.ExpandPrototypeCounterTemplate()
newTempl, err := counterTemplate.ExpandPrototypeCounterTemplate(filenamesInUse)
if err != nil {
return errors.Wrap(err, "error trying to convert a counter template into another counter template")
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/json_to_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ func jsonToAsset(inputPath, outputPath string) (err error) {
newTemplate.OutputFolder = outputPath
}

return output.CountersToPNG(newTemplate)
output.CountersToPNG(newTemplate)

return nil
}

func jsonToBlock(blockBack string) (err error) {
Expand Down
12 changes: 11 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"flag"
"os"
"runtime/pprof"

"github.com/alecthomas/kong"
"github.com/charmbracelet/log"
Expand All @@ -25,13 +27,21 @@ var Cli struct {
}

func main() {
flag.Parse()
f, err := os.Create("cpu.prof")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()

logger.SetReportTimestamp(false)
logger.SetReportCaller(false)
logger.SetLevel(log.DebugLevel)

ctx := kong.Parse(&Cli)

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

logger.Info("Done")
Expand Down
92 changes: 81 additions & 11 deletions counter.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package counters

import (
"bytes"
"fmt"
"io"
"strings"
"sync"
"text/template"

"github.com/fogleman/gg"
"github.com/google/uuid"

Check failure on line 12 in counter.go

View workflow job for this annotation

GitHub Actions / build

missing go.sum entry for module providing package github.com/google/uuid (imported by github.com/sayden/counters); to add:
"github.com/pkg/errors"
"github.com/thehivecorporation/log"
)
Expand All @@ -23,7 +27,10 @@ type Counter struct {
Extra *Extra `json:"extra,omitempty"`

// Generate the following counter with 'back' suffix in its filename
Back *Counter `json:",omitempty"`
Back *Counter `json:"back,omitempty"`

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

type Counters []Counter
Expand Down Expand Up @@ -65,9 +72,14 @@ 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(position int, suffix string, filenumber int, filenamesInUse map[string]bool) string {
func (c *Counter) GetCounterFilename(position int, filenamesInUse *sync.Map) string {
if c.Filename != "" {
return c.Filename
}

var b strings.Builder
name := c.GetTextInPosition(position)
var name string
name = c.GetTextInPosition(position)

if c.Extra != nil {
if c.Extra.TitlePosition != nil && *c.Extra.TitlePosition != position {
Expand Down Expand Up @@ -97,26 +109,33 @@ func (c *Counter) GetCounterFilename(position int, suffix string, filenumber int
b.WriteString(name + " ")
}

if suffix != "" {
b.WriteString(suffix)
}

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

if filenamesInUse[res] {
if filenumber >= 0 {
res += fmt.Sprintf(" %03d", filenumber)
filenumber := 0
_, isFound := filenamesInUse.Load(res)
if isFound {
filenumber = 0
for {
tempRes := fmt.Sprintf("%s%03d", res, filenumber)
_, isFound = filenamesInUse.Load(tempRes)
if !isFound {
break
}
filenumber++
}
}

if res == "" {
res = fmt.Sprintf("%03d", filenumber)
}
res = strings.TrimSpace(res)

filenamesInUse[res] = true
filenamesInUse.Store(res, true)
c.Extra.Title = res

res += ".png"
c.Filename = res

return res
}
Expand Down Expand Up @@ -198,3 +217,54 @@ func (c *Counter) canvas() (*gg.Context, error) {

return dc, nil
}

func (c *Counter) ToVassal(sideName string) error {
if c.Filename == "" {
return errors.New("vassal: counter filename is empty")
}
if sideName == "" {
return errors.New("vassal: side name is empty")
}

if strings.Contains(c.Extra.Title, "_back") {
return nil
}

pieceTemplate := "+/null/prototype;Basic Pieces emb2;" +
"{{ .FlipName }};128;A;;128;;;128;;;;1;false;0;0;" +
"{{ .BackFilename }};Back;true;Flip;;;false;;1;1;false;;;;Description;1.0;;true\\ piece;;;" +
"{{ .FrontFilename }};" +
"{{ .PieceName }}/ -1\\ null;0;0;;1;ppScale;1.0"

xmlTemplate, err := template.New("xml").Parse(pieceTemplate)
if err != nil {
return fmt.Errorf("could not parse template string %w", err)
}

uuid := uuid.New().String()
buf := bytes.NewBufferString("")
pieceTemp := PieceTemplateData{
FrontFilename: c.Filename,
BackFilename: c.Extra.Title + "_back.png",
FlipName: sideName,
PieceName: c.Filename,
Id: uuid,
}

err = xmlTemplate.ExecuteTemplate(buf, "xml", pieceTemp)
if err != nil {
return fmt.Errorf("could not execute template %w", err)
}

piece := PieceSlot{
EntryName: c.Filename,
Gpid: uuid,
Height: c.Height,
Width: c.Width,
Data: buf.String(),
}

c.VassalPiece = &piece

return nil
}
Loading

0 comments on commit bc529f2

Please sign in to comment.