feat: finish game; store reports
This commit is contained in:
@@ -32,6 +32,9 @@ type Repo interface {
|
|||||||
|
|
||||||
// SaveBombing stores all prodused bombings for turn t
|
// SaveBombing stores all prodused bombings for turn t
|
||||||
SaveBombings(t uint, b []*game.Bombing) error
|
SaveBombings(t uint, b []*game.Bombing) error
|
||||||
|
|
||||||
|
// SaveReport stores latest report for a race
|
||||||
|
SaveReport(t uint, rep *report.Report) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type Controller struct {
|
type Controller struct {
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ func MakeTurn(c *Controller, r Repo) error {
|
|||||||
|
|
||||||
// 16. Происходит голосование.
|
// 16. Происходит голосование.
|
||||||
winners := c.Cache.TurnCalculateVotes()
|
winners := c.Cache.TurnCalculateVotes()
|
||||||
|
c.Cache.TurnAcceptWinners(winners)
|
||||||
|
|
||||||
/*** Last steps ***/
|
/*** Last steps ***/
|
||||||
|
|
||||||
@@ -107,15 +108,16 @@ func MakeTurn(c *Controller, r Repo) error {
|
|||||||
c.Cache.DeleteKilledShipGroups()
|
c.Cache.DeleteKilledShipGroups()
|
||||||
|
|
||||||
// Store game state for the new turn and 'current' state as well
|
// Store game state for the new turn and 'current' state as well
|
||||||
r.SaveTurn(c.Cache.g.Turn, c.Cache.g)
|
if err := r.SaveTurn(c.Cache.g.Turn, c.Cache.g); err != nil {
|
||||||
|
return err
|
||||||
// TODO: Store individual reports
|
}
|
||||||
for ri := range c.Cache.g.Race {
|
|
||||||
_ = ri
|
for rep := range c.Cache.Report(c.Cache.g.Turn, battleReport, bombingReport) {
|
||||||
// c.Cache.GenerateReport(ri)
|
if err := r.SaveReport(c.Cache.g.Turn, rep); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = winners
|
|
||||||
// [ ] monitor memory consumption at this point?
|
// [ ] monitor memory consumption at this point?
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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) })
|
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) {
|
func (c *Cache) ReportOtherShipClass(ri int, rep *mr.Report) {
|
||||||
c.validateRaceIndex(ri)
|
c.validateRaceIndex(ri)
|
||||||
r := &c.g.Race[ri]
|
r := &c.g.Race[ri]
|
||||||
|
|||||||
@@ -30,6 +30,18 @@ func (n VoteNode) String() string {
|
|||||||
return fmt.Sprintf("%s%d%s", lh, n.ID, rh)
|
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 {
|
func (c *Cache) TurnCalculateVotes() []int {
|
||||||
raceVotes := c.votesByRace()
|
raceVotes := c.votesByRace()
|
||||||
calc := GroupVotes(raceVotes, VotingGraph(c.g.Race, c.RaceIndex))
|
calc := GroupVotes(raceVotes, VotingGraph(c.g.Race, c.RaceIndex))
|
||||||
|
|||||||
@@ -47,6 +47,11 @@ type Game struct {
|
|||||||
Votes float64 `json:"votes"`
|
Votes float64 `json:"votes"`
|
||||||
ShipGroups []ShipGroup `json:"shipGroup,omitempty"`
|
ShipGroups []ShipGroup `json:"shipGroup,omitempty"`
|
||||||
Fleets []Fleet `json:"fleet,omitempty"`
|
Fleets []Fleet `json:"fleet,omitempty"`
|
||||||
|
Winner []uuid.UUID `json:"winner,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g Game) Finished() bool {
|
||||||
|
return len(g.Winner) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
type GameMeta struct {
|
type GameMeta struct {
|
||||||
|
|||||||
+14
-2
@@ -22,6 +22,18 @@ const (
|
|||||||
metaPath = "meta.json"
|
metaPath = "meta.json"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (r *repo) SaveReport(t uint, rep *report.Report) error {
|
||||||
|
return saveReport(r.s, t, rep)
|
||||||
|
}
|
||||||
|
|
||||||
|
func saveReport(s Storage, t uint, v *report.Report) error {
|
||||||
|
path := fmt.Sprintf("%s/report/%s.json", turnDir(t), v.RaceID.String())
|
||||||
|
if err := s.Write(path, v); err != nil {
|
||||||
|
return NewStorageError(err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *repo) SaveTurn(t uint, g *game.Game) error {
|
func (r *repo) SaveTurn(t uint, g *game.Game) error {
|
||||||
return saveTurn(r.s, t, g)
|
return saveTurn(r.s, t, g)
|
||||||
}
|
}
|
||||||
@@ -127,13 +139,13 @@ func (r *repo) SaveBattle(t uint, b *report.BattleReport, m *game.BattleMeta) er
|
|||||||
}
|
}
|
||||||
|
|
||||||
func saveBattle(s Storage, t uint, b *report.BattleReport) error {
|
func saveBattle(s Storage, t uint, b *report.BattleReport) error {
|
||||||
path := fmt.Sprintf("%s/battle/%s.json", turnDir(t), b.ID)
|
path := fmt.Sprintf("%s/battle/%s.json", turnDir(t), b.ID.String())
|
||||||
exist, err := s.Exists(path)
|
exist, err := s.Exists(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return NewStorageError(err)
|
return NewStorageError(err)
|
||||||
}
|
}
|
||||||
if exist {
|
if exist {
|
||||||
return NewStateError(fmt.Sprintf("battle %s for turn %d already has been saved", b.ID, t))
|
return NewStateError(fmt.Sprintf("battle %v for turn %d already has been saved", b.ID, t))
|
||||||
}
|
}
|
||||||
if err := s.Write(path, b); err != nil {
|
if err := s.Write(path, b); err != nil {
|
||||||
return NewStorageError(err)
|
return NewStorageError(err)
|
||||||
|
|||||||
Reference in New Issue
Block a user