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.

93 lines
1.6KB

  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. // PlayerJSON a player
  43. type PlayerJSON struct {
  44. ID int64
  45. Name string
  46. Url string
  47. Entries []EntryJSON
  48. }
  49. // URL get the player's URL
  50. func (p *PlayerJSON) URL() string {
  51. return "/player?id=" + strconv.Itoa(int(p.ID))
  52. }
  53. // ErrorJSON a query error response
  54. type ErrorJSON struct {
  55. Reason string
  56. StatusCode int
  57. }
  58. // Result a query result
  59. type Result struct {
  60. StatusCode int
  61. Items []interface{}
  62. Elapsed int64 // query time (ms)
  63. Count int
  64. Query string
  65. URL string
  66. Start time.Time
  67. }
  68. // NewResult build a query struct
  69. func NewResult(q string, url string) (r Result) {
  70. r.Start = time.Now()
  71. r.Query = q
  72. r.URL = url
  73. r.StatusCode = 200
  74. return
  75. }
  76. // Complete finish the result
  77. func (r *Result) Complete() {
  78. r.Count = len(r.Items)
  79. r.Elapsed = time.Since(r.Start).Milliseconds()
  80. }