wip: refactor controller

This commit is contained in:
Ilia Denisov
2026-01-14 22:17:24 +02:00
parent 1bfc9242af
commit fe8a8d4150
43 changed files with 4710 additions and 2188 deletions
+22 -61
View File
@@ -11,6 +11,7 @@ import (
type Cache struct {
g *game.Game
cacheRaceIndexByID map[uuid.UUID]int
cacheFleetIndexByID map[uuid.UUID]int
raceIndexByShipGroupIndex map[int]int
shipClassByShipGroupIndex map[int]*game.ShipType
planetByPlanetNumber map[uint]*game.Planet
@@ -27,53 +28,9 @@ func NewCache(g *game.Game) *Cache {
return c
}
func (c *Cache) Relation(r1, r2 int) game.Relation {
if c.cacheRelation == nil {
c.cacheRelation = make(map[int]map[int]game.Relation)
for r1 := range c.g.Race {
for r2 := range c.g.Race {
if r1 == r2 {
continue
}
rel := slices.IndexFunc(c.g.Race[r1].Relations, func(r game.RaceRelation) bool { return r.RaceID == c.g.Race[r2].ID })
if rel < 0 {
panic(fmt.Sprintf("Relation: opponent not found idx=%d", r2))
}
c.updateRelationCache(r1, r2, c.g.Race[r1].Relations[rel].Relation)
// if _, ok := c.cacheRelation[r1]; !ok {
// c.cacheRelation[r1] = make(map[int]game.Relation)
// }
// c.cacheRelation[r1][r2] = c.g.Race[r1].Relations[rel].Relation
}
}
}
if _, ok := c.cacheRelation[r1]; !ok {
panic(fmt.Sprintf("Relation: no left race idx=%d", r1))
}
if v, ok := c.cacheRelation[r1][r2]; !ok {
panic(fmt.Sprintf("Relation: no right race idx=%d", r2))
} else {
return v
}
}
func (c *Cache) updateRelationCache(r1, r2 int, rel game.Relation) {
if r1 == r2 {
return
}
if c.cacheRelation == nil {
c.cacheRelation = make(map[int]map[int]game.Relation)
}
if _, ok := c.cacheRelation[r1]; !ok {
c.cacheRelation[r1] = make(map[int]game.Relation)
}
c.cacheRelation[r1][r2] = rel
}
func (c *Cache) ShipGroupShipClass(groupIndex int) *game.ShipType {
if c.shipClassByShipGroupIndex == nil {
c.fillShipsAndGroups()
if c.shipClassByShipGroupIndex == nil || len(c.shipClassByShipGroupIndex) == 0 {
c.cacheShipsAndGroups()
}
c.validateShipGroupIndex(groupIndex)
if v, ok := c.shipClassByShipGroupIndex[groupIndex]; ok {
@@ -97,21 +54,7 @@ func (c *Cache) RaceIndex(ID uuid.UUID) int {
}
}
func (c *Cache) unsafeDeleteShipGroup(i int) {
c.g.ShipGroups = append(c.g.ShipGroups[:i], c.g.ShipGroups[i+1:]...)
delete(c.raceIndexByShipGroupIndex, i)
delete(c.shipClassByShipGroupIndex, i)
}
// Internal
func (c *Cache) validateShipGroupIndex(i int) {
if i >= len(c.g.ShipGroups) {
panic(fmt.Sprintf("group index out of groups len: %d >= %d", i, len(c.g.ShipGroups)))
}
}
func (c *Cache) fillShipsAndGroups() {
func (c *Cache) cacheShipsAndGroups() {
if c.raceIndexByShipGroupIndex != nil {
clear(c.raceIndexByShipGroupIndex)
} else {
@@ -133,6 +76,24 @@ func (c *Cache) fillShipsAndGroups() {
}
}
func (c *Cache) cacheShipGroup(groupIndex, ri int, class *game.ShipType) {
if c.raceIndexByShipGroupIndex != nil {
c.raceIndexByShipGroupIndex[groupIndex] = ri
}
if c.shipClassByShipGroupIndex != nil {
c.shipClassByShipGroupIndex[groupIndex] = class
}
}
func (c *Cache) invalidateShipGroupCache() {
if c.raceIndexByShipGroupIndex != nil {
clear(c.raceIndexByShipGroupIndex)
}
if c.shipClassByShipGroupIndex != nil {
clear(c.shipClassByShipGroupIndex)
}
}
// Helpers
func ShipClassIndex(g *game.Game, ri int, classID uuid.UUID) (int, bool) {