summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--derelict.go21
-rw-r--r--models.go13
-rw-r--r--recent.tmpl2
-rw-r--r--storage.go33
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 @@
<td><a href="/view/{{.Id}}">{{.Id}}</a>
<td>some wrecks
<td>some grids
- <td>{{.Date.Format "January 02, 2006 15:04 UTC"}}
+ <td>{{.LastModified.Format "January 02, 2006 15:04 UTC"}}
{{end}}
</table>
<h2>About</h2>
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