83 lines
2.1 KiB
Go
83 lines
2.1 KiB
Go
package controller
|
|
|
|
import (
|
|
"slices"
|
|
|
|
e "github.com/iliadenisov/galaxy/internal/error"
|
|
|
|
"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
|
|
|
|
// // return g.relationInternal(ri, other)
|
|
// }
|
|
|
|
func (c *Cache) UpdateRelation(race, opponent string, rel game.Relation) error {
|
|
r1, err := c.raceIndex(race)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var r2 int
|
|
if race == opponent {
|
|
r2 = r1
|
|
} else if r2, err = c.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 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")
|
|
// }
|
|
// return nil
|
|
// return g.updateRelationInternal(ri, other, rel)
|
|
}
|
|
|
|
func (c *Cache) raceIndex(name string) (int, error) {
|
|
i := slices.IndexFunc(c.g.Race, func(r game.Race) bool { return r.Name == name })
|
|
if i < 0 {
|
|
return i, e.NewRaceUnknownError(name)
|
|
}
|
|
return i, nil
|
|
}
|