refactor: battle at controller

This commit is contained in:
Ilia Denisov
2026-01-14 14:40:04 +02:00
parent 004529cdd3
commit 1bfc9242af
16 changed files with 583 additions and 546 deletions
+15 -65
View File
@@ -2,7 +2,6 @@ package controller
import (
"fmt"
"iter"
"slices"
"github.com/google/uuid"
@@ -30,6 +29,7 @@ func NewCache(g *game.Game) *Cache {
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 {
@@ -39,10 +39,11 @@ func (c *Cache) Relation(r1, r2 int) game.Relation {
if rel < 0 {
panic(fmt.Sprintf("Relation: opponent not found idx=%d", r2))
}
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
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
}
}
@@ -57,18 +58,17 @@ func (c *Cache) Relation(r1, r2 int) game.Relation {
}
}
func (c *Cache) Planet(planetNumber uint) *game.Planet {
if c.planetByPlanetNumber == nil {
c.planetByPlanetNumber = make(map[uint]*game.Planet)
for p := range c.g.Map.Planet {
c.planetByPlanetNumber[c.g.Map.Planet[p].Number] = &c.g.Map.Planet[p]
}
func (c *Cache) updateRelationCache(r1, r2 int, rel game.Relation) {
if r1 == r2 {
return
}
if v, ok := c.planetByPlanetNumber[planetNumber]; ok {
return v
} else {
panic(fmt.Sprintf("Planet: not found by number=%d", planetNumber))
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 {
@@ -97,56 +97,6 @@ func (c *Cache) RaceIndex(ID uuid.UUID) int {
}
}
// ShipGroup is a proxy func, nothing to cache
func (c *Cache) ShipGroup(groupIndex int) *game.ShipGroup {
c.validateShipGroupIndex(groupIndex)
return &c.g.ShipGroups[groupIndex]
}
func (c *Cache) ShipGroupsIndex() iter.Seq[int] {
return func(yield func(int) bool) {
for i := range c.g.ShipGroups {
if !yield(i) {
return
}
}
}
}
func (c *Cache) ShipGroupOwnerRaceIndex(groupIndex int) int {
if c.raceIndexByShipGroupIndex == nil {
c.fillShipsAndGroups()
}
c.validateShipGroupIndex(groupIndex)
if v, ok := c.raceIndexByShipGroupIndex[groupIndex]; ok {
return v
} else {
panic(fmt.Sprintf("ShipGroupRace: group not found by index=%v", groupIndex))
}
}
func (c *Cache) ShipGroupOwnerRace(groupIndex int) *game.Race {
return &c.g.Race[c.ShipGroupOwnerRaceIndex(groupIndex)]
}
func (c *Cache) ShipGroupNumber(i int, n uint) {
c.validateShipGroupIndex(i)
c.g.ShipGroups[i].Number = n
}
func (c *Cache) DeleteShipGroup(i int) {
c.validateShipGroupIndex(i)
c.unsafeDeleteShipGroup(i)
}
func (c *Cache) DeleteKilledShipGroups() {
for i := len(c.g.ShipGroups) - 1; i >= 0; i-- {
if c.g.ShipGroups[i].Number == 0 {
c.unsafeDeleteShipGroup(i)
}
}
}
func (c *Cache) unsafeDeleteShipGroup(i int) {
c.g.ShipGroups = append(c.g.ShipGroups[:i], c.g.ShipGroups[i+1:]...)
delete(c.raceIndexByShipGroupIndex, i)