mirror of https://github.com/dirtbags/moth.git
78 lines
1.3 KiB
Go
78 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type Award struct {
|
|
// Unix epoch time of this event
|
|
When int64
|
|
TeamID string
|
|
Category string
|
|
Points int
|
|
}
|
|
|
|
type AwardList []*Award
|
|
|
|
// Implement sort.Interface on AwardList
|
|
func (awards AwardList) Len() int {
|
|
return len(awards)
|
|
}
|
|
|
|
func (awards AwardList) Less(i, j int) bool {
|
|
return awards[i].When < awards[j].When
|
|
}
|
|
|
|
func (awards AwardList) Swap(i, j int) {
|
|
tmp := awards[i]
|
|
awards[i] = awards[j]
|
|
awards[j] = tmp
|
|
}
|
|
|
|
func ParseAward(s string) (*Award, error) {
|
|
ret := Award{}
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
n, err := fmt.Sscanf(s, "%d %s %s %d", &ret.When, &ret.TeamID, &ret.Category, &ret.Points)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if n != 4 {
|
|
return nil, fmt.Errorf("Malformed award string: only parsed %d fields", n)
|
|
}
|
|
|
|
return &ret, nil
|
|
}
|
|
|
|
func (a *Award) String() string {
|
|
return fmt.Sprintf("%d %s %s %d", a.When, a.TeamID, a.Category, a.Points)
|
|
}
|
|
|
|
func (a *Award) MarshalJSON() ([]byte, error) {
|
|
if a == nil {
|
|
return []byte("null"), nil
|
|
}
|
|
ao := []interface{}{
|
|
a.When,
|
|
a.TeamID,
|
|
a.Category,
|
|
a.Points,
|
|
}
|
|
|
|
return json.Marshal(ao)
|
|
}
|
|
|
|
func (a *Award) Same(o *Award) bool {
|
|
switch {
|
|
case a.TeamID != o.TeamID:
|
|
return false
|
|
case a.Category != o.Category:
|
|
return false
|
|
case a.Points != o.Points:
|
|
return false
|
|
}
|
|
return true
|
|
}
|