Skip to content

Commit

Permalink
feat: compare bases percentage against highest unit
Browse files Browse the repository at this point in the history
  • Loading branch information
wezzle committed Sep 25, 2024
1 parent c82ec92 commit f33d86a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 15 deletions.
24 changes: 23 additions & 1 deletion model/compare_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/wezzle/bar-unit-info/gamedata"
"golang.org/x/term"
)

Expand Down Expand Up @@ -48,9 +49,30 @@ func NewCompareModel(mainModel *MainModel, refs ...string) CompareModel {
UnitModels: make([]*Unit, 0),
}

// Calculate base percentages
bv := &BaseValues{}
for _, r := range refs {
up, ok := gamedata.GetUnitPropertiesByRef(r)
if !ok {
continue
}
bv.MetalCost = max(bv.MetalCost, float64(up.MetalCost)/100)
bv.EnergyCost = max(bv.EnergyCost, float64(up.EnergyCost)/100)
bv.Buildtime = max(bv.Buildtime, float64(up.Buildtime)/100)
bv.Health = max(bv.Health, float64(up.Health)/100)
bv.Speed = max(bv.Speed, float64(up.Speed)/100)
bv.SightDistance = max(bv.SightDistance, float64(up.SightDistance)/100)
bv.RadarDistance = max(bv.RadarDistance, float64(up.RadarDistance)/100)
bv.JammerDistance = max(bv.JammerDistance, float64(up.JammerDistance)/100)
bv.SonarDistance = max(bv.SonarDistance, float64(up.SonarDistance)/100)
bv.Buildpower = max(bv.Buildpower, float64(up.Buildpower)/100)
bv.DPS = max(bv.DPS, up.DPS()/100)
bv.WeaponRange = max(bv.WeaponRange, up.MaxWeaponRange()/100)
}

components := make([]string, 0)
for _, r := range refs {
um := NewUnitModel(r, mainModel)
um := NewUnitModel(r, mainModel, bv)
m.UnitModels = append(m.UnitModels, um)
components = append(components, paddingStyle.Render(um.View()))
}
Expand Down
2 changes: 1 addition & 1 deletion model/table_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ func (m *Table) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
return NewCompareModel(m.mainModel, m.selectedRows...), cmd
}
return NewUnitModel(m.Table.SelectedRow()[0], m.mainModel), cmd
return NewUnitModel(m.Table.SelectedRow()[0], m.mainModel, nil), cmd
case key.Matches(msg, tableKeys.Left):
s := max(m.SelectedCol-1, 0)
selectedCol = &s
Expand Down
60 changes: 47 additions & 13 deletions model/unit_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,25 @@ var (
"Cortex": "124",
"Legion": "34",
}
defaultBaseValues = map[string]float64{}
)

