Follow the leader with help from a server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

89 lines
1.5KB

  1. // NGnius 2020-01-30
  2. package main
  3. import (
  4. "encoding/json"
  5. "strconv"
  6. "time"
  7. )
  8. // BoardJSON a leaderboard
  9. type BoardJSON struct {
  10. ID int64
  11. Entries []EntryJSON
  12. Name string
  13. Description string
  14. }
  15. // EntryJSON an entry in a leaderboard
  16. type EntryJSON struct {
  17. ID int64
  18. Rank int64
  19. Score int64
  20. PlayerName string
  21. PlayerURL string
  22. PlayerID int64
  23. BoardID int64
  24. }
  25. // NewEntryJSON a new entry to be saved to a leaderboard
  26. type NewEntryJSON struct {
  27. Score int64
  28. PlayerID int64
  29. BoardID int64
  30. }
  31. func UnmarshalNewEntryJSON(data []byte) (NewEntryJSON, error) {
  32. var neJson NewEntryJSON
  33. jsonErr := json.Unmarshal(data, &neJson)
  34. if jsonErr != nil {
  35. return neJson, jsonErr
  36. }
  37. return neJson, nil
  38. }
  39. // PlayerJSON a player
  40. type PlayerJSON struct {
  41. ID int64
  42. Name string
  43. Entries []EntryJSON
  44. }
  45. // URL get the player's URL
  46. func (p *PlayerJSON) URL() string {
  47. return "/player?id=" + strconv.Itoa(int(p.ID))
  48. }
  49. // ErrorJSON a query error response
  50. type ErrorJSON struct {
  51. Reason string
  52. StatusCode int
  53. }
  54. // Result a query result
  55. type Result struct {
  56. StatusCode int
  57. Items []interface{}
  58. Elapsed int64 // query time (ms)
  59. Count int
  60. Query string
  61. URL string
  62. Start time.Time
  63. }
  64. // NewResult build a query struct
  65. func NewResult(q string, url string) (r Result) {
  66. r.Start = time.Now()
  67. r.Query = q
  68. r.URL = url
  69. r.StatusCode = 200
  70. return
  71. }
  72. // Complete finish the result
  73. func (r *Result) Complete() {
  74. r.Count = len(r.Items)
  75. r.Elapsed = time.Since(r.Start).Milliseconds()
  76. }