// NGnius 2020-01-30 package main import ( "encoding/json" "strconv" "time" ) // BoardJSON a leaderboard type BoardJSON struct { ID int64 Entries []EntryJSON Name string Description string Url string } // EntryJSON an entry in a leaderboard type EntryJSON struct { ID int64 Rank int64 Score int64 PlayerName string PlayerURL string PlayerID int64 BoardName string BoardURL string BoardID int64 } // NewEntryJSON a new entry to be saved to a leaderboard type NewEntryJSON struct { Score int64 PlayerID int64 BoardID int64 Password string } func UnmarshalNewEntryJSON(data []byte) (NewEntryJSON, error) { var neJson NewEntryJSON jsonErr := json.Unmarshal(data, &neJson) if jsonErr != nil { return neJson, jsonErr } return neJson, nil } // KeyJSON an API key for making new entry requests type KeyJSON struct { Token string PlayerID int64 } // NewKeyJSON a new API key to be generated type NewKeyJSON struct { PlayerID int64 PlayerName string } func UnmarshalNewKeyJSON(data []byte) (NewKeyJSON, error) { var nkJson NewKeyJSON jsonErr := json.Unmarshal(data, &nkJson) if jsonErr != nil { return nkJson, jsonErr } return nkJson, nil } // PlayerJSON a player type PlayerJSON struct { ID int64 Name string Url string Entries []EntryJSON } // URL get the player's URL func (p *PlayerJSON) URL() string { return "/player?id=" + strconv.Itoa(int(p.ID)) } // ErrorJSON a query error response type ErrorJSON struct { Reason string StatusCode int } // Result a query result type Result struct { StatusCode int Items []interface{} Elapsed int64 // query time (ns) Count int Query string URL string Start time.Time } // NewResult build a query struct func NewResult(q string, url string) (r Result) { r.Start = time.Now() r.Query = q r.URL = url r.StatusCode = 200 return } // Complete finish the result func (r *Result) Complete() { r.Count = len(r.Items) r.Elapsed = time.Since(r.Start).Nanoseconds() }