From f394c105b03052968b15c30dfb1de09a0f623f27 Mon Sep 17 00:00:00 2001 From: Ilia Denisov Date: Tue, 17 Feb 2026 19:57:08 +0200 Subject: [PATCH] fix: ship send without any planets left --- internal/controller/ship_group_send.go | 17 +++++++++++------ internal/controller/ship_group_send_test.go | 8 ++++++++ internal/error/generic.go | 5 ++++- internal/error/input.go | 4 ++++ 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/internal/controller/ship_group_send.go b/internal/controller/ship_group_send.go index 49dd8e4..7914813 100644 --- a/internal/controller/ship_group_send.go +++ b/internal/controller/ship_group_send.go @@ -7,12 +7,6 @@ import ( "github.com/iliadenisov/galaxy/internal/util" ) -/* -TODO: остались ли планеты у расы - -Если у расы не осталось планет, то оставшиеся корабли не смогут -покинуть своего места пребывания. -*/ func (c *Cache) shipGroupSend(ri int, groupID uuid.UUID, planetNumber uint) error { c.validateRaceIndex(ri) @@ -22,6 +16,17 @@ func (c *Cache) shipGroupSend(ri int, groupID uuid.UUID, planetNumber uint) erro } st := c.ShipGroupShipClass(sgi) + pcount := 0 + for i := range c.g.Map.Planet { + if c.g.Map.Planet[i].OwnedBy(c.g.Race[ri].ID) { + pcount++ + break + } + } + if pcount == 0 { + return e.NewSendShipOwnerHasNoPlanetsError() + } + if st.DriveBlockMass() == 0 { return e.NewSendShipHasNoDrivesError() } diff --git a/internal/controller/ship_group_send_test.go b/internal/controller/ship_group_send_test.go index 36ec914..016a4aa 100644 --- a/internal/controller/ship_group_send_test.go +++ b/internal/controller/ship_group_send_test.go @@ -21,6 +21,11 @@ func TestShipGroupSend(t *testing.T) { g.ShipClassCreate(Race_0.Name, "Fortress", 0, 50, 30, 100, 0) assert.NoError(t, c.CreateShips(Race_0_idx, "Fortress", R0_Planet_0_num, 1)) + shiplessRace := "Shipless" + ri, _ := c.AddRace(shiplessRace) + assert.NoError(t, c.ShipClassCreate(ri, "Drone", 1, 0, 0, 0, 0)) + sgi := c.CreateShipsUnsafe_T(ri, c.MustShipClass(ri, "Drone").ID, R0_Planet_0_num, 1) + assert.ErrorContains(t, g.ShipGroupSend(UnknownRace, c.ShipGroup(0).ID, 2), e.GenericErrorText(e.ErrInputUnknownRace)) @@ -36,6 +41,9 @@ func TestShipGroupSend(t *testing.T) { assert.ErrorContains(t, g.ShipGroupSend(Race_0.Name, c.ShipGroup(1).ID, 1), e.GenericErrorText(e.ErrShipsBusy)) + assert.ErrorContains(t, + g.ShipGroupSend(shiplessRace, c.ShipGroup(sgi).ID, 2), + e.GenericErrorText(e.ErrSendShipOwnerHasNoPlanets)) assert.ErrorContains(t, g.ShipGroupSend(Race_0.Name, c.ShipGroup(2).ID, 2), e.GenericErrorText(e.ErrSendShipHasNoDrives)) diff --git a/internal/error/generic.go b/internal/error/generic.go index 262d0e5..084cb41 100644 --- a/internal/error/generic.go +++ b/internal/error/generic.go @@ -27,7 +27,8 @@ const ( ErrUpgradeInsufficientResources = 5011 ErrSendShipHasNoDrives = 5012 ErrSendUnreachableDestination = 5013 - ErrRaceExinct = 5014 + ErrSendShipOwnerHasNoPlanets = 5014 + ErrRaceExinct = 5015 ) const ( @@ -165,6 +166,8 @@ func GenericErrorText(code int) string { return "One or more ships are not equipped with hyperdrive and cannot be moved" case ErrSendUnreachableDestination: return "Destination planet is too far for current Drive level" + case ErrSendShipOwnerHasNoPlanets: + return "Race is not owning any planet, all flights impossible" case ErrRaceExinct: return "Race is extinct" default: diff --git a/internal/error/input.go b/internal/error/input.go index 6a0957d..862f0ca 100644 --- a/internal/error/input.go +++ b/internal/error/input.go @@ -167,3 +167,7 @@ func NewSendShipHasNoDrivesError(arg ...any) error { func NewSendUnreachableDestinationError(arg ...any) error { return newGenericError(ErrSendUnreachableDestination, arg...) } + +func NewSendShipOwnerHasNoPlanetsError(arg ...any) error { + return newGenericError(ErrSendShipOwnerHasNoPlanets, arg...) +}