|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- // 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
- }
-
- // EntryJSON an entry in a leaderboard
- type EntryJSON struct {
- ID int64
- Rank int64
- Score int64
- PlayerName string
- PlayerURL string
- PlayerID int64
- BoardID int64
- }
-
- // NewEntryJSON a new entry to be saved to a leaderboard
- type NewEntryJSON struct {
- Score int64
- PlayerID int64
- BoardID int64
- }
-
- func UnmarshalNewEntryJSON(data []byte) (NewEntryJSON, error) {
- var neJson NewEntryJSON
- jsonErr := json.Unmarshal(data, &neJson)
- if jsonErr != nil {
- return neJson, jsonErr
- }
- return neJson, nil
- }
-
- // PlayerJSON a player
- type PlayerJSON struct {
- ID int64
- Name 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 (ms)
- 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).Milliseconds()
- }
|