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.

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