refactor: battle at controller
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/iliadenisov/galaxy/internal/model/game"
|
||||
)
|
||||
|
||||
func (c *Cache) Planet(planetNumber uint) (*game.Planet, bool) {
|
||||
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]
|
||||
}
|
||||
}
|
||||
if v, ok := c.planetByPlanetNumber[planetNumber]; ok {
|
||||
return v, true
|
||||
} else {
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) MustPlanet(planetNumber uint) *game.Planet {
|
||||
if v, ok := c.Planet(planetNumber); ok {
|
||||
return v
|
||||
} else {
|
||||
panic(fmt.Sprintf("Planet: not found by number=%d", planetNumber))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user