From 31caf8496ada3837da199bba3b1c27f5d6462f9a Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 26 Apr 2021 23:29:02 +0200 Subject: Remodeled battle storage --- derelict.go | 21 +++++++-------------- models.go | 13 ++++++------- recent.tmpl | 2 +- storage.go | 33 ++++++++++++++++++++++++++++----- 4 files changed, 42 insertions(+), 27 deletions(-) diff --git a/derelict.go b/derelict.go index d72bfb8..cf72dd8 100644 --- a/derelict.go +++ b/derelict.go @@ -4,7 +4,6 @@ import ( "embed" "encoding/json" "html/template" - "io/ioutil" "log" "net/http" "os" @@ -57,24 +56,18 @@ func handleBattlesPost(w http.ResponseWriter, r *http.Request) { http.Error(w, "Unauthorized", http.StatusUnauthorized) return } - var wrecks []Wreck - data, err := ioutil.ReadAll(r.Body) - if err != nil { - http.Error(w, "Unexpected error 2", http.StatusInternalServerError) - return - } - err = json.Unmarshal(data, &wrecks) - if err != nil { - http.Error(w, "Error in body", http.StatusBadRequest) + decoder := json.NewDecoder(r.Body) + var battle Battle + if err := decoder.Decode(&battle); err != nil { + http.Error(w, "Error reading battle", http.StatusBadRequest) return } - name, err := storage.AddBattle(data) + err = storage.AddBattle(&battle) if err != nil { - http.Error(w, "Error writing", http.StatusInternalServerError) + http.Error(w, "Error writing battle", http.StatusInternalServerError) return } - w.WriteHeader(http.StatusOK) - w.Write([]byte(name)) + w.Write([]byte(battle.Id)) } func handleBattlesId(w http.ResponseWriter, r *http.Request) { diff --git a/models.go b/models.go index 37059dc..9a0a67f 100644 --- a/models.go +++ b/models.go @@ -5,16 +5,15 @@ import ( ) type Killmail struct { - Id string `json:"id"` + Id uint64 `json:"id"` Hash string `json:"hash"` } -type Wreck struct { - Killmail - Team string `json:"team"` -} +type Team []uint32 type Battle struct { - Id string - Date time.Time + Id string `json:"-"` + LastModified time.Time `json:"-"` + Teams [2]Team `json:"teams"` + Killmails []Killmail `json:"killmails"` } diff --git a/recent.tmpl b/recent.tmpl index b4ca11a..14b8bba 100644 --- a/recent.tmpl +++ b/recent.tmpl @@ -18,7 +18,7 @@ {{.Id}} some wrecks some grids - {{.Date.Format "January 02, 2006 15:04 UTC"}} + {{.LastModified.Format "January 02, 2006 15:04 UTC"}} {{end}}

About

diff --git a/storage.go b/storage.go index 22a226d..9172e96 100644 --- a/storage.go +++ b/storage.go @@ -3,12 +3,15 @@ package main import ( "crypto/sha1" "encoding/hex" + "encoding/json" "errors" + "io" "io/ioutil" "net/http" "os" "regexp" "sort" + "strconv" ) type Storage struct { @@ -34,10 +37,30 @@ func (s *Storage) CanPost(token string) (bool, error) { return err == nil, nil } -func (s *Storage) AddBattle(data []byte) (string, error) { - hash := sha1.Sum(data) - id := hex.EncodeToString(hash[:]) - return id, ioutil.WriteFile(s.Path+"/battles/"+id, data, 0644) +func (b *Battle) CalculateHash() { + hash := sha1.New() + killmails := b.Killmails + sort.Slice(killmails, func(lhs, rhs int) bool { + return killmails[lhs].Id < killmails[rhs].Id + }) + for _, km := range killmails { + io.WriteString(hash, strconv.FormatUint(km.Id, 10)) + } + sum := hash.Sum(nil) + b.Id = hex.EncodeToString(sum[:]) +} + +func (s *Storage) AddBattle(battle *Battle) error { + if len(battle.Killmails) < 1 { + return errors.New("missing killmails") + } + battle.CalculateHash() + data, err := json.Marshal(battle) + if err != nil { + return err + } + + return ioutil.WriteFile(s.Path+"/battles/"+battle.Id, data, 0644) } func (s *Storage) ServeBattle(id string, w http.ResponseWriter, r *http.Request) { @@ -58,7 +81,7 @@ func (s *Storage) ListRecentBattles(count int) ([]Battle, error) { battles := make([]Battle, 0, count) for i := 0; i < count && i < len(files); i++ { - battles = append(battles, Battle{Id: files[i].Name(), Date: files[i].ModTime()}) + battles = append(battles, Battle{Id: files[i].Name(), LastModified: files[i].ModTime()}) } return battles, nil -- cgit v1.1