summaryrefslogtreecommitdiffhomepage
path: root/storage.go
diff options
context:
space:
mode:
Diffstat (limited to 'storage.go')
-rw-r--r--storage.go33
1 files changed, 28 insertions, 5 deletions
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