summaryrefslogtreecommitdiffhomepage
path: root/storage.go
diff options
context:
space:
mode:
Diffstat (limited to 'storage.go')
-rw-r--r--storage.go99
1 files changed, 68 insertions, 31 deletions
diff --git a/storage.go b/storage.go
index ee3489a..e57e0f2 100644
--- a/storage.go
+++ b/storage.go
@@ -36,7 +36,7 @@ func (s *Storage) CanPost(token string) (bool, error) {
return err == nil, nil
}
-func (b *Battle) CalculateHash() {
+func (b *BattleStub) CalculateHash() string {
hash := sha1.New()
killmails := b.Killmails
sort.Slice(killmails, func(lhs, rhs int) bool {
@@ -46,7 +46,73 @@ func (b *Battle) CalculateHash() {
fmt.Fprint(hash, km.Id)
}
sum := hash.Sum(nil)
- b.Id = hex.EncodeToString(sum[:])
+ return hex.EncodeToString(sum[:])
+}
+
+func (b *Battle) From(stub *BattleStub) error {
+ if len(stub.Killmails) < 1 {
+ return errors.New("missing killmails")
+ }
+ b.Id = stub.CalculateHash()
+ b.Ships = make(map[int32]Ship)
+ b.Locations = make(map[int32]Location)
+ b.Names = make(map[int32]string)
+ for _, km := range stub.Killmails {
+ details, err := GetKillmail(km.Id, km.Hash)
+ if err != nil {
+ return errors.New("could not retrieve killmail details")
+ }
+ details.Hash = km.Hash
+ b.Locations[details.SolarSystemId] = Location{}
+ b.Ships[details.Victim.ShipTypeId] = Ship{}
+ b.Names[details.Victim.CharacterId] = ""
+ b.Names[details.Victim.CorporationId] = ""
+ b.Names[details.Victim.AllianceId] = ""
+ b.Names[details.Victim.FactionId] = ""
+ if b.StartTime.IsZero() || b.StartTime.After(details.Time) {
+ b.StartTime = details.Time
+ }
+ if b.EndTime.IsZero() || b.EndTime.Before(details.Time) {
+ b.EndTime = details.Time
+ }
+ b.Killmails = append(b.Killmails, details)
+ }
+ b.Teams = stub.Teams
+ for key := range b.Ships {
+ t, err := GetType(key)
+ if err != nil {
+ return errors.New("could not retrieve type details")
+ }
+ g, err := GetGroup(t.GroupId)
+ if err != nil {
+ return errors.New("could not retrieve group details")
+ }
+ b.Ships[key] = Ship{Name: t.Name, Group: g.Name}
+ }
+ b.Name = "Untitled Fight"
+ for key := range b.Locations {
+ s, err := GetSolarSystem(key)
+ if err != nil {
+ return errors.New("could not retrieve solar system details")
+ }
+ c, err := GetConstellation(s.ConstellationId)
+ if err != nil {
+ return errors.New("could not retrieve constellation details")
+ }
+ r, err := GetRegion(c.RegionId)
+ if err != nil {
+ return errors.New("could not retrieve region details")
+ }
+ b.Locations[key] = Location{
+ Name: s.Name,
+ Security: s.Security,
+ Position: s.Position,
+ Constellation: c.Name,
+ Region: r.Name,
+ }
+ }
+ // TODO: Resolve Names
+ return nil
}
func (s *Storage) LoadBattle(battle *Battle) error {
@@ -63,35 +129,6 @@ func (s *Storage) LoadBattle(battle *Battle) error {
}
func (s *Storage) AddBattle(battle *Battle) error {
- if len(battle.Killmails) < 1 {
- return errors.New("missing killmails")
- }
- battle.CalculateHash()
- killmail := battle.Killmails[0]
- details, err := GetKillmail(killmail.Id, killmail.Hash)
- if err != nil {
- return err
- }
- system, err := GetSolarSystem(details.SolarSystemId)
- if err != nil {
- return err
- }
- battle.Name = fmt.Sprintf("Fight in %s", system.Name)
- battle.StartTime = details.Time
- battle.EndTime = details.Time
- for _, km := range battle.Killmails[1:] {
- details, err := GetKillmail(km.Id, km.Hash)
- if err != nil {
- return err
- }
- if battle.StartTime.After(details.Time) {
- battle.StartTime = details.Time
- }
- if battle.EndTime.Before(details.Time) {
- battle.EndTime = details.Time
- }
- }
-
data, err := json.Marshal(battle)
if err != nil {
return err