The new Ripple frontend.
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.

107 lines
2.5KB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "sort"
  8. "strconv"
  9. "github.com/gin-gonic/gin"
  10. "github.com/osuripple/cheesegull/models"
  11. )
  12. type beatmapPageData struct {
  13. baseTemplateData
  14. Found bool
  15. Beatmap models.Beatmap
  16. Beatmapset models.Set
  17. SetJSON string
  18. }
  19. func beatmapInfo(c *gin.Context) {
  20. data := new(beatmapPageData)
  21. defer resp(c, 200, "beatmap.html", data)
  22. b := c.Param("bid")
  23. if _, err := strconv.Atoi(b); err != nil {
  24. c.Error(err)
  25. } else {
  26. data.Beatmap, err = getBeatmapData(b)
  27. if err != nil {
  28. c.Error(err)
  29. return
  30. }
  31. data.Beatmapset, err = getBeatmapSetData(data.Beatmap)
  32. if err != nil {
  33. c.Error(err)
  34. return
  35. }
  36. sort.Slice(data.Beatmapset.ChildrenBeatmaps, func(i, j int) bool {
  37. if data.Beatmapset.ChildrenBeatmaps[i].Mode != data.Beatmapset.ChildrenBeatmaps[j].Mode {
  38. return data.Beatmapset.ChildrenBeatmaps[i].Mode < data.Beatmapset.ChildrenBeatmaps[j].Mode
  39. }
  40. return data.Beatmapset.ChildrenBeatmaps[i].DifficultyRating < data.Beatmapset.ChildrenBeatmaps[j].DifficultyRating
  41. })
  42. }
  43. if data.Beatmapset.ID == 0 {
  44. data.TitleBar = T(c, "Beatmap not found.")
  45. data.Messages = append(data.Messages, errorMessage{T(c, "Beatmap could not be found.")})
  46. return
  47. }
  48. data.KyutGrill = fmt.Sprintf("https://assets.ppy.sh/beatmaps/%d/covers/cover.jpg?%d", data.Beatmapset.ID, data.Beatmapset.LastUpdate.Unix())
  49. data.KyutGrillAbsolute = true
  50. setJSON, err := json.Marshal(data.Beatmapset)
  51. if err == nil {
  52. data.SetJSON = string(setJSON)
  53. } else {
  54. data.SetJSON = "[]"
  55. }
  56. data.TitleBar = T(c, "%s - %s", data.Beatmapset.Artist, data.Beatmapset.Title)
  57. data.Scripts = append(data.Scripts, "/static/tablesort.js", "/static/beatmap.js")
  58. }
  59. func getBeatmapData(b string) (beatmap models.Beatmap, err error) {
  60. resp, err := http.Get(config.CheesegullAPI + "/b/" + b)
  61. if err != nil {
  62. return beatmap, err
  63. }
  64. defer resp.Body.Close()
  65. body, err := ioutil.ReadAll(resp.Body)
  66. if err != nil {
  67. return beatmap, err
  68. }
  69. err = json.Unmarshal(body, &beatmap)
  70. if err != nil {
  71. return beatmap, err
  72. }
  73. return beatmap, nil
  74. }
  75. func getBeatmapSetData(beatmap models.Beatmap) (bset models.Set, err error) {
  76. resp, err := http.Get(config.CheesegullAPI + "/s/" + strconv.Itoa(beatmap.ParentSetID))
  77. if err != nil {
  78. return bset, err
  79. }
  80. defer resp.Body.Close()
  81. body, err := ioutil.ReadAll(resp.Body)
  82. if err != nil {
  83. return bset, err
  84. }
  85. err = json.Unmarshal(body, &bset)
  86. if err != nil {
  87. return bset, err
  88. }
  89. return bset, nil
  90. }