cmd: join group to fleet

This commit is contained in:
Ilia Denisov
2025-11-26 23:03:07 +03:00
parent c86ab84ad7
commit 92ad81b2a2
6 changed files with 133 additions and 36 deletions
+27 -4
View File
@@ -1,6 +1,7 @@
package game
import (
"iter"
"math"
"slices"
@@ -30,7 +31,7 @@ func (g Game) FleetSpeed(fl Fleet) float64 {
return result
}
func (g Game) JoinShipGroupToFleet(raceName, fleetName string, group, count uint) error {
func (g *Game) JoinShipGroupToFleet(raceName, fleetName string, group, count uint) error {
ri, err := g.raceIndex(raceName)
if err != nil {
return err
@@ -38,14 +39,14 @@ func (g Game) JoinShipGroupToFleet(raceName, fleetName string, group, count uint
return g.joinShipGroupToFleetInternal(ri, fleetName, group, count)
}
func (g Game) joinShipGroupToFleetInternal(ri int, fleetName string, group, count uint) (err error) {
func (g *Game) joinShipGroupToFleetInternal(ri int, fleetName string, group, count uint) (err error) {
name, ok := validateTypeName(fleetName)
if !ok {
return e.NewEntityTypeNameValidationError("%q", name)
}
sgi := -1
var maxIndex uint
for i, sg := range g.listShipGroups(ri) {
for i, sg := range g.listIndexShipGroups(ri) {
if sgi < 0 && sg.Index == group {
sgi = i
}
@@ -87,7 +88,7 @@ func (g Game) fleetIndex(ri int, name string) int {
return slices.IndexFunc(g.Fleets, func(f Fleet) bool { return f.OwnerID == g.Race[ri].ID && f.Name == name })
}
func (g Game) createFleet(ri int, name string) (int, error) {
func (g *Game) createFleet(ri int, name string) (int, error) {
n, ok := validateTypeName(name)
if !ok {
return 0, e.NewEntityTypeNameValidationError("%q", n)
@@ -102,3 +103,25 @@ func (g Game) createFleet(ri int, name string) (int, error) {
})
return len(g.Fleets) - 1, nil
}
func (g Game) listFleets(ri int) iter.Seq[Fleet] {
return func(yield func(Fleet) bool) {
for _, fl := range g.listIndexFleets(ri) {
if !yield(fl) {
return
}
}
}
}
func (g Game) listIndexFleets(ri int) iter.Seq2[int, Fleet] {
return func(yield func(int, Fleet) bool) {
for i := range g.Fleets {
if g.Fleets[i].OwnerID == g.Race[ri].ID {
if !yield(i, g.Fleets[i]) {
return
}
}
}
}
}
+69 -1
View File
@@ -1,7 +1,75 @@
package game_test
import "testing"
import (
"slices"
"testing"
e "github.com/iliadenisov/galaxy/internal/error"
"github.com/stretchr/testify/assert"
)
func TestJoinShipGroupToFleet(t *testing.T) {
g := copyGame()
var groupIndex uint = 1
assert.ErrorContains(t,
g.JoinShipGroupToFleet(Race_0.Name, " ", groupIndex, 0),
e.GenericErrorText(e.ErrInputEntityTypeNameInvalid))
assert.ErrorContains(t,
g.JoinShipGroupToFleet(Race_0.Name, "Unnamed", groupIndex, 0),
e.GenericErrorText(e.ErrInputEntityNotExists))
// creating ShipGroup
assert.NoError(t, g.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 5))
assert.ErrorContains(t,
g.JoinShipGroupToFleet(Race_0.Name, "Unnamed", groupIndex, 6),
e.GenericErrorText(e.ErrJoinFleetGroupNumberNotEnough))
// ensure race has no Fleets
assert.Len(t, slices.Collect(g.ListFleets(Race_0_idx)), 0)
fleetOne := "R0_Fleet_one"
fleetTwo := "R0_Fleet_two"
assert.NoError(t, g.JoinShipGroupToFleet(Race_0.Name, fleetOne, groupIndex, 0))
fleets := slices.Collect(g.ListFleets(Race_0_idx))
assert.Len(t, fleets, 1)
assert.Equal(t, fleets[0].Name, fleetOne)
groups := slices.Collect(g.ListShipGroups(Race_0_idx))
assert.Len(t, groups, 1)
gi := 0
assert.NotNil(t, groups[gi].FleetID)
assert.Equal(t, fleets[0].ID, *groups[gi].FleetID)
// create another ShipGroup
assert.NoError(t, g.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 3))
groupIndex = 2
assert.NoError(t, g.JoinShipGroupToFleet(Race_0.Name, fleetTwo, groupIndex, 2))
fleets = slices.Collect(g.ListFleets(Race_0_idx))
assert.Len(t, fleets, 2)
assert.Equal(t, fleets[1].Name, fleetTwo)
groups = slices.Collect(g.ListShipGroups(Race_0_idx))
assert.Len(t, groups, 3)
gi = 1
assert.NotNil(t, groups[gi].FleetID)
assert.Equal(t, fleets[1].ID, *groups[gi].FleetID)
assert.Equal(t, uint(2), groups[gi].Number)
assert.Equal(t, uint(2), groups[gi].Index)
gi = 2
assert.Nil(t, groups[gi].FleetID)
assert.Equal(t, uint(1), groups[gi].Number)
assert.Equal(t, uint(3), groups[gi].Index)
groupIndex = groups[gi].Index
assert.NoError(t, g.JoinShipGroupToFleet(Race_0.Name, fleetOne, groupIndex, 0))
fleets = slices.Collect(g.ListFleets(Race_0_idx))
assert.Len(t, fleets, 2)
groups = slices.Collect(g.ListShipGroups(Race_0_idx))
assert.NotNil(t, groups[gi].FleetID)
assert.Equal(t, fleets[0].ID, *groups[gi].FleetID)
}
+1 -1
View File
@@ -98,7 +98,7 @@ func validateTypeName(v string) (string, bool) {
if len(s) > 0 {
return s, true
}
// TODO: special symbols
// TODO: special symbols AND include error check in all user-input test
return s, false
}
+5 -1
View File
@@ -6,6 +6,10 @@ func (g *Game) CreateShips(ri int, shipTypeName string, planetNumber uint, quant
return g.createShips(ri, shipTypeName, int(planetNumber), quantity)
}
func (g Game) ListShipGroups(ri int) iter.Seq2[int, ShipGroup] {
func (g Game) ListShipGroups(ri int) iter.Seq[ShipGroup] {
return g.listShipGroups(ri)
}
func (g Game) ListFleets(ri int) iter.Seq[Fleet] {
return g.listFleets(ri)
}
+16 -6
View File
@@ -121,7 +121,7 @@ func (g *Game) JoinEqualGroups(raceName string) error {
}
func (g *Game) joinEqualGroupsInternal(ri int) {
shipGroups := slices.Collect(maps.Values(maps.Collect(g.listShipGroups(ri))))
shipGroups := slices.Collect(maps.Values(maps.Collect(g.listIndexShipGroups(ri))))
origin := len(shipGroups)
if origin < 2 {
return
@@ -156,7 +156,7 @@ func (g *Game) createShips(ri int, shipTypeName string, planetNumber int, quanti
}
var maxIndex uint
for _, sg := range g.listShipGroups(ri) {
for _, sg := range g.listIndexShipGroups(ri) {
if sg.Index > maxIndex {
maxIndex = sg.Index
}
@@ -176,11 +176,21 @@ func (g *Game) createShips(ri int, shipTypeName string, planetNumber int, quanti
return nil
}
func (g Game) listShipGroups(ri int) iter.Seq2[int, ShipGroup] {
func (g Game) listShipGroups(ri int) iter.Seq[ShipGroup] {
return func(yield func(ShipGroup) bool) {
for _, sg := range g.listIndexShipGroups(ri) {
if !yield(sg) {
return
}
}
}
}
func (g Game) listIndexShipGroups(ri int) iter.Seq2[int, ShipGroup] {
return func(yield func(int, ShipGroup) bool) {
for sg := range g.ShipGroups {
if g.ShipGroups[sg].OwnerID == g.Race[ri].ID {
if !yield(sg, g.ShipGroups[sg]) {
for i := range g.ShipGroups {
if g.ShipGroups[i].OwnerID == g.Race[ri].ID {
if !yield(i, g.ShipGroups[i]) {
return
}
}
+15 -23
View File
@@ -1,7 +1,6 @@
package game_test
import (
"iter"
"math/rand/v2"
"slices"
"testing"
@@ -272,24 +271,25 @@ func TestShipGroupEqual(t *testing.T) {
func TestCreateShips(t *testing.T) {
g := copyGame()
var err error
err = g.CreateShips(Race_0_idx, "Unknown_Ship_Type", R0_Planet_0_num, 2)
assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputEntityNotExists))
err = g.CreateShips(Race_0_idx, Race_0_Gunship, R1_Planet_1_num, 2)
assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputEntityNotOwned))
assert.ErrorContains(t,
g.CreateShips(Race_0_idx, "Unknown_Ship_Type", R0_Planet_0_num, 2),
e.GenericErrorText(e.ErrInputEntityNotExists))
assert.ErrorContains(t,
g.CreateShips(Race_0_idx, Race_0_Gunship, R1_Planet_1_num, 2),
e.GenericErrorText(e.ErrInputEntityNotOwned))
assert.NoError(t, g.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 1))
assert.Len(t, collectGroups(g.ListShipGroups(Race_0_idx)), 1)
assert.Len(t, slices.Collect(g.ListShipGroups(Race_0_idx)), 1)
assert.NoError(t, g.CreateShips(Race_1_idx, Race_1_Freighter, R1_Planet_1_num, 1))
assert.Len(t, collectGroups(g.ListShipGroups(1)), 1)
assert.Len(t, slices.Collect(g.ListShipGroups(1)), 1)
assert.NoError(t, g.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 6))
assert.Len(t, collectGroups(g.ListShipGroups(Race_0_idx)), 2)
assert.Len(t, slices.Collect(g.ListShipGroups(Race_0_idx)), 2)
assert.NoError(t, g.CreateShips(Race_1_idx, Race_1_Gunship, R1_Planet_1_num, 1))
assert.Len(t, collectGroups(g.ListShipGroups(1)), 2)
assert.Len(t, slices.Collect(g.ListShipGroups(1)), 2)
}
func TestJoinEqualGroups(t *testing.T) {
@@ -307,16 +307,16 @@ func TestJoinEqualGroups(t *testing.T) {
assert.NoError(t, g.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 4)) // (6)
assert.NoError(t, g.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 4)) // (7)
assert.Len(t, collectGroups(g.ListShipGroups(Race_0_idx)), 7)
assert.Len(t, slices.Collect(g.ListShipGroups(Race_0_idx)), 7)
g.Race[Race_1_idx].Shields = 2.0
assert.NoError(t, g.CreateShips(1, Race_1_Freighter, R1_Planet_1_num, 1))
assert.Len(t, collectGroups(g.ListShipGroups(Race_1_idx)), 3)
assert.Len(t, slices.Collect(g.ListShipGroups(Race_1_idx)), 3)
assert.NoError(t, g.JoinEqualGroups(Race_0.Name))
assert.Len(t, collectGroups(g.ListShipGroups(Race_1_idx)), 3)
assert.Len(t, collectGroups(g.ListShipGroups(Race_0_idx)), 4)
assert.Len(t, slices.Collect(g.ListShipGroups(Race_1_idx)), 3)
assert.Len(t, slices.Collect(g.ListShipGroups(Race_0_idx)), 4)
shipTypeID := func(ri int, name string) uuid.UUID {
st := slices.IndexFunc(g.Race[ri].ShipTypes, func(v game.ShipType) bool { return v.Name == name })
@@ -327,7 +327,7 @@ func TestJoinEqualGroups(t *testing.T) {
return g.Race[ri].ShipTypes[st].ID
}
for _, sg := range g.ListShipGroups(Race_0_idx) {
for sg := range g.ListShipGroups(Race_0_idx) {
switch {
case sg.TypeID == shipTypeID(Race_0_idx, Race_0_Freighter) && sg.Drive == 1.1:
assert.Equal(t, uint(7), sg.Number)
@@ -346,11 +346,3 @@ func TestJoinEqualGroups(t *testing.T) {
}
}
}
func collectGroups(i iter.Seq2[int, game.ShipGroup]) []game.ShipGroup {
result := make([]game.ShipGroup, 0)
for _, sg := range i {
result = append(result, sg)
}
return result
}