cmd: join group to fleet
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package game
|
package game
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"iter"
|
||||||
"math"
|
"math"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
@@ -30,7 +31,7 @@ func (g Game) FleetSpeed(fl Fleet) float64 {
|
|||||||
return result
|
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)
|
ri, err := g.raceIndex(raceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -38,14 +39,14 @@ func (g Game) JoinShipGroupToFleet(raceName, fleetName string, group, count uint
|
|||||||
return g.joinShipGroupToFleetInternal(ri, fleetName, group, count)
|
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)
|
name, ok := validateTypeName(fleetName)
|
||||||
if !ok {
|
if !ok {
|
||||||
return e.NewEntityTypeNameValidationError("%q", name)
|
return e.NewEntityTypeNameValidationError("%q", name)
|
||||||
}
|
}
|
||||||
sgi := -1
|
sgi := -1
|
||||||
var maxIndex uint
|
var maxIndex uint
|
||||||
for i, sg := range g.listShipGroups(ri) {
|
for i, sg := range g.listIndexShipGroups(ri) {
|
||||||
if sgi < 0 && sg.Index == group {
|
if sgi < 0 && sg.Index == group {
|
||||||
sgi = i
|
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 })
|
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)
|
n, ok := validateTypeName(name)
|
||||||
if !ok {
|
if !ok {
|
||||||
return 0, e.NewEntityTypeNameValidationError("%q", n)
|
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
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,75 @@
|
|||||||
package game_test
|
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) {
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ func validateTypeName(v string) (string, bool) {
|
|||||||
if len(s) > 0 {
|
if len(s) > 0 {
|
||||||
return s, true
|
return s, true
|
||||||
}
|
}
|
||||||
// TODO: special symbols
|
// TODO: special symbols AND include error check in all user-input test
|
||||||
return s, false
|
return s, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,10 @@ func (g *Game) CreateShips(ri int, shipTypeName string, planetNumber uint, quant
|
|||||||
return g.createShips(ri, shipTypeName, int(planetNumber), quantity)
|
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)
|
return g.listShipGroups(ri)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g Game) ListFleets(ri int) iter.Seq[Fleet] {
|
||||||
|
return g.listFleets(ri)
|
||||||
|
}
|
||||||
|
|||||||
@@ -121,7 +121,7 @@ func (g *Game) JoinEqualGroups(raceName string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *Game) joinEqualGroupsInternal(ri int) {
|
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)
|
origin := len(shipGroups)
|
||||||
if origin < 2 {
|
if origin < 2 {
|
||||||
return
|
return
|
||||||
@@ -156,7 +156,7 @@ func (g *Game) createShips(ri int, shipTypeName string, planetNumber int, quanti
|
|||||||
}
|
}
|
||||||
|
|
||||||
var maxIndex uint
|
var maxIndex uint
|
||||||
for _, sg := range g.listShipGroups(ri) {
|
for _, sg := range g.listIndexShipGroups(ri) {
|
||||||
if sg.Index > maxIndex {
|
if sg.Index > maxIndex {
|
||||||
maxIndex = sg.Index
|
maxIndex = sg.Index
|
||||||
}
|
}
|
||||||
@@ -176,11 +176,21 @@ func (g *Game) createShips(ri int, shipTypeName string, planetNumber int, quanti
|
|||||||
return nil
|
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) {
|
return func(yield func(int, ShipGroup) bool) {
|
||||||
for sg := range g.ShipGroups {
|
for i := range g.ShipGroups {
|
||||||
if g.ShipGroups[sg].OwnerID == g.Race[ri].ID {
|
if g.ShipGroups[i].OwnerID == g.Race[ri].ID {
|
||||||
if !yield(sg, g.ShipGroups[sg]) {
|
if !yield(i, g.ShipGroups[i]) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package game_test
|
package game_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"iter"
|
|
||||||
"math/rand/v2"
|
"math/rand/v2"
|
||||||
"slices"
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -272,24 +271,25 @@ func TestShipGroupEqual(t *testing.T) {
|
|||||||
|
|
||||||
func TestCreateShips(t *testing.T) {
|
func TestCreateShips(t *testing.T) {
|
||||||
g := copyGame()
|
g := copyGame()
|
||||||
var err error
|
|
||||||
|
|
||||||
err = g.CreateShips(Race_0_idx, "Unknown_Ship_Type", R0_Planet_0_num, 2)
|
assert.ErrorContains(t,
|
||||||
assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputEntityNotExists))
|
g.CreateShips(Race_0_idx, "Unknown_Ship_Type", R0_Planet_0_num, 2),
|
||||||
err = g.CreateShips(Race_0_idx, Race_0_Gunship, R1_Planet_1_num, 2)
|
e.GenericErrorText(e.ErrInputEntityNotExists))
|
||||||
assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputEntityNotOwned))
|
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.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.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.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.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) {
|
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_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.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
|
g.Race[Race_1_idx].Shields = 2.0
|
||||||
assert.NoError(t, g.CreateShips(1, Race_1_Freighter, R1_Planet_1_num, 1))
|
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.NoError(t, g.JoinEqualGroups(Race_0.Name))
|
||||||
|
|
||||||
assert.Len(t, collectGroups(g.ListShipGroups(Race_1_idx)), 3)
|
assert.Len(t, slices.Collect(g.ListShipGroups(Race_1_idx)), 3)
|
||||||
assert.Len(t, collectGroups(g.ListShipGroups(Race_0_idx)), 4)
|
assert.Len(t, slices.Collect(g.ListShipGroups(Race_0_idx)), 4)
|
||||||
|
|
||||||
shipTypeID := func(ri int, name string) uuid.UUID {
|
shipTypeID := func(ri int, name string) uuid.UUID {
|
||||||
st := slices.IndexFunc(g.Race[ri].ShipTypes, func(v game.ShipType) bool { return v.Name == name })
|
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
|
return g.Race[ri].ShipTypes[st].ID
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, sg := range g.ListShipGroups(Race_0_idx) {
|
for sg := range g.ListShipGroups(Race_0_idx) {
|
||||||
switch {
|
switch {
|
||||||
case sg.TypeID == shipTypeID(Race_0_idx, Race_0_Freighter) && sg.Drive == 1.1:
|
case sg.TypeID == shipTypeID(Race_0_idx, Race_0_Freighter) && sg.Drive == 1.1:
|
||||||
assert.Equal(t, uint(7), sg.Number)
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user