145 lines
5.0 KiB
Go
145 lines
5.0 KiB
Go
package controller_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/iliadenisov/galaxy/internal/controller"
|
|
"github.com/iliadenisov/galaxy/internal/model/game"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestBombPlanet(t *testing.T) {
|
|
p := controller.NewPlanet(0, "Planet_0", uuid.New(), 1, 1, 1000, 300, 200, 10, game.ResearchDrive.AsType(uuid.Nil))
|
|
(&p).Colonists = 100.
|
|
assert.Equal(t, 0., p.Material.F())
|
|
|
|
controller.BombPlanet(&p, 55.)
|
|
assert.Equal(t, 245., p.Population.F())
|
|
assert.Equal(t, 45., p.Colonists.F())
|
|
assert.Equal(t, 145., p.Industry.F())
|
|
assert.Equal(t, 55., p.Material.F())
|
|
|
|
controller.BombPlanet(&p, 56.)
|
|
assert.Equal(t, 189., p.Population.F())
|
|
assert.Equal(t, 0., p.Colonists.F())
|
|
assert.Equal(t, 89., p.Industry.F())
|
|
assert.Equal(t, 111., p.Material.F())
|
|
|
|
controller.BombPlanet(&p, 200.)
|
|
assert.Equal(t, 0., p.Population.F())
|
|
assert.Equal(t, 0., p.Colonists.F())
|
|
assert.Equal(t, 0., p.Industry.F())
|
|
assert.Equal(t, 200., p.Material.F())
|
|
}
|
|
|
|
func TestCollectBombingGroups(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.RelationWar))
|
|
|
|
// 1: idx = 0 / Ready to bomb: Race_1/Planet_1
|
|
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 2)) // bombs
|
|
c.ShipGroup(0).Destination = R1_Planet_1_num
|
|
|
|
// 2: idx = 1 / Ready to bomb: Race_0/Planet_2
|
|
assert.NoError(t, c.CreateShips(Race_1_idx, Race_1_Gunship, R1_Planet_1_num, 3)) // bombs
|
|
c.ShipGroup(1).Destination = R0_Planet_2_num
|
|
|
|
// 3: idx = 2 / In_Space
|
|
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 1))
|
|
c.ShipGroup(2).StateInSpace = &game.InSpace{
|
|
Origin: 2,
|
|
Range: 31.337,
|
|
}
|
|
|
|
// 4: idx = 3 / Has no Ammo
|
|
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 1))
|
|
c.ShipGroup(3).Destination = R1_Planet_1_num
|
|
|
|
// 5: idx = 4 / On it's own planet
|
|
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 1))
|
|
|
|
// 6: idx = 5 / On uninhabited planet
|
|
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 2))
|
|
c.ShipGroup(5).Destination = Uninhabited_Planet_3_num
|
|
|
|
bg := c.CollectBombingGroups()
|
|
|
|
assert.Len(t, bg, 2)
|
|
assert.Contains(t, bg, R1_Planet_1_num)
|
|
assert.Contains(t, bg, R0_Planet_2_num)
|
|
|
|
assert.Len(t, bg[R1_Planet_1_num], 1)
|
|
assert.Contains(t, bg[R1_Planet_1_num], Race_0_idx)
|
|
|
|
assert.Len(t, bg[R0_Planet_2_num], 1)
|
|
assert.Contains(t, bg[R0_Planet_2_num], Race_1_idx)
|
|
|
|
assert.Len(t, bg[R1_Planet_1_num][Race_0_idx], 1)
|
|
assert.Equal(t, 0, bg[R1_Planet_1_num][Race_0_idx][0])
|
|
|
|
assert.Len(t, bg[R0_Planet_2_num][Race_1_idx], 1)
|
|
assert.Equal(t, 1, bg[R0_Planet_2_num][Race_1_idx][0])
|
|
|
|
// remove bombings from Race_1
|
|
assert.NoError(t, g.UpdateRelation(Race_1.Name, Race_0.Name, game.RelationPeace))
|
|
bg = c.CollectBombingGroups()
|
|
assert.Len(t, bg, 1)
|
|
assert.Contains(t, bg, R1_Planet_1_num)
|
|
assert.Len(t, bg[R1_Planet_1_num], 1)
|
|
assert.Contains(t, bg[R1_Planet_1_num], Race_0_idx)
|
|
assert.Len(t, bg[R1_Planet_1_num][Race_0_idx], 1)
|
|
assert.Equal(t, 0, bg[R1_Planet_1_num][Race_0_idx][0])
|
|
}
|
|
|
|
func TestProduceBombings(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.RelationWar))
|
|
|
|
// 1: idx = 0 / Bombs on: Race_1/Planet_1
|
|
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 3))
|
|
c.ShipGroup(0).Destination = R1_Planet_1_num
|
|
// 2: idx = 1 / Bombs on: Race_1/Planet_1
|
|
assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 7))
|
|
c.ShipGroup(1).Destination = R1_Planet_1_num
|
|
|
|
// 3: idx = 2 / Bombs on: Race_0/Planet_2
|
|
assert.NoError(t, c.CreateShips(Race_1_idx, Race_1_Gunship, R1_Planet_1_num, 1))
|
|
c.ShipGroup(2).Destination = R0_Planet_2_num
|
|
c.MustPlanet(R0_Planet_2_num).Population = 500
|
|
|
|
assert.NoError(t, g.SetRoute(Race_0.Name, "CAP", R0_Planet_2_num, R0_Planet_0_num))
|
|
assert.NotEmpty(t, c.MustPlanet(R0_Planet_2_num).Route)
|
|
|
|
assert.NoError(t, g.SetRoute(Race_1.Name, "EMP", R1_Planet_1_num, R0_Planet_2_num))
|
|
assert.NotEmpty(t, c.MustPlanet(R1_Planet_1_num).Route)
|
|
|
|
reports := c.ProduceBombings()
|
|
assert.Len(t, reports, 2)
|
|
for _, r := range reports {
|
|
assert.NotEqual(t, uuid.Nil, r.ID)
|
|
switch pn := r.Number; pn {
|
|
case R1_Planet_1_num:
|
|
assert.Equal(t, Race_1.Name, r.Owner)
|
|
assert.Equal(t, Race_0.Name, r.Attacker)
|
|
assert.InDelta(t, 697.857, r.AttackPower.F(), 0.0003)
|
|
assert.True(t, r.Wiped)
|
|
assert.Equal(t, uuid.Nil, c.MustPlanet(pn).Owner)
|
|
assert.Empty(t, c.MustPlanet(pn).Route)
|
|
assert.Equal(t, 0., c.MustPlanet(pn).Population.F())
|
|
case R0_Planet_2_num:
|
|
assert.Equal(t, Race_0.Name, r.Owner)
|
|
assert.Equal(t, Race_1.Name, r.Attacker)
|
|
assert.Equal(t, 358.856, r.AttackPower.F())
|
|
assert.False(t, r.Wiped)
|
|
assert.Equal(t, Race_0_ID, c.MustPlanet(pn).Owner)
|
|
assert.NotEmpty(t, c.MustPlanet(pn).Route)
|
|
assert.InDelta(t, 500.-358.85596, c.MustPlanet(pn).Population.F(), 0.000001)
|
|
}
|
|
}
|
|
}
|