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.

113 lines
2.0KB

  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. Url string
  15. }
  16. // EntryJSON an entry in a leaderboard
  17. type EntryJSON struct {
  18. ID int64
  19. Rank int64
  20. Score int64
  21. PlayerName string
  22. PlayerURL string
  23. PlayerID int64
  24. BoardName string
  25. BoardURL string
  26. BoardID int64
  27. }
  28. // NewEntryJSON a new entry to be saved to a leaderboard
  29. type NewEntryJSON struct {
  30. Score int64
  31. PlayerID int64
  32. BoardID int64
  33. }
  34. func UnmarshalNewEntryJSON(data []byte) (NewEntryJSON, error) {
  35. var neJson NewEntryJSON
  36. jsonErr := json.Unmarshal(data, &neJson)
  37. if jsonErr != nil {
  38. return neJson, jsonErr
  39. }
  40. return neJson, nil
  41. }
  42. // KeyJSON an API key for making new entry requests
  43. type KeyJSON struct {
  44. Token string
  45. PlayerID int64
  46. }
  47. // NewKeyJSON a new API key to be generated
  48. type NewKeyJSON struct {
  49. PlayerID int64
  50. }
  51. func UnmarshalNewKeyJSON(data []byte) (NewKeyJSON, error) {
  52. var nkJson NewKeyJSON
  53. jsonErr := json.Unmarshal(data, &nkJson)
  54. if jsonErr != nil {
  55. return nkJson, jsonErr
  56. }
  57. return nkJson, nil
  58. }
  59. // PlayerJSON a player
  60. type PlayerJSON struct {
  61. ID int64
  62. Name string
  63. Url string
  64. Entries []EntryJSON
  65. }
  66. // URL get the player's URL
  67. func (p *PlayerJSON) URL() string {
  68. return "/player?id=" + strconv.Itoa(int(p.ID))
  69. }
  70. // ErrorJSON a query error response
  71. type ErrorJSON struct {
  72. Reason string
  73. StatusCode int
  74. }
  75. // Result a query result
  76. type Result struct {
  77. StatusCode int
  78. Items []interface{}
  79. Elapsed int64 // query time (ns)
  80. Count int
  81. Query string
  82. URL string
  83. Start time.Time
  84. }
  85. // NewResult build a query struct
  86. func NewResult(q string, url string) (r Result) {
  87. r.Start = time.Now()
  88. r.Query = q
  89. r.URL = url
  90. r.StatusCode = 200
  91. return
  92. }
  93. // Complete finish the result
  94. func (r *Result) Complete() {
  95. r.Count = len(r.Items)
  96. r.Elapsed = time.Since(r.Start).Nanoseconds()
  97. }