From b928bb2976eef29acf5b86ad9b7c3917a818a2bf Mon Sep 17 00:00:00 2001 From: Ilia Denisov Date: Sun, 8 Feb 2026 19:29:38 +0200 Subject: [PATCH] refactor: group launched/in_space state --- internal/controller/battle_test.go | 2 +- internal/controller/bombing_test.go | 5 +--- internal/controller/controller_test.go | 7 ++++++ internal/controller/fleet_send_test.go | 2 +- internal/controller/fleet_test.go | 7 ++---- internal/controller/route_test.go | 10 ++------ internal/controller/ship_group_move.go | 5 ++-- internal/controller/ship_group_move_test.go | 5 +--- internal/controller/ship_group_send.go | 4 +-- internal/controller/ship_group_send_test.go | 10 ++++---- internal/controller/ship_group_test.go | 25 ++++--------------- .../controller/ship_group_upgrade_test.go | 2 +- internal/model/game/group.go | 17 +++++++------ internal/model/game/group_test.go | 4 ++- 14 files changed, 43 insertions(+), 62 deletions(-) diff --git a/internal/controller/battle_test.go b/internal/controller/battle_test.go index 7fc72c1..74167f4 100644 --- a/internal/controller/battle_test.go +++ b/internal/controller/battle_test.go @@ -68,7 +68,7 @@ func TestCollectPlanetGroups(t *testing.T) { assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 10)) // 1 #0 assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 3)) // 2 #1 assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 3)) // 3 #2 - c.ShipGroup(2).StateInSpace = &game.InSpace{Origin: 2, Range: 1.23} // 3 #2 -> In_Space + c.ShipGroup(2).StateInSpace = &InSpace // 3 #2 -> In_Space assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 1)) // 4 #3 c.ShipGroup(3).Destination = R1_Planet_1_num // 4 #3 -> Planet_1 assert.NoError(t, c.CreateShips(Race_1_idx, Race_1_Gunship, R1_Planet_1_num, 15)) // 5 #4 diff --git a/internal/controller/bombing_test.go b/internal/controller/bombing_test.go index 1debcde..8450fdb 100644 --- a/internal/controller/bombing_test.go +++ b/internal/controller/bombing_test.go @@ -50,10 +50,7 @@ func TestCollectBombingGroups(t *testing.T) { // 3: idx = 2 / In_Space assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 1)) - c.ShipGroup(2).StateInSpace = &game.InSpace{ - Origin: 2, - Range: 31.337, - } + c.ShipGroup(2).StateInSpace = &InSpace // 4: idx = 3 / Has no Ammo assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 1)) diff --git a/internal/controller/controller_test.go b/internal/controller/controller_test.go index fe38c14..972f1d3 100644 --- a/internal/controller/controller_test.go +++ b/internal/controller/controller_test.go @@ -99,6 +99,8 @@ var ( BadEntityName = "Bad(entitty)Name" UnknownRace = "UnknownRace" + + InSpace = game.InSpace{Origin: 2, X: floatRef(1.23), Y: floatRef(1.23)} ) // [ ] Delete this fake test @@ -153,3 +155,8 @@ func newCache() (*controller.Cache, *controller.Controller) { return c, ctl } + +func floatRef(v float64) *game.Float { + f := game.Float(v) + return &f +} diff --git a/internal/controller/fleet_send_test.go b/internal/controller/fleet_send_test.go index 4c3bd25..7c00bec 100644 --- a/internal/controller/fleet_send_test.go +++ b/internal/controller/fleet_send_test.go @@ -48,7 +48,7 @@ func TestSendFleet(t *testing.T) { assert.Len(t, slices.Collect(c.ListFleets(Race_0_idx)), 3) // group #2 - in_space - c.ShipGroup(1).StateInSpace = &game.InSpace{Origin: 2, Range: 1.23} + c.ShipGroup(1).StateInSpace = &InSpace assert.ErrorContains(t, g.SendFleet(UnknownRace, fleetSending, 2), diff --git a/internal/controller/fleet_test.go b/internal/controller/fleet_test.go index a1425fc..2b1459c 100644 --- a/internal/controller/fleet_test.go +++ b/internal/controller/fleet_test.go @@ -92,17 +92,14 @@ func TestJoinShipGroupToFleet(t *testing.T) { // group not In_Orbit assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 7)) gi = 3 - c.ShipGroup(gi).StateInSpace = &game.InSpace{ - Origin: 2, - Range: 1, - } + c.ShipGroup(gi).StateInSpace = &InSpace assert.ErrorContains(t, g.JoinShipGroupToFleet(Race_0.Name, fleetOne, c.ShipGroup(gi).Index, 0), e.GenericErrorText(e.ErrShipsBusy)) c.ShipGroup(gi).StateInSpace = nil // existing fleet not on the same planet or in_orbit - c.ShipGroup(0).StateInSpace = &game.InSpace{Origin: 2, Range: 1} + c.ShipGroup(0).StateInSpace = &InSpace c.ShipGroup(1).StateInSpace = c.ShipGroup(0).StateInSpace assert.ErrorContains(t, g.JoinShipGroupToFleet(Race_0.Name, fleetOne, c.ShipGroup(gi).Index, 0), diff --git a/internal/controller/route_test.go b/internal/controller/route_test.go index bfe99fe..d80e415 100644 --- a/internal/controller/route_test.go +++ b/internal/controller/route_test.go @@ -109,10 +109,7 @@ func TestListRoutedSendGroupIds(t *testing.T) { // 3: idx = 2 / In_Space assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7)) - c.ShipGroup(2).StateInSpace = &game.InSpace{ - Origin: 2, - Range: 31.337, - } + c.ShipGroup(2).StateInSpace = &InSpace // 4: idx = 3 / loaded with COL assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 11)) @@ -275,10 +272,7 @@ func TestListRoutedUnloadShipGroupIds(t *testing.T) { // 3: idx = 2 / In_Space assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7)) - c.ShipGroup(2).StateInSpace = &game.InSpace{ - Origin: 2, - Range: 31.337, - } + c.ShipGroup(2).StateInSpace = &InSpace // 4: idx = 3 / loaded with COL assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 11)) diff --git a/internal/controller/ship_group_move.go b/internal/controller/ship_group_move.go index 4395759..b2f6d2a 100644 --- a/internal/controller/ship_group_move.go +++ b/internal/controller/ship_group_move.go @@ -42,8 +42,9 @@ func (c *Cache) moveShipGroup(i int, delta float64) { var x, y float64 x, y, arrived = util.NextTravelCoord(c.g.Map.Width, c.g.Map.Height, originX, originY, destPlanet.X.F(), destPlanet.Y.F(), delta) - sg.StateInSpace.X = game.F(x) - sg.StateInSpace.Y = game.F(y) + fx, fy := game.F(x), game.F(y) + sg.StateInSpace.X = &fx + sg.StateInSpace.Y = &fy if arrived { sg.StateInSpace = nil } diff --git a/internal/controller/ship_group_move_test.go b/internal/controller/ship_group_move_test.go index a167f5c..0f2cf47 100644 --- a/internal/controller/ship_group_move_test.go +++ b/internal/controller/ship_group_move_test.go @@ -24,10 +24,7 @@ func TestListMoveableGroupIds(t *testing.T) { // 4: idx = 3 / [v] In_Space assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7)) - c.ShipGroup(3).StateInSpace = &game.InSpace{ - Origin: 2, - Range: 31.337, - } + c.ShipGroup(3).StateInSpace = &InSpace // 5: idx = 4 / [x] In_Upgrage assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7)) diff --git a/internal/controller/ship_group_send.go b/internal/controller/ship_group_send.go index 2e6f327..27cbb03 100644 --- a/internal/controller/ship_group_send.go +++ b/internal/controller/ship_group_send.go @@ -97,8 +97,8 @@ func (c *Cache) UnsendShips(sg *game.ShipGroup) *game.ShipGroup { func LaunchShips(sg game.ShipGroup, destination uint, originX, originY float64) game.ShipGroup { sg.StateInSpace = &game.InSpace{ Origin: sg.Destination, - X: game.F(originX), - Y: game.F(originY), + X: nil, + Y: nil, } sg.Destination = destination return sg diff --git a/internal/controller/ship_group_send_test.go b/internal/controller/ship_group_send_test.go index 7b46ed1..4ab06e2 100644 --- a/internal/controller/ship_group_send_test.go +++ b/internal/controller/ship_group_send_test.go @@ -17,7 +17,7 @@ func TestSendGroup(t *testing.T) { // group #2 - in_space assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 1)) // g.ShipGroups[1].StateInSpace = &game.InSpace{Origin: 2, Range: 1.23} - c.ShipGroup(1).StateInSpace = &game.InSpace{Origin: 2, Range: 1.23} + c.ShipGroup(1).StateInSpace = &InSpace // group #3 - in_orbit, unmovable g.CreateShipType(Race_0.Name, "Fortress", 0, 50, 30, 100, 0) assert.NoError(t, c.CreateShips(Race_0_idx, "Fortress", R0_Planet_0_num, 1)) @@ -54,8 +54,8 @@ func TestSendGroup(t *testing.T) { assert.Equal(t, uint(3), c.ShipGroup(3).Number) assert.Equal(t, game.StateLaunched, c.ShipGroup(3).State()) assert.NotNil(t, c.ShipGroup(3).StateInSpace) - assert.Equal(t, c.MustPlanet(R0_Planet_0_num).X, c.ShipGroup(3).StateInSpace.X) - assert.Equal(t, c.MustPlanet(R0_Planet_0_num).Y, c.ShipGroup(3).StateInSpace.Y) + assert.Nil(t, c.ShipGroup(3).StateInSpace.X) + assert.Nil(t, c.ShipGroup(3).StateInSpace.Y) assert.NoError(t, g.SendGroup(Race_0.Name, 4, R0_Planet_0_num, 2)) // un-send 2 of 3 assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 4) @@ -64,8 +64,8 @@ func TestSendGroup(t *testing.T) { assert.Equal(t, uint(1), c.MustShipGroup(Race_0_idx, 4).Number) assert.Equal(t, game.StateLaunched, c.MustShipGroup(Race_0_idx, 4).State()) assert.NotNil(t, c.MustShipGroup(Race_0_idx, 4).StateInSpace) - assert.Equal(t, c.MustPlanet(R0_Planet_0_num).X, c.MustShipGroup(Race_0_idx, 4).StateInSpace.X) - assert.Equal(t, c.MustPlanet(R0_Planet_0_num).Y, c.MustShipGroup(Race_0_idx, 4).StateInSpace.Y) + assert.Nil(t, c.MustShipGroup(Race_0_idx, 4).StateInSpace.X) + assert.Nil(t, c.MustShipGroup(Race_0_idx, 4).StateInSpace.Y) assert.NoError(t, g.SendGroup(Race_0.Name, 4, R0_Planet_0_num, 0)) // un-send the rest 1 assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 3) diff --git a/internal/controller/ship_group_test.go b/internal/controller/ship_group_test.go index b3e766c..6b18758 100644 --- a/internal/controller/ship_group_test.go +++ b/internal/controller/ship_group_test.go @@ -102,10 +102,7 @@ func TestBreakGroup(t *testing.T) { c, g := newCache() assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 13)) // group #1 (0) assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 7)) // group #2 (1) - In_Space - c.ShipGroup(1).StateInSpace = &game.InSpace{ - Origin: 1, - Range: 1, - } + c.ShipGroup(1).StateInSpace = &InSpace fleet := "R0_Fleet" assert.NoError(t, g.JoinShipGroupToFleet(Race_0.Name, fleet, 1, 0)) @@ -178,10 +175,7 @@ func TestTransferGroup(t *testing.T) { assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 17)) // group #2 (2) - In_Space assert.NoError(t, g.JoinShipGroupToFleet(Race_0.Name, "R0_Fleet", 2, 0)) assert.NotNil(t, c.ShipGroup(2).FleetID) - c.ShipGroup(2).StateInSpace = &game.InSpace{ - Origin: 2, - Range: 31.337, - } + c.ShipGroup(2).StateInSpace = &InSpace c.ShipGroup(2).CargoType = game.CargoMaterial.Ref() c.ShipGroup(2).Load = 1.234 @@ -271,10 +265,7 @@ func TestLoadCargo(t *testing.T) { // 3: idx = 2 / In_Space assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7)) - c.ShipGroup(2).StateInSpace = &game.InSpace{ - Origin: 2, - Range: 31.337, - } + c.ShipGroup(2).StateInSpace = &InSpace // 4: idx = 3 / loaded with COL assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 11)) @@ -394,10 +385,7 @@ func TestUnloadCargo(t *testing.T) { // 3: idx = 2 / In_Space assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7)) - c.ShipGroup(2).StateInSpace = &game.InSpace{ - Origin: 2, - Range: 31.337, - } + c.ShipGroup(2).StateInSpace = &InSpace // 4: idx = 3 / loaded with COL assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 11)) @@ -491,10 +479,7 @@ func TestDisassembleGroup(t *testing.T) { // 2: idx = 1 / In_Space assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7)) - c.ShipGroup(1).StateInSpace = &game.InSpace{ - Origin: 2, - Range: 31.337, - } + c.ShipGroup(1).StateInSpace = &InSpace // 3: idx = 2 / loaded with COL assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 10)) diff --git a/internal/controller/ship_group_upgrade_test.go b/internal/controller/ship_group_upgrade_test.go index f0765d1..0459e3e 100644 --- a/internal/controller/ship_group_upgrade_test.go +++ b/internal/controller/ship_group_upgrade_test.go @@ -119,7 +119,7 @@ func TestUpgradeGroup(t *testing.T) { assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 10)) // group #2 - in_space assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 1)) - c.ShipGroup(1).StateInSpace = &game.InSpace{Origin: 2, Range: 1.23} + c.ShipGroup(1).StateInSpace = &InSpace // group #3 - in_orbit, foreign planet assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 1)) c.ShipGroup(2).Destination = R1_Planet_1_num diff --git a/internal/model/game/group.go b/internal/model/game/group.go index ba94ab3..b398edb 100644 --- a/internal/model/game/group.go +++ b/internal/model/game/group.go @@ -46,12 +46,10 @@ func (sgs ShipGroupState) String() string { } type InSpace struct { - Origin uint `json:"origin"` - X Float `json:"x"` - Y Float `json:"y"` - // zero is for Launched status - // TODO: calculate range dynamically -BUT- if affects ShipGroup.State() - Range Float `json:"range"` + // X, Y are nil for Launched state + Origin uint `json:"origin"` + X *Float `json:"x,omitempty"` + Y *Float `json:"y,omitempty"` } func (is InSpace) Equal(other InSpace) bool { @@ -59,7 +57,10 @@ func (is InSpace) Equal(other InSpace) bool { } func (is InSpace) Launched() bool { - return is.Range == 0 + if (is.X == nil && is.Y != nil) || (is.X != nil && is.Y == nil) { + panic("group in space state invalid: one of coordinate is not set") + } + return is.X == nil && is.Y == is.X } type InUpgrade struct { @@ -132,7 +133,7 @@ func (sg ShipGroup) State() ShipGroupState { case sg.StateInSpace == nil && sg.StateUpgrade == nil: return StateInOrbit case sg.StateInSpace != nil && sg.StateUpgrade == nil: - if sg.StateInSpace.Range > 0 { + if !sg.StateInSpace.Launched() { return StateInSpace } if sg.StateTransfer { diff --git a/internal/model/game/group_test.go b/internal/model/game/group_test.go index 2f02c7c..14b20d9 100644 --- a/internal/model/game/group_test.go +++ b/internal/model/game/group_test.go @@ -198,9 +198,11 @@ func TestShipGroupEqual(t *testing.T) { assert.False(t, left.Equal(right)) right = *left + coord := game.Float(1) left.StateInSpace = &game.InSpace{ Origin: 1, - Range: 1, + X: &coord, + Y: &coord, } assert.False(t, left.Equal(right))