recator, test: race relation
This commit is contained in:
@@ -140,21 +140,10 @@ func TestProduceBattles(t *testing.T) {
|
|||||||
assert.NoError(t, g.UpdateRelation(race_C_name, race_D_name, game.RelationWar))
|
assert.NoError(t, g.UpdateRelation(race_C_name, race_D_name, game.RelationWar))
|
||||||
assert.NoError(t, g.UpdateRelation(race_D_name, race_C_name, game.RelationWar))
|
assert.NoError(t, g.UpdateRelation(race_D_name, race_C_name, game.RelationWar))
|
||||||
|
|
||||||
rel, err := g.Relation(Race_0.Name, race_C_name)
|
assert.Equal(t, game.RelationPeace, c.Relation(Race_0_idx, race_C_idx))
|
||||||
assert.NoError(t, err)
|
assert.Equal(t, game.RelationPeace, c.Relation(Race_1_idx, race_C_idx))
|
||||||
assert.Equal(t, game.RelationPeace, rel)
|
assert.Equal(t, game.RelationPeace, c.Relation(Race_0_idx, race_D_idx))
|
||||||
|
assert.Equal(t, game.RelationPeace, c.Relation(Race_1_idx, race_D_idx))
|
||||||
rel, err = g.Relation(Race_1.Name, race_C_name)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, game.RelationPeace, rel)
|
|
||||||
|
|
||||||
rel, err = g.Relation(Race_0.Name, race_D_name)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, game.RelationPeace, rel)
|
|
||||||
|
|
||||||
rel, err = g.Relation(Race_1.Name, race_D_name)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, game.RelationPeace, rel)
|
|
||||||
|
|
||||||
// Race_0
|
// Race_0
|
||||||
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 10))
|
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 10))
|
||||||
|
|||||||
@@ -29,19 +29,6 @@ func (c Controller) GiveVotes(actor, acceptor string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: remove, not a command
|
|
||||||
func (c Controller) Relation(actor, acceptor string) (game.Relation, error) {
|
|
||||||
r1, err := c.Cache.validActor(actor)
|
|
||||||
if err != nil {
|
|
||||||
return game.Relation(""), err
|
|
||||||
}
|
|
||||||
r2, err := c.Cache.validRace(acceptor)
|
|
||||||
if err != nil {
|
|
||||||
return game.Relation(""), err
|
|
||||||
}
|
|
||||||
return c.Cache.Relation(r1, r2), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c Controller) UpdateRelation(actor, acceptor string, rel game.Relation) error {
|
func (c Controller) UpdateRelation(actor, acceptor string, rel game.Relation) error {
|
||||||
ri, err := c.Cache.validActor(actor)
|
ri, err := c.Cache.validActor(actor)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -129,3 +129,7 @@ func VotingWinners(calc []*VoteGroup, gameVotes float64) []int {
|
|||||||
func (c *Cache) CreateShipsUnsafe_T(ri int, classID uuid.UUID, planet uint, quantity uint) {
|
func (c *Cache) CreateShipsUnsafe_T(ri int, classID uuid.UUID, planet uint, quantity uint) {
|
||||||
c.createShipsUnsafe(ri, classID, planet, quantity)
|
c.createShipsUnsafe(ri, classID, planet, quantity)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Cache) WipeRace(ri int) {
|
||||||
|
c.wipeRace(ri)
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
e "github.com/iliadenisov/galaxy/internal/error"
|
e "github.com/iliadenisov/galaxy/internal/error"
|
||||||
|
"github.com/iliadenisov/galaxy/internal/model/game"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,3 +21,23 @@ func TestGiveVotes(t *testing.T) {
|
|||||||
assert.ErrorContains(t, g.GiveVotes("UnknownRace", Race_1.Name), e.GenericErrorText(e.ErrInputUnknownRace))
|
assert.ErrorContains(t, g.GiveVotes("UnknownRace", Race_1.Name), e.GenericErrorText(e.ErrInputUnknownRace))
|
||||||
assert.ErrorContains(t, g.GiveVotes(Race_0.Name, "UnknownRace"), e.GenericErrorText(e.ErrInputUnknownRace))
|
assert.ErrorContains(t, g.GiveVotes(Race_0.Name, "UnknownRace"), e.GenericErrorText(e.ErrInputUnknownRace))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRelation(t *testing.T) {
|
||||||
|
c, g := newCache()
|
||||||
|
|
||||||
|
assert.NoError(t, g.UpdateRelation(Race_0.Name, Race_1.Name, game.RelationWar))
|
||||||
|
assert.NoError(t, g.UpdateRelation(Race_1.Name, Race_0.Name, game.RelationPeace))
|
||||||
|
|
||||||
|
assert.Equal(t, game.RelationWar, c.Relation(Race_0_idx, Race_1_idx))
|
||||||
|
assert.Equal(t, game.RelationPeace, c.Relation(Race_1_idx, Race_0_idx))
|
||||||
|
|
||||||
|
c.WipeRace(Race_1_idx)
|
||||||
|
|
||||||
|
assert.ErrorContains(t,
|
||||||
|
g.UpdateRelation(Race_0.Name, Race_1.Name, game.RelationWar),
|
||||||
|
e.GenericErrorText(e.ErrRaceExinct))
|
||||||
|
|
||||||
|
assert.ErrorContains(t,
|
||||||
|
g.UpdateRelation(Race_1.Name, Race_0.Name, game.RelationWar),
|
||||||
|
e.GenericErrorText(e.ErrRaceExinct))
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/iliadenisov/galaxy/internal/controller"
|
"github.com/iliadenisov/galaxy/internal/controller"
|
||||||
e "github.com/iliadenisov/galaxy/internal/error"
|
|
||||||
|
|
||||||
"github.com/iliadenisov/galaxy/internal/game"
|
"github.com/iliadenisov/galaxy/internal/game"
|
||||||
mg "github.com/iliadenisov/galaxy/internal/model/game"
|
mg "github.com/iliadenisov/galaxy/internal/model/game"
|
||||||
@@ -13,66 +12,46 @@ import (
|
|||||||
|
|
||||||
func TestDeclarePeaceAndWarSingle(t *testing.T) {
|
func TestDeclarePeaceAndWarSingle(t *testing.T) {
|
||||||
c(t, func(f func(*controller.Param), ctrl func() *controller.Controller) {
|
c(t, func(f func(*controller.Param), ctrl func() *controller.Controller) {
|
||||||
hostRace := "race_05"
|
hostRace := 5
|
||||||
opponentRace := "race_01"
|
opponentRace := 1
|
||||||
|
|
||||||
r, err := ctrl().Relation(hostRace, opponentRace)
|
assert.Equal(t, mg.RelationWar, ctrl().Cache.Relation(5, 1))
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, mg.RelationWar, r)
|
|
||||||
|
|
||||||
r, err = ctrl().Relation(unknownRaceName, opponentRace) // TODO: test on dead race
|
assert.NoError(t, game.DeclarePeace(f, raceNum(hostRace), raceNum(opponentRace)))
|
||||||
assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputUnknownRace))
|
assert.Equal(t, mg.RelationPeace, ctrl().Cache.Relation(hostRace, opponentRace))
|
||||||
r, err = ctrl().Relation(hostRace, unknownRaceName) // TODO: test on dead race
|
|
||||||
assert.ErrorContains(t, err, e.GenericErrorText(e.ErrInputUnknownRace))
|
|
||||||
|
|
||||||
assert.NoError(t, game.DeclarePeace(f, hostRace, opponentRace))
|
assert.NoError(t, game.DeclareWar(f, raceNum(hostRace), raceNum(opponentRace)))
|
||||||
r, err = ctrl().Relation(hostRace, opponentRace)
|
assert.Equal(t, mg.RelationWar, ctrl().Cache.Relation(hostRace, opponentRace))
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, mg.RelationPeace, r)
|
|
||||||
|
|
||||||
assert.NoError(t, game.DeclareWar(f, hostRace, opponentRace))
|
|
||||||
r, err = ctrl().Relation(hostRace, opponentRace)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, mg.RelationWar, r)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDeclarePeaceAndWarAll(t *testing.T) {
|
func TestDeclarePeaceAndWarAll(t *testing.T) {
|
||||||
c(t, func(f func(*controller.Param), ctrl func() *controller.Controller) {
|
c(t, func(f func(*controller.Param), ctrl func() *controller.Controller) {
|
||||||
hostRace := "race_07"
|
hostRace := 7
|
||||||
|
|
||||||
for i := range testRaceCount {
|
for i := range testRaceCount {
|
||||||
opponentRace := raceNum(i)
|
if i == hostRace {
|
||||||
if opponentRace == hostRace {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
r, err := ctrl().Relation(hostRace, opponentRace)
|
assert.Equal(t, mg.RelationWar, ctrl().Cache.Relation(hostRace, i))
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, mg.RelationWar, r)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.NoError(t, game.DeclarePeace(f, hostRace, hostRace))
|
assert.NoError(t, game.DeclarePeace(f, raceNum(hostRace), raceNum(hostRace)))
|
||||||
|
|
||||||
for i := range testRaceCount {
|
for i := range testRaceCount {
|
||||||
opponentRace := raceNum(i)
|
if i == hostRace {
|
||||||
if opponentRace == hostRace {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
r, err := ctrl().Relation(hostRace, opponentRace)
|
assert.Equal(t, mg.RelationPeace, ctrl().Cache.Relation(hostRace, i))
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, mg.RelationPeace, r)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.NoError(t, game.DeclareWar(f, hostRace, hostRace))
|
assert.NoError(t, game.DeclareWar(f, raceNum(hostRace), raceNum(hostRace)))
|
||||||
|
|
||||||
for i := range testRaceCount {
|
for i := range testRaceCount {
|
||||||
opponentRace := raceNum(i)
|
if i == hostRace {
|
||||||
if opponentRace == hostRace {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
r, err := ctrl().Relation(hostRace, opponentRace)
|
assert.Equal(t, mg.RelationWar, ctrl().Cache.Relation(hostRace, i))
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, mg.RelationWar, r)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ func TestComposeGame(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func g(t *testing.T, f func(p func(*controller.Param), g func() *mg.Game)) {
|
func g(t *testing.T, f func(func(*controller.Param), func() *mg.Game)) {
|
||||||
root, cleanup := util.CreateWorkDir(t)
|
root, cleanup := util.CreateWorkDir(t)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
races := make([]string, testRaceCount)
|
races := make([]string, testRaceCount)
|
||||||
@@ -53,7 +53,7 @@ func g(t *testing.T, f func(p func(*controller.Param), g func() *mg.Game)) {
|
|||||||
f(p, g)
|
f(p, g)
|
||||||
}
|
}
|
||||||
|
|
||||||
func c(t *testing.T, f func(p func(*controller.Param), g func() *controller.Controller)) {
|
func c(t *testing.T, f func(func(*controller.Param), func() *controller.Controller)) {
|
||||||
root, cleanup := util.CreateWorkDir(t)
|
root, cleanup := util.CreateWorkDir(t)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
races := make([]string, testRaceCount)
|
races := make([]string, testRaceCount)
|
||||||
@@ -66,7 +66,7 @@ func c(t *testing.T, f func(p func(*controller.Param), g func() *controller.Cont
|
|||||||
assert.FailNow(t, "c: GenerateGame", err)
|
assert.FailNow(t, "c: GenerateGame", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctl := func() *controller.Controller {
|
ctrl := func() *controller.Controller {
|
||||||
c, err := controller.NewController(p)
|
c, err := controller.NewController(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
assert.FailNow(t, "c: NewController", err)
|
assert.FailNow(t, "c: NewController", err)
|
||||||
@@ -80,5 +80,5 @@ func c(t *testing.T, f func(p func(*controller.Param), g func() *controller.Cont
|
|||||||
c.Cache = controller.NewCache(g)
|
c.Cache = controller.NewCache(g)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
f(p, ctl)
|
f(p, ctrl)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user