test: unknown and extict races for commands

This commit is contained in:
Ilia Denisov
2026-02-08 14:57:38 +02:00
parent c36857e702
commit 175fb98c3a
17 changed files with 292 additions and 63 deletions
+14 -15
View File
@@ -13,7 +13,7 @@ import (
func (c *Cache) CreateShipType(ri int, typeName string, drive float64, ammo int, weapons, shileds, cargo float64) error {
c.validateRaceIndex(ri)
if err := checkShipTypeValues(drive, ammo, weapons, shileds, cargo); err != nil {
if err := validateShipTypeValues(drive, ammo, weapons, shileds, cargo); err != nil {
return err
}
n, ok := util.ValidateTypeName(typeName)
@@ -37,24 +37,24 @@ func (c *Cache) CreateShipType(ri int, typeName string, drive float64, ammo int,
return nil
}
func (c *Cache) MergeShipType(ri int, name, targetName string) error {
func (c *Cache) MergeShipType(ri int, sourceName, targetName string) error {
c.validateRaceIndex(ri)
st, sti, ok := c.ShipClass(ri, name)
sourceClass, sti, ok := c.ShipClass(ri, sourceName)
if !ok {
return e.NewEntityNotExistsError("source ship type %q", name)
return e.NewEntityNotExistsError("source ship type %q", sourceName)
}
tt, _, ok := c.ShipClass(ri, targetName)
targetClass, _, ok := c.ShipClass(ri, targetName)
if !ok {
return e.NewEntityNotExistsError("target ship type %q", name)
return e.NewEntityNotExistsError("target ship type %q", sourceName)
}
if st.Name == tt.Name {
if sourceClass.Name == targetClass.Name {
return e.NewEntityTypeNameEqualityError("ship type %q", targetName)
}
if !st.Equal(*tt) {
if !sourceClass.Equal(*targetClass) {
return e.NewMergeShipTypeNotEqualError()
}
@@ -63,16 +63,16 @@ func (c *Cache) MergeShipType(ri int, name, targetName string) error {
if c.g.Map.Planet[pl].OwnedBy(c.g.Race[ri].ID) &&
c.g.Map.Planet[pl].Production.Type == game.ProductionShip &&
c.g.Map.Planet[pl].Production.SubjectID != nil &&
*c.g.Map.Planet[pl].Production.SubjectID == st.ID {
*c.g.Map.Planet[pl].Production.SubjectID == sourceClass.ID {
c.g.Map.Planet[pl].Production.SubjectID = &tt.ID
c.g.Map.Planet[pl].Production.SubjectID = &targetClass.ID
}
}
// switch ship groups to the new type
for sg := range c.g.ShipGroups {
if c.g.ShipGroups[sg].OwnerID == c.g.Race[ri].ID && c.g.ShipGroups[sg].TypeID == st.ID {
c.g.ShipGroups[sg].TypeID = tt.ID
if c.g.ShipGroups[sg].OwnerID == c.g.Race[ri].ID && c.g.ShipGroups[sg].TypeID == sourceClass.ID {
c.g.ShipGroups[sg].TypeID = targetClass.ID
}
}
@@ -141,7 +141,6 @@ func (c *Cache) ShipClass(ri int, name string) (*game.ShipType, int, bool) {
}
func (c *Cache) ShipType(ri int, ID uuid.UUID) (*game.ShipType, bool) {
// TODO: cache + invalidate
c.validateRaceIndex(ri)
for i := range c.g.Race[ri].ShipTypes {
if c.g.Race[ri].ShipTypes[i].ID == ID {
@@ -155,10 +154,10 @@ func (c *Cache) MustShipType(ri int, ID uuid.UUID) *game.ShipType {
if v, ok := c.ShipType(ri, ID); ok {
return v
}
panic(fmt.Sprintf("ship_class not found: race_id=%d id=%v", ri, ID))
panic(fmt.Sprintf("ship class not found: race_idx=%d id=%v", ri, ID))
}
func checkShipTypeValues(d float64, a int, w, s, c float64) error {
func validateShipTypeValues(d float64, a int, w, s, c float64) error {
if !checkShipTypeValueDWSC(d) {
return e.NewDriveValueError(d)
}