feat: finish game; store reports

This commit is contained in:
Ilia Denisov
2026-02-04 14:21:37 +02:00
parent abc98ee72f
commit 9a6d4112cf
6 changed files with 43 additions and 10 deletions
+3
View File
@@ -32,6 +32,9 @@ type Repo interface {
// SaveBombing stores all prodused bombings for turn t
SaveBombings(t uint, b []*game.Bombing) error
// SaveReport stores latest report for a race
SaveReport(t uint, rep *report.Report) error
}
type Controller struct {
+9 -7
View File
@@ -50,6 +50,7 @@ func MakeTurn(c *Controller, r Repo) error {
// 16. Происходит голосование.
winners := c.Cache.TurnCalculateVotes()
c.Cache.TurnAcceptWinners(winners)
/*** Last steps ***/
@@ -107,15 +108,16 @@ func MakeTurn(c *Controller, r Repo) error {
c.Cache.DeleteKilledShipGroups()
// Store game state for the new turn and 'current' state as well
r.SaveTurn(c.Cache.g.Turn, c.Cache.g)
// TODO: Store individual reports
for ri := range c.Cache.g.Race {
_ = ri
// c.Cache.GenerateReport(ri)
if err := r.SaveTurn(c.Cache.g.Turn, c.Cache.g); err != nil {
return err
}
for rep := range c.Cache.Report(c.Cache.g.Turn, battleReport, bombingReport) {
if err := r.SaveReport(c.Cache.g.Turn, rep); err != nil {
return err
}
}
_ = winners
// [ ] monitor memory consumption at this point?
return nil
}
-1
View File
@@ -248,7 +248,6 @@ func (c *Cache) ReportLocalShipClass(ri int, report *mr.Report) {
slices.SortFunc(report.LocalShipClass, func(a, b mr.ShipClass) int { return cmp.Compare(a.Name, b.Name) })
}
// FIXME: add ships bombed/wiped planets without battles
func (c *Cache) ReportOtherShipClass(ri int, rep *mr.Report) {
c.validateRaceIndex(ri)
r := &c.g.Race[ri]
+12
View File
@@ -30,6 +30,18 @@ func (n VoteNode) String() string {
return fmt.Sprintf("%s%d%s", lh, n.ID, rh)
}
func (c *Cache) TurnAcceptWinners(v []int) {
if c.g.Finished() {
panic("game is already has its winner(s)")
}
if len(v) == 0 {
return
}
for _, ri := range v {
c.g.Winner = append(c.g.Winner, c.g.Race[ri].ID)
}
}
func (c *Cache) TurnCalculateVotes() []int {
raceVotes := c.votesByRace()
calc := GroupVotes(raceVotes, VotingGraph(c.g.Race, c.RaceIndex))