feat: load player's report
This commit is contained in:
+26
-3
@@ -1,7 +1,6 @@
|
||||
package repo
|
||||
|
||||
/*
|
||||
TODO: only state will be saved once (current, turn); meta and bombings are saved at turn generation and saved twice
|
||||
/state.json
|
||||
/0001/state.json
|
||||
/0001/meta.json
|
||||
@@ -13,6 +12,7 @@ package repo
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/iliadenisov/galaxy/internal/model/game"
|
||||
"github.com/iliadenisov/galaxy/internal/model/report"
|
||||
)
|
||||
@@ -27,13 +27,33 @@ func (r *repo) SaveReport(t uint, rep *report.Report) error {
|
||||
}
|
||||
|
||||
func saveReport(s Storage, t uint, v *report.Report) error {
|
||||
path := fmt.Sprintf("%s/report/%s.json", turnDir(t), v.RaceID.String())
|
||||
path := repDir(t, v.RaceID)
|
||||
if err := s.Write(path, v); err != nil {
|
||||
return NewStorageError(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *repo) LoadReport(t uint, id uuid.UUID) (*report.Report, error) {
|
||||
return loadReport(r.s, t, id)
|
||||
}
|
||||
|
||||
func loadReport(s Storage, t uint, id uuid.UUID) (*report.Report, error) {
|
||||
path := repDir(t, id)
|
||||
result := new(report.Report)
|
||||
exist, err := s.Exists(path)
|
||||
if err != nil {
|
||||
return nil, NewStorageError(err)
|
||||
}
|
||||
if !exist {
|
||||
return nil, NewReportNotFoundError()
|
||||
}
|
||||
if err := s.ReadSafe(path, result); err != nil {
|
||||
return nil, NewStorageError(err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *repo) SaveNewTurn(t uint, g *game.Game) error {
|
||||
return saveNewTurn(r.s, t, g)
|
||||
}
|
||||
@@ -104,7 +124,6 @@ func loadMeta(s Storage) (*game.GameMeta, error) {
|
||||
if !exist {
|
||||
return result, nil
|
||||
}
|
||||
// TODO: create separate Read func for meta ops
|
||||
if err := s.ReadSafe(path, result); err != nil {
|
||||
return nil, NewStorageError(err)
|
||||
}
|
||||
@@ -164,6 +183,10 @@ func (r *repo) SaveBombings(t uint, b []*game.Bombing) error {
|
||||
return saveMeta(r.s, t, meta)
|
||||
}
|
||||
|
||||
func repDir(t uint, id uuid.UUID) string {
|
||||
return fmt.Sprintf("%s/report/%s.json", turnDir(t), id.String())
|
||||
}
|
||||
|
||||
func turnDir(t uint) string {
|
||||
return fmt.Sprintf("%04d", t)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user