refactor: battle at controller

This commit is contained in:
Ilia Denisov
2026-01-14 14:40:04 +02:00
parent 004529cdd3
commit 1bfc9242af
16 changed files with 583 additions and 546 deletions
+47
View File
@@ -0,0 +1,47 @@
package controller
import (
// "github.com/iliadenisov/galaxy/internal/controller"
e "github.com/iliadenisov/galaxy/internal/error"
// "github.com/iliadenisov/galaxy/internal/game/battle"
"github.com/iliadenisov/galaxy/internal/model/game"
)
func MakeTurn(c *Controller, r Repo, g *game.Game) error {
// Next turn
g.Age += 1
// 01. Корабли, где это возможно, объединяются в группы.
game.JoinEqualGroups(g)
// 02. Враждующие корабли вступают в схватку.
battles := ProduceBattles(c.Cache)
// Internal control: after battles there are can't be groups with no ships left
for i := range g.ShipGroups {
if g.ShipGroups[i].Number == 0 {
return e.NewGameStateError("")
}
}
/*** Last steps ***/
// Store battles
if len(battles) > 0 {
for i := range battles {
// TODO: add In_Battle / Out_Battle participants?
br := TransformBattle(c.Cache, battles[i])
if err := r.SaveBattle(g.Age, br); err != nil {
return err
}
}
}
// Remove killed ship groups
c.Cache.DeleteKilledShipGroups()
// TODO: Store game state
// TODO: Store individual reports
return nil
}