wip: refactor controller
This commit is contained in:
+114
-48
@@ -1,6 +1,7 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
e "github.com/iliadenisov/galaxy/internal/error"
|
||||
@@ -8,69 +9,129 @@ import (
|
||||
"github.com/iliadenisov/galaxy/internal/model/game"
|
||||
)
|
||||
|
||||
// func (c *Cache) CmdRelation(hostRace, opponentRace string) (game.RaceRelation, error) {
|
||||
// ri, err := c.raceIndex(hostRace)
|
||||
// if err != nil {
|
||||
// return game.RaceRelation{}, err
|
||||
// }
|
||||
// other, err := c.raceIndex(opponentRace)
|
||||
// if err != nil {
|
||||
// return game.RaceRelation{}, err
|
||||
// }
|
||||
// if ri == other {
|
||||
// return game.RaceRelation{
|
||||
// RaceID: c.g.Race[ri].ID,
|
||||
// Relation: game.RelationPeace,
|
||||
// }, nil
|
||||
// }
|
||||
// rel := slices.IndexFunc(c.g.Race[ri].Relations, func(r game.RaceRelation) bool { return r.RaceID == c.g.Race[other].ID })
|
||||
// if rel < 0 {
|
||||
// return game.RaceRelation{}, e.NewGameStateError("Relation: opponent not found")
|
||||
// }
|
||||
// return c.g.Race[ri].Relations[rel], nil
|
||||
func (c Controller) Relation(from, to string) (game.Relation, error) {
|
||||
r1, err := c.Cache.raceIndex(from)
|
||||
if err != nil {
|
||||
return game.Relation(""), err
|
||||
}
|
||||
r2, err := c.Cache.raceIndex(to)
|
||||
if err != nil {
|
||||
return game.Relation(""), err
|
||||
}
|
||||
return c.Cache.Relation(r1, r2), nil
|
||||
}
|
||||
|
||||
// // return g.relationInternal(ri, other)
|
||||
// }
|
||||
|
||||
func (c *Cache) UpdateRelation(race, opponent string, rel game.Relation) error {
|
||||
r1, err := c.raceIndex(race)
|
||||
func (c Controller) UpdateRelation(race, opponent string, rel game.Relation) error {
|
||||
ri, err := c.Cache.raceIndex(race)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var r2 int
|
||||
var other int
|
||||
if race == opponent {
|
||||
r2 = r1
|
||||
} else if r2, err = c.raceIndex(opponent); err != nil {
|
||||
other = ri
|
||||
} else if other, err = c.Cache.raceIndex(opponent); err != nil {
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if r2 == r1 {
|
||||
return nil
|
||||
}
|
||||
for o := range c.g.Race[r1].Relations {
|
||||
if c.g.Race[r1].Relations[o].RaceID == c.g.Race[r2].ID {
|
||||
c.g.Race[r1].Relations[o].Relation = rel
|
||||
if c.cacheRelation != nil {
|
||||
c.updateRelationCache(r1, r2, rel)
|
||||
return c.Cache.UpdateRelation(ri, other, rel)
|
||||
}
|
||||
|
||||
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 {
|
||||
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) GiveVotes(race, recipient string) error {
|
||||
ri, err := c.raceIndex(race)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rec, err := c.raceIndex(recipient)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.g.Race[ri].Vote = c.g.Race[rec].ID
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) Voted(ri int) int {
|
||||
c.validateRaceIndex(ri)
|
||||
return c.RaceIndex(c.g.Race[ri].Vote)
|
||||
}
|
||||
|
||||
func (c *Cache) UpdateRelation(ri, other int, rel game.Relation) (err error) {
|
||||
defer func() {
|
||||
if err == nil && c.cacheRelation != nil {
|
||||
c.updateRelationCache(ri, other, rel)
|
||||
}
|
||||
}()
|
||||
for o := range c.g.Race[ri].Relations {
|
||||
switch {
|
||||
case ri == other:
|
||||
c.g.Race[ri].Relations[o].Relation = rel
|
||||
case c.g.Race[ri].Relations[o].RaceID == c.g.Race[other].ID:
|
||||
c.g.Race[ri].Relations[o].Relation = rel
|
||||
return nil
|
||||
}
|
||||
// switch {
|
||||
// case r1 == r2:
|
||||
// c.g.Race[r1].Relations[o].Relation = rel
|
||||
// case c.g.Race[r1].Relations[o].RaceID == c.g.Race[r2].ID:
|
||||
// c.g.Race[r1].Relations[o].Relation = rel
|
||||
// return nil
|
||||
// }
|
||||
}
|
||||
return e.NewGameStateError("UpdateRelation: opponent not found")
|
||||
// if r1 != r2 {
|
||||
// return e.NewGameStateError("UpdateRelation: opponent not found")
|
||||
if ri != other {
|
||||
err = e.NewGameStateError("UpdateRelation: opponent not found")
|
||||
}
|
||||
return
|
||||
|
||||
// for o := range c.g.Race[r1].Relations {
|
||||
// if c.g.Race[r1].Relations[o].RaceID == c.g.Race[r2].ID {
|
||||
// c.g.Race[r1].Relations[o].Relation = rel
|
||||
// if c.cacheRelation != nil {
|
||||
// c.updateRelationCache(r1, r2, rel)
|
||||
// }
|
||||
// return nil
|
||||
// }
|
||||
// }
|
||||
// return nil
|
||||
// return g.updateRelationInternal(ri, other, rel)
|
||||
// return e.NewGameStateError("UpdateRelation: opponent not found")
|
||||
}
|
||||
|
||||
func (c *Cache) validateRaceIndex(i int) {
|
||||
if i >= len(c.g.Race) {
|
||||
panic(fmt.Sprintf("race index out of range: %d >= %d", i, len(c.g.Race)))
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) raceIndex(name string) (int, error) {
|
||||
@@ -80,3 +141,8 @@ func (c *Cache) raceIndex(name string) (int, error) {
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
func (c *Cache) racetTechLevel(ri int, t game.Tech, v float64) {
|
||||
c.validateFleetIndex(ri)
|
||||
c.g.Race[ri].Tech = c.g.Race[ri].Tech.Set(t, v)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user