refactor: game funcs moved to controller
This commit is contained in:
@@ -9,13 +9,13 @@ 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
|
||||
cacheRelation map[int]map[int]game.Relation
|
||||
g *game.Game
|
||||
cacheRaceIndexByID map[uuid.UUID]int
|
||||
cacheFleetIndexByID map[uuid.UUID]int
|
||||
cacheRaceIndexByShipGroupIndex map[int]int
|
||||
cacheShipClassByShipGroupIndex map[int]*game.ShipType
|
||||
cachePlanetByPlanetNumber map[uint]*game.Planet
|
||||
cacheRelation map[int]map[int]game.Relation
|
||||
}
|
||||
|
||||
func NewCache(g *game.Game) *Cache {
|
||||
@@ -29,11 +29,11 @@ func NewCache(g *game.Game) *Cache {
|
||||
}
|
||||
|
||||
func (c *Cache) ShipGroupShipClass(groupIndex int) *game.ShipType {
|
||||
if c.shipClassByShipGroupIndex == nil || len(c.shipClassByShipGroupIndex) == 0 {
|
||||
if len(c.cacheShipClassByShipGroupIndex) == 0 {
|
||||
c.cacheShipsAndGroups()
|
||||
}
|
||||
c.validateShipGroupIndex(groupIndex)
|
||||
if v, ok := c.shipClassByShipGroupIndex[groupIndex]; ok {
|
||||
if v, ok := c.cacheShipClassByShipGroupIndex[groupIndex]; ok {
|
||||
return v
|
||||
} else {
|
||||
panic(fmt.Sprintf("ShipClassByShipGroupIndex: group not found by index=%v", groupIndex))
|
||||
@@ -55,43 +55,34 @@ func (c *Cache) RaceIndex(ID uuid.UUID) int {
|
||||
}
|
||||
|
||||
func (c *Cache) cacheShipsAndGroups() {
|
||||
if c.raceIndexByShipGroupIndex != nil {
|
||||
clear(c.raceIndexByShipGroupIndex)
|
||||
if c.cacheRaceIndexByShipGroupIndex != nil {
|
||||
clear(c.cacheRaceIndexByShipGroupIndex)
|
||||
} else {
|
||||
c.raceIndexByShipGroupIndex = make(map[int]int)
|
||||
c.cacheRaceIndexByShipGroupIndex = make(map[int]int)
|
||||
}
|
||||
if c.shipClassByShipGroupIndex != nil {
|
||||
clear(c.shipClassByShipGroupIndex)
|
||||
if c.cacheShipClassByShipGroupIndex != nil {
|
||||
clear(c.cacheShipClassByShipGroupIndex)
|
||||
} else {
|
||||
c.shipClassByShipGroupIndex = make(map[int]*game.ShipType)
|
||||
c.cacheShipClassByShipGroupIndex = make(map[int]*game.ShipType)
|
||||
}
|
||||
for groupIndex := range c.g.ShipGroups {
|
||||
ri := c.RaceIndex(c.g.ShipGroups[groupIndex].OwnerID)
|
||||
c.raceIndexByShipGroupIndex[groupIndex] = ri
|
||||
c.cacheRaceIndexByShipGroupIndex[groupIndex] = ri
|
||||
sti, ok := ShipClassIndex(c.g, ri, c.g.ShipGroups[groupIndex].TypeID)
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("CollectPlanetGroups: ship class not found for race=%q group=%v", c.g.Race[ri].Name, c.g.ShipGroups[groupIndex].Index))
|
||||
}
|
||||
c.shipClassByShipGroupIndex[groupIndex] = &c.g.Race[ri].ShipTypes[sti]
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
c.cacheShipClassByShipGroupIndex[groupIndex] = &c.g.Race[ri].ShipTypes[sti]
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) invalidateShipGroupCache() {
|
||||
if c.raceIndexByShipGroupIndex != nil {
|
||||
clear(c.raceIndexByShipGroupIndex)
|
||||
}
|
||||
if c.shipClassByShipGroupIndex != nil {
|
||||
clear(c.shipClassByShipGroupIndex)
|
||||
}
|
||||
clear(c.cacheRaceIndexByShipGroupIndex)
|
||||
clear(c.cacheShipClassByShipGroupIndex)
|
||||
}
|
||||
|
||||
func (c *Cache) invalidateFleetCache() {
|
||||
clear(c.cacheFleetIndexByID)
|
||||
}
|
||||
|
||||
// Helpers
|
||||
|
||||
Reference in New Issue
Block a user