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.

114 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. }
  52. func UnmarshalNewKeyJSON(data []byte) (NewKeyJSON, error) {
  53. var nkJson NewKeyJSON
  54. jsonErr := json.Unmarshal(data, &nkJson)
  55. if jsonErr != nil {
  56. return nkJson, jsonErr
  57. }
  58. return nkJson, nil
  59. }
  60. // PlayerJSON a player
  61. type PlayerJSON struct {
  62. ID int64
  63. Name string
  64. Url string
  65. Entries []EntryJSON
  66. }
  67. // URL get the player's URL
  68. func (p *PlayerJSON) URL() string {
  69. return "/player?id=" + strconv.Itoa(int(p.ID))
  70. }
  71. // ErrorJSON a query error response
  72. type ErrorJSON struct {
  73. Reason string
  74. StatusCode int
  75. }
  76. // Result a query result
  77. type Result struct {
  78. StatusCode int
  79. Items []interface{}
  80. Elapsed int64 // query time (ns)
  81. Count int
  82. Query string
  83. URL string
  84. Start time.Time
  85. }
  86. // NewResult build a query struct
  87. func NewResult(q string, url string) (r Result) {
  88. r.Start = time.Now()
  89. r.Query = q
  90. r.URL = url
  91. r.StatusCode = 200
  92. return
  93. }
  94. // Complete finish the result
  95. func (r *Result) Complete() {
  96. r.Count = len(r.Items)
  97. r.Elapsed = time.Since(r.Start).Nanoseconds()
  98. }