race quit, transfer state, refactor

This commit is contained in:
Ilia Denisov
2026-02-07 01:59:11 +02:00
parent 43ba5eb07c
commit fc73cbf83a
27 changed files with 520 additions and 341 deletions
+17 -41
View File
@@ -13,18 +13,6 @@ import (
"github.com/iliadenisov/galaxy/internal/util"
)
func (c *Controller) SetRoute(raceName, loadType string, origin, destination uint) error {
ri, err := c.Cache.raceIndex(raceName)
if err != nil {
return err
}
rt, ok := game.RouteTypeSet[loadType]
if !ok {
return e.NewCargoTypeInvalidError(loadType)
}
return c.Cache.SetRoute(ri, rt, origin, destination)
}
func (c *Cache) SetRoute(ri int, rt game.RouteType, origin, destination uint) error {
c.validateRaceIndex(ri)
p1, ok := c.Planet(origin)
@@ -48,18 +36,6 @@ func (c *Cache) SetRoute(ri int, rt game.RouteType, origin, destination uint) er
return nil
}
func (c *Controller) RemoveRoute(raceName, loadType string, origin uint) error {
ri, err := c.Cache.raceIndex(raceName)
if err != nil {
return err
}
rt, ok := game.RouteTypeSet[loadType]
if !ok {
return e.NewCargoTypeInvalidError(loadType)
}
return c.Cache.RemoveRoute(ri, rt, origin)
}
func (c *Cache) RemoveRoute(ri int, rt game.RouteType, origin uint) error {
c.validateRaceIndex(ri)
p1, ok := c.Planet(origin)
@@ -269,23 +245,6 @@ func (c *Cache) selectColUnloadGroup(groups []int) (result iter.Seq[int]) {
return
}
func MaxOrRandomLoadId(IDtoLoad map[int]float64) int {
if len(IDtoLoad) < 2 {
panic("IDtoLoad must contain at least 2 keys")
}
IDs := slices.Collect(maps.Keys(IDtoLoad))
slices.SortFunc(IDs, func(id1, id2 int) int { return cmp.Compare(IDtoLoad[id2], IDtoLoad[id1]) })
// no single winner with highest load
if IDtoLoad[IDs[0]] == IDtoLoad[IDs[1]] {
// remove IDs which load less than maximum
IDs = slices.DeleteFunc(IDs, func(v int) bool { return IDtoLoad[v] < IDtoLoad[IDs[0]] })
// IDs[0] will have random index
rand.Shuffle(len(IDs), func(i, j int) { IDs[i], IDs[j] = IDs[j], IDs[i] })
}
return IDs[0]
}
func (c *Cache) listRoutedUnloadShipGroupIds(pn uint, routeType game.RouteType) iter.Seq[int] {
return func(yield func(int) bool) {
yielded := make(map[int]bool)
@@ -311,3 +270,20 @@ func (c *Cache) listRoutedUnloadShipGroupIds(pn uint, routeType game.RouteType)
}
}
}
func MaxOrRandomLoadId(IDtoLoad map[int]float64) int {
if len(IDtoLoad) < 2 {
panic("IDtoLoad must contain at least 2 keys")
}
IDs := slices.Collect(maps.Keys(IDtoLoad))
slices.SortFunc(IDs, func(id1, id2 int) int { return cmp.Compare(IDtoLoad[id2], IDtoLoad[id1]) })
// no single winner with highest load
if IDtoLoad[IDs[0]] == IDtoLoad[IDs[1]] {
// remove IDs which load less than maximum
IDs = slices.DeleteFunc(IDs, func(v int) bool { return IDtoLoad[v] < IDtoLoad[IDs[0]] })
// IDs[0] will have random index
rand.Shuffle(len(IDs), func(i, j int) { IDs[i], IDs[j] = IDs[j], IDs[i] })
}
return IDs[0]
}