func NewUnitModel(ref types.UnitRef, mainModel *MainModel) *Unit {
type BaseValues struct {
MetalCost float64
EnergyCost float64
Buildtime float64
Health float64
Speed float64
SightDistance float64
RadarDistance float64
JammerDistance float64
SonarDistance float64
Buildpower float64
DPS float64
WeaponRange float64
}

func NewUnitModel(ref types.UnitRef, mainModel *MainModel, baseValues *BaseValues) *Unit {
m := Unit{}
m.ref = ref
m.mainModel = mainModel
Expand All @@ -51,6 +67,22 @@ func NewUnitModel(ref types.UnitRef, mainModel *MainModel) *Unit {
panic("unit properties file not generated")
}

if baseValues == nil {
baseValues = &BaseValues{
MetalCost: 250,
EnergyCost: 900,
Buildtime: 1000,
Health: 150,
Speed: 1.5,
SightDistance: 35,
RadarDistance: 35,
JammerDistance: 10,
SonarDistance: 35,
Buildpower: 3,
}
}
m.baseValues = baseValues

m.faction = util.FactionForRef(ref)
m.metalCost = progress.New(progress.WithSolidFill("#383C3F"), progress.WithoutPercentage())
m.energyCost = progress.New(progress.WithSolidFill("#9E6802"), progress.WithoutPercentage())
Expand Down Expand Up @@ -91,6 +123,8 @@ type Unit struct {
sonarRange progress.Model
weaponDps progress.Model
weaponRange progress.Model

baseValues *BaseValues
}

func (m *Unit) Init() tea.Cmd {
Expand Down Expand Up @@ -155,31 +189,31 @@ func (m *Unit) View() string {

d := time.Second * time.Duration(m.properties.Buildtime/100)
stats := [][]string{
{"Metal cost", m.metalCost.ViewAs(m.PercentageWithBase(m.properties.MetalCost, 250)), strconv.Itoa(m.properties.MetalCost)},
{"Energy cost", m.energyCost.ViewAs(m.PercentageWithBase(m.properties.EnergyCost, 900)), strconv.Itoa(m.properties.EnergyCost)},
{"Buildtime", m.buildtime.ViewAs(m.PercentageWithBase(m.properties.Buildtime, 1000)), d.String()},
{"Health", m.health.ViewAs(m.PercentageWithBase(m.properties.Health, 150)), strconv.Itoa(m.properties.Health)},
{"Speed", m.speed.ViewAs(m.PercentageWithBaseF(m.properties.Speed, 1.5)), strconv.Itoa(m.properties.SightDistance)},
{"Sight range", m.sightRange.ViewAs(m.PercentageWithBase(m.properties.SightDistance, 35)), strconv.Itoa(m.properties.SightDistance)},
{"Metal cost", m.metalCost.ViewAs(m.PercentageWithBase(m.properties.MetalCost, m.baseValues.MetalCost)), strconv.Itoa(m.properties.MetalCost)},
{"Energy cost", m.energyCost.ViewAs(m.PercentageWithBase(m.properties.EnergyCost, m.baseValues.EnergyCost)), strconv.Itoa(m.properties.EnergyCost)},
{"Buildtime", m.buildtime.ViewAs(m.PercentageWithBase(m.properties.Buildtime, m.baseValues.Buildtime)), d.String()},
{"Health", m.health.ViewAs(m.PercentageWithBase(m.properties.Health, m.baseValues.Health)), strconv.Itoa(m.properties.Health)},
{"Speed", m.speed.ViewAs(m.PercentageWithBaseF(m.properties.Speed, m.baseValues.Speed)), strconv.Itoa(m.properties.SightDistance)},
{"Sight range", m.sightRange.ViewAs(m.PercentageWithBase(m.properties.SightDistance, m.baseValues.SightDistance)), strconv.Itoa(m.properties.SightDistance)},
}

if m.properties.RadarDistance != 0 {
stats = append(stats, []string{"Radar range", m.radarRange.ViewAs(m.PercentageWithBase(m.properties.RadarDistance, 35)), strconv.Itoa(m.properties.RadarDistance)})
stats = append(stats, []string{"Radar range", m.radarRange.ViewAs(m.PercentageWithBase(m.properties.RadarDistance, m.baseValues.RadarDistance)), strconv.Itoa(m.properties.RadarDistance)})
}
if m.properties.JammerDistance != 0 {
stats = append(stats, []string{"Jammer range", m.jammerRange.ViewAs(m.PercentageWithBase(m.properties.JammerDistance, 10)), strconv.Itoa(m.properties.JammerDistance)})
stats = append(stats, []string{"Jammer range", m.jammerRange.ViewAs(m.PercentageWithBase(m.properties.JammerDistance, m.baseValues.JammerDistance)), strconv.Itoa(m.properties.JammerDistance)})
}
if m.properties.SonarDistance != 0 {
stats = append(stats, []string{"Sonar range", m.sonarRange.ViewAs(m.PercentageWithBase(m.properties.SonarDistance, 35)), strconv.Itoa(m.properties.SonarDistance)})
stats = append(stats, []string{"Sonar range", m.sonarRange.ViewAs(m.PercentageWithBase(m.properties.SonarDistance, m.baseValues.SonarDistance)), strconv.Itoa(m.properties.SonarDistance)})
}
if m.properties.Buildpower != 0 {
stats = append(stats, []string{"Buildpower", m.buildpower.ViewAs(m.PercentageWithBase(m.properties.Buildpower, 3)), strconv.Itoa(m.properties.Buildpower)})
stats = append(stats, []string{"Buildpower", m.buildpower.ViewAs(m.PercentageWithBase(m.properties.Buildpower, m.baseValues.Buildpower)), strconv.Itoa(m.properties.Buildpower)})
}

weaponStats := [][]string{
{"Weapons", weaponStyle.Render(m.properties.SummarizeWeaponTypes()), ""},
{"DPS", m.weaponDps.ViewAs(m.PercentageWithBase(int(math.Round(m.properties.DPS())), 15)), strconv.Itoa(int(math.Round(m.properties.DPS())))},
{"Weapon range", m.weaponRange.ViewAs(m.PercentageWithBase(int(m.properties.MaxWeaponRange()), 20)), strconv.Itoa(int(m.properties.MaxWeaponRange()))},
{"DPS", m.weaponDps.ViewAs(m.PercentageWithBase(int(math.Round(m.properties.DPS())), m.baseValues.DPS)), strconv.Itoa(int(math.Round(m.properties.DPS())))},
{"Weapon range", m.weaponRange.ViewAs(m.PercentageWithBase(int(m.properties.MaxWeaponRange()), m.baseValues.WeaponRange)), strconv.Itoa(int(m.properties.MaxWeaponRange()))},
}

allStats := append(stats, weaponStats...)
Expand Down

0 comments on commit f33d86a

Please sign in to comment.