aboutsummaryrefslogtreecommitdiff
path: root/backend/handlers/home.go
diff options
context:
space:
mode:
Diffstat (limited to 'backend/handlers/home.go')
-rw-r--r--backend/handlers/home.go56
1 files changed, 9 insertions, 47 deletions
diff --git a/backend/handlers/home.go b/backend/handlers/home.go
index 57e44c1..714610a 100644
--- a/backend/handlers/home.go
+++ b/backend/handlers/home.go
@@ -6,7 +6,6 @@ import (
6 "log" 6 "log"
7 "net/http" 7 "net/http"
8 "os" 8 "os"
9 "sort"
10 "strings" 9 "strings"
11 10
12 "lphub/database" 11 "lphub/database"
@@ -64,66 +63,33 @@ func RankingsLPHUB(c *gin.Context) {
64 Overall: []models.UserRanking{}, 63 Overall: []models.UserRanking{},
65 } 64 }
66 // Singleplayer rankings 65 // Singleplayer rankings
67 sql := `SELECT u.steam_id, u.user_name, u.avatar_link, COUNT(DISTINCT map_id), 66 rows, err := database.DB.Query(`SELECT * FROM get_rankings_singleplayer();`)
68 (SELECT COUNT(maps.name) FROM maps INNER JOIN games g ON maps.game_id = g.id WHERE g."name" = 'Portal 2 - Singleplayer' AND is_disabled = false),
69 (SELECT SUM(min_score_count) AS total_min_score_count FROM (
70 SELECT
71 user_id,
72 MIN(score_count) AS min_score_count
73 FROM records_sp WHERE is_deleted = false
74 GROUP BY user_id, map_id
75 ) AS subquery
76 WHERE user_id = u.steam_id)
77 FROM records_sp sp JOIN users u ON u.steam_id = sp.user_id WHERE is_deleted = false GROUP BY u.steam_id, u.user_name`
78 rows, err := database.DB.Query(sql)
79 if err != nil { 67 if err != nil {
80 c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) 68 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
81 return 69 return
82 } 70 }
83 for rows.Next() { 71 for rows.Next() {
84 ranking := models.UserRanking{} 72 ranking := models.UserRanking{}
85 var currentCount int 73 err = rows.Scan(&ranking.User.SteamID, &ranking.User.UserName, &ranking.User.AvatarLink, &ranking.TotalScore)
86 var totalCount int
87 err = rows.Scan(&ranking.User.SteamID, &ranking.User.UserName, &ranking.User.AvatarLink, &currentCount, &totalCount, &ranking.TotalScore)
88 if err != nil { 74 if err != nil {
89 c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) 75 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
90 return 76 return
91 } 77 }
92 if currentCount != totalCount {
93 continue
94 }
95 response.Singleplayer = append(response.Singleplayer, ranking) 78 response.Singleplayer = append(response.Singleplayer, ranking)
96 } 79 }
97 // Multiplayer rankings 80 // Multiplayer rankings
98 sql = `SELECT u.steam_id, u.user_name, u.avatar_link, COUNT(DISTINCT map_id), 81 rows, err = database.DB.Query(`SELECT * FROM get_rankings_multiplayer();`)
99 (SELECT COUNT(maps.name) FROM maps INNER JOIN games g ON maps.game_id = g.id WHERE g."name" = 'Portal 2 - Cooperative' AND is_disabled = false),
100 (SELECT SUM(min_score_count) AS total_min_score_count FROM (
101 SELECT
102 host_id,
103 partner_id,
104 MIN(score_count) AS min_score_count
105 FROM records_mp WHERE is_deleted = false
106 GROUP BY host_id, partner_id, map_id
107 ) AS subquery
108 WHERE host_id = u.steam_id OR partner_id = u.steam_id)
109 FROM records_mp mp JOIN users u ON u.steam_id = mp.host_id OR u.steam_id = mp.partner_id WHERE is_deleted = false GROUP BY u.steam_id, u.user_name`
110 rows, err = database.DB.Query(sql)
111 if err != nil { 82 if err != nil {
112 c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) 83 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
113 return 84 return
114 } 85 }
115 for rows.Next() { 86 for rows.Next() {
116 ranking := models.UserRanking{} 87 ranking := models.UserRanking{}
117 var currentCount int 88 err = rows.Scan(&ranking.User.SteamID, &ranking.User.UserName, &ranking.User.AvatarLink, &ranking.TotalScore)
118 var totalCount int
119 err = rows.Scan(&ranking.User.SteamID, &ranking.User.UserName, &ranking.User.AvatarLink, &currentCount, &totalCount, &ranking.TotalScore)
120 if err != nil { 89 if err != nil {
121 c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) 90 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
122 return 91 return
123 } 92 }
124 if currentCount != totalCount {
125 continue
126 }
127 response.Multiplayer = append(response.Multiplayer, ranking) 93 response.Multiplayer = append(response.Multiplayer, ranking)
128 } 94 }
129 // Has both so they are qualified for overall ranking 95 // Has both so they are qualified for overall ranking
@@ -136,12 +102,11 @@ func RankingsLPHUB(c *gin.Context) {
136 TotalScore: totalScore, 102 TotalScore: totalScore,
137 } 103 }
138 response.Overall = append(response.Overall, overallRanking) 104 response.Overall = append(response.Overall, overallRanking)
105 break
139 } 106 }
140 } 107 }
141 } 108 }
142 sort.Slice(response.Singleplayer, func(i, j int) bool { 109
143 return response.Singleplayer[i].TotalScore < response.Singleplayer[j].TotalScore
144 })
145 placement := 1 110 placement := 1
146 ties := 0 111 ties := 0
147 for index := 0; index < len(response.Singleplayer); index++ { 112 for index := 0; index < len(response.Singleplayer); index++ {
@@ -154,9 +119,7 @@ func RankingsLPHUB(c *gin.Context) {
154 } 119 }
155 placement++ 120 placement++
156 } 121 }
157 sort.Slice(response.Multiplayer, func(i, j int) bool { 122
158 return response.Multiplayer[i].TotalScore < response.Multiplayer[j].TotalScore
159 })
160 placement = 1 123 placement = 1
161 ties = 0 124 ties = 0
162 for index := 0; index < len(response.Multiplayer); index++ { 125 for index := 0; index < len(response.Multiplayer); index++ {
@@ -169,9 +132,7 @@ func RankingsLPHUB(c *gin.Context) {
169 } 132 }
170 placement++ 133 placement++
171 } 134 }
172 sort.Slice(response.Overall, func(i, j int) bool { 135
173 return response.Overall[i].TotalScore < response.Overall[j].TotalScore
174 })
175 placement = 1 136 placement = 1
176 ties = 0 137 ties = 0
177 for index := 0; index < len(response.Overall); index++ { 138 for index := 0; index < len(response.Overall); index++ {
@@ -184,6 +145,7 @@ func RankingsLPHUB(c *gin.Context) {
184 } 145 }
185 placement++ 146 placement++
186 } 147 }
148
187 c.JSON(http.StatusOK, models.Response{ 149 c.JSON(http.StatusOK, models.Response{
188 Success: true, 150 Success: true,
189 Message: "Successfully retrieved rankings.", 151 Message: "Successfully retrieved rankings.